| name | malware-analysis |
| description | 恶意软件分析 — 沙箱、动态分析、脱壳、反混淆、YARA规则 |
| version | 1.0.0 |
| author | Hermes Agent |
| license | MIT |
| metadata | {"hermes":{"tags":["malware","sandbox","dynamic-analysis","unpacking","yara","reverse-engineering"]}} |
恶意软件分析工具链
核心工具
| 工具 | 用途 |
|---|
| YARA | 模式匹配/恶意软件分类 |
| PEframe | PE文件静态分析 |
| PEiD | 壳/编译器检测 |
| Detect It Easy (DIE) | 壳/编译器检测(PEiD替代) |
| UPX | 脱壳/加壳 |
| Fakenet-NG | 网络模拟 |
| Procmon | 进程监控(Windows) |
| x64dbg/OllyDbg | Windows调试器 |
| Cuckoo Sandbox | 自动化沙箱 |
| CAPE | Cuckoo改进版沙箱 |
| Any.Run | 在线沙箱 |
| VirusTotal | 在线扫描 |
| MalwareBazaar | 恶意样本库 |
| strings | 字符串提取 |
| ssdeep | 模糊哈希 |
| Volatility | 内存取证 |
| ClamAV | 开源杀毒引擎 |
| Radare2/Ghidra | 反编译 |
1. YARA — 模式匹配
安装
sudo apt install yara
pip install yara-python
基本用法
yara rules.yar target_file
yara -r rules.yar /path/to/scan/
yara -s rules.yar target_file
yara -m rules.yar target_file
yara -w rules.yar target_file
YARA规则编写
rule malware_detection {
meta:
description = "Detects ExampleMalware"
author = "Hermes"
date = "2024-01-01"
hash = "abc123..."
strings:
$s1 = "cmd.exe /c" ascii
$s2 = "HKEY_LOCAL_MACHINE" ascii
$s3 = { 6A 40 68 00 30 00 00 6A 14 } // hex pattern
$s4 = /https?:\/\/[a-z0-9\.]+\/[a-z]+/ // regex
$mutex = "Global\\MyMalwareMutex" wide
condition:
uint16(0) == 0x5A4D and // PE文件
filesize < 500KB and
2 of ($s*) and
$mutex
}
YARA进阶
// PE导入表检测
rule pe_imports {
condition:
pe.imports("kernel32.dll", "VirtualAlloc") and
pe.imports("ws2_32.dll", "connect")
}
// 加壳检测
rule packed_pe {
condition:
pe.sections[0].name == "UPX0" or
pe.number_of_sections < 3
}
// 字符串加密检测
rule encrypted_strings {
strings:
$xor = { 31 C0 40 31 DB } // XOR解密循环
condition:
$xor and filesize < 100KB
}
在线YARA仓库
git clone https://github.com/Yara-Rules/rules.git
2. PE文件分析
PEframe
pip install peframe
peframe malware.exe
pefile (Python)
import pefile
pe = pefile.PE("malware.exe")
print(f"Entry: 0x{pe.OPTIONAL_HEADER.AddressOfEntryPoint:x}")
print(f"ImageBase: 0x{pe.OPTIONAL_HEADER.ImageBase:x}")
print(f"Sections: {len(pe.sections)}")
for section in pe.sections:
print(f"{section.Name}: VA=0x{section.VirtualAddress:x} Size={section.Misc_VirtualSize}")
if hasattr(pe, 'DIRECTORY_ENTRY_IMPORT'):
for entry in pe.DIRECTORY_ENTRY_IMPORT:
print(f"\n{entry.dll.decode()}")
for imp in entry.imports:
print(f" {imp.name.decode() if imp.name else 'ordinal'}")
data = open("malware.exe", "rb").read()
import re
strings = re.findall(b'[\x20-\x7e]{6,}', data)
for s in strings[:20]:
print(s.decode())
Detect It Easy (DIE)
wget https://github.com/horsicq/DIE-engine/releases/latest/download/die_linux64_portable.tar.gz
tar xzf die_linux64_portable.tar.gz
die malware.exe
diec malware.exe
3. 脱壳
UPX脱壳
upx -t malware.exe
upx -d malware.exe -o unpacked.exe
手动脱壳流程
1. 用x64dbg加载
2. F8单步到JMP/跳转指令
3. 找到跳向OEP的跳转(通常是一个大的JMP)
4. 在OEP处设置断点
5. 运行到断点
6. 使用Scylla/MiniDump dump进程
7. 修复导入表(IAT)
自动脱壳工具
4. 动态分析
基本流程
1. 隔离环境(VM/沙箱)
2. 快照当前状态
3. 启动监控工具
4. 运行恶意软件
5. 收集行为数据
6. 恢复快照
网络监控
tshark -i eth0 -w capture.pcap
tshark -r capture.pcap -Y "http.request"
sudo apt install inetsim
inetsim --conf /etc/inetsim/inetsim.conf
文件系统监控
strace -e trace=file -f malware
inotifywait -mr /tmp/monitor/
mkdir -p /tmp/monitor && cd /tmp/monitor
find . -newer marker_file -type f
进程监控
ps aux | grep malware
ls -la /proc/PID/exe
cat /proc/PID/maps
ss -tlnp
netstat -tlnp
lsof -i
pstree -p PID
5. Cuckoo/CAPE沙箱
Cuckoo安装(简化)
sudo apt install virtualbox
pip install cuckoo
cuckoo init
cuckoo machine
cuckoo submit malware.exe
cuckoo report 1
在线沙箱
https://www.virustotal.com # 多引擎扫描
https://any.run # 交互式沙箱
https://www.hybrid-analysis.com
https://unpac.me # 脱壳专用
https://bazaar.abuse.ch # 恶意样本库
6. 混淆/反调试绕过
常见反调试技术
1. IsDebuggerPresent()
2. NtQueryInformationProcess()
3. CheckRemoteDebuggerPresent()
4. 时间检查(RDTSC)
5. INT 2D
6. PEB.BeingDebugged
7. TLS回调
8. 异常处理
9. 父进程检查
10. 沙箱检测(鼠标移动、进程数、MAC地址)
绕过方法
脱壳+去混淆流程
1. DIE检测壳类型
2. UPX直接脱壳
3. 其他壳: x64dbg手动脱壳
4. Ghidra分析去混淆后的代码
5. 提取C2地址、通信协议
6. 编写解密脚本
7. 特征提取
从恶意软件提取IOC
md5sum malware.exe
sha256sum malware.exe
ssdeep malware.exe
strings -n 8 malware.exe
strings -el malware.exe
strings malware.exe | grep -E 'https?://|[\d\.]+:\d+'
strings malware.exe | grep -E '\.onion|\.xyz|\.top'
strings malware.exe | grep -i 'HKEY_\|CurrentVersion\\\\Run'
strings malware.exe | grep -i 'AppData\\\\|Temp\\\\|System32\\\\'
ssdeep模糊哈希
pip install ssdeep
ssdeep -b malware1.exe > hash1.txt
ssdeep -b malware2.exe > hash2.txt
ssdeep -br hash1.txt hash2.txt
import ssdeep
h1 = ssdeep.hash_from_file("malware1.exe")
h2 = ssdeep.hash_from_file("malware2.exe")
print(ssdeep.compare(h1, h2))
Pitfalls
- 永远在隔离环境分析 — 用VM或物理隔离机器
- 断网分析 — 除非需要抓C2通信
- 先拍照再运行 — VM快照是恢复的保障
- YARA规则要更新 — 恶意软件变种快
- 手动脱壳需要练习 — 没有万能方法
- 沙箱检测是常见的 — 恶意软件会检测沙箱并改变行为
- 字符串可能加密 — 需要先解密再分析
- 加壳不等于恶意 — 很多合法软件也加壳