Using Reflection for AMSI Bypass

Converting an already available AMSI Bypass to FULL in memory AMSI Bypass

Already Existing Bypass and the Issue

I was reading for the AMSI Bypasses and found the Bypass documented by Contextis at https://www.contextis.com/en/blog/amsi-bypass. Now everything as good here with the bypass except one problem. The problem is that the bypass is using Add-Type . Whenever you use Add-Type, the code gets written to a temporary file and then csc.exe is used to compile a binary which stays on disk. This creates a problem when you want to stay stealthy and don't want to write any artifact on disk.

PowerShell writing to disk

Once Powershell Writes the script on disk, CSC then compiles it

CSC.exe compiling the script

Solution: Reflection

Matt Graeber in his post on exploit-monday.com go into great detail on how to use reflection for accessing Win32 API . Please refer to blog post to understand how Reflection works.

Modified Script

After using Reflection here is the Modified Script to Bypass AMSI.

The script can also be downloaded from this gist https://gist.github.com/shantanu561993/6483e524dc225a188de04465c8512909

The advantage of using reflection is that there is no Temporary file and no calls to csc which allows the script to stay fully in memory.

No Temporary files by powershell and no CSC.exe compilation .

This means the bypass is full in memory which is the end result. :)

Credits

Matt: https://twitter.com/mattifestation Paul: https://twitter.com/am0nsec

Last updated

Was this helpful?