fix: L0-026 假损坏 zip + L1-013 内嵌 EICAR + L5-004 手工 PE 分析

对账: 162/168 通过 (96.4%),剩余 6 条为样本占位符问题

structure.py:
- _check_zip_trailing_garbage(): 新增模块级函数检测 ZIP EOCD 尾部
  多余字节(L0-026 corrupt_direntry.zip 伪装损坏模式)
- _check_embedded_eicar(): 新增模块级函数,作为 ClamAV 只匹配精确
  68B 纯 EICAR 的补充,对文本/代码文件扫描内嵌 EICAR 字节串
  (L1-013 大文件尾部含 EICAR)
- scan() 主流程在 4b/4c 后加 4d 调用 _check_embedded_eicar

binary.py:
- _manual_pe_analysis(): 新增模块级函数,pefile 解析失败时手工搜索
  PE\\0\\0 签名并解析节表 Characteristics,检测 RWX 可写可执行节
  (L5-004 假 PE,e_lfanew 偏移错误导致 pefile 报 Invalid NT Headers)
- scan() 在 pefile 异常后 fallback 调用 _manual_pe_analysis

bugfix: _check_zip_trailing_garbage 越界 crash (IndexError)
- EOCD 被截断/comment_len 溢出时跳过,不抛异常
- 修复 NEG-017 4layer_nested.zip 误触发 engine_failure fail_closed
This commit is contained in:
2026-10-29 16:11:55 +08:00
parent 1a0a24e672
commit 5c776785b9
2 changed files with 168 additions and 0 deletions

View File

@@ -16,6 +16,84 @@ from app.engines.base import Engine, EngineResult, Finding, ScanTarget, Severity
BINARY_EXTS = {".exe", ".dll", ".sys", ".scr", ".com", ".cpl", ".ocx", ".msi", ".apk", ".jar", ".so", ".bin", ".elf"}
# ---------- pefile 失败时的手工 PE 分析 fallback ----------
# PE Section Characteristics 常量(与 pefile.SECTION_CHARACTERISTICS 一致)
_PE_SCN_MEM_EXECUTE = 0x20000000
_PE_SCN_MEM_WRITE = 0x80000000
def _manual_pe_analysis(path: str) -> List[Finding]:
"""pefile 解析失败时,手工搜索 PE\\0\\0 签名并解析节表。
覆盖场景:假 PE / 伪装 PE / e_lfanew 偏移错误。pefile 严格校验
e_lfanew 指向的签名是否为 PE\\0\\0,但攻击者可以让 e_lfanew 偏移
错几个字节让 pefile 报错,同时文件里仍有真实的 PE\\0\\0 和节表。
"""
import struct
findings: List[Finding] = []
try:
with open(path, "rb") as fh:
data = fh.read()
except OSError:
return findings
# 1) MZ 头存在?(PE 文件必需)
if len(data) < 2 or data[:2] != b"MZ":
return findings # 根本不是 PE
# 2) 尝试从 e_lfanew 取 PE 偏移;不对就全局搜索 PE\0\0
pe_off = -1
if len(data) >= 0x40:
try:
e_lfanew = struct.unpack_from("<I", data, 0x3C)[0]
if e_lfanew + 4 <= len(data) and data[e_lfanew:e_lfanew + 4] == b"PE\x00\x00":
pe_off = e_lfanew
except struct.error:
pass
if pe_off < 0:
pe_off = data.find(b"PE\x00\x00")
if pe_off < 0:
return findings # 没找到 PE 签名
# 3) COFF header 在 PE\0\0 后 4 字节
coff_off = pe_off + 4
if coff_off + 24 > len(data):
return findings
num_sections = struct.unpack_from("<H", data, coff_off + 2)[0]
size_opt_header = struct.unpack_from("<H", data, coff_off + 16)[0]
section_table_off = coff_off + 20 + size_opt_header
if num_sections == 0 or num_sections > 96: # 合理上限
return findings
if section_table_off + num_sections * 40 > len(data):
return findings
# 4) 逐节扫描 RWX 属性
rwx_sections = []
for i in range(num_sections):
sec_off = section_table_off + i * 40
name = data[sec_off:sec_off + 8].rstrip(b"\x00")
characteristics = struct.unpack_from("<I", data, sec_off + 36)[0]
if (characteristics & _PE_SCN_MEM_EXECUTE) and (characteristics & _PE_SCN_MEM_WRITE):
rwx_sections.append(name.decode("utf-8", errors="replace"))
if rwx_sections:
findings.append(
Finding(
engine="binary", layer="L2B",
severity=Severity.HIGH.value, rule="RWXSectionDetected",
detail=f"手工 PE 分析检测到可写且可执行的危险节:{', '.join(rwx_sections)}",
weight=50.0,
)
)
return findings
# ---------- 模块级全局 YARA 规则单例 ----------
# 所有 YaraEngine 实例共享同一个编译结果,避免重复 compile
# rules_dir 不同时(极少见)才分叉加载
@@ -174,6 +252,10 @@ class BinaryEngine(Engine):
weight=10.0,
)
)
# --- pefile 失败时尝试手工 PE 分析(覆盖假 PE / 伪装 PE)---
manual = _manual_pe_analysis(target.path)
if manual:
result.findings.extend(manual)
return result
try: