api.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:CAPE 作者: ctxis 项目源码 文件源码
def tasks_report(task_id, report_format="json"):
    formats = {
        "json": "report.json",
        "html": "report.html",
        "htmlsumary": "summary-report.html",
        "pdf": "report.pdf",
        "maec": "report.maec-4.1.xml",
        "metadata": "report.metadata.xml",
    }

    bz_formats = {
        "all": {"type": "-", "files": ["memory.dmp"]},
        "dropped": {"type": "+", "files": ["files"]},
        "dist" : {"type": "+", "files": ["shots", "reports"]},
        "dist2": {"type": "-", "files": ["shots", "reports", "binary"]},
    }

    tar_formats = {
        "bz2": "w:bz2",
        "gz": "w:gz",
        "tar": "w",
    }

    if report_format.lower() in formats:
        report_path = os.path.join(CUCKOO_ROOT, "storage", "analyses",
                                   "%d" % task_id, "reports",
                                   formats[report_format.lower()])
    elif report_format.lower() in bz_formats:
            bzf = bz_formats[report_format.lower()]
            srcdir = os.path.join(CUCKOO_ROOT, "storage",
                                  "analyses", "%d" % task_id)
            s = StringIO()

            # By default go for bz2 encoded tar files (for legacy reasons.)
            tarmode = tar_formats.get(request.GET.get("tar"), "w:bz2")

            tar = tarfile.open(fileobj=s, mode=tarmode)
            for filedir in os.listdir(srcdir):
                if bzf["type"] == "-" and filedir not in bzf["files"]:
                    tar.add(os.path.join(srcdir, filedir), arcname=filedir)
                if bzf["type"] == "+" and filedir in bzf["files"]:
                    tar.add(os.path.join(srcdir, filedir), arcname=filedir)

            if report_format.lower() == "dist" and FULL_DB:
                buf = results_db.analysis.find_one({"info.id": task_id})
                tarinfo = tarfile.TarInfo("mongo.json")
                buf_dumped = json_util.dumps(buf)
                tarinfo.size = len(buf_dumped)
                buf = StringIO(buf_dumped)
                tar.addfile(tarinfo, buf)

            tar.close()
            response.content_type = "application/x-tar; charset=UTF-8"
            return s.getvalue()
    else:
        return HTTPError(400, "Invalid report format")

    if os.path.exists(report_path):
        return open(report_path, "rb").read()
    else:
        return HTTPError(404, "Report not found")
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号