MouhBbt/exchange-hybrid-threat-hunting

GitHub: MouhBbt/exchange-hybrid-threat-hunting

一套企业级 Velociraptor artifact 库,用于在 Exchange 和 M365 环境中自动化检测 BEC、Webshell、恶意传输规则及 OAuth 滥用等威胁。

Stars: 0 | Forks: 0

# VeloLibrary: Exchange 与混合基础设施威胁狩猎
![VeloLibrary Banner](https://img.shields.io/badge/Velociraptor-Artifact%20Library-blue?style=for-the-badge&logo=data:image/png;base64,iVBORw0KGgo=) [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg?style=for-the-badge)](LICENSE) [![Velociraptor](https://img.shields.io/badge/Velociraptor-≥%200.7.0-orange?style=for-the-badge)](https://www.velocidex.com/) [![ATT&CK](https://img.shields.io/badge/MITRE%20ATT%26CK-v14-red?style=for-the-badge)](https://attack.mitre.org/) [![Tests](https://img.shields.io/badge/Tests-pytest-brightgreen?style=for-the-badge)](tests/) **一个企业级的开源 Velociraptor artifact 库,专为 Microsoft Exchange 和 M365 威胁狩猎设计。解决超越基础日志解析的真实 DFIR 瓶颈 —— 重构攻击者会话、检测 OAuth 持久化、识别 webshell,并自动化 BEC 取证分析。** [快速开始](#-quick-start) · [Artifacts](#-artifact-catalog) · [文档](#-documentation) · [ATT&CK 覆盖范围](#-mitre-attck-coverage) · [贡献](#-contributing)
## 🔴 解决的问题 Exchange 和 M365 环境是现代入侵活动中价值最高的目标。DFIR 团队面临着持续的操作瓶颈: | 真实世界的问题 | 本库如何解决 | |-------------------|------------------------------| | **“我们有 90 天的 IIS 日志,但无法快速找到初始的 SSRF 请求”** | `ProxyVulnDetector` 通过一次查询解析并关联 IIS + HttpProxy 日志,匹配所有已知的 ProxyChain CVE 特征 | | **“攻击者使用了 EWS —— 我们如何重构他们读取/发送的内容?”** | `EWSSessionReconstructor` 关联 EWS 日志中的会话 ID,重构完整的有状态操作时间线 | | **“我们需要在 5,000 个邮箱中查找所有恶意的收件箱规则”** | `InboxRuleHunter` 解析 CmdletInfra、OWA 和事件日志,使用针对 BEC 的启发式算法对每条规则进行 0–100 的评分 | | **“一个 OAuth 应用被授予了 Mail.ReadWrite 权限 —— 我们如何找到它接触过的所有内容?”** | `OAuthAnomalyDetector` 将 UAL 授权许可与随后的收件箱规则、传输规则和 EWS 活动进行关联 | | **“是否存在 webshell?Exchange 的 web 目录下有 50,000 个文件”** | `WebshellHunter` 结合文件系统熵评分、已知特征匹配和 IIS 日志关联,查找被植入的 ASPX shell | ## 🏗️ 架构 ``` velolibrary/ ├── artifacts/ # VQL Artifact YAML files │ ├── exchange_bec/ # Business Email Compromise hunting │ │ ├── Exchange.BEC.InboxRuleHunter.yaml # Malicious inbox rule detection │ │ ├── Exchange.BEC.EWSSessionReconstructor.yaml # EWS session timeline reconstruction │ │ └── Exchange.BEC.AuthAnomalyEngine.yaml # Auth anomaly & impossible travel │ ├── exchange_persistence/ # Persistence mechanism hunting │ │ ├── Exchange.Persistence.OAuthAnomalyDetector.yaml # OAuth app & consent phishing │ │ └── Exchange.Persistence.TransportRuleHunter.yaml # Transport rule exfiltration │ └── exchange_exploitation/ # Exploitation & post-exploitation hunting │ ├── Exchange.Exploitation.WebshellHunter.yaml # Webshell detection │ └── Exchange.Exploitation.ProxyVulnDetector.yaml # ProxyShell/ProxyNotShell/OWASSRF │ ├── tests/ # Test suite & mock data │ ├── mock_data/ │ │ ├── ews_logs/ # Mock EWS log files (BEC attack scenario) │ │ ├── iis_logs/ # Mock IIS W3C logs (ProxyNotShell scenario) │ │ ├── httpproxy_logs/ # Mock HttpProxy logs (SSRF chain) │ │ └── ual_exports/ # Mock M365 UAL JSON exports │ ├── conftest.py # pytest fixtures │ └── test_artifacts.py # 30+ automated test cases │ ├── docs/ # Documentation │ ├── hunting_playbook.md # Step-by-step DFIR playbook for 6 scenarios │ └── deployment_guide.md # Velociraptor deployment instructions │ ├── scripts/ │ ├── build_bundle.py # YAML validation + unified bundle generator │ └── deploy.sh # One-command deploy to Velociraptor server │ ├── dist/ # Generated bundle (created by build_bundle.py) │ └── velolibrary_exchange_bundle.yaml │ ├── README.md └── LICENSE # MIT ``` ## 🚀 快速开始 ### 选项 1:部署打包文件 (最快) ``` # 克隆并构建 git clone https://github.com/velolibrary/exchange-threat-hunting.git cd exchange-threat-hunting pip install pyyaml colorama # 验证并构建 bundle python3 scripts/build_bundle.py # 预期输出:"All artifacts passed validation. ✓" # Bundle: dist/velolibrary_exchange_bundle.yaml # 要通过 CLI 在本地运行 collection,请使用 --definitions flag: velociraptor --definitions dist/velolibrary_exchange_bundle.yaml artifacts collect Exchange.BEC.InboxRuleHunter # 部署到服务器配置: velociraptor --config server.config.yaml query "SELECT artifact_set(definition=read_file(filename='dist/velolibrary_exchange_bundle.yaml')) FROM scope()" # 或者验证它们在本地是否可用: velociraptor --definitions dist/velolibrary_exchange_bundle.yaml artifacts list | grep "^Exchange\." ``` ### 选项 2:单条命令部署 ``` # 带 preflight 检查的完整部署 bash scripts/deploy.sh --server https://velociraptor.corp:8001 # Dry run(仅构建,不部署) bash scripts/deploy.sh --dry-run ``` ### 选项 3:Velociraptor GUI 上传 1. 从 [发布页面](https://github.com/velolibrary/exchange-threat-hunting/releases) 下载 `dist/velolibrary_exchange_bundle.yaml` 2. 在 Velociraptor Web UI 中 → **View Artifacts** → **Upload Artifact** 3. 选择该打包的 YAML 文件 4. 所有 7 个 artifact 将加载到 `Exchange.*` 命名空间下 ## 📦 Artifact 目录 ### 🔍 商业电子邮件妥协 (BEC) #### [`Exchange.BEC.InboxRuleHunter`](artifacts/exchange_bec/Exchange.BEC.InboxRuleHunter.yaml) **解决的问题:** 快速识别所有邮箱中转发到外部地址、隐藏财务邮件或将邮件移动到隐蔽文件夹的规则 —— 所有规则均按可疑程度进行评分,以减少分析师的疲劳。 **解析的内容:** - Exchange CmdletInfra 日志 (`C:\...\Logging\CmdletInfra\`) - OWA 活动日志 - MSExchange 管理事件日志 (EventID 1) **主要参数:** | 参数 | 类型 | 默认值 | 描述 | |-----------|------|---------|-------------| | `StartTime` | timestamp | 7d ago | 分析窗口的起始时间 | | `EndTime` | timestamp | now | 分析窗口的结束时间 | | `TargetMailboxes` | csv | (所有) | 指定要分析的 UPN | | `ExternalDomainWhitelist` | csv | — | 受信任的外部域名 | | `KeywordMatchList` | csv | `invoice,payment,wire,bank,urgent` | BEC 触发关键词 | **输出示例:** ``` RuleCreatedTime | MailboxOwner | RuleName | ExternalForwardAddress | SuspiciousScore | TriggerReason -----------------------|-------------------------|---------------|-------------------------|-----------------|------------------ 2026-07-10T00:15:00Z | attacker@contoso.com | SyncRule | exfil@evil.com | 95 | ExternalForward+Keyword+OutsideHours 2026-07-09T22:30:00Z | finance@contoso.com | _rule1 | c2@attacker.io | 88 | BlankName+BCC+DeleteItems ``` **MITRE:** T1114.003, T1564.008 #### [`Exchange.BEC.EWSSessionReconstructor`](artifacts/exchange_bec/Exchange.BEC.EWSSessionReconstructor.yaml) **解决的问题:** 当攻击者使用 EWS (Outlook/apps) 时,标准的日志审查只能提供孤立的请求行。此 artifact 根据会话 ID 关联请求以重构:按何种顺序读取了什么、发送了什么、创建了什么规则,并附带精确的时间戳。 **解析的内容:** - EWS 日志 (`C:\...\Logging\EWS\`) — W3C 格式 - HttpProxy EWS 日志 (`C:\...\Logging\HttpProxy\Ews\`) - RPC/HTTP Proxy 日志 **会话评分标记:** | 操作模式 | ExfiltrationScore 增加值 | |------------------|--------------------------| | `FindItem` 且 `MaxEntries > 500` | +25 | | `CreateItem` 且 `SendOnly` 发送至外部 | +35 | | `DeleteItem` 且使用 `HardDelete` | +20 | | `CreateRule` (任意) | +40 | | 来自非企业 IP 的会话 | +15 | | User-Agent 包含 `python`, `curl`, `Go-http` | +20 | **MITRE:** T1114.002, T1059.001 #### [`Exchange.BEC.AuthAnomalyEngine`](artifacts/exchange_bec/Exchange.BEC.AuthAnomalyEngine.yaml) **检测类型:** | 检测项 | 工作原理 | |-----------|-------------| | **不可能的旅行** | 同一用户账户在可配置的时间窗口内(默认:60 分钟)从两个地理位置相距甚远的 IP 进行身份验证 | | **MFA 绕过** | N 次 MFA 失败 (EventID 4625 且失败代码为 0xC0000064) 后紧接着成功 (4624) — 阈值可配置 | | **UA 激增** | 一个之前已建立的账户在基线窗口内未使用过,但出现了新的 User-Agent 字符串 | | **遗留协议** | 在 Exchange Online 或强制使用现代身份验证的环境中,检测到账户使用 NTLM 或 Basic 认证 | | **密码喷洒** | 单个源 IP 在 5 分钟内对 10 个以上不同账户进行身份验证 | **MITRE:** T1078.002, T1110.003, T1133, T1556.006 ### 🔒 持久化机制 #### [`Exchange.Persistence.OAuthAnomalyDetector`](artifacts/exchange_persistence/Exchange.Persistence.OAuthAnomalyDetector.yaml) **强大之处:** 它不仅能发现危险的 OAuth 授权,还能在可配置的时间窗口内将授权事件与后续活动(收件箱规则、传输规则、EWS 会话)进行关联,提供完整的击杀链视图。 **评分风险因素:** | 因素 | 分值 | |--------|--------| | 危险权限 (Mail.ReadWrite, Mail.Send 等) | +30/权限 | | 可疑应用名称 (正则匹配) | +20 | | 用户授权 (非管理员授权) | +25 | | 授权窗口内的收件箱/传输规则 | +35 | | 多 IP token 刷新 | +40 | | 应用注册时间不到 30 天 | +15 | **接受输入:** UAL CSV/JSON 导出文件、Azure AD 登录日志 JSON、本地 Event 日志 **MITRE:** T1098.002, T1528, T1550.001 #### [`Exchange.Persistence.TransportRuleHunter`](artifacts/exchange_persistence/Exchange.Persistence.TransportRuleHunter.yaml) **检测:** - 密送 (BCC) 至不在白名单中的外部域的规则 - 静默转发 (RemoveHeader + Redirect 组合) - 禁用基于 SCL 的垃圾邮件过滤的规则 - 针对高管邮箱的规则 (条件中包含 CEO/CFO 关键词模式) - 在非工作时间创建的规则 **解析内容:** 管理员审核日志 (Admin Audit Log)、邮件追踪日志 (Message Tracking Logs)、MSExchange 管理事件日志、PowerShell 记录 **MITRE:** T1114.003, T1020, T1048.003 ### 💥 漏洞利用检测 #### [`Exchange.Exploitation.WebshellHunter`](artifacts/exchange_exploitation/Exchange.Exploitation.WebshellHunter.yaml) **多向量检测:** 1. **熵评分** — web 目录中 Shannon 熵 > 5.0 (可配置) 的文件 2. **特征匹配** — 中国菜刀、AspxSpy、Antsword、Godzilla 模式 3. **IIS 关联** — 在 IIS 日志中具有 POST 请求 + 大响应体的 web 目录中的文件 4. **进程祖系** — w3wp.exe 的子进程 (需要 Sysmon 或 EDR 数据) 5. **时间戳异常** — 静态 web 目录中比 Exchange 安装日期还要新的文件 **扫描目录:** ``` C:\Program Files\Microsoft\Exchange Server\V15\FrontEnd\HttpProxy\ecp\ C:\Program Files\Microsoft\Exchange Server\V15\FrontEnd\HttpProxy\owa\ C:\inetpub\wwwroot\aspnet_client\ C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\ ``` **MITRE:** T1505.003, T1190, T1059.001 #### [`Exchange.Exploitation.ProxyVulnDetector`](artifacts/exchange_exploitation/Exchange.Exploitation.ProxyVulnDetector.yaml) **特定 CVE 检测特征:** | CVE | 名称 | 检测模式 | |-----|------|-----------------| | CVE-2021-26855 | ProxyLogon | 带有 `X-BEResource` cookie 篡改的 `/ecp/` 请求 | | CVE-2021-34473 | ProxyShell SSRF | `/autodiscover/autodiscover.json?a=` 模式 | | CVE-2022-41040 | ProxyNotShell SSRF | `/autodiscover/autodiscover.json?@[domain]/` | | CVE-2022-41082 | ProxyNotShell RCE | SSRF 之后对 PowerShell 后端的访问 | | CVE-2022-41080 | OWASSRF | `/owa/auth/Current/` 对 PowerShell 的 SSRF | **多源关联:** IIS 日志 + HttpProxy 日志 + 漏洞利用后的文件系统检查 — 全部通过时间戳和源 IP 进行关联,以生成置信度评分和攻击阶段图。 **MITRE:** T1190, T1505.003, T1071.001, T1059.001 ## 🧪 测试 ``` # 安装测试依赖 pip install pytest pyyaml # 运行完整测试套件 cd exchange-threat-hunting python3 -m pytest tests/ -v # 运行特定测试类 python3 -m pytest tests/test_artifacts.py::TestProxyVulnDetectorLogic -v # 运行并生成覆盖率报告 pip install pytest-cov python3 -m pytest tests/ --cov=scripts --cov-report=term-missing ``` **测试覆盖范围包括:** - ✅ 所有 7 个 artifact 作为有效的 YAML 加载 - ✅ 包含所有必需的 Velociraptor 结构键 - ✅ Artifact 名称与文件名完全匹配 - ✅ 所有 artifact 中均包含 MITRE ATT&CK 引用 - ✅ VQL 查询包含 SELECT 语句 - ✅ VQL 括号匹配平衡 - ✅ BEC 检测逻辑已通过模拟 EWS 日志验证 - ✅ OAuth 异常检测已通过模拟 UAL 数据验证 - ✅ ProxyNotShell 攻击链已验证 (SSRF → webshell 序列) - ✅ 不可能的旅行检测已验证 - ✅ 构建脚本是包含所需函数的有效 Python 脚本 ## 🗺️ MITRE ATT&CK 覆盖范围 ``` Enterprise ATT&CK Coverage Matrix: INITIAL ACCESS EXECUTION PERSISTENCE DEFENSE EVASION T1133 External Svcs ● T1059.001 PS ● T1098.002 Perms ● T1564.008 Email Hide ● T1190 Exploit App ● T1505.003 Webshell ● T1550.001 Alt Auth ● T1528 Steal Token ● CREDENTIAL ACCESS COLLECTION EXFILTRATION IMPACT T1078.002 Domain ● T1114.002 Remote ● T1020 Auto Exfil ● — T1110.003 Spray ● T1114.003 Fwd Rule ● T1048.003 Alt Proto ● T1556.006 MFA Bypass ● ● = Detected by this library ``` | Artifact | 覆盖的技术 | |----------|-------------------| | InboxRuleHunter | T1114.003, T1564.008 | | EWSSessionReconstructor | T1114.002, T1059.001 | | AuthAnomalyEngine | T1078.002, T1110.003, T1133, T1556.006 | | OAuthAnomalyDetector | T1098.002, T1528, T1550.001 | | TransportRuleHunter | T1114.003, T1020, T1048.003 | | WebshellHunter | T1505.003, T1190, T1059.001 | | ProxyVulnDetector | T1190, T1505.003, T1071.001, T1059.001 | ## 📚 文档 | 文档 | 描述 | |----------|-------------| | [狩猎手册](docs/hunting_playbook.md) | 针对 6 种 Exchange 攻击场景的逐步 IR 操作手册 | | [部署指南](docs/deployment_guide.md) | 安装、配置与 Velociraptor 集成 | | [Artifact 目录](#-artifact-catalog) | 本 README — 完整的 artifact 描述 | ## 🛠️ 开发 ### 添加新的 Artifact ``` # 1. 创建 artifact YAML cp artifacts/exchange_bec/Exchange.BEC.InboxRuleHunter.yaml \ artifacts/exchange_bec/Exchange.BEC.MyNewArtifact.yaml # 2. 编辑 artifact # 确保:name 与文件名匹配,type 有效,sources 包含 query # 3. 验证 python3 scripts/build_bundle.py --validate-only --verbose # 4. 在 tests/test_artifacts.py 中编写测试 # 添加到 EXPECTED_ARTIFACTS 列表并编写检测逻辑测试 # 5. 运行测试 python3 -m pytest tests/ -v # 6. 重新构建 bundle python3 scripts/build_bundle.py ``` ### VQL 风格指南 ``` -- ✅ DO: Use LET for reusability LET ExchangePath <= pathspec( path_components=["C:", "Program Files", "Microsoft", "Exchange Server", "V15"] ) -- ✅ DO: Use lazy evaluation with foreach + limit SELECT * FROM foreach( row={SELECT OSPath FROM glob(globs=LogGlob)}, query={SELECT * FROM parse_csv(filename=OSPath) LIMIT 10000} ) -- ✅ DO: Use streaming regex WHERE OSPath =~ '''(?i)autodiscover\.json\?@''' -- ❌ DON'T: Read entire file into memory for large logs -- LET content = read_file(filename=LogPath) -- avoid for large files -- ✅ DO: Use parse_lines for large log files SELECT * FROM foreach( row={SELECT OSPath FROM glob(globs=LogGlob)}, query={SELECT Line FROM parse_lines(filename=OSPath)} ) ``` ### Artifact 提交检查清单 - [ ] Artifact 名称遵循 `Exchange..` 约定 - [ ] YAML 有效且加载无错误 - [ ] `description 清楚地说明了威胁场景 - [ ] 至少包含一个 `author` 字段 - [ ] `references` 中包含 MITRE ATT&CK 技术 - [ ] 所有参数均具有 `name`, `type`, `description`, 和 `default` - [ ] VQL 中没有占位符/TODO 文本 - [ ] `test_artifacts.py` 中至少有一个测试覆盖了检测逻辑 - [ ] 如果测试检测模式,则需提供模拟数据 ## 📋 更新日志 ### v1.0.0 — 2026-07-21 - 首次发布包含 7 个生产级 artifact - 提供针对 BEC、OAuth 持久化、ProxyChain 漏洞利用和 webshell 狩猎的完整 DFIR 操作手册 - 包含 30 多个测试用例的 pytest 测试套件 - 带有 JSON CI 输出模式的 Python 构建/验证脚本 - 带有 Velociraptor 服务器集成的 Bash 部署脚本 ## ⚖️ 法律与负责任的使用 本库旨在对您拥有或获得明确书面授权测试的系统进行**授权的安全测试、事件响应和威胁狩猎**。在未经授权的情况下对系统使用这些工具可能会违反《计算机欺诈和滥用法》(CFAA)、《计算机滥用法》(英国) 以及其他司法管辖区的类似法律。 **作者对滥用行为不承担任何责任。** ## 📄 许可证 MIT 许可证 — 完整文本请参见 [LICENSE](LICENSE)。
**为 DFIR 社区而生。永久开源。** [⭐ 为本仓库点星](https://github.com/velolibrary/exchange-threat-hunting) · [🐛 报告 Bug](https://github.com/velolibrary/exchange-threat-hunting/issues) · [💡 请求新功能](https://github.com/velolibrary/exchange-threat-hunting/issues)
标签:安全规则引擎, 逆向工具