re-doc-malware
Analyzes dangerous PDFs and Word documents to find hidden malware and exploits.
Installation
Paste this into Claude Code, Cursor, or any agent that can run commands.
What this skill does
What it does:
- Analyzes suspicious PDF and Office files that might contain malware
- Finds hidden code and scripts inside documents
- Extracts embedded files and malicious macros from Word and Excel files
- Identifies tricks used to make documents dangerous
- Recovers the actual malware payload hidden in the document
When to use it:
- You received a suspicious email attachment that is a PDF or Office file
- A document file contains hidden macros or scripts
- You need to understand how a phishing document tries to infect computers
- You want to extract and analyze the malware hidden inside a document
SKILL.mdShow the author's original SKILL.md (not in English)
---
name: re-doc-malware
type: atomic
description: >
恶意文档分析:PDF/Office 武器化、宏链、文档漏洞利用、载荷提取。
触发词:恶意文档、钓鱼文档、PDF恶意、宏文档、docm、文档漏洞、恶意附件。
---
# 恶意文档分析
## 何时使用 / 何时不用
- 用:钓鱼附件(PDF/Office/RTF)、文档漏洞利用样本、宏文档、文档型恶意载荷
- 用:仅拿到单文档样本,需还原载荷提取链(宏 → 脚本 → 可执行 → 回连)
- 不用:纯脚本宏(转 [[re-script-deob]]);文档仅是载体(核心逻辑在下载载荷)
- 不用:只有网页/邮件网关侧元数据(无样本文件本身)——先要样本再分析
- 注意:动态打开文档必须沙箱([[re-sandbox]],[[platform-tips]] 最高原则);文档可能检测环境后不触发(见坑 6)
## 工具准备
### pdf-parser / pdfid(PDF 结构分析)
- Linux: `apt install pdf-parser pdfid`(Kali 常预装;Debian/Ubuntu 仓库同名包)或 GitHub `DidierStevens/DidierStevensSuite` 源码运行;macOS: `brew install pdf-parser` / pip
- Windows: pip(WSL 亦可)
- 验证: `pdf-parser --version`(无则 `python3 pdf_parser.py --help`);`pdfid.py sample.pdf` 能输出对象计数
- 补充(备注:peepdf 已归档、Python 2 时代,PyPI 仅 0.3.6——首选 pdf-parser/pdfid)
### qpdf(PDF 流解压,可选)
- Linux: `apt install qpdf`;macOS: `brew install qpdf`
- 验证: `qpdf --version`
- 用途: 展开对象流/压缩流为可读形式(`qpdf --qdf --object-streams=disable in.pdf out.pdf`),配合 `-d` dump 对照
### olevba / rtfobj(Office/RTF 宏与嵌入对象提取)
- 多平台: `pip install oletools`(oletools 内含 olevba、rtfobj、oleobj、msodde、oleid、mraptor 等)
- 验证: `olevba --help`;`rtfobj --help`
- 注意: oledump **不在** oletools 包内——是独立工具(GitHub `DidierStevens/DidierStevensSuite` 取 oledump.py,需 `pip install olefile` 依赖;`python3 oledump.py -s <流号> <文件>` 按流转储)
### xlmdeobfuscator(Excel 4.0 宏去混淆,可选)
- `pip install xlmdeobfuscator`
- 验证: `xlmdeobfuscator --help`
- 适用: .xls 里的 XLM 宏表(olevba 0.54+ 已能识别 XLM,混淆深时用本工具还原)
### LibreOffice(沙箱打开验证,可选)
- Linux: `apt install libreoffice` / `dnf install libreoffice`;macOS: `brew install --cask libreoffice`;Windows: 官方安装包
- 验证: `libreoffice --version`
- 注意: LibreOffice 默认**不执行** VBA 宏(宏安全性限制)——沙箱里要验证宏行为需装真实 Office 或改 LibreOffice 宏安全设置(见 [[gotchas]])
### 7z(OLE/OOXML 解包)
- Linux: `apt install p7zip-full`;macOS: `brew install sevenzip`;Windows: 官方安装包
- 验证: `7z --help`
- 7z 可直接列出 OLE 复合文档内部流(WordDocument/1Table/VBA/PROJECT 等)与 OOXML 包内文件
## 操作步骤
按顺序执行;每步产物存档(路径 + sha256,见 [[re-triage]])。
1. **文档类型识别**:
```sh
file sample.pdf sample.docm sample.rtf
```
- PDF:`%PDF` 头;Office 旧格式:CFB(OLE 复合文档,`D0 CF 11 E0`);OOXML:zip(`PK`);RTF:`{\rtf`
- 类型决定后续工具链;多类型混合(如 PDF 内嵌 OLE)按各分支分别走
2. **PDF 静态分析**:
```sh
pdfid.py sample.pdf # 对象计数,恶意特征速览
pdf-parser -f sample.pdf | head -50 # 对象树遍历(-f 经过 filter 解压流)
pdf-parser -s /JS sample.pdf # JavaScript 动作
pdf-parser -s /OpenAction sample.pdf # 打开即执行动作
pdf-parser -O sample.pdf # 解析对象流(ObjStm)内的对象
```
- 恶意特征:`/JS`(JavaScript 动作)、`/OpenAction`(打开触发)、`/AA`(附加动作,页级/文档级)、`/Launch`(外部程序)、`/EmbeddedFile`(嵌入文件)、`/RichMedia`(Flash 遗留)
- 漏洞文档:`/JBIG2Decode`(JBIG2 漏洞)、`/RichMedia` 等 CVE 对应结构——按结构特征(非版本号)对照已知利用模式
- JS 载荷 → [[re-script-deob]] 还原;结构可疑但对象树不全时先 `qpdf --qdf --object-streams=disable` 展开再看
3. **Office 宏提取**:
```sh
olevba -c sample.docm > macro.txt # 提取宏源码(-c 只输出源码)
olevba --decode -c sample.docm > macro_decoded.txt # 自动解常见字符串混淆(--decode 自 oletools 0.24 起;源码内解码串显示用 --reveal)
olevba --deobf -c sample.docm # 表达式级去混淆(0.46+)
strings sample.docm | grep -iE 'DDEAUTO|http|powershell' | head
```
- 宏链入口:Word `Document_Open` / `AutoOpen` / `AutoExec`;Excel `Workbook_Open` / `Auto_Open`;表单控件/快捷键/自定义按钮触发也不少见
- 其他向量:DDE 域(`DDEAUTO`,较新 Word 默认禁用——旧版仍触发)、外部链接(`/hyperlink`)、OLE 嵌入对象(`oleobj` 提取后逐个分析)、Excel 4.0 XLM 宏表(`xlmdeobfuscator`)
- olevba 输出警告 `VBA stomping` 时注意:实际执行的是 P-code 而非提取出的源码(见坑 2)
- 宏载荷 → [[re-script-deob]] 去混淆链
4. **RTF 分析**:
```sh
rtfobj sample.rtf # 列出嵌入 OLE 对象与 CLSID
```
- RTF 可整篇 `\'XX` 十六进制转义混淆——先还原转义再查关键字
- 嵌入对象(`\objdata`)提取后按 OLE/CFB 分支继续;漏洞文档常见 `\pict` 内嵌数据
5. **模板注入与关系文件(OOXML)**:
```sh
7z l sample.docx | head -40
7z x sample.docx -oext/
grep -r 'TargetMode="External"' ext/word/_rels/ 2>/dev/null
```
- 检查全部 rels 文件:`document.xml.rels` 中 `attachedTemplate` / `externalLink` 外部目标 = 远程模板注入
- 模板注入让文档无宏也有外联行为(远程模板带宏)——解包后逐个 rels 核对
6. **载荷提取**:
- 提取项:脚本/URL/二进制/多级载荷(每层存档编号 + sha256,路径与哈希进 [[analysis-contract]])
- 指标提取 → [[re-ioc]](URL/域名/哈希);提取物初勘 → [[re-triage]]
- 多级链(文档 → 脚本 → 下载器 → 可执行)逐层展开,每层注明触发方式
7. **动态验证**(沙箱):
- [[re-sandbox]] 内用 LibreOffice/阅读器打开,网络隔离(INetSim/fake DNS)
- 观察:文件释放、进程链、网络回连([[re-behavior]] 衔接);延长观察窗口、模拟用户交互对抗延迟执行(见坑 6)
- 宏执行验证优先用真实 Office(LibreOffice 默认不执行 VBA,见工具准备)
## 跨域联合
- [[re-malware]] 网关:本技能归属(选择树「钓鱼附件」分支)
- [[re-script-deob]]:宏/JS 去混淆还原
- [[re-sandbox]]:动态打开强制前置
- [[re-ioc]]:提取指标
- [[re-behavior]]:行为验证衔接
- [[re-triage]]:哈希/初勘与载荷提取存证
## 常见坑与陷阱
- **PDF 对象流压缩未解**:现象——pdf-parser 输出无 `/JS`;原因——对象在 FlateDecode 压缩流或 ObjStm 内;对策——`-f` 强制解压、`-O` 展开对象流再查
- **VBA stomping(执行与源码不一致)**:现象——olevba 提取的源码看起来无害/乱码,但动态行为异常;原因——Office 执行编译后的 P-code,源码只是存档(可被替换/截断);对策——olevba 警告时以动态行为为准,必要时 pcodedmp 反汇编 P-code 对照
- **宏被混淆**:现象——olevba 提取后满屏拼接/编码;原因——宏混淆;对策——`--decode`/`--reveal`/`--deobf` 逐层解,[[re-script-deob]] 去混淆链
- **文档漏洞版本特征**:现象——结构特征与已知 CVE 不符;原因——利用代码针对特定版本;对策——按结构特征(非版本号)对照利用模式,标注版本假设
- **MOTW 拦宏(沙箱执行失败)**:现象——沙箱里打开文档宏不触发;原因——下载文件带 Mark-of-the-Web(Zone.Identifier),Office 2016+ 默认拦截其宏;对策——Windows 侧 `Unblock-File`(PowerShell)或删 Zone.Identifier ADS 后再开;检测型文档见坑 6
- **沙箱检测文档(延迟执行)**:现象——沙箱内无行为;原因——文档检测环境后不触发或推迟触发;对策——延长观察窗口、模拟用户交互(滚动/点击/Office 版本核对)
- **模板注入易漏**:现象——宏正常但仍有外联;原因——`/word/_rels` 远程模板;对策——解包后检查全部 rels 文件(步骤 5)
- 工具差异与更多边界(oledump 用法、RTF 反例、LibreOffice 宏设置)见 [[gotchas]] 与 [[decision-tree]]
Ships with 2 supporting files:
- references/decision-tree.md
- references/gotchas.md
Mirrored from the author's public source. Install counts from the open skills registry.