Bypass Windows Defense
Bypass Windows Defense
Bypass Windows Defense
https://twitter.com/EmericNasi
http://blog.sevagas.com - https://github.com/sevagas
License: This work is licensed under a Creative Commons Attribution 4.0 International License
I. Introduction
The last years, I have been doing some research around Windows security. I liked exploring
APT/Redteam techniques and payload used for social engineering and airgap bypass attacks. I am
naturally interested into new security features such as ASR.
Microsoft introduced Attack Surface Reduction (ASR) as part of Windows defender exploit guard.
ASR is composed of a set of configurable rules such as: "Block Office applications from creating child
process". While these rules seem effective against common Office and scripts malwares, there are
ways to bypass all of them. We will go over multiple rules, mainly related to malicious Office or VB
scripts behavior, analyze how It work behind the scene and find a way to bypass it.
Note: I wrote the macro_pack tool to automatize generation and obfuscation of these kind of payloads
(malicious Office, VBScript, HTA, LNK, etc.). You can have look at macro_pack tool on GitHub. We are
going to rely on this tool to generate the payloads in the current document
I. Introduction ..................................................................................................................................... 1
II. Table of content .............................................................................................................................. 1
III. What is ASR?................................................................................................................................ 3
What is great about ASR? .................................................................................................................... 3
Configure ASR ...................................................................................................................................... 4
Monitor ASR ........................................................................................................................................ 5
IV. Context ........................................................................................................................................ 6
V. Block all Office applications from creating child processes ............................................................ 7
Trigger rule .......................................................................................................................................... 7
Partial bypass....................................................................................................................................... 8
Full bypass ........................................................................................................................................... 9
VI. Block Office applications from creating executable content .................................................... 12
Trigger rule ........................................................................................................................................ 12
Bypass rule ........................................................................................................................................ 13
VII. Block Win32 API calls from Office macro .................................................................................. 14
Trigger rule ........................................................................................................................................ 14
Bypass rule ........................................................................................................................................ 15
VIII. Block Office applications from injecting code into other processes ......................................... 16
Trigger rule ........................................................................................................................................ 16
Bypass rule ........................................................................................................................................ 17
1
IX. Block JavaScript or VBScript from launching downloaded executable content........................ 18
Trigger rule? ...................................................................................................................................... 18
Trigger rule! ....................................................................................................................................... 19
Bypass rule ........................................................................................................................................ 19
X. Block execution of potentially obfuscated scripts ........................................................................ 20
Trigger rule ....................................................................................................................................... 20
XI. Block untrusted and unsigned processes that run from USB.................................................... 21
Trigger rule ........................................................................................................................................ 21
Bypass rule ........................................................................................................................................ 22
XII. Block process creations originating from PSExec and WMI commands ................................... 23
Lateral movement workaround ........................................................................................................ 23
More about lateral movement .......................................................................................................... 24
Break the PsExec rule ........................................................................................................................ 24
XIII. Bypass ALL Scenario .................................................................................................................. 26
Entry Point ......................................................................................................................................... 26
Download .......................................................................................................................................... 26
Execute and bypass ASR .................................................................................................................... 27
Bypass UAC ........................................................................................................................................ 27
Test result .......................................................................................................................................... 28
XIV. To sum up .................................................................................................................................. 29
2
III. What is ASR?
“Attack surface reduction is a feature that helps prevent actions and apps that are typically used by
exploit-seeking malware to infect machines.”
https://docs.microsoft.com/en-us/windows/security/threat-protection/windows-defender-exploit-
guard/enable-attack-surface-reduction
Office documents and scripts are also often used in advanced attack scenario to bypass security
mechanisms.
My opinion is that with ASR, Microsoft attempt to shut down whole category of phishing exploits.
For example, the rule “Block all Office applications from creating child processes” probably block 99.9%
macro-based droppers found in the wild.
The Malicious Office VBA malware described in the Botconf 2018 talk ““Stagecraft of Malicious Office
Documents – A look at Recent Campaigns” could all be disarmed by this single rule.
In my opinion again, such security policy could change the future of information security (imagine no
more malicious VBA, no more droppers, no more malicious USB key…)
The problem is currently, ASR rules are easy to bypass and often rules are too limited or even broken.
3
Configure ASR
Basically, ASR is a policy consisting in a set of rules which can be set to:
• 0 – Disabled (default)
• 1 – Enabled
• 2 – Audit
To configure the rules you may use Group policy or PowerShell (Follow instructions at
https://docs.microsoft.com/en-us/windows/security/threat-protection/windows-defender-exploit-
guard/enable-attack-surface-reduction)
Via Group Policy Management Editor you can access this GUI (not really user friendly as you have to
know and type the GUID without help about the related rule description)
4
Note: Rules can be found in registry.
• Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Group Policy
Objects\{5B492C3C-4EAB-494D-B7DD-
F0FB0FD3A17D}Machine\Software\Policies\Microsoft\Windows Defender\Windows
Defender Exploit Guard\ASR\Rules
• HKLM\SOFTWARE\Policies\Microsoft\Windows Defender\Windows Defender Exploit
Guard\ASR\Rules\d1e49aac-8f56-4280-b9ba-993a6d77406c
• \HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Group Policy
Objects\{9CC79454-DCDF-422D-A24C-
81990D96B449}Machine\Software\Policies\Microsoft\Windows Defender\Windows
Defender Exploit Guard\ASR\Rules
Monitor ASR
You can monitor ASR relative events with Event Viewer by following the instructions here.
5
IV. Context
Since I have been writing Office and VbScript payloads, I wanted to test Office and scripts related rules.
I also added the WMI/PSexec prevention and the USB related rules because these are commonly used
in attack scenarios.
If you are familiar with common malwares and offensive tools, you may already realize that the above
set of rules is enough to block most malicious vectors and attack scenario.
6
V. Block all Office applications from creating child processes
docs.microsoft.com
Trigger rule
This rule is very effective, it prevents running and program or command line from an Office application,
it is effective against all kind of attacks such as macro or DDE.
So how to bypass? Well the answer is in the name of the rule. “Block all Office applications from
creating child processes”. Let’s assume the rule is not buggy and does not have flaws. Instead of
bypassing it, we can just go around!
We just have to execute processes in a way they are not an office application child! And there are
plenty of methods to do that, at least from inside a macro.
The next code snippet is a classic way to execute a payload in VBA or VBScript.
This code is obviously blocked by the ASR rule. Same as using VBA “Shell”, “ShellExecute” functions,
using DDE attacks or using Excel COM object.
7
Partial bypass
Test with WMI
Execution using WMI is a classic for macro malware. Here is one way to do it:
Another COM object which is often describe as an alternative to execute a command it the Outlook
Application object.
8
Full bypass
Test with Task Scheduler
This is the first method I came with when I heard about ASR. I thought, well, if my application is not
allowed to start a process, let’s just use the task scheduler for that!
This method allows to execute any commands with all ASR rules enabled.
“Represents a collection of the open windows that belong to the Shell. Methods associated with this
objects can control and execute commands within the Shell, and obtain other Shell-related objects.”
https://docs.microsoft.com/en-gb/windows/desktop/shell/shellwindows
9
The parent process is Explorer.exe so it’s not caught by the ASR
Since we have access to the registry, we can simply just create a new rogue COM object with
LocalServer32 set and call it.
10
When this code is run, the target application is executed when the object is created. This method is a
full ASR bypass
To sum up
Note: There are other bypass methods for this ASR rule. Discovering them is left as an exercise for
the reader ☺
11
VI. Block Office applications from creating executable content
Extensions will be blocked from being used by Office apps. Typically these
extensions use the Windows Scripting Host (.wsh files) to run scripts that
automate certain tasks or provide user-created add-on features.”
docs.microsoft.com
Trigger rule
This rule prevents an office application from saving an executable file or a script on the filesystem.
We instruct macro_pack to create an Excel dropper which will download putty, save it as
“dropped.exe” in TEMP, and execute it.
Test Results:
• Dropping a file with .hta extension -> Blocked by ASR
• Dropping a file with .exe extension -> Blocked by VBA AMSI
It seems it’s the same for child process which create files (example using curl).
After some tests, I figured out that this feature seems to be based only on the extension. For example,
it is possible to download and save a Visual Basic Script file as txt file and ASR will not be triggered.
Then it’s possible to use one of the methods described in de previous section to move/rename the file.
12
Bypass rule
Here is an example of code which can be use by a dropper and bypass both ASR and VBA AMSI.
In this code, I download the file using a decoy “.txt” file. Then I use the command line to move this file
to the real path with the real extension.
Note: On previous implementation of this rule (before AMSI was enforced on Office VBA), the rule was
behaving differently for binary files. Downloading and saving a binary file as txt used to trigger ASR. So,
it means that for binaries, the format was evaluated, not the extension.
Note 2: It seems that this ASR rule will not work depending on the file name. I have no clue why it’s
the case.
13
VII. Block Win32 API calls from Office macro
This rule attempts to block Office files that contain macro code that is capable of
importing Win32 DLLs.”
docs.microsoft.com
Trigger rule
One of the reason VBA is so powerful is that you can call any function from windows API. VBA can in
fact load DLL and call functions. This is used for example in the VBA format of meterpreter payload
generated by msfvenom. Here Microsoft is telling us that this kind of usage is disabled by ASR.
It seems this rule will not trigger for an existing instance of Excel. It will however prevent a document
which contains macro calling Win32 API to start. Even before macro are enabled, the document will
not be loaded. I
f this rule is activated while an Excel instance is already running, it will not prevent the already activated
macro to call Win32, however saving the document will not be possible.
The fact the document is rejected even before macro is enabled is interesting. How is the call to Win32
API recognized? Maybe by recognizing Win32 DLL name in the code?
I already know that it’s possible to load any DLL from a macro, and not only WIN32 API. All you need
for that is to have the macro running in the same path as the DLL. This is used for example by the
DROPPER_DLL macro_pack template.
14
Bypass rule
To attempt ASR rule bypass, I found out I just have to copy the DLL I need in a folder, change the macro
current directory to the same folder, then call the Win32 API function. The next code shows how you
can bypass ASR and call the Kernel32.dll Sleep function:
This confirm what I tough, this rule is probably based only on a blacklist of Win32 DLL and is easy to
bypass.
Note: The loaded DLL does not have necessarily to have “.dll” extension. This is interesting to know if
you need to drop/load malicious DLLs from and Office macro.
15
VIII. Block Office applications from injecting code into other
processes
This is typically used by malware to run malicious code in an attempt to hide the
activity from antivirus scanning engines.”
docs.microsoft.com
Trigger rule
To test this rule I used a macro_pack reverse HTTPS meterpreter template. It is based on VBS meter
by @Cneelis (original script is available at: https://github.com/Cn33liz/VBSMeter ).
The command will generate a PowerPoint file containing the malicious macro and a “webmeter.rc”
file to run with msfconsole -r.
webmeter.rc contains:
The behavior when ASR is disabled is it automatically spawn a notepad.exe child process and inject
into it. But when ASR rule 75668C1F-73B5-4CF0-BB93-3ECF5CB7CC84 is enabled:
16
Same if I try to migrate manually from the meterpreter session:
Bypass rule
The migration failed but meterpreter is running meaning it could inject into the office application in
the first place. It seems that the current Office application process is not itself concerned by the process
injection rule.
So if we want a nice silent background session, we can just spawn another hidden instance of office
and run the meterpreter from there. This can be done with macro_pack “--background” option.
Now if migration is really what you are interested in, there are other methods to tests and I didn’t push
that far (I don’t want to redevelop PE injection and all other possibilities in VBA!). However, since we
can bypass the execution prevention rule, it is always possible to drop an executable/script which will
not run as an Office process child and is not restricted by ASR.
17
IX. Block JavaScript or VBScript from launching downloaded
executable content
This rule prevents these scripts from being allowed to launch apps, thus
preventing malicious use of the scripts to spread malware and infect machines.”
docs.microsoft.com
Trigger rule?
Here is the code of a classic VB dropper:
You can generate similar payload using macro_pack with DROPPER template.
Strangely enough, this script does not trigger the ASR rule. It is in fact prevented to run but not by ASR.
The script is blocked by Windows Defender and AMSI
18
In fact, Windows defender will prevent execution if it detects a call to URL download and the call to
CreateObject(“Wscript.Shell”).Run in the same file.
OK so this is not related to ASR but we cannot just stay with our dropper being detected by Windows
Defender! So as a side note here is a little trick to bypass AMSI with just one line:
➔ Bypass!
Trigger rule!
Let’s go back to ASR, we still don’t know when the rule is triggered.
Turns out it activates if you try to execute a file with the Zone.Identifier Alternate Data Stream present.
This ADS is used to identify trust zones associated to downloaded files. You can have a look at
https://msdn.microsoft.com/en-us/library/dn392609.aspx for more information.
The Zone.Identifier ADS file is not created when using VB download methods such as
MSXML2.ServerXMLHTTP.6.0. Therefore the ASR rule is not trigger by classic VB droppers.
From tests I ran, the ASR does not seem to care about the trust level indicated inside the Zone.Identifier
ADS. It just rely on it existence to decide that the file is coming from the Internet.
Bypass rule
One way to bypass the rule is to remove the ADS which is not that simple on a Windows 10 machine.
Here are command lines you can use to remove the ADS:
After that, the target application can be called, ASR will not be triggered.
Note: Most dropper will use methods such as the script in the previous page which does not create a
Zone.Identifier ADS so this ASR rule seems pretty useless.
19
X. Block execution of potentially obfuscated scripts
docs.microsoft.com
Trigger rule ☹
First I tried to test using macro_pack obfuscation. I created a non-obfuscated CMD vbs file using:
The result:
The result:
Obviously, anyone could see that the second script is obfuscated, however when I executes it, ASR was
not triggered. This ASR rules was tested by other people without success either. It seems the feature
is not mature, see https://www.darkoperator.com/blog/2017/11/8/windows-defender-exploit-guard-
asr-obfuscated-script-rule. The author tested basic public encoder for VBscript and Powershell and
they did not trigger the rule.
20
XI. Block untrusted and unsigned processes that run from USB
docs.microsoft.com
Trigger rule
I generated several payloads using the macro_pack CMD template to generate a payload which starts
calc.exe. Here my USB key drive is “G:”
HTA payload:
echo calc.exe | macro_pack.py -t CMD -G G:\test.hta
No problem to run script, ASR rule not triggered.
VBS payload:
echo calc.exe | macro_pack.py -t CMD -G G:\test.vbs
No problem to run script, ASR rule not triggered.
LNK payload:
echo calc.exe . | macro_pack.py -G G:\test.lnk
No problem to run shortcut, ASR rule not triggered.
Non-signed binary:
curl https://the.earth.li/~sgtatham/putty/0.70/w32/putty.exe --output G:\putty_badsignature.exe
echo 0 >> G:\ putty_badsignature.exe # Break signature by appending a char at EOF
ASR rule triggered!
21
Bypass rule
Since scripts are not blocked all I need to run an unsigned executable is to:
It happens macro_pack has an option to embed an exe inside a script or an office macro:
macro_pack -t EMBED_EXE -e G:\putty_badsignature.exe -G drop_bad_putty.vbs
This rule has very interesting potential but current implementation is way too limited to be useful
against intelligent attackers.
22
XII. Block process creations originating from PSExec and WMI
commands
docs.microsoft.com
Since I already described how to download and execute with ASR enabled. In this section I want to
put emphasis on PsExec itself and lateral movement.
Since these are blocked, let’s use another other way. One solution is to use DCOM object methods.
We already used some DCOM objects earlier to bypass the execution prevention rule.
Using ShellBrowserWindow
If windows firewall is enabled, it will popup and ask if you want to authorize “explorer.exe”.
23
More about lateral movement
Being able to move laterally on a Domain generally means you have some administrator rights. And
qdmin can remotely disable ASR!
This can be done using remote PowerShell and the Set-MpPreference cmdlet
https://www.fortynorthsecurity.com/windows-asr-rules-reenabling-wmi-when-blocked/
PsExec relies on the PSEXESVC service. Each time PsExec is run, the PSEXESVC.exe file is extracted
and dropped In C:/Windows and used to start a service.
First we extract PSEXESVC (you can just find it in %windir% when you run PsExec).
Next we copy the file in %TEMP% for example, and register the service with:
PSEXESVC.exe -install
You can see a service called PsInfo Service installed.
24
When you are done you can remove the PsInfo service with:
PSEXESVC.exe -remove
25
XIII. Bypass ALL Scenario
As a grand finally let’s enable all ASR rules and write a malicious PowerPoint document which:
• Is obfuscated
• Bypasses ASR
• Bypasses AMSI & Antivirus
• Bypasses UAC
• Downloads and Drop putty and run it with elevated privileges
Entry Point
This function is automatically called when macro are enabled on the document. You can see we
download putty.exe and save is as dropped.exe in %TEMP%. Then dropped.exe is executed with high
privileges (BypassUACExec function).
Download
Classic download function modified to use a decoy txt file to avoid ASR and AMSI.
26
Execute and bypass ASR
This function executes a command line by trying different methods, if a method is caught, the
exception handler prevents the script from stopping and the second method is tried.
Bypass UAC
This fileless UAC bypass method combines well with ASR bypass. See http://blog.sevagas.com/?Yet-
another-sdclt-UAC-bypass for the explanation on how this UAC bypass works.
27
Test result
When the PowerPoint file is opened, click on “Enable macro”.
With Sysinternals ProcExp you can verify that putty was downloaded as “dropped.exe” and stared
with elevated privileges.
28
XIV. To sum up
I think ASR are a great feature to prevent common malware attacks. At the same time, most rules seem
broken or way too easy to bypass. In fact, during my tests I can say I had more problems with bypassing
AMSI for scripts/office documents than ASR.
Currently, ASR is not well known by blue teams. Its probable that as more defenders adopt these
measures, attackers will adapt their tools to bypass them.
29