peteedoo/project-template / .agents/skills/imported/mukul975--anthropic-cybersecurity-skills/analyzing-powershell-script-block-logging
analyzing-powershell-script-block-logging
VerifiedParse Windows PowerShell Script Block Logs (Event ID 4104) from EVTX files to detect obfuscated commands, encoded payloads, and living-off-the-land techniques. Uses python-evtx to extract and reconstruct multi-block scripts, applies entropy analysis and pattern matching for Base64-encoded commands, Invoke-Expression abuse, download cradles, and AMSI bypass attempts.
| File | Kind | Size |
|---|---|---|
| LICENSE | other | 11.0 KB |
| SKILL.md | skill md | 2.6 KB |
| references/api-reference.md | markdown | 1.8 KB |
| scripts/agent.py | script | 7.9 KB |
references/api-reference.md
View on GitHub# API Reference: PowerShell Script Block Logging Analysis
## python-evtx Library
### FileHeader
```python
from Evtx.Evtx import FileHeader
with open(evtx_path, "rb") as f:
fh = FileHeader(f)
for record in fh.records():
xml_string = record.xml() # Returns XML string of the event
```
### Event XML Structure (Event ID 4104)
```xml
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<EventID>4104</EventID>
<TimeCreated SystemTime="2024-01-15T10:30:00.000Z"/>
</System>
<EventData>
<Data Name="MessageNumber">1</Data>
<Data Name="MessageTotal">3</Data>
<Data Name="ScriptBlockText">...powershell code...</Data>
<Data Name="ScriptBlockId">guid-string</Data>
<Data Name="Path">C:\script.ps1</Data>
</EventData>
</Event>
```
## lxml etree Parsing
```python
from lxml import etree
NS = {"evt": "http://schemas.microsoft.com/win/2004/08/events/event"}
root = etree.fromstring(xml_bytes)
event_id = root.find(".//evt:System/evt:EventID", NS).text
data_elems = root.findall(".//evt:EventData/evt:Data", NS)
for elem in data_elems:
name = elem.get("Name")
value = elem.text
```
## Script Block Reconstruction
Large PowerShell scripts are split across multiple Event 4104 entries:
- `ScriptBlockId`: Unique GUID shared across all parts
- `MessageNumber`: Part index (1-based)
- `MessageTotal`: Total number of parts
- Reconstruct: concatenate parts ordered by MessageNumber
## Key Detection Patterns
| Pattern | MITRE | Risk |
|---------|-------|------|
| `-EncodedCommand` | T1059.001 | High |
| `FromBase64String` | T1140 | High |
| `Invoke-Expression` / `iex` | T1059.001 | High |
| `DownloadString` / `Net.WebClient` | T1105 | Critical |
| `AmsiUtils` / `amsiInitFailed` | T1562.001 | Critical |
| `Invoke-Mimikatz` | T1003 | Critical |
| High entropy (>5.5) | T1027 | Medium |
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementation | Stars: 318k | Updated | Claude CodeCodex | Verified | Install |
| requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirements | Stars: 318k | Updated | Claude CodeCodex | Verified | Install |
| using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallback | Stars: 318k | Updated | Claude CodeCodex | Verified | Install |
| verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions always | Stars: 318k | Updated | Claude CodeCodex | Verified | Install |