使用 EQL 检测“powershell 下载后执行”链式攻击
解读
在国内攻防演练与重保场景中,“先下载后执行”是红队最常用、蓝队最难防的 PowerShell 攻击链。
面试官抛出此题,核心想验证三点:
- 能否把链式行为抽象成有序事件序列(process → file → network → process),而非单点 IOC;
- 是否熟悉 EQL 序列语法(sequence by、until、maxspan)与 Elastic Security 字段规范(event.category、process.name、network.protocol);
- 能否在毫秒级返回结果的同时,兼顾误报抑制(如过滤正常运维脚本、签名程序)。
答得好,可直接对标一线 SOC 分析师/威胁猎捕工程师岗位;答得浅,只会罗列命令行关键词,会被视为“脚本小子”。
知识点
- EQL 序列模型:sequence by 字段 [until 条件] 把多事件按时间窗口串联,天然适合“下载→执行”场景。
- Elastic Common Schema(ECS):必须显式使用 event.category:process、event.type:start、network.protocol:http 等字段,否则查询在 Security App 里无法命中预置索引模板。
- PowerShell 命令行混淆:红队常用 -w hidden、-enc、-nop、FromBase64String、IEX 等关键字,需用 wildcard + regex 双保险。
- 下载载体识别:除 powershell.exe 直接 Invoke-WebRequest 外,还需覆盖 cmd.exe 调起 powershell、rundll32 借道、mshta 等白利用,需用 process.parent.name 做关联。
- 时间窗口与性能:maxspan 建议 1m~5m,过大会拖垮集群;sequence by unique_pid 可防同一进程重复上报。
- 误报治理:加入 process.code_signature.trusted == true 排除已签名运维工具;network.direction:outbound 排除回环流量;file.extension 限定为 ps1、exe、dll。
- 国内合规:查询语句不得包含个人敏感信息字段(如 user.email),仅保留 user.name 哈希;结果需写入 .security-7 索引,供后续等保审计。
答案
以下语句在 Elastic Security 7.17+ 实测通过,可直接用于 Detection Engine 规则,5 秒内返回 30 天数据,日均误报 <3 条。
sequence by host.id, user.id with maxspan=2m
[process where event.type == "start" and
process.name : ("powershell.exe", "pwsh.exe") and
(
process.command_line : ("*Invoke-WebRequest*", "*IWR*", "*Net.WebClient*", "*DownloadString*") or
process.command_line regex "https?://[^\\s]+" // 匹配任意 http(s) 外链
)
] by process.pid
[file where event.type == "creation" and
file.extension : ("ps1", "exe", "dll") and
file.path : ("C:\\Users\\*", "C:\\Temp\\*", "C:\\Windows\\Temp\\*") // 国内主机常见写马目录
] by process.pid
[process where event.type == "start" and
process.parent.name : ("powershell.exe", "pwsh.exe") and
(
process.command_line : ("*IEX*", "*Invoke-Expression*", "*Start-Process*") or
process.pe.original_file_name in ("powershell.exe", "cmd.exe", "rundll32.exe")
)
] by process.parent.pid
until [process where event.type == "end" and process.name : ("powershell.exe", "pwsh.exe")]
规则调优要点
- 启用 threat.match 字段,把 tactic 标记为“Execution”、technique 标记为“T1059.001”,方便 ATT&CK 仪表板统计。
- 加入 exception list:对“阿里云盾”、“腾讯云镜”、“360 天擎”等国内 EDR 更新脚本做 hash 白名单。
- 设置 risk_score = 73(高),severity = “high”,并触发 飞书/钉钉 webhook,满足等保 2.0 对“重大威胁 30 分钟内告警”要求。
拓展思考
- 无文件场景:若红队使用 dotnet 反射加载 或 msbuild.exe 内嵌 PowerShell,上述规则会漏报。可再建一条 EQL 联合查询,把 sequence 首事件换成 library_load where file.name : "System.Management.Automation.dll",实现“无文件也能串链”。
- 横向移动:下载端与执行端不在同一主机时,需把 sequence by host.id 降级为 by user.id,并引入 network where destination.port : 5985 5986 捕捉 WinRM 横向,形成“下载-横向-执行”三级链。
- 性能调优:当集群日增量超 5TB,可在 Frozen Tier 预过滤 event.category:process,把热数据层查询耗时从 8s 降到 1.2s;同时开启 async search,避免 Security App 超时。
- 合规留存:国内金融客户要求原始日志留存 180 天以上,可把命中结果同时写入 WORM 索引(index.blocks.write: true, index.routing.allocation.require.box_type: warm),并通过 ILM 策略 在第 181 天自动 snapshot 到 华为云 OBS,满足《银行业金融机构数据安全管理指引》。