Detect Template Injection in Elastic Security
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/
Elastic Detection Query
any where
(
(
event.category == "network" and
event.type == "connection" and
process.name in~ ("winword.exe", "excel.exe", "powerpnt.exe", "mspub.exe", "msaccess.exe", "visio.exe") and
(
(
destination.port in (80, 443, 8080, 8443) and
not destination.domain : (
"*.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 destination.port == 445
)
)
or
(
event.category == "process" and
event.type == "start" and
process.parent.name in~ (
"winword.exe", "excel.exe", "powerpnt.exe",
"mspub.exe", "msaccess.exe", "visio.exe"
) and
process.name in~ (
"cmd.exe", "powershell.exe", "pwsh.exe", "wscript.exe", "cscript.exe",
"mshta.exe", "rundll32.exe", "regsvr32.exe", "certutil.exe",
"bitsadmin.exe", "wmic.exe", "msiexec.exe", "svchost.exe", "conhost.exe"
)
)
) Detects T1221 Template Injection across three behavioral branches using Elastic ECS fields. Branch 1: Office applications (winword.exe, excel.exe, powerpnt.exe, mspub.exe, msaccess.exe, visio.exe) initiating outbound HTTP/HTTPS connections to non-Microsoft external hosts, indicating weaponised OOXML or RTF files fetching remote templates containing macros, exploits, or shellcode absent from the lure document. Branch 2: Office applications connecting to remote port 445, indicating forced NTLM authentication via UNC path template references (e.g. Dragonfly/Phishery campaigns). Branch 3: Office applications spawning LOLBin child processes (cmd.exe, powershell.exe, mshta.exe, etc.), indicating macro or exploit execution following remote template load. Runs across logs-endpoint.events.network-* and logs-endpoint.events.process-* index sets.
Data Sources
Required Tables
False Positives & Tuning
- Third-party Office add-ins such as Grammarly, DocuSign, or Adobe Sign that establish outbound HTTP/HTTPS connections from within Office process space during normal document open or interaction — tune by adding verified vendor domains to the destination.domain exclusion list
- Corporate document templates legitimately hosted on non-Microsoft external providers such as Box, Dropbox, or on-premises SharePoint with a custom domain — these trigger RemoteTemplateFetch events but are benign; maintain an approved template-hosting domain allowlist and extend the exclusion wildcard set
- IT automation and document-processing pipelines (PDF converters, batch reporting tools, legal contract management systems) that drive Office via COM automation and subsequently invoke PowerShell or cmd.exe for file output or post-processing — correlate process.parent.command_line to distinguish scripted automation from interactive user sessions
- Security tooling such as EDR sensors or DLP agents that hook into Office processes and generate synthetic network connections or child process events as part of their monitoring instrumentation
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.