# Analyzing Powershell Script Block Logging

> 从 EVTX 文件中解析 Windows PowerShell 脚本块日志（事件 ID 4104），以检测混淆命令、编码载荷和离地攻击技术（living-off-the-land）。使用 python-evtx 提取并重建多块脚本，通过熵分析和模式匹配检测 Base64 编码命令、Invoke-Expression 滥用、下载植入器（download cradles）和 AMSI 绕过尝试。

- Skill: `killvxk/analyzing-powershell-script-block-logging` (Agent Skill, multi-file: 4 files)
- Install (CLI): `npx skillmds@latest add killvxk/analyzing-powershell-script-block-logging`
- Raw SKILL.md: https://api.skillmd.com/api/skills/killvxk/analyzing-powershell-script-block-logging/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- License: Apache-2.0
- Author: killvxk (https://skillmd.com/u/killvxk)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/killvxk/analyzing-powershell-script-block-logging

---


## 使用说明

1. 安装依赖: `pip install python-evtx lxml`
2. 收集 PowerShell 操作日志: `Microsoft-Windows-PowerShell%4Operational.evtx`
3. 使用 python-evtx 解析事件 ID 4104 条目，提取 ScriptBlockText、ScriptBlockId 以及 MessageNumber/MessageTotal，用于多块脚本重建。
4. 应用检测启发式规则：
   - Base64 编码命令（`-EncodedCommand`、`FromBase64String`）
   - 下载植入器（`DownloadString`、`DownloadFile`、`Invoke-WebRequest`、`Net.WebClient`）
   - AMSI 绕过模式（`AmsiUtils`、`amsiInitFailed`）
   - 混淆指标（高熵、反引号插入、字符串拼接）
5. 生成报告，包含重建脚本、风险评分和 MITRE ATT&CK 映射。

```bash
python scripts/agent.py --evtx-file /path/to/PowerShell-Operational.evtx --output ps_analysis.json
```

## 示例

### 检测编码命令执行
```python
import base64
if "-encodedcommand" in script_text.lower():
    encoded = script_text.split()[-1]
    decoded = base64.b64decode(encoded).decode("utf-16-le")
```

### 重建多块脚本
拆分在多个 4104 事件中的脚本共享一个 `ScriptBlockId`。按 `MessageNumber` 顺序拼接各块以恢复完整脚本。

