def task_screenshots(request, task_id, screenshot=None): folder_path = os.path.join(cwd(), "storage", "analyses", str(task_id), "shots") if os.path.exists(folder_path): if screenshot: screenshot_name = "{0}.jpg".format(screenshot) screenshot_path = os.path.join(folder_path, screenshot_name) if os.path.exists(screenshot_path): response = HttpResponse(FileWrapper( open(screenshot_path, "rb")), content_type='image/jpeg') return response else: return json_error_response("Screenshot not found") else: zip_data = io.BytesIO() zip_file = zipfile.ZipFile(zip_data, "w", zipfile.ZIP_STORED) for shot_name in os.listdir(folder_path): zip_file.write(os.path.join(folder_path, shot_name), shot_name) zip_file.close() zip_data.seek(0) response = file_response( data=zip_data, filename="analysis_screenshots_%s.tar" % str(task_id), content_type="application/zip") return response return json_error_response("Task not found")
def task_screenshots(request, task_id, screenshot=None): folder_path = os.path.join(cwd(), "storage", "analyses", str(task_id), "shots") if os.path.exists(folder_path): if screenshot: screenshot_name = "{0}.jpg".format(screenshot) screenshot_path = os.path.join(folder_path, screenshot_name) if os.path.exists(screenshot_path): response = HttpResponse(FileWrapper(open(screenshot_path, "rb")), content_type='image/jpeg') return response else: return json_error_response("Screenshot not found") else: zip_data = io.BytesIO() zip_file = zipfile.ZipFile(zip_data, "w", zipfile.ZIP_STORED) for shot_name in os.listdir(folder_path): zip_file.write(os.path.join(folder_path, shot_name), shot_name) zip_file.close() zip_data.seek(0) response = file_response(data=zip_data, filename="analysis_screenshots_%s.tar" % str(task_id), content_type="application/zip") return response return json_error_response("Task not found")
def get(request, task_id): file_path = cwd("dump.pcap", analysis=task_id) if not os.path.exists(file_path): return json_error_response("File not found") return file_response( data=open(file_path, "rb"), filename="analysis_pcap_dump_%s.pcap" % str(task_id), content_type="application/octet-stream; charset=UTF-8" )
def task_report(request, task_id, report_format="json"): # @TO-DO: test /api/task/report/<task_id>/all/?tarmode=bz2 # duplicate filenames? task_id = int(task_id) tarmode = request.REQUEST.get("tarmode", "bz2") formats = { "json": "report.json", "html": "report.html", } bz_formats = { "all": { "type": "-", "files": ["memory.dmp"] }, "dropped": { "type": "+", "files": ["files"] }, "package_files": { "type": "+", "files": ["package_files"] }, } tar_formats = { "bz2": "w:bz2", "gz": "w:gz", "tar": "w", } if report_format.lower() in formats: report_path = os.path.join(cwd(), "storage", "analyses", str(task_id), "reports", formats[report_format.lower()]) elif report_format.lower() in bz_formats: bzf = bz_formats[report_format.lower()] srcdir = os.path.join(cwd(), "storage", "analyses", str(task_id)) s = io.BytesIO() # By default go for bz2 encoded tar files (for legacy reasons). if tarmode not in tar_formats: tarmode = tar_formats["bz2"] else: tarmode = tar_formats[tarmode] tar = tarfile.open(fileobj=s, mode=tarmode, dereference=True) for filedir in os.listdir(srcdir): filepath = os.path.join(srcdir, filedir) if not os.path.exists(filepath): continue if bzf["type"] == "-" and filedir not in bzf["files"]: tar.add(filepath, arcname=filedir) if bzf["type"] == "+" and filedir in bzf["files"]: tar.add(filepath, arcname=filedir) tar.close() s.seek(0) response = file_response( data=s, filename="analysis_report_%s.tar" % str(task_id), content_type="application/x-tar; charset=UTF-8") return response else: return json_fatal_response("Invalid report format") if os.path.exists(report_path): if report_format == "json": response = file_response( data=open(report_path, "rb"), filename="analysis_report_%s.json" % str(task_id), content_type="application/json; charset=UTF-8") return response else: return open(report_path, "rb").read() else: return json_error_response("Report not found")
def task_report(request, task_id, report_format="json"): # @TO-DO: test /api/task/report/<task_id>/all/?tarmode=bz2 # duplicate filenames? task_id = int(task_id) tarmode = request.REQUEST.get("tarmode", "bz2") formats = { "json": "report.json", "html": "report.html", } bz_formats = { "all": {"type": "-", "files": ["memory.dmp"]}, "dropped": {"type": "+", "files": ["files"]}, "package_files": {"type": "+", "files": ["package_files"]}, } tar_formats = { "bz2": "w:bz2", "gz": "w:gz", "tar": "w", } if report_format.lower() in formats: report_path = os.path.join(cwd(), "storage", "analyses", str(task_id), "reports", formats[report_format.lower()]) elif report_format.lower() in bz_formats: bzf = bz_formats[report_format.lower()] srcdir = os.path.join(cwd(), "storage", "analyses", str(task_id)) s = io.BytesIO() # By default go for bz2 encoded tar files (for legacy reasons). if tarmode not in tar_formats: tarmode = tar_formats["bz2"] else: tarmode = tar_formats[tarmode] tar = tarfile.open(fileobj=s, mode=tarmode, dereference=True) for filedir in os.listdir(srcdir): filepath = os.path.join(srcdir, filedir) if not os.path.exists(filepath): continue if bzf["type"] == "-" and filedir not in bzf["files"]: tar.add(filepath, arcname=filedir) if bzf["type"] == "+" and filedir in bzf["files"]: tar.add(filepath, arcname=filedir) tar.close() s.seek(0) response = file_response(data=s, filename="analysis_report_%s.tar" % str(task_id), content_type="application/x-tar; charset=UTF-8") return response else: return json_fatal_response("Invalid report format") if os.path.exists(report_path): if report_format == "json": response = file_response(data=open(report_path, "rb"), filename="analysis_report_%s.json" % str(task_id), content_type="application/json; charset=UTF-8") return response else: return open(report_path, "rb").read() else: return json_error_response("Report not found")