Skip to content

Lesson 3 of 14 · 20m

PowerShell Logging and Script Blocks

Script Block Logging (EID 4104) records the decoded script the `-enc` flag exists to hide — enable it, join split blocks, and alert on the `-version 2` downgrade.

A 4688 event lands in your SIEM: powershell.exe -enc SQBFAFgA… followed by four hundred characters of base64. The attacker is betting you can't read it, and without Script Block Logging, they're right. With it, the decoded script — every IEX, DownloadString, and FromBase64String — arrives in your logs in plain text. Command lines are what the attacker typed. Script blocks are what the engine ran.

Turn it on

Enable Turn on PowerShell Script Block Logging under Computer Configuration → Administrative Templates → Windows Components → Windows PowerShell (registry equivalent: HKLM\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging, EnableScriptBlockLogging = 1). Events land in Microsoft-Windows-PowerShell/Operational as EID 4104.

Two details that matter at query time. First, long scripts are split across multiple 4104 events sharing one ScriptBlockId — join on ScriptBlockId and order by MessageNumber/MessageTotal before you pattern-match, or a rule matching a string that spans the split never fires. Second, PowerShell 5+ automatically logs script blocks containing suspicious commands even without the policy — you get the worst fragments for free, but only fragments. Enable the policy anyway.

One gotcha: pwsh.exe (PowerShell 7) does not read the Windows PowerShell policy keys — it logs 4104 to PowerShellCore/Operational and needs Script Block Logging enabled through its own settings and policy paths. Most orgs forget pwsh entirely.

The rest of the logging stack

Module Logging (EID 4103, same channel) records pipeline execution detail per named module — Microsoft.PowerShell.*, Netsecurity, whatever you list in the GPO. It's the deepest visibility PowerShell offers and it's extremely noisy: enable it for a hunting window, not as permanent posture.

Transcription writes full session transcripts to files on disk. Useful forensically, but the transcript sits where the attacker can read and delete it — treat it as evidence that can be tampered with, not telemetry. Microsoft-Windows-PowerShell/Operational 4104s are the durable record.

AMSI (Antimalware Scan Interface) is built into PowerShell 5+: before a script executes, its content is scanned in memory — including content that never touched disk. It catches obfuscated payloads that no command-line rule could. Bypasses exist and are public — AmsiScanBuffer patching, amsiInitFailed coercion — but every published bypass is a distinctive string, which means bypass attempts are themselves detection opportunities. Alert on amsi artifacts in 4104 content.

The downgrade play

powershell.exe -version 2 asks for the .NET 2.0-era engine, which has no Script Block Logging, no AMSI, nothing — the attacker types -enc garbage and your fancy logging stack watches version 2 execute it blind. It only works where the PowerShell 2.0 optional feature is still enabled, which is exactly why you alert on it: nobody has a legitimate reason. Match -v, -ve, -ver, -version followed by 2 in the command line, and watch the classic Windows PowerShell log's EID 400 — its EngineVersion field reports which engine started.

Remoting changes where the telemetry lands

Invoke-Command and Enter-PSSession don't execute code on the source host — the commands run inside wsmprovhost.exe on the target, and the script blocks land in the target's 4104 stream. On the target, WinRM's Operational log adds EID 91 (shell creation) and 168 (authorization). If you only collect PowerShell logs from endpoints users sit at, remoting-based lateral movement — which is most of it — happens off-camera.

Two adjacent hardening facts shape what the logs can tell you. Constrained Language Mode — enforced by AppLocker/WDAC policy — removes the .NET surface attackers lean on (Add-Type, COM, P/Invoke), and enforcement drops ScriptBlock warnings into the log when code demands full language. And JEA (Just Enough Administration) endpoints constrain remoting to whitelisted commands — the 4104 stream from a JEA endpoint is short and predictable, so anything unusual there is loud.

The practice

Enable Script Block Logging by GPO on every managed host; enable it for pwsh separately. Alert on three things in 4104 content: encoding/decoding primitives (FromBase64String, IEX wrapping a variable), AMSI tampering strings, and network cradles (DownloadString, Invoke-WebRequest, Net.WebClient). Alert on -version 2 anywhere. Forward Microsoft-Windows-PowerShell/Operational from servers with the same priority as workstations — lateral movement lands there.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.