AMSI Bypass
AMSI (Antimalware Scan Interface) is the point where Windows lets the antivirus look at script content after it has been deobfuscated, right before it executes. It is why pasting a well known script into a PowerShell console fails even when it is Base64 encoded: the engine unwraps the content and hands it to AMSI before interpreting it.
Mechanism. The process about to run code (PowerShell, WScript, the .NET engine) loads amsi.dll inside its own address space and calls AmsiScanBuffer with the content. The antivirus answers whether it considers it malicious. That is the structural weakness: the check lives in the attacker's process, which is allowed to write to its own memory.
Three families of evasion follow, in order of noise:
- Obfuscate the content so the signature does not match. It does not touch AMSI, it just stops looking like what the antivirus knows. That is what payload obfuscation covers.
- Break the initialisation, corrupting the context structure the process uses to talk to AMSI so later calls return an error and the engine carries on.
- Patch the function in memory, overwriting the start of
AmsiScanBufferso it returns "clean content" without scanning anything.
The first step is checking whether AMSI is active, which any signatured string reveals:
[Ref].Assembly.GetType('System.Management.Automation.AmsiUtils') amsiInitFailed # the field several techniques corruptAt line:1 char:1 + ... This script contains malicious content and has been blocked by your antivirus software.
Requirements. Code execution on the host. Administrator rights are not needed: the patching happens inside the process itself, with the permissions already held.
How to detect it. 4104 (Script Block Logging) records the block already deobfuscated, so it sees the attempt even when the string arrives encoded, and it is the primary source. Strong signals are reflective access to AmsiUtils or amsiInitFailed, a VirtualProtect call over the amsi.dll region from the process itself, and the pattern of a process that loads amsi.dll and from some point on stops producing scan events while it keeps executing. AMSI going quiet is itself the indicator.
Defence. Keep Script Block Logging and Module Logging on and shipped off the host, because they record before the bypass. Above that, what cuts the whole family is not AMSI but application control: WDAC in enforced mode leaves PowerShell in Constrained Language Mode, where reflection and .NET type based techniques are unavailable. Uninstalling PowerShell v2, which has no AMSI, is mandatory: without that, asking for that version is enough.