Wednesday, November 18, 2026

AMSI and script-block logging: the cat-and-mouse explained

AMSI and script-block logging: the cat-and-mouse explained

An educational analysis of how AMSI and PowerShell script-block logging interact, why attackers try to evade them, and what defenders should understand about this ongoing arms race.

TopicAMSI (Antimalware Scan Interface) and PowerShell script-block logging — inspection mechanisms for script-based payloads
CategoryDefensive telemetry and evasion analysis
Primary UseUnderstanding how AMSI and script-block logging capture script content so defenders can harden and monitor hosts
Safe UsePurely educational and documentary analysis for authorized defenders, blue teams, and security researchers working in owned environments
Telemetry NoteAMSI scans decrypted script content before execution; script-block logging records script text to Event ID 4104 in the Windows PowerShell operational log

Script-based attack tooling has been the backbone of post-exploitation on Windows for more than a decade, and the reason is simple: PowerShell, WMI, and related automation surfaces are already present on nearly every target, which means an adversary needs to drop nothing to disk in order to get meaningful execution. That convenience cuts both ways. Because scripts must eventually exist as readable text inside the interpreter before they can run, Microsoft built inspection points that operate at exactly that moment of decomposition. The two most consequential of these are the Antimalware Scan Interface (AMSI) and PowerShell script-block logging, and understanding how they work — and how offense tries to work around them — is foundational knowledge for any defender running Windows estates.

AMSI is not a product; it is an interface. It defines a Windows API surface (AmsiInitialize, AmsiScanBuffer, AmsiScanString, and friends) that any application can call to submit content for inspection by a registered antimalware provider, typically the real-time engine inside Windows Defender or a third-party AV product that implements the provider contract. The design insight is architectural rather than algorithmic: PowerShell, Windows Script Host, JavaScript/VBScript via mshta, Excel/Word macro execution through VBA, and .NET assembly loading all reach a point where encrypted, encoded, or packed content has been transformed into plain script text. By calling AMSI at that moment, the interface defeats naive obfuscation, because the content submitted for scanning is what the interpreter is actually about to execute — after the base64 has been decoded, after the XOR loop has run, after the compression has been unwrapped.

Script-block logging is the complementary telemetry stream. When enabled — and on modern Windows it is on by default for Windows PowerShell 5.1 in many configurations, and controllable via Group Policy under the Windows Components/Windows PowerShell/Script Block Logging path — the engine emits Event ID 4104 in the Microsoft-Windows-PowerShell/Operational log each time a script block is executed. The critical property, and the one that matters most to defenders, is that the logged text is the de-obfuscated content. A payload that arrives as a wall of base64 in Event ID 400 engine lifecycle events or in a parent process command line will appear in 4104 as the decoded script that actually ran, which makes the event log a far richer source for hunting than process command lines alone.

These two mechanisms create the modern cat-and-mouse dynamic. From the adversary's perspective, AMSI is an obstacle sitting between their staged payload and execution, and script-block logging is a silent witness writing evidence to disk. Evasion research therefore clusters into a few recognizable families. The oldest family targets the interface itself: patching the in-memory amsi.dll — typically by modifying the AmsiScanBuffer function's prologue so it returns a clean verdict without actually scanning — or corrupting the amsiInitFailed flag in the session's .NET context so the scan is skipped entirely. These are well-documented, heavily detected techniques, and defenders should treat attempts at them as high-signal indicators rather than background noise.

A second family avoids tampering with the interface and instead attacks the content submission boundary: if the script can break the buffer before it is handed to AMSI — for example, by inserting characters that make the engine submit truncated or malformed content — the scan may evaluate something different from what ultimately executes. A third, and pragmatically the most durable family, simply sidesteps the problem: don't use a host that calls AMSI at all. Downgrade attacks that force PowerShell version 2 — which predates AMSI integration — fall in this category, as does moving execution into C# assemblies loaded via tooling, native binaries, or interpreters that never implemented the interface. The lesson for defenders is that evasion capability is less about defeating the scan than about choosing an execution path that was never covered by it.

The defensive countermeasures follow directly from that taxonomy. Downgrade protection is the single highest-value hardening step: removing PowerShell 2.0 from endpoints, or setting the WMF environment so the legacy engine cannot be invoked, closes the most common historical bypass. Constrained Language Mode, enabled through __PSLockdownPolicy or AppLocker/WDAC policy enforcement, restricts what the language can do even when a script executes successfully. And because 4104 events are only as useful as their retention and transport, defenders should forward the operational log to a central SIEM, because an attacker with administrative privileges will attempt to clear or disable logging locally, and the absence of expected telemetry is itself an event worth alerting on.

It is worth being honest about the limits of the model. AMSI is a point-in-time scan of point-in-time content; it is not a sandbox, and a provider that returns a verdict quickly on a novel, memory-resident payload may simply miss it. Script-block logging records what the engine saw, not what a reflective assembly loaded into a .NET process did afterward. Neither mechanism covers C# executed via arbitrary Assembly.Load paths beyond specific AMSI-integrated loaders, and neither covers native code at all. The realistic framing is that these controls dramatically raise the cost and noise floor of commodity script-based attacks — the vast majority of commodity intrusions — while sophisticated actors route around them with compiled tooling. That is still a trade worth making, because it converts a large class of silent tradecraft into loggable, scannable events.

For defenders building detections, the telemetry around evasion attempts is often louder than the evasion itself. Patching attempts against amsi.dll typically require modifying memory in a loaded module, which EDR sensors observe; the string AMSI appearing in unusual process contexts, System.Management.Automation being loaded by non-standard hosts, and PowerShell version-2 command-line flags such as -version 2 all make clean analytic starting points. The 4104 event stream itself is best treated as a hunting corpus: searching decoded script text for patterns characteristic of post-exploitation frameworks yields high-fidelity results precisely because the log has already done the de-obfuscation work for the analyst.

The deeper takeaway for the security community is architectural. AMSI and script-block logging represent a design pattern — instrument the interpreter, not the file — and that pattern has been progressively extended across the Windows ecosystem, from Office macro submission to .NET assembly context. Every time a new runtime gains an inspection boundary, offensive research re-prices its techniques, and the equilibrium shifts. Neither side has ever held a permanent advantage here, and that is the point of understanding the mechanics rather than memorizing this week's bypass: the mechanics tell you where the next boundary will be drawn, and what telemetry will exist when it is. For authorized defenders, that predictive understanding — knowing which execution surfaces emit what evidence — is worth more than any single detection rule.

Everything in this analysis is presented for educational and defensive purposes, oriented toward hardening owned and authorized environments. The intent is that a blue team reading it comes away better equipped to enable, retain, forward, and hunt through AMSI and script-block telemetry, and to recognize the signatures of evasion attempts against those controls when they appear in legitimate monitoring of systems the team is charged with protecting.

Educational analysis for authorized security professionals. Use only in controlled, authorized environments.

Sponsored
Share articleFacebookXLinkedIn

Continue exploring

Browse all articles →

0 comentários:

Post a Comment

Note: Only a member of this blog may post a comment.