On this blog post I will cover my testing of the Attack Surface Reduction rule for Potentially Obfuscated Scripts. This is one of the features that intrigued me the most. One obfuscates the scripts for several reasons:
- Bypass detection controls like AV, automatic log analysis and other controls.
- Hinder analysis of the script to determine its purpose and action.
On the offense side it is a requirement given how more tools are improving their detection the obfuscation allows to bypass them but also has the drawback of bringing attention to the code since it will look out of place. By detecting obfuscation and blocking its execution it would impact the capabilities of some actors limiting the tool-set they can use.
Microsoft describes the rule as follows in their documentation:
Rule: Block execution of potentially obfuscated scripts Malware and other threats can attempt to obfuscate or hide their malicious code in some script files. This rule prevents scripts that appear to be obfuscated from running. It uses the AntiMalwareScanInterface (AMSI) to determine if a script is potentially obfuscated, and then blocks such a script, or blocks scripts when an attempt is made to access them.
Based on the description the rule is meant to block potentially obfuscated scripts. To test this I will use the "Windows Defender Exploit Guard evaluation package" tool to see how Microsoft is testing the rule.
I launch the tool, select the rule for script obfuscation and select the setting to be Audit. I run the scenario the scenario twice.
In Advanced Settings I select the option of "Leave Dirty" so I can take a look at the files it used for the test.
The PowerShell script that it used for testing did not looked obfuscated at all .
The Jscript did looked like it had an offuscated string that it never used and it then launched notpad.exe using a none obfuscated wscript.shell call.
I enabled using the PowerShell cmdlet the rule for obfuscated scripts.
Add-MpPreference -AttackSurfaceReductionRules_Ids 5BEB7EFE-FD9A-4556-801D-275E5FFC04CC -AttackSurfaceReductionRules_Actions Enabled
I will test first how well it detects obfuscated PowerShell script. First test is a control one, I will run the same script that MS provides for testing.
We can see it was blocked. It states it was for the PowerSploit cmdlet name. In the event log the event has an ID of 1121 like all ASR rules, the rule is specified in the EventData section of the event and it shows that was the script that was blocked.
I decided to build my own script and have the PowerSploit command in it, I decided to use Daniel Bohannon Invoke-Obfuscation module to obfuscate the script to see how well it works.
Write-Host 'Getting system info' Get-WmiObject -Class "win32_operatingsystem" Write-Host 'Starting a process' Start-Process 'notepad.exe' invoke-reflectivepeinjection -pebytes
I saved the script on the target machine with no obfuscation. As soon as I saved it was caught and blocked by the ASR rule. I did not even needed to run script.
First method of obfuscation I uses was the string concatination technique in the tool.
I ran the script and it executed with no problems to my surprise.
Next I obfuscated the script with the reorder technique
Ran the script again and to my surprised it executed without any problems. The rule is not catching at all the obfuscated script.
For PowerShell the ASR rule provided in my testing no detection of any obfuscated script.
For JS I ran it's own copy of the obfuscated JS and it triggered. Once I removed the none obfuscated section that launched notepad.exe it was not being blocked.
Sadly I do not have a tool to obfuscate Jscript reliably but there are many for VBScript. I will use the Scripting.Encoder COM object to encode a script that launches notepad.exe to see if it gets detected.
Dim obj0 Set obj0 = CreateObject("Wscript.Shell") obj0.Run "notepad.exe"
Once encoded it looks like
#@~^TgAAAA==9b:~K4N!@#@&?OPK4%!,'~/M+lDnr(L+1OcJq/1DrwO UtnV^E#@#@&G8N!R]; PExKOwC9R6+r5hcAAA==^#~@
Next I tried obfuscation instead of encoding by using a free online obfuscation tool https://isvbscriptdead.com/vbs-obfuscator/.
Again the script executed without any issues.
I have to admit I'm very disappointed with this ASR mitigation. I was hopping it would behave like Daniel Bohannon and Lee Holmes tool Revoke-Obfuscation that detects obfuscated scripts with great accuracy.
I would recommend to not rely on this rule and hopefully Microsoft will improve on it making it trully valuable mitigation.