ruancarlosrc/lab-event-viwer
GitHub: ruancarlosrc/lab-event-viwer
一个蓝队实验室项目,通过模拟 Windows 持久化攻击技术并使用原生工具进行检测,帮助 SOC 分析师掌握事件分流与日志分析的实操技能。
Stars: 0 | Forks: 0
# 🛡️ Windows 基础知识 — 检测持久化
**蓝队实验室 | SOC 必备 Windows 基础**
模拟 Windows 持久化技术,并使用原生工具进行检测:Task Scheduler、Registry、Event Viewer 和 PowerShell。
## 🎯 目标
模拟攻击者使用的两种经典持久化机制,并通过日志分析、系统审计以及 PowerShell 自动化来检测它们 —— 重现 SOC 分析师在事件分流期间的典型工作流程。
## 🗺️ MITRE ATT&CK 映射
| 技术 | ID | 描述 |
|---|---|---|
| Scheduled Task/Job | T1053.005 | 通过计划任务实现持久化 |
| Registry Run Keys | T1547.001 | 通过注册表 Run 键实现持久化 |
| PowerShell | T1059.001 | 通过 PowerShell 执行 |
| Impair Defenses | T1562.002 | 启用审计作为防御措施 |
## 🧰 使用的工具
- Windows 10 (VM VirtualBox)
- Task Scheduler (`taskschd.msc`)
- Registry Editor (`regedit.exe`)
- Event Viewer (`eventvwr.msc`)
- PowerShell (Admin)
- `auditpol.exe`, `schtasks.exe`, `reg.exe`
## 📋 环境
| 项目 | 详情 |
|---|---|
| 操作系统 | Windows 10 (VM) |
| 用户 | `user` @ `DESKTOP-GPM14TF` |
| 语言 | PT-BR |
| Hypervisor | Oracle VirtualBox |
## 🔴 阶段 1 — Task Scheduler (T1053.005)
### 目标
模拟创建恶意的计划任务,其名称伪装成 Windows 的合法进程。
### 审计配置
```
# 通用 GUID — 适用于任何语言的 Windows
auditpol /set /subcategory:"{0CCE9232-69AE-11D9-BED3-505054503030}" /success:enable /failure:enable
```
### 创建的任务
**任务 1 — 通过 GUI (WindowsUpdateHelper)**
```
cmd.exe
/c "echo pwned > C:\Users\Public\persist.txt"
HighestAvailable
```
**任务 2 — 通过命令行 (SecurityHealthUpdate)**
```
schtasks /create /tn "SecurityHealthUpdate" /tr "powershell.exe -WindowStyle Hidden -Command 'whoami > C:\Users\Public\recon.txt'" /sc onlogon /ru SYSTEM /f
```
```
powershell.exe
-WindowStyle Hidden -Command "whoami > C:\Users\Public\recon.txt"
S-1-5-18
```
### 列出 Microsoft 命名空间之外的任务
```
Get-ScheduledTask | Where-Object { $_.TaskPath -notlike "\Microsoft\*" } |
Select-Object TaskName, TaskPath, State | Format-Table -AutoSize
```
## 🔴 阶段 2 — Registry Run Keys (T1547.001)
### 目标
使用与阶段 1 任务相同的名称,模拟通过 Run Key 实现持久化 —— 这是一种常用于阻碍关联分析的技巧。
### 修改前的 Baseline
```
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run]
# 空键值 — 用户没有 autorun 条目
```
### 创建的 Run Key
```
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v "WindowsUpdateHelper" /t REG_SZ /d "powershell.exe -WindowStyle Hidden -Command 'whoami > C:\Users\Public\recon2.txt'" /f
```
### 修改后的状态
```
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run]
"WindowsUpdateHelper"="powershell.exe -WindowStyle Hidden -Command 'whoami > C:\Users\Public\recon2.txt'"
```
### 扫描四个主要的 Run Key
```
$keys = @(
"HKCU:\Software\Microsoft\Windows\CurrentVersion\Run",
"HKCU:\Software\Microsoft\Windows\CurrentVersion\RunOnce",
"HKLM:\Software\Microsoft\Windows\CurrentVersion\Run",
"HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce"
)
foreach ($key in $keys) {
Write-Host "`n=== $key ===" -ForegroundColor Cyan
Get-ItemProperty $key -ErrorAction SilentlyContinue
}
```
**结果:** 在 `HKCU\Run` 中发现了带有隐藏 PowerShell payload 的 `WindowsUpdateHelper`。识别出的合法条目包括:`SecurityHealth`、`VBoxTray`、`MicrosoftEdgeAutoLaunch`。
## 🔵 阶段 3 — Event Viewer (日志分析)
### 启用的审计
```
# 进程创建 (Event ID 4688)
auditpol /set /subcategory:"{0CCE922B-69AE-11D9-BED3-505054503030}" /success:enable
# Command line logging(需要 gpupdate /force + 重启才能应用)
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\Audit" /v ProcessCreationIncludeCmdLine_Enabled /t REG_DWORD /d 1 /f
gpupdate /force
```
### 相关的 Event ID
| Event ID | 日志 | 含义 |
|---|---|---|
| 4698 | Security | 创建了计划任务 |
| 4702 | Security | 计划任务被修改 |
| 4657 | Security | 注册表值被修改 |
| **4688** | Security | **创建了新进程** ✅ 已捕获 |
| 4624 | Security | 登录成功 |
| 4625 | Security | 登录失败 |
| **4104** | PS/Operational | **Script Block Logging** ✅ 已捕获 |
### 捕获的证据 — Event ID 4688
**被 powershell.exe 调用的 schtasks.exe (阶段 1):**
```
Nome do Novo Processo: C:\Windows\System32\schtasks.exe
Nome do Processo do Criador: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Linha de Comando: "C:\Windows\system32\schtasks.exe" /create /tn TestCmdLine /tr "powershell.exe -Command 'whoami'" /sc onlogon /f
```
**被 powershell.exe 调用的 reg.exe (阶段 2):**
```
Nome do Novo Processo: C:\Windows\System32\reg.exe
Nome do Processo do Criador: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
```
**作为 SYSTEM 运行的 whoami.exe — 正在执行的 payload:**
```
Nome do Novo Processo: C:\Windows\System32\whoami.exe
Nome do Processo do Criador: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
ID de Segurança (criador): S-1-5-18 (SYSTEM)
Tipo de Elevação: %%1936 (token completo — Tipo 1)
```
### 用于事件追踪的 PowerShell 查询
```
Get-WinEvent -LogName Security -FilterXPath "*[System[EventID=4688]]" |
Where-Object { $_.Message -like "*schtasks*" -or $_.Message -like "*reg.exe*" -or $_.Message -like "*whoami*" } |
Select-Object TimeCreated, Message |
Export-Csv C:\Users\Public\evidence_4688.csv -NoTypeInformation -Encoding UTF8
```
## 🟢 阶段 4 — PowerShell (自动化检测)
### 启用 Script Block Logging
```
reg add "HKLM\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging" /v EnableScriptBlockLogging /t REG_DWORD /d 1 /f
gpupdate /force
```
执行脚本后记录的 Event ID **4104**:
```
ID de ScriptBlock: 526593ae-5b4a-449c-bb69-f3c2ee8b05aa
Caminho: C:\Users\Public\detect_persistence.ps1
```
### 检测脚本 — detect_persistence.ps1
开发此脚本是为了自动扫描三个持久化攻击向量并导出 CSV 报告:
- **代码块 1:** `\Microsoft\` 之外的计划任务 → MITRE T1053.005
- **代码块 2:** 四个主要键 (HKCU + HKLM) 中的 Run Keys → MITRE T1547.001
- **代码块 3:** 过去 24 小时内过滤可疑进程的 Event ID 4688 → MITRE T1059.001
### 执行结果
```
[*] Iniciando varredura de persistência...
[*] Verificando tarefas agendadas suspeitas...
Encontradas: 6 tarefa(s)
[*] Verificando Run Keys...
Encontradas: 4 entrada(s)
[*] Verificando Event ID 4688 (últimas 24h)...
Encontrados: 11 evento(s)
[+] Relatório exportado: persistence_report_2026-06-09_11-36.csv
[+] Total de artefatos encontrados: 21
```
**脚本识别出的恶意 Artefact:**
| 类型 | 名称 | 指标 |
|---|---|---|
| ScheduledTask | `WindowsUpdateHelper` | cmd.exe payload,触发器为 boot+logon |
| ScheduledTask | `SecurityHealthUpdate` | 以 SYSTEM 身份运行,隐藏的 PS |
| ScheduledTask | `TestCmdLine` | 带有 whoami 的 PowerShell |
| RunKey | `WindowsUpdateHelper` | HKCU 中的 PS `-WindowStyle Hidden` |
| ProcessEvent | `schtasks.exe` | 被 powershell.exe 调用 |
| ProcessEvent | `whoami.exe` | 以 SYSTEM 身份执行 |
## 📊 经验教训
| # | 教训 |
|---|---|
| 1 | 在 `auditpol` 中使用 GUID 可确保跨语言的兼容性 |
| 2 | Command line logging 需要重启 —— 重启之前的事件将缺失此字段 |
| 3 | 只有在创建时启用了审计,才会生成 Event ID 4698 |
| 4 | `powershell.exe → schtasks.exe` 或 `powershell.exe → reg.exe` 的调用链是一个警报指标 |
| 5 | 以 SYSTEM 身份运行且 token 类型为 Type 1 的进程需要立即调查 |
| 6 | 将恶意 Artefact 命名为与合法进程相同的名称是一种真实的逃避技术 |
## 🔗 参考
- [MITRE ATT&CK T1053.005](https://attack.mitre.org/techniques/T1053/005/)
- [MITRE ATT&CK T1547.001](https://attack.mitre.org/techniques/T1547/001/)
- [Windows Security Event IDs — ultimatewindowssecurity.com](https://www.ultimatewindowssecurity.com/securitylog/encyclopedia/)
- [Auditpol GUIDs Reference](https://docs.microsoft.com/en-us/windows/security/threat-protection/auditing/audit-other-object-access-events)
标签:AI合规, IPv6, OpenCanary, PowerShell, SOC分析, 安全实验室, 权限维持检测