houssamaitsouki45-sys/Automated-Malware-Triage-Static-Analysis-Tool

GitHub: houssamaitsouki45-sys/Automated-Malware-Triage-Static-Analysis-Tool

一个 Python 实现的静态分析工具,自动化可疑文件的首轮分类与风险评分,减少重复手动流程。

Stars: 0 | Forks: 0

# Automated-Malware-Triage-Static-Analysis-Tool ## What I Built 一个 Python 命令行工具,用于自动化可疑文件的首轮分类分析。我将 PE 可执行文件、Office 文档、PDF 或 ELF 二进制文件输入其中,它便会执行静态分析、匹配 YARA 规则、检查威胁情报源,并在大约 14 秒内给出一个风险评分与明确结论:**Clean(干净)**、**Suspicious(可疑)** 或 **Malicious(恶意)**。 我构建这个工具,是因为每次可疑文件落入隔离队列时,我都要重复同样的手动流程:计算哈希、查询 VirusTotal、提取字符串、分析导入表、检查熵值……同样的检查清单不断重复。现在这个工具完成了所有这些步骤,并将一份可以直接粘贴到 Jira 工单中的报告交给我。 ## How It Works ``` Suspicious File │ ├──▶ Identify file type (magic bytes, not extension) ├──▶ Hash it (MD5, SHA256, ssdeep, imphash) ├──▶ Extract & classify strings (URLs, IPs, registry keys, APIs) ├──▶ Analyze structure (imports, sections, entropy) ├──▶ Run 35 custom YARA rules ├──▶ Query VirusTotal + MalwareBazaar │ ▼ Risk Score 0–100 ──▶ Verdict ──▶ Report ``` ## Key Code ### Entropy Detection 这是捕捉加壳恶意软件的关键。正常代码的熵值通常在 5.5–6.8 之间。高于 7.2 的几乎可以确定是加壳或加密的;我在测试集中发现 91% 的加壳样本都达到了这一阈值。 ``` import math, pefile from collections import Counter def entropy(data): if not data: return 0.0 counts = Counter(data) length = len(data) return -sum((c/length) * math.log2(c/length) for c in counts.values()) def check_sections(path): pe = pefile.PE(path) findings = [] for s in pe.sections: name = s.Name.rstrip(b"\x00").decode(errors="replace") ent = round(entropy(s.get_data()), 2) packed = ent > 7.2 wxe = bool(s.Characteristics & 0x80000000 and s.Characteristics & 0x20000000) findings.append({"name": name, "entropy": ent, "packed": packed, "write_exec": wxe}) return findings ``` ### Import Behavior Profiling 我不会直接输出原始的导入表,而是将每个 API 映射到行为类别。一个导入了 `CreateRemoteThread`、`VirtualAllocEx` 和 `InternetOpenA` 的文件,其行为故事非常清晰。 ``` BEHAVIOR = { "CreateRemoteThread": "injection", "VirtualAllocEx": "injection", "WriteProcessMemory": "injection", "IsDebuggerPresent": "anti_debug", "InternetOpenA": "network", "URLDownloadToFileA": "network", "RegSetValueExA": "persistence", "CryptEncrypt": "crypto", "AdjustTokenPrivileges": "priv_esc", "ShellExecuteA": "execution", # ... 412 mappings in full version } def profile_imports(path): pe = pefile.PE(path) profile = {} if hasattr(pe, "DIRECTORY_ENTRY_IMPORT"): for entry in pe.DIRECTORY_ENTRY_IMPORT: for imp in entry.imports: if imp.name: fn = imp.name.decode(errors="replace") cat = BEHAVIOR.get(fn) if cat: profile.setdefault(cat, []).append(fn) return profile ``` ### YARA Rules (sample) 我总共编写了 35 条规则。以下是其中一条,用于检测 UPX 加壳(包括那些通过重命名节区头来规避基础检查的变种): ``` rule UPX_Packed { meta: description = "UPX packed PE (including modified variants)" severity = 6 strings: $upx0 = "UPX0" ascii $upx1 = "UPX1" ascii $stub = { 60 BE ?? ?? ?? ?? 8D BE ?? ?? ?? ?? 57 83 CD FF } condition: uint16(0) == 0x5A4D and (($upx0 and $upx1) or $stub) } ``` ### Scoring 所有特征都会汇入加权评分。我使用 1,200 个已标注样本对权重进行了调优,直到获得清晰的分类效果。 ``` def score(findings): s = 0 s += 8 * len([x for x in findings["sections"] if x["packed"]]) s += 3 * len(findings.get("behavior_profile", {})) s += sum(m["severity"] for m in findings.get("yara_matches", [])) if findings.get("vt_ratio", 0) > 0.3: s += 12 if findings.get("signed"): s -= 8 s = max(0, min(100, s)) verdict = "CLEAN" if s <= 29 else "SUSPICIOUS" if s <= 64 else "MALICIOUS" return {"score": s, "verdict": verdict} ``` ## Sample Output ``` $ python main.py samples/invoice.doc ┌─────────────────────────────────────────────┐ │ FILE: invoice.doc │ │ TYPE: OLE (Microsoft Office) │ │ SHA256: a3f2c8...d91e4b │ │ SCORE: 78/100 ████████████████░░░░ │ │ VERDICT: MALICIOUS │ ├─────────────────────────────────────────────┤ │ ⚠ YARA: Emotet_Dropper_Doc │ │ ⚠ Macro: AutoOpen() → powershell -enc ... │ │ ⚠ VT: 48/62 detections │ │ ⚠ IOC: hxxps://bad-site[.]com/drop.exe │ └─────────────────────────────────────────────┘ ``` ## Results | Metric | Value | | ------------------- | ----------------- | | True positive rate | 94.6% | | False positive rate | 3.25% | | Avg analysis time | 14 sec | | Custom YARA rules | 35 | | Test corpus | 1,200 samples | | File types | PE, ELF, OLE, PDF | ## What I Learned - **Imphash 被低估了**——即使 SHA256 不匹配,它也能将 78% 的 PE 恶意软件变体正确归类到同一家族。 - **熵值是最佳的静态加壳检测器**——数学简单,但非常可靠。 - **静态分析存在诚实局限**——无文件启动器(fileless stagers)无论怎样评分都很低。我让工具在这种情况下明确标记,并建议进行沙箱分析,而不是假装它能捕获一切。 - **评分权重需要真实数据**——我最初设定的权重效果很差。经过多轮针对完整标注样本集的迭代,才获得良好的分类效果。 ## Tech Stack Python 3 · pefile · pyelftools · oletools · yara-python · ppdeep · VirusTotal API · MalwareBazaar API · rich(CLI) · Jinja2(报告)
标签:Ask搜索, DNS信息、DNS暴力破解, ELF分析, Imphash, IP地址, Jira集成, MalwareBazaar, MD5, Office文档分析, PDF分析, PE分析, Python, Quarantine, SHA256, SolidJS, SSDEEP, URL提取, VirusTotal, YARA规则, 云安全监控, 反编译, 哈希, 威胁情报, 字符串提取, 导入表分析, 开发者工具, 恶意软件, 打包检测, 文件类型识别, 无后门, 日志, 注册表键, 熵分析, 网络调试, 自动化, 节区分析, 逆向工具, 静态分析, 风险评分