Event ID 4104
Event ID 4104: PowerShell Script Block Logging
PowerShell script block logging: creating Scriptblock text
- Log source
- Microsoft-Windows-PowerShell/Operational
- How to enable it
- Group Policy: Turn on PowerShell Script Block Logging. Suspicious blocks are logged at Warning level even when it is off
- Category
- Process Execution
- Priority
- High
- Typical volume
- High
- MITRE ATT&CK
-
- T1059.001 Command and Scripting Interpreter: PowerShell
- T1027.010 Obfuscated Files or Information: Command Obfuscation
- T1140 Deobfuscate/Decode Files or Information
- T1685 Disable or Modify Tools
T1685 replaces T1562, T1562.001, T1562.006, which older guides still use.
What Event ID 4104 means
4104 contains the source text of PowerShell code at the moment it is compiled. An encoded command is decoded, a downloaded script is shown in full, and code that was never written to disk still appears. For most intrusions that involve PowerShell it is the single most informative event.
Code that builds and runs other code produces one event for each layer. Following the layers downwards leads from a short encoded launcher to the real payload in clear text, and no manual decoding is needed.
Long scripts are divided over several events. MessageNumber and MessageTotal give the order, and ScriptBlockId ties the parts together. Even with the policy off, PowerShell 5 and later log blocks that contain certain suspicious terms at Warning level, so the log is worth reading on any host.
Key fields to inspect
| Field | Meaning | Suspicious when |
|---|---|---|
ScriptBlockText | The code, or a part of it. | IEX, DownloadString, FromBase64String, Reflection.Assembly, VirtualAlloc, AmsiUtils, or long lines with random names. |
ScriptBlockId | Identifier that links the parts of one script. | Use it to put the script together. |
MessageNumber | Number of this part. | Sort by it before you join the parts. |
MessageTotal | Number of parts. | Dozens of parts point to a large offensive framework. |
Path | File the code came from. Empty for interactive or in-memory code. | Empty together with long code, or a file in a temporary folder. |
Detection queries
Reassemble scripts and find in-memory loading, AMSI tampering and download-and-run code. Adjust names and thresholds to your environment. About these queries.
Splunk SPL
index=windows source="XmlWinEventLog:Microsoft-Windows-PowerShell/Operational" EventCode=4104
| sort 0 MessageNumber
| stats list(ScriptBlockText) AS parts values(Path) AS path min(_time) AS time BY Computer ScriptBlockId
| eval script=mvjoin(parts, "")
| regex script="(?i)(AmsiUtils|amsiInitFailed|Reflection\.Assembly|VirtualAlloc|FromBase64String|DownloadString|DownloadData|Invoke-Expression|\bIEX\b|Invoke-Mimikatz)"
| eval preview=substr(script, 1, 500)
| table time Computer path preview ScriptBlockId Microsoft Sentinel KQL
Event
| where EventLog == "Microsoft-Windows-PowerShell/Operational" and EventID == 4104
| extend ScriptBlockText = extract(@'Name="ScriptBlockText">([^<]*)<', 1, EventData)
| extend ScriptBlockId = extract(@'Name="ScriptBlockId">([^<]*)<', 1, EventData)
| extend MessageNumber = extract(@'Name="MessageNumber">([^<]*)<', 1, EventData)
| extend ScriptPath = extract(@'Name="Path">([^<]*)<', 1, EventData)
| extend Part = toint(MessageNumber)
| sort by ScriptBlockId asc, Part asc
| summarize Script = strcat_array(make_list(ScriptBlockText, 200), ""), First = min(TimeGenerated), ScriptPath = take_any(ScriptPath) by Computer, ScriptBlockId
| where Script matches regex @"(?i)(AmsiUtils|amsiInitFailed|Reflection\.Assembly|VirtualAlloc|FromBase64String|DownloadString|DownloadData|Invoke-Expression|\bIEX\b|Invoke-Mimikatz)"
| project First, Computer, ScriptPath, Preview = substring(Script, 0, 500), ScriptBlockId PowerShell
Get-WinEvent -FilterHashtable @{ LogName = 'Microsoft-Windows-PowerShell/Operational'; Id = 4104; StartTime = (Get-Date).AddDays(-1) } |
ForEach-Object { $d = @{ Time = $_.TimeCreated }; ([xml]$_.ToXml()).Event.EventData.Data | ForEach-Object { $d[$_.Name] = $_.'#text' }; [pscustomobject]$d } |
Where-Object { $_.ScriptBlockText -match 'FromBase64String|DownloadString|AmsiUtils|VirtualAlloc|Reflection\.Assembly' } |
Select-Object Time, ScriptBlockId, MessageNumber, Path, ScriptBlockText | Format-List Attack scenarios
Download cradle
powershell -enc starts a short block that calls DownloadString and IEX. The next 4104 in the same process holds the downloaded script in full, even though it never touched the disk.
AMSI bypass
Code that sets amsiInitFailed or patches AmsiScanBuffer tries to blind antivirus scanning. The bypass itself is compiled first, so it is usually logged, and what follows deserves priority.
Offensive frameworks
PowerView, PowerUp, Rubeus loaders and Invoke-Mimikatz are recognizable by function names that survive light editing. Many parts with one ScriptBlockId and an empty Path are typical.
Investigation checklist
- Join all parts with the same ScriptBlockId in order of MessageNumber.
- Read the blocks before and after in the same process to see every layer.
- Extract addresses, hashes and names from the final layer.
- Find the parent process and the user in 4688 or Sysmon event 1.
- Search the same strings on all hosts.
Common benign causes
- Windows and vendor modules, which log large but well-known scripts.
- Administrative scripts that use Base64 or Add-Type legitimately.
- Security products that run detection scripts containing suspicious terms.
Event ID 4104 FAQ
How do I reassemble a script from several 4104 events?
Group by ScriptBlockId, sort by MessageNumber as a number, and join ScriptBlockText without separators. Check that you have as many parts as MessageTotal says.
Is 4104 logged if script block logging is disabled?
Partly. Windows PowerShell 5 and later log blocks with suspicious content at Warning level, unless logging was explicitly turned off by policy. With the policy enabled, all blocks are logged.
Can attackers avoid event 4104?
They can start PowerShell 2 where it is still installed, tamper with the logging settings in memory, or run .NET code outside PowerShell. Remove the version 2 engine and watch process creation for -Version 2.