Weaponizing .SettingContent-ms Extensions for Code Execution

Weaponizing .SettingContent-ms Extensions for Code Execution

Matt Nelson (@engima0x3) from SpecterOps recently released a blog post on leveraging a newly discovered filetype extension with the possibility of command execution. This was a fantastic blog, and as attackers, we typically try to find multiple ways to execute code from different delivery systems. This blog is leveraging the awesome research from Matt and using it for code execution on a remote system.

Here are some of the key findings from Matt’s blog post:

Microsoft acknowledged that they probably would not be fixing it.

Works directly from a browser for execution.

Can be embedded into Office documents and can be used for direct execution without Macros.

Can be crafted in order to bypass Microsoft’s Attack Surface Reduction (ASR) using a whitelisted directory through Microsoft Office folders for spawning child processes within the Microsoft Office directory.

Many of these are promising because as attackers, we typically try to find new ways of compromising a system without the ability to use traditional methods (such as Macros, HTAs, etc.). Matt did a great job in researching and explaining how he went from Point A to Point Z to get to the place where he was able to circumvent ASR and get command execution.

On a recent engagement, the customer we were doing a red team engagement for had some solid detection capabilities, and this blog post was timely for us to use in the wild in a real-world simulation. With other traditional discoveries that go public such as DDE Auto in the past, attackers weaponize these techniques quickly (https://sensepost.com/blog/2017/macro-less-code-exec-in-msword/). With DDE, we saw attack vectors within 24 hours. Instead of needing to burn zero-day techniques with our customers (responsible disclosure afterwards), using public techniques that get disclosed is often a good measure of how fast their teams can move to detection (or preventative capabilities).

I decided to take a look at the research and see if I could weaponize it quickly for an engagement. Matt went all the way up to the point of spawning calc.exe (commonly used to demonstrate command/code execution) but didn’t go as far as using this in a weaponized format. The first thing I decided to do was spin up Unicorn (github.com/trustedsec.com/unicorn) to see if I could get an obfuscated payload to run.

Using Unicorn, I generated a quick meterpreter shell to test and pasted it into Matt’s proof of concept, replacing the cmd.exe with powershell.exe:

This will create an obfuscated PowerShell payload and export that as powershell_attacks.txt:

Figure 1 – Obfuscated PowerShell Code

When inserting the long PowerShell string into the .SettingContent-ms file and executing, it appears that the <DeepLink></DeepLink> field errors out:

Figure 2 – Erroring Out Due to Long String

After doing some investigation on the DeepLink field, I noticed that the max character size allowed (including path and everything) is 517 characters. While this is still a great size to play with for our payloads, this means that using large payloads would not be feasible from this attack vector.

There are a number of ways to gain code execution within 517 characters. In the next example, we’ll use a commonly attacked binary with web capabilities called “mshta.exe”. MSHTA is on every supported operating system from Microsoft and has built in ability to download and execute HTA files remotely from the command line. We could use a number of other methods such as the nps_payload (https://www.trustedsec.com/2017/07/new-tool-release-nps_payload/) method, certutil (https://twitter.com/subtee/status/889173356284530688?lang=en), or other methods. HTAs are easy, and you do not need to specify an actual “hta” extension, as most of these extensions are typically blocked.

Let’s first create our payload with Unicorn, which supports HTAs, and see if we can get code execution.

First, we need to generate our malicious HTA, head to the unicorn directory and type:

This will generate the appropriate HTA file once Unicorn has completed. Below is a screenshot of running Unicorn:

Figure 3 – Running Unicorn to Generate Our Payload

We then copy the HTA file over and rename it to LICENSE.txt. In most cases if a incident responder is looking at malicious files, an extension type of .txt or others may throw them off. In addition, if this were a real-world simulation, you would want to use HTTPS and use a legitimate and high reputation (and categorized) domain in order to throw off the blue team.

Lastly, we start Apache in order to host our malicious HTA.

Below is an example of the output from Unicorn of the malicious/obfuscated HTA:

Figure 4 – Output from Unicorn

Next, we create a Test.SettingContent-ms file with our URL to the malicious HTA (LICENSE.txt):

Success! Obviously in this example we used Metasploit, but this will work for pretty much any payload you want. MSHTA is only one example of the ability to remotely download and well within the character size restriction of 517. In addition, you can easily embed these into office documents and ship them off, and as soon as they open, you gain the ability to compromise the computer. You can also host the extension type on a website and coax the user to click on a link. If you want to get crafty, changing things like the icon and others may help. Below is a simple site with a link to download and execute (just a normal href):

Figure 6 – Simple Site with Link

Important Download is the hosted SettingContent-ms file. Once opened, it will compromise the system in a similar fashion.

For defenders, the best and easiest course of action is to block .SettingContent-ms types from the perimeter (to block the web-based attacks), or establish an alert whenever one is downloaded or executed. Additionally, consider changing the file association types for .SettingContent-ms to notepad or something different that does not execute the code.

If you want a quick tool to automatically do this for you, I’ve written a quick tool called auto_settingcontent-ms.py which creates the Metasploit payload, HTA, and Test.SettingContent-ms file for you automatically. This assumes you have Metasploit, Python, and Apache installed already. Note that this is just an example, there are many other methods, this is just a quick POC.

This is a simple script that will generate a .SettingContent-ms file which can be used for command/code execution on the system. This file can be hosted inside of an office document, or on a website for the victim to click and gain code execution. This is just a POC and used with Metasploit. You can use whatever you want as far as payload, in this example we are just using windows/meterpreter/reverse_https.