分析 PowerShell 脚本块日志 是做什么的?
使用场景
- 调查安全事件时,需要分析 PowerShell 脚本块日志
- 为此领域构建检测规则或威胁狩猎查询时
- SOC 分析师需要该分析类型的结构化流程时
- 验证相关攻击技术的安全监控覆盖范围时
先决条件
- 熟悉安全运营概念和工具
- 可访问测试或实验室环境进行安全执行
- 安装 Python 3.8+ 及所需依赖
- 已获得执行测试活动的适当授权
操作步骤
- 安装依赖:
pip install python-evtx lxml - 收集 PowerShell 操作日志:
Microsoft-Windows-PowerShell%4Operational.evtx - 使用 python-evtx 解析事件 ID 4104 条目,提取 ScriptBlockText、ScriptBlockId 以及用于多块脚本重构的 MessageNumber/MessageTotal。
- 应用检测启发式方法:
- Base64 编码命令(
-EncodedCommand、FromBase64String) - 下载源(
DownloadString、DownloadFile、Invoke-WebRequest、Net.WebClient) - AMSI 绕过模式(
AmsiUtils、amsiInitFailed) - 混淆指标(高熵、刻痕插入、字符串拼接)
- Base64 编码命令(
- 生成包含重构脚本、风险评分和 MITRE ATT&CK 映射的报告。
python scripts/agent.py --evtx-file /path/to/PowerShell-Operational.evtx --output ps_analysis.json
示例
检测编码命令执行
import base64
if "-encodedcommand" in script_text.lower():
encoded = script_text.split()[-1]
decoded = base64.b64decode(encoded).decode("utf-16-le")
重构多块脚本
跨多个 4104 事件拆分的脚本共享一个 ScriptBlockId。按 MessageNumber 排序后连接各块,即可恢复完整脚本。