Powershell Reflection
Last updated
Last updated
Reflection allows a user or administrator to access and interact with .NET assemblies. From the Microsoft docs, "Assemblies form the fundamental units of deployment, version control, reuse, activation scoping, and security permissions for .NET-based applications." .NET assemblies may seem foreign; however, we can make them more familiar by knowing they take shape in familiar formats such as exe (executable) and dll (dynamic-link library).
PowerShell reflection can be abused to modify and identify information from valuable DLLs.
The AMSI utilities for PowerShell are stored in the AMSIUtils
.NET assembly located in System.Management.Automation.AmsiUtils
.
Matt Graeber published a one-liner to accomplish the goal of using Reflection to modify and bypass the AMSI utility. This one-line can be seen in the code block below.
To explain the code functionality:
First, the snippet will call the reflection function and specify it wants to use an assembly from [Ref.Assembly]
it will then obtain the type of the AMSI utility using GetType
.
The information collected from the previous section will be forwarded to the next function to obtain a specified field within the assembly using GetField
.
The assembly and field information will then be forwarded to the next parameter to set the value from $false
to $true
using SetValue
.
Once the amsiInitFailed
field is set to $true
, AMSI will respond with the response code: AMSI_RESULT_NOT_DETECTED = 1