Triages and classifies malware samples using YARA rules to match strings, byte sequences, file patterns, and structures. Guides rule creation, scanning, and workflow integration for signature-based detection.
npx claudepluginhub killvxk/cybersecurity-skills-zhThis skill uses the workspace's default tool permissions.
- 快速将大批恶意软件样本与已知家族签名进行匹配分类
Performs malware triage by scanning files with YARA rules, writing custom signatures for patterns/strings, and integrating into analysis pipelines. For classifying samples or hunting threats.
Performs malware triage and classification using YARA rules to match file patterns, strings, and bytes against known families. Guides scanning, rule writing, and pipeline integration for sample analysis.
Develops precise YARA rules for malware detection by analyzing executables for unique byte patterns, strings, PE imports, and behaviors to minimize false positives.
Share bugs, ideas, or general feedback.
不适用作为唯一分析方法;YARA 分级可识别已知模式,但无法揭示新型或未知恶意软件的行为。
apt install yara 或 pip install yara-python)yara-python 进行脚本化扫描应用社区和商业 YARA 规则对样本进行分类:
# 扫描单个文件
yara -s malware_rules.yar suspect.exe
# 扫描样本目录
yara -r malware_rules.yar /path/to/samples/
# 使用多个规则文件扫描
yara -r rules/apt_rules.yar rules/ransomware_rules.yar rules/trojan_rules.yar suspect.exe
# 设置超时(防止大文件卡死)
yara -t 30 malware_rules.yar suspect.exe
# 扫描并显示匹配字符串
yara -s -r malware_rules.yar suspect.exe
# 使用编译规则扫描(重复扫描时速度更快)
yarac malware_rules.yar compiled_rules.yarc
yara compiled_rules.yarc suspect.exe
# 下载社区规则集
git clone https://github.com/Yara-Rules/rules.git yara-community-rules
git clone https://github.com/Neo23x0/signature-base.git signature-base
# 使用 signature-base 扫描
yara -r signature-base/yara/*.yar suspect.exe
根据恶意软件分析中提取的字符串创建 YARA 规则:
rule MalwareX_Strings {
meta:
description = "Detects MalwareX based on unique strings"
author = "analyst"
date = "2025-09-15"
reference = "Internal Analysis Report #1547"
hash = "e3b0c44298fc1c149afbf4c8996fb924"
tlp = "WHITE"
strings:
// C2 URL 模式
$url1 = "/gate.php?id=" ascii
$url2 = "/panel/connect.php" ascii
// 唯一互斥体名称
$mutex = "Global\\CryptLocker_2025" ascii wide
// User-Agent 字符串
$ua = "Mozilla/5.0 (compatible; MSIE 10.0)" ascii
// 注册表持久化路径
$reg = "Software\\Microsoft\\Windows\\CurrentVersion\\Run\\WindowsUpdate" ascii
// 活动标识符
$campaign = "campaign_2025_q3" ascii
condition:
uint16(0) == 0x5A4D and // PE 文件(MZ 头)
filesize < 500KB and // 大小限制
($url1 or $url2) and // 至少一个 C2 URL
($mutex or $campaign) and // 活动标识符
$ua // 特定 User-Agent
}
创建匹配特定代码序列的规则:
rule MalwareX_Decryptor {
meta:
description = "Detects MalwareX XOR decryption routine"
author = "analyst"
date = "2025-09-15"
strings:
// XOR 解密循环(x86 汇编)
// mov al, [esi+ecx]
// xor al, [edi+ecx]
// mov [esi+ecx], al
// inc ecx
// cmp ecx, edx
// jl loop
$xor_loop = { 8A 04 0E 32 04 0F 88 04 0E 41 3B CA 7C F3 }
// RC4 KSA 初始化(256 字节循环)
$rc4_ksa = { 33 C0 88 04 ?8 40 3D 00 01 00 00 7? }
// 嵌入的 RSA 公钥标记
$rsa_key = { 06 02 00 00 00 A4 00 00 52 53 41 31 } // PUBLICKEYBLOB
condition:
uint16(0) == 0x5A4D and
($xor_loop or $rc4_ksa) and
$rsa_key
}
利用 YARA 的 PE 模块进行结构化检测:
import "pe"
import "hash"
import "math"
rule MalwareX_PE_Characteristics {
meta:
description = "Detects MalwareX by PE structure and imports"
author = "analyst"
condition:
pe.is_pe and
// 在特定时间范围内编译
pe.timestamp > 1693526400 and // 2023-09-01 之后
pe.timestamp < 1727740800 and // 2024-10-01 之前
// 特定导入哈希
pe.imphash() == "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6" or
// 可疑导入组合
(
pe.imports("kernel32.dll", "VirtualAllocEx") and
pe.imports("kernel32.dll", "WriteProcessMemory") and
pe.imports("kernel32.dll", "CreateRemoteThread") and
pe.imports("wininet.dll", "InternetOpenA")
) or
// 高熵 .text 节(已打包)
(
for any section in pe.sections : (
section.name == ".text" and
math.entropy(section.raw_data_offset, section.raw_data_size) > 7.0
)
)
}
rule MalwareX_Rich_Header {
meta:
description = "Detects MalwareX by Rich header hash"
condition:
pe.is_pe and
hash.md5(pe.rich_signature.clear_data) == "abc123def456abc123def456abc123de"
}
自动化扫描样本集合:
import yara
import os
import json
import hashlib
from datetime import datetime
# 编译所有规则文件
rule_files = {
"apt": "rules/apt_rules.yar",
"ransomware": "rules/ransomware_rules.yar",
"trojan": "rules/trojan_rules.yar",
"custom": "rules/custom_rules.yar",
}
rules = yara.compile(filepaths=rule_files)
# 扫描样本目录
results = []
sample_dir = "/path/to/samples"
for filename in os.listdir(sample_dir):
filepath = os.path.join(sample_dir, filename)
if not os.path.isfile(filepath):
continue
with open(filepath, "rb") as f:
data = f.read()
sha256 = hashlib.sha256(data).hexdigest()
matches = rules.match(filepath)
result = {
"filename": filename,
"sha256": sha256,
"size": len(data),
"matches": [],
"classification": "UNKNOWN",
}
for match in matches:
result["matches"].append({
"rule": match.rule,
"namespace": match.namespace,
"tags": match.tags,
"strings": [(hex(s[0]), s[1], s[2].decode("utf-8", errors="replace")[:100])
for s in match.strings] if match.strings else []
})
if result["matches"]:
result["classification"] = result["matches"][0]["namespace"].upper()
results.append(result)
# 汇总
classified = sum(1 for r in results if r["classification"] != "UNKNOWN")
print(f"已扫描:{len(results)} 个样本")
print(f"已分类:{classified} 个({classified/len(results)*100:.1f}%)")
print(f"未知:{len(results)-classified} 个")
# 导出结果
with open("triage_results.json", "w") as f:
json.dump(results, f, indent=2)
测试规则的误报率和性能:
# 检查规则语法
yara -C custom_rules.yar
# 扫描已知干净目录以检查误报
yara -r custom_rules.yar /path/to/clean_files/ > false_positives.txt
wc -l false_positives.txt
# 基准测试规则性能
time yara -r custom_rules.yar /path/to/large_sample_collection/
# 分析单个规则性能
yara -p custom_rules.yar suspect.exe
| 术语 | 定义 |
|---|---|
| YARA 规则 | 模式匹配规则,定义字符串、字节序列和条件,用于识别特定文件或恶意软件家族 |
| 条件(Condition) | 将字符串匹配、文件属性和模块函数组合的布尔表达式,用于判断规则是否匹配 |
| 十六进制字符串(Hex String) | 带有可选通配符(??)和跳转([N-M])的字节模式,用于匹配机器码或二进制数据 |
| PE 模块 | YARA 模块,提供对 PE 文件属性(导入、节、时间戳、资源)的访问,用于结构化匹配 |
| Imphash | PE 文件导入表的 MD5 哈希;同一家族的样本通常共享相同的导入哈希 |
| Rich Header | PE 文件中未记录的结构,包含编译器/链接器元数据;在恶意软件构建环境中保持一致 |
| YARA-C | 编译后的 YARA 规则格式,通过预编译规则加快重复扫描速度 |
背景:对新恶意软件样本的逆向工程已识别出唯一字符串、字节模式和 PE 特征。需要 YARA 规则用于企业范围内的追踪和持续检测。
方法:
注意事项:
YARA 分级结果
=====================
扫描日期: 2025-09-15
规则集: apt_rules(847 个规则)、ransomware_rules(312 个规则)、
trojan_rules(1,204 个规则)、custom_rules(45 个规则)
已扫描样本: 2,500 个
处理时间: 47 秒
分类汇总
APT: 12 个样本(0.5%)
勒索软件: 187 个样本(7.5%)
木马: 423 个样本(16.9%)
未知: 1,878 个样本(75.1%)
命中频率最高的规则
规则 命中数 家族
MalwareX_C2_Beacon 45 MalwareX
LockBit3_Ransom_Note 38 LockBit 3.0
Emotet_Epoch5_Loader 32 Emotet
CobaltStrike_Beacon_Config 28 Cobalt Strike
QakBot_DLL_Loader 25 QakBot
样本详情
文件: suspect.exe
SHA-256: e3b0c44298fc1c149afbf4c8996fb924...
命中规则:
[1] MalwareX_Strings(custom)
- $url1 位于 0x4A20:"/gate.php?id="
- $mutex 位于 0x5100:"Global\\CryptLocker_2025"
[2] MalwareX_Decryptor(custom)
- $xor_loop 位于 0x401200:{ 8A 04 0E 32 04 0F ... }
[3] MalwareX_PE_Characteristics(custom)
- PE 导入组合匹配
分类结果:MALWAREX(高置信度)