Detect Template Injection in Google Chronicle
Adversaries abuse template references embedded in Office Open XML (OOXML) documents and RTF files to conceal and deliver malicious payloads. DOCX, XLSX, and PPTX files are ZIP archives containing an XML relationship file (word/_rels/document.xml.rels) that can reference an external template URL via an attachedTemplate relationship. When the document is opened, the Office application fetches the remote template, which may deliver VBA macros, exploits, or shellcode that are absent from the original lure document — bypassing static file analysis. RTF files can be modified to include a \*\template control word pointing to a remote URL, triggering a fetch on open. Both vectors are used to deliver malicious macros (APT28 remote template macro delivery), execute exploits (Confucius, WarzoneRAT via RTF exploit embedding), or capture NTLM credentials by injecting SMB UNC paths that trigger forced authentication (Dragonfly, DarkHydrus/Phishery). Real-world campaigns frequently deliver these lures via phishing (T1566) or tainted shared content (T1080). The technique is effective because the initial document contains no traditional indicators — no embedded VBA, no OLE streams, no scripts — making gateway scanning and sandboxes that do not perform dynamic network fetching ineffective.
MITRE ATT&CK
- Tactic
- Defense Evasion
- Technique
- T1221 Template Injection
- Canonical reference
- https://attack.mitre.org/techniques/T1221/
YARA-L Detection Query
rule t1221_template_injection_detection {
meta:
author = "Detection Engineering"
description = "Detects MITRE ATT&CK T1221 Template Injection via Office remote template HTTP/SMB fetch and LOLBin child process spawning"
severity = "HIGH"
priority = "HIGH"
mitre_attack_tactic = "Defense Evasion"
mitre_attack_technique = "T1221"
mitre_attack_technique_id = "T1221"
reference = "https://attack.mitre.org/techniques/T1221/"
created = "2026-04-13"
version = "1.0"
events:
(
(
// Branch 1 & 2: Office process initiating outbound network connection
$e.metadata.event_type = "NETWORK_CONNECTION"
and re.regex(
$e.principal.process.file.full_path,
`(?i)(winword|excel|powerpnt|mspub|msaccess|visio)\.exe$`
)
and (
(
// Branch 1: Remote template HTTP/HTTPS fetch to non-Microsoft host
(
$e.target.port = 80 or
$e.target.port = 443 or
$e.target.port = 8080 or
$e.target.port = 8443
)
and not re.regex(
$e.target.hostname,
`(?i)(microsoft\.com|office\.com|live\.com|microsoftonline\.com|windows\.net|sharepoint\.com|officecdn\.microsoft\.com|officecdna\.microsoft\.com|azure\.com|azurefd\.net|bing\.com|msecnd\.net|skype\.com|trafficmanager\.net|msftncsi\.com)`
)
)
or
// Branch 2: Forced NTLM auth via SMB UNC path template reference
$e.target.port = 445
)
)
or
(
// Branch 3: Office process spawning LOLBin child — post-template payload execution
$e.metadata.event_type = "PROCESS_LAUNCH"
and re.regex(
$e.principal.process.file.full_path,
`(?i)(winword|excel|powerpnt|mspub|msaccess|visio)\.exe$`
)
and re.regex(
$e.target.process.file.full_path,
`(?i)(cmd|powershell|pwsh|wscript|cscript|mshta|rundll32|regsvr32|certutil|bitsadmin|wmic|msiexec|svchost|conhost)\.exe$`
)
)
)
condition:
$e
} Chronicle YARA-L 2.0 rule detecting MITRE ATT&CK T1221 Template Injection using three UDM behavioral patterns. NETWORK_CONNECTION events: Office applications (winword.exe, excel.exe, powerpnt.exe, mspub.exe, msaccess.exe, visio.exe) connecting to non-Microsoft HTTP/HTTPS endpoints on ports 80, 443, 8080, or 8443, indicating weaponised OOXML/RTF documents fetching remote templates containing macros or exploits absent from the lure file. NETWORK_CONNECTION on port 445: Office applications initiating SMB connections indicating forced NTLM authentication via UNC path template injection (Dragonfly, DarkHydrus/Phishery techniques). PROCESS_LAUNCH: Office applications spawning LOLBin child processes (cmd.exe, powershell.exe, mshta.exe, rundll32.exe, etc.) indicating macro or exploit execution following remote template load. Non-Microsoft infrastructure exclusion applied to the HTTP/HTTPS branch via re.regex on target.hostname.
Data Sources
Required Tables
False Positives & Tuning
- Legitimate corporate document templates hosted on external cloud storage providers or partner portals (Box, Dropbox, vendor extranets) where the template URL references a non-Microsoft host — maintain a Chronicle reference list of approved external template hosting domains and extend the not re.regex exclusion to match against it
- Enterprise Office add-ins deployed via centralised policy that call vendor licensing, telemetry, or content endpoints over HTTP/HTTPS from within Office process context — these appear identical to remote template fetch NETWORK_CONNECTION events in UDM; cross-reference the add-in inventory against flagged destination hostnames
- Automated document processing workflows (legal contract management, insurance form ingestion, financial report generation) where Office is driven via COM automation and spawns PowerShell or CMD for file output or format conversion — distinguish using principal.process.command_line to identify non-interactive invocations
Other platforms for T1221
Testing Methodology
Validate this detection against 5 adversary techniques from Atomic Red Team. Each test below lists the behaviour to exercise and the telemetry you should expect to see. Executable commands and cleanup steps are available with Pro.
- Test 1OOXML Remote Template Injection — DOCX with External attachedTemplate
Expected signal: When document is opened in Word: Sysmon Event ID 3 with Image=winword.exe, DestinationIp=127.0.0.1, DestinationPort=8080. Sysmon Event ID 22 (DNS Query) if a FQDN is used instead of localhost. DeviceNetworkEvents in MDE will show InitiatingProcessFileName=winword.exe with RemoteIP=127.0.0.1 and RemotePort=8080. The connection will be refused (no listener) but the event fires before the TCP RST.
- Test 2RTF Template Injection — \*\template Control Word with HTTP URL
Expected signal: When opened in Word: Sysmon Event ID 3 with Image=winword.exe, DestinationIp=127.0.0.1, DestinationPort=8080. DeviceFileEvents may show the RTF file being read from its download location. The RTF \*\template control word triggers the same network fetch mechanism as the OOXML attachedTemplate relationship.
- Test 3Forced Authentication via SMB UNC Template Reference
Expected signal: Sysmon Event ID 3 with Image=winword.exe, DestinationPort=445, DestinationIp=127.0.0.1. On a real attack with an external IP: Security Event ID 4648 (explicit credential use) or 4624 (NTLM logon type 3) on the domain controller. Sysmon Event ID 22 may show DNS lookup if a hostname is used instead of IP. DeviceNetworkEvents in MDE shows InitiatingProcessFileName=winword.exe, RemotePort=445.
- Test 4Phishery-style Template URL Injection into Existing DOCX
Expected signal: File creation events (Sysmon EventCode=11) for both the original and injected DOCX in %TEMP%. When the injected DOCX is opened: Sysmon EventCode=3 from winword.exe to 127.0.0.1:8080. The manipulation of the ZIP archive using System.IO.Compression is visible as PowerShell process events (Sysmon EventCode=1) before the Office network event — the chain of PowerShell→file creation→Office network connection is the full kill chain telemetry.
- Test 5Verify Template Injection Document Structure — Manual Inspection
Expected signal: PowerShell process creation (Sysmon EventCode=1) with command line containing System.IO.Compression.ZipFile. No network events — this is static analysis only. The script outputs the injected URL to the console, confirming the template injection payload is present before any execution occurs.
References (12)
- https://attack.mitre.org/techniques/T1221/
- https://www.sans.org/reading-room/whitepapers/testing/template-injection-attacks-bypassing-security-controls-living-land-38780
- http://blog.redxorblue.com/2018/07/executing-macros-from-docx-with-remote.html
- https://www.proofpoint.com/us/blog/threat-insight/injection-new-black-novel-rtf-template-inject-technique-poised-widespread
- https://blog.talosintelligence.com/2017/07/template-injection.html
- https://github.com/ryhanson/phishery
- https://forum.anomali.com/t/credential-harvesting-and-malicious-file-delivery-using-microsoft-office-template-injection/2104
- https://us-cert.cisa.gov/ncas/alerts/TA18-074A
- https://www.cybereason.com/blog/chaes-malware-targeting-latin-american-banking-customers
- https://unit42.paloaltonetworks.com/unit42-sofacy-continues-global-attacks-wheels-new-cannon-trojan/
- https://docs.microsoft.com/previous-versions/office/developer/office-2007/aa338205(v=office.12)
- https://github.com/redcanaryco/atomic-red-team/blob/master/atomics/T1221/T1221.md
Unlock Pro Content
Get the full detection package for T1221 including response playbook, investigation guide, and atomic red team tests.