azurekid/spraycat

GitHub: azurekid/spraycat

针对 Microsoft Entra ID 的密码喷洒工具,通过横向喷洒、批量冷却和多应用轮换等技术规避 Smart Lockout 检测并识别有效凭据。

Stars: 0 | Forks: 0

# Invoke-SprayCat 🐾 ![alt text](https://raw.githubusercontent.com/azurekid/spraycat/main/image.png) Invoke-SprayCat 是一款复杂的密码喷洒工具,专为针对 Microsoft Entra ID(原 Azure AD)租户的渗透测试而设计。它实施了多种规避技术,以最大限度地减少检测并避免账户锁定,同时识别有效凭据。 ## 目录 - [功能特性](#features) - [安装说明](#installation) - [快速开始](#quick-start) - [参数说明](#parameters) - [工作原理](#how-it-works) - [IP 信誉与批量模式](#ip-reputation--batch-mode) - [规避技术](#evasion-techniques) - [输出与结果](#output--results) - [AADSTS 错误智能分析](#aadsts-error-intelligence) - [最佳实践](#best-practices) - [示例](#examples) - [法律免责声明](#legal-disclaimer) ## 功能特性 | 特性 | 描述 | |---------|-------------| | **横向喷洒 (Horizontal Spray)** | 在移动到下一个密码之前,针对所有用户测试一个密码 | | **批量模式 (Batch Mode)** | 喷洒 N 个用户,然后暂停以重置 IP 信誉(避免基于 IP 的锁定) | | **智能延迟** | 轮次和用户之间可配置的延迟,以规避 Smart Lockout | | **IP 信誉规避** | 检测到连续锁定后的自动冷却 | | **多应用轮换** | 轮换使用 24 个以上的合法 Microsoft 应用程序 client ID | | **流量合法性** | 随机化的 User-Agent、Header 和 Accept-Language 值 | | **MFA 检测** | 即使 MFA 阻止了完整认证,也能识别有效凭据 | | **AADSTS 智能** | 全面的错误代码解析,包含 25 个以上的分类响应 | | **进度指示器** | 实时进度条,显示当前用户和完成百分比 | | **CSV 导出** | 自动导出带有时间戳和 Token 的有效凭据 | | **锁定保护** | 达到锁定阈值时自动停止 | ## 安装说明 ``` # 克隆 SprayCat 仓库 git clone https://github.com/azurekid/spraycat.git # 导入函数 . ./spraycat/Invoke-SprayCat.ps1 ``` ### 系统要求 - PowerShell 5.1 或更高版本 - 访问 `login.microsoftonline.com` 的网络权限 - 无需额外的模块 ### 支持的输入格式 SprayCat 自动检测用户列表文件格式: | 格式 | 描述 | 示例 | |--------|-------------|---------| | `.txt` | 每行一个 UPN | `user@domain.com` | | `.csv` | 包含 userPrincipalName、email 或 mail 列的 CSV | Entra ID 导出文件 | | `.json` | JSON 数组或 Microsoft Graph 响应 | `{"value": [...]}` | ## 快速开始 ``` # 使用默认批量设置进行基本喷洒(25 个用户,5 分钟冷却) Invoke-SprayCat -UserListFile users.txt -PasswordListFile passwords.txt # 使用直接从 Entra ID 导出的 CSV Invoke-SprayCat -UserListFile exportUsers.csv -PasswordListFile passwords.txt # 保守批量模式(推荐用于大型用户列表) Invoke-SprayCat -UserListFile users.txt -PasswordListFile passwords.txt -BatchSize 20 -BatchCooldown 600 # 隐身模式,每轮密码尝试之间延迟 30 分钟 Invoke-SprayCat -UserListFile users.txt -PasswordListFile passwords.txt -DelayBetweenAttempts 1800 # 快速测试(观察模式 - 跳过轮次间延迟,不适用于生产环境) Invoke-SprayCat -UserListFile users.txt -PasswordListFile passwords.txt -ObservationMode ``` ## 参数说明 | 参数 | 类型 | 默认值 | 描述 | |-----------|------|---------|-------------| | `UserListFile` | String | *必填* | 包含目标用户名的文件路径(自动检测 txt/csv/json) | | `PasswordListFile` | String | *必填* | 包含待测试密码的文件路径(每行一个) | | `MaxFailures` | Int | 100 | 停止前最大失败尝试次数 | | `DelayBetweenAttempts` | Int | 30 | 密码喷洒轮次之间的秒数 (1-3600) | | `DelayBetweenUsers` | Int | 5 | 每个用户尝试之间的秒数 (0-1800) | | `BatchSize` | Int | 25 | 触发冷却前喷洒的用户数量 (5-100) | | `BatchCooldown` | Int | 300 | 批次之间等待 IP 信誉重置的秒数 (60-7200) | | `NoDelay` | Switch | False | 跳过所有延迟(谨慎使用) | | `ObservationMode` | Switch | False | 跳过轮次间延迟以进行测试 | ### 批量模式建议 | 场景 | BatchSize | BatchCooldown | 风险等级 | |----------|-----------|---------------|------------| | 测试/实验室 | 50 | 60s | 🔴 高 | | 标准评估 | 25 | 300s (5 分钟) | 🟡 中 | | 生产/隐蔽 | 15-20 | 600s (10 分钟) | 🟢 低 | ### 延迟建议 | 场景 | DelayBetweenAttempts | 风险等级 | |----------|---------------------|------------| | 测试/实验室 | 30 秒 | 🔴 高 | | 快速评估 | 300 秒 (5 分钟) | 🟡 中 | | 生产/隐蔽 | 1800-3600 秒 (30-60 分钟) | 🟢 低 | ## 工作原理 ### 横向喷洒方法论 与垂直喷洒(针对一个用户测试多个密码)不同,SprayCat 使用横向喷洒: ``` Round 1: Password1 → User1, User2, User3, ... UserN Round 2: Password2 → User1, User2, User3, ... UserN Round 3: Password3 → User1, User2, User3, ... UserN ``` 这种方法确保每个用户每轮只接收一次认证尝试,远低于 Smart Lockout 的 10 次失败尝试阈值。 ### 为什么选择横向喷洒?(背后的数学原理) 您可能会问:*“为什么不同时向不同用户分发不同的密码?`Password1→User1, Password2→User2, Password3→User3` 难道不会更随机、更难检测吗?”* **残酷的真相是:那种方法导致锁定*更快*,而不是更慢。** #### Smart Lockout 不在乎您尝试哪个密码 Microsoft 的 Smart Lockout 统计的是**每个用户的失败尝试**,无论尝试的是哪个密码: | 模式 | User1 在 5 分钟内的遭遇 | 锁定风险 | |---------|------------------------------|--------------| | **横向喷洒** | 1 次失败尝试 (仅 Password1) | 🟢 极低 | | **对角/随机** | 3-5 次失败尝试 (Password1, Password3, Password5...) | 🔴 高 | #### 视觉对比 ``` HORIZONTAL (Safe) - Each user gets 1 attempt, then we wait: ┌─────────────────────────────────────────────────────────────┐ │ Round 1: Password1 → User1, User2, User3, User4, User5 │ │ [WAIT 30 MINUTES - lockout counters reset] │ │ Round 2: Password2 → User1, User2, User3, User4, User5 │ │ [WAIT 30 MINUTES - lockout counters reset] │ └─────────────────────────────────────────────────────────────┘ User1 failures: 1 per 30 minutes = NEVER locks out DIAGONAL (Risky) - Multiple passwords hit same user quickly: ┌─────────────────────────────────────────────────────────────┐ │ Batch 1: Pass1→User1, Pass2→User2, Pass3→User3, Pass4→User4│ │ Batch 2: Pass2→User1, Pass3→User2, Pass4→User3, Pass1→User4│ │ Batch 3: Pass3→User1, Pass4→User2, Pass1→User3, Pass2→User4│ └─────────────────────────────────────────────────────────────┘ User1 failures: 3 within minutes = APPROACHING LOCKOUT ``` #### 对角喷洒声称能解决(但实际上解决不了)的问题 | 声称的好处 | 现实情况 | |-----------------|---------| | 规避“相同密码,多个用户”的检测 | Microsoft 没有这种检测机制 | | 在日志中看起来更“随机” | 现代 SIEM 无论模式如何都能检测到喷洒行为 | | 心理混淆 | 通过隐蔽性实现的安全性 ≠ 实际的安全性 | #### 对角喷洒实际会破坏什么 1. **延迟逻辑变得复杂** - 何时延迟?需要跟踪每个用户的状态 2. **崩溃恢复噩梦** - 如果脚本停止,哪些用户/密码组合已完成? 3. **更高的锁定风险** - 一个实施漏洞 = 大规模账户锁定 4. **没有检测优势** - Entra ID Identity Protection 无论如何都会标记异常登录模式 #### 什么时候对角喷洒有用? 只有当 Microsoft 实施了以下措施时: - “跨多个用户的相同密码哈希”检测(需要存储明文 - 他们不这样做) - 基于密码模式的分析(在大规模计算上成本过高) **这两者都不存在。带有轮次间延迟的横向喷洒在数学上是最优的。** ## IP 信誉与批量模式 ### 隐藏层:基于 IP 的聚合检测 大多数密码喷洒指南关注每账户锁定阈值(每个用户 10 次失败尝试)。然而,Azure Smart Lockout 有一个**第二层,较少记录的层级**: #### 这意味着什么 | 检测层 | 阈值 | 范围 | 被锁定的内容 | |-----------------|-----------|-------|------------------| | **每账户** | 10 次失败尝试 | 单个用户 | 该特定账户 | | **IP 信誉** | ~40 次失败尝试 | 来自该 IP 的所有用户 | 来自该 IP 的所有账户 | #### 问题所在 当从单个 IP 地址喷洒时: 1. Azure 跟踪来自您 IP 的**跨所有账户的总失败尝试次数** 2. 大约 40 次尝试后,您的 IP 被标记为“不熟悉/敌对位置” 3. Azure 开始对所有用户返回 `AADSTS50053`,无论每账户尝试次数如何 4. 您的喷洒实际上被阻止了 ### 解决方案:批量模式 SprayCat 实施了带有冷却期的**基于批次的喷洒**: ``` ┌─────────────────────────────────────────────────────────────────────────┐ │ Batch 1: Users 1-25 → [5 min cooldown - IP reputation resets] │ │ Batch 2: Users 26-50 → [5 min cooldown - IP reputation resets] │ │ Batch 3: Users 51-75 → [5 min cooldown - IP reputation resets] │ │ ... │ └─────────────────────────────────────────────────────────────────────────┘ ``` #### 工作原理 1. **BatchSize (默认: 25)**:暂停前测试的用户数量 2. **BatchCooldown (默认: 300s)**:等待 IP 信誉重置的秒数 3. **早期检测**:3 次以上连续锁定触发紧急冷却 #### 为什么是 25 个用户 / 5 分钟? - Microsoft 的观察窗口似乎在不活动一段时间后重置 - 25 个用户安全地低于 ~40 的阈值 - 5 分钟为信誉评分衰减提供了足够的时间 - 对于敏感目标,建议使用保守设置(20 个用户 / 10 分钟) ### 熟悉与不熟悉的位置 Smart Lockout 为熟悉和不熟悉的位置维护**独立的计数器**: 您的喷洒 IP 始终被视为**不熟悉的位置**,这意味着: - 适用更严格的阈值 - 对失败尝试的容忍度更低 - IP 信誉下降更快 ### 推荐批量设置 ``` # 标准评估(50+ 用户) Invoke-SprayCat -UserListFile users.txt -PasswordListFile passwords.txt -BatchSize 25 -BatchCooldown 300 # 大型用户列表(200+ 用户) Invoke-SprayCat -UserListFile users.txt -PasswordListFile passwords.txt -BatchSize 20 -BatchCooldown 600 # 最大隐蔽性(敏感目标) Invoke-SprayCat -UserListFile users.txt -PasswordListFile passwords.txt -BatchSize 15 -BatchCooldown 900 ``` ### 认证流程 ``` graph TD A[Select Random App] --> B[Generate Random Headers] B --> C[Build OAuth2 ROPC Request] C --> D[Send to login.microsoftonline.com] D --> E{Response?} E -->|Success| F[Extract Access Token] E -->|Error| G[Parse AADSTS Code] G --> H{Error Category} H -->|MFA Required| I[Log Valid Credentials] H -->|Invalid Creds| J[Continue] H -->|Locked| K[Increment Lockout Counter] H -->|Throttled| L[Exponential Backoff] ``` ## 规避技术 ### 1. 多应用程序轮换 SprayCat 在 24 个合法的 Microsoft 应用程序 client ID 之间轮换: | 类别 | 应用程序 | |----------|--------------| | **Azure 与管理** | Azure PowerShell, Azure CLI, Azure Portal, VS Code, Visual Studio | | **Microsoft 365** | Office, Teams, OneDrive, Outlook, SharePoint, Word, Excel, PowerPoint | | **移动与认证** | Authenticator, Company Portal, Intune | | **Windows** | Windows Login, Windows Store | | **Power Platform** | Power BI, Power Automate, Power Apps | | **Dynamics** | Dynamics CRM | ### 2. 随机 User-Agent 跨越多个平台的 19 个真实浏览器 user-agent: - **Windows**: Chrome, Edge, Firefox - **macOS**: Chrome, Safari, Firefox - **iOS**: Safari (iPhone, iPad) - **Android**: Chrome (Pixel, Samsung) - **Linux**: Chrome, Firefox ### 3. 地理位置多样性 Accept-Language Header 在 12 个区域设置之间轮换: - 英语 (US, GB, AU, CA) - 欧洲 (NL, DE, FR, ES, IT, PT-BR) - 亚洲 (JA) ### 4. 真实的请求头 每个请求包含随机化的: - `X-Ms-Client-Request-Id` (关联 ID) - `Sec-Ch-Ua-Platform` (操作系统平台) - `X-Client-Os` (客户端操作系统) - 安全 Header (`Sec-Fetch-*`) - MSAL 客户端元数据 ## 输出与结果 ### 控制台输出 ``` ╔══════════════════════════════════════════════════════════════════════════╗ ║ ║ ║ ███████╗██████╗ ██████╗ █████╗ ██╗ ██╗ ██████╗ █████╗ ████████╗ ║ ║ ██╔════╝██╔══██╗██╔══██╗██╔══██╗╚██╗ ██╔╝ ██╔════╝██╔══██╗╚══██╔══╝ ║ ║ ███████╗██████╔╝██████╔╝███████║ ╚████╔╝ ██║ ███████║ ██║ ║ ║ ╚════██║██╔═══╝ ██╔══██╗██╔══██║ ╚██╔╝ ██║ ██╔══██║ ██║ ║ ║ ███████║██║ ██║ ██║██║ ██║ ██║ ╚██████╗██║ ██║ ██║ ║ ║ ╚══════╝╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝ ╚═╝ ║ ║ ║ ║ 🐾 ENTRA ID PASSWORD SPRAY TOOL 🐾 ║ ║ ║ ║ /\_/\ Smart Lockout Evasion ║ ║ ( o.o ) Horizontal Spray Methodology ║ ║ > ^ < MFA Detection & Reporting ║ ║ /| |\ AADSTS Error Intelligence ║ ║ (_| |_) CSV Export & Analytics ║ ║ ║ ║ [+] Stealth Mode [+] Multi-App Rotation [+] Smart Delays ║ ║ ║ ╚══════════════════════════════════════════════════════════════════════════╝ [+] Spray: HORIZONTAL (Batch Mode) [*] Targets: 50 users × 3 passwords = 150 attempts [*] Batches: 25 users per batch, 300s cooldown between batches [*] Delays: 1800s between rounds, 5s between users [*] IP Evasion: 2 batches × 3 rounds = 6 cooldown periods [*] ═══ ROUND 1/3 ═══ [████████████░░░░░░░░] 60% (15/25) Testing: john.doe... [+] VALID PASSWORD: user@contoso.com : Summer2024! → MFA REQUIRED Valid credentials - MFA required | Microsoft Teams [!] Batch 1 complete. Cooling down 300s (IP reputation reset)... [▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░] 180s remaining... [*] Resuming spray... [+] FULL ACCESS: admin@contoso.com : Welcome123! Full access granted | Azure PowerShell Access Token: Acquired [*] Round 1: Valid=2 ╔══════════════════════════════════════════════════════════════════════════╗ ║ SPRAY RESULTS ║ ╚══════════════════════════════════════════════════════════════════════════╝ [*] Completed in 45.2 minutes [+] Valid Credentials: 2 user@contoso.com : Summer2024! admin@contoso.com : Welcome123! [+] Exported to: ValidCredentials_20260120_143052.csv [*] Stats: Attempts=150 | InvalidCredentials=148 ``` ### CSV 导出 有效凭据会自动导出到 CSV,包含以下列: | 列名 | 描述 | |--------|-------------| | Timestamp | 凭据验证的时间 | | Username | 标准化用户名(UPN 格式) | | Password | 有效密码 | | Application | 用于认证的 Microsoft 应用程序 | | Resource | 目标资源(Graph, Management 等) | | ErrorCode | AADSTS 代码(如果被 MFA/CA 阻止) | | Details | 人类可读的状态消息 | | AccessToken | JWT Token(用于完全访问场景) | ## AADSTS 错误智能分析 SprayCat 将 25 个以上的 AADSTS 错误代码分类为可操作的类别: ### 有效凭据(密码已确认) | 代码 | 类别 | 含义 | |------|----------|---------| | AADSTS50076 | MFA 必需 | 密码有效,MFA 阻止 | | AADSTS50079 | MFA 必需 | 需要 MFA 交互 | | AADSTS50074 | MFA 必需 | 需要强认证 | | AADSTS50158 | 条件访问 | CA 策略要求 MFA/设备 | | AADSTS50055 | 密码过期 | 密码有效但已过期 | ### 账户问题 | 代码 | 类别 | 操作 | |------|----------|--------| | AADSTS50126 | 无效凭据 | 继续喷洒 | | AADSTS50053 | 账户锁定 | 暂停,增加锁定计数器 | | AADSTS50057 | 账户禁用 | 跳过用户 | | AADSTS50034 | 用户未找到 | 跳过用户 | ### 阻止条件 | 代码 | 类别 | 操作 | |------|----------|--------| | AADSTS53003 | CA 阻止 | 记录并继续 | | AADSTS530034 | 位置阻止 | 记录并继续 | | AADSTS50196 | 限流 | 指数退避 | ## 最佳实践 ### 开始之前 1.获得授权**:在测试前务必获得书面许可 2. **与蓝队协调**:通知 SOC/安全团队测试时间窗口 3. **先使用测试账户**:在非生产环境中验证工具行为 4. **预估时长**:计算包括批次冷却在内的总时间 ### 喷洒期间 1. **使用批量模式**:用户列表超过 25 个用户时务必启用 2. **从长延迟开始**:轮次之间使用 1800+ 秒 3. **保守的批量设置**:敏感目标使用 20 个用户 / 600s 冷却 4. **监控锁定**:如果检测到连续锁定立即停止 5. **限制密码数量**:每次会话 2-3 个密码更安全 6. **战略性安排**:在工作时间测试以混入正常流量 ### 时间计算 ``` # 示例:100 个用户,3 个密码,BatchSize=25,BatchCooldown=300s # 每轮批次:100/25 = 4 个批次 # 每轮冷却:3 次(在第 1、2、3 批次后 - 最后一次后无) # 总冷却时间:3 轮 × 3 次冷却 × 300s = 2700s = 45 分钟 # 加上喷洒时间和轮次间延迟 ``` ### 喷洒之后 1. **保护结果**:加密包含有效凭据的 CSV 文件 2. **及时报告**:立即向客户通报任何有效凭据 3. **删除 Token**:如果经过测试,CSV 中的 Access Token 应被撤销 ## 示例 ### 示例 1:标准评估 ``` # 通过枚举创建用户列表 Get-Content enumerated-users.txt | Out-File users.txt # 创建密码列表(常见模式) @('Spring2024!', 'Summer2024!', 'Welcome123!') | Out-File passwords.txt # 运行喷洒并设置 30 分钟延迟 Invoke-SprayCat -UserListFile users.txt -PasswordListFile passwords.txt -DelayBetweenAttempts 1800 ``` ### 示例 2:快速验证(仅限实验室) ``` # 用于实验室环境测试的快速模式 Invoke-SprayCat -UserListFile users.txt -PasswordListFile passwords.txt -ObservationMode -NoDelay ``` ### 示例 3:外部访客账户 SprayCat 自动处理外部访客账户格式: ``` # 输入文件可以包含: # john.doe_contoso.com#EXT#@fabrikam.onmicrosoft.com # jane.smith@fabrikam.com # 两种格式均会被标准化并正确测试 Invoke-SprayCat -UserListFile mixed-users.txt -PasswordListFile passwords.txt ``` ### 示例 4:详细输出 ``` # 启用详细日志记录以进行故障排除 Invoke-SprayCat -UserListFile users.txt -PasswordListFile passwords.txt -Verbose ``` ## 返回值 该函数返回一个包含详细统计信息的哈希表: ``` $results = Invoke-SprayCat -UserListFile users.txt -PasswordListFile passwords.txt # 访问结果 $results.ValidCredentials # List of valid credential objects $results.TotalAttempts # Total authentication attempts $results.InvalidCredentials # Count of invalid password attempts $results.AccountLocked # Count of locked accounts encountered $results.SprayRounds # Number of password rounds completed ``` ## 法律免责声明 ⚠️ **警告**:此工具仅供授权安全测试使用。 - **始终**在测试前获得书面授权 - 未经授权访问计算机系统是**违法的** - 作者不对本工具的滥用承担任何责任 - 您有责任遵守所有适用法律 使用此工具即表示您同意仅在获得适当授权的情况下将其用于合法的安全测试。 ## 贡献 发现了 Bug 或有功能请求?请在 [SprayCat GitHub 仓库](https://github.com/azurekid/spraycat) 提交 issue。 ## 参考资料 - [Microsoft AADSTS 错误代码](https://learn.microsoft.com/en-us/entra/identity-platform/reference-error-codes) - [Azure AD Smart Lockout](https://learn.microsoft.com/en-us/entra/identity/authentication/howto-password-smart-lockout) - [OAuth 2.0 ROPC Flow](https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth-ropc) *[BlackCat](https://github.com/azurekid/blackcat) 安全工具包的一部分。*
标签:AI合规, Azure AD, IAM 安全, IPv6, Libemu, Libemu, MFA 检测, Microsoft Entra ID, PoC, PowerShell, 凭据攻击, 初始访问, 密码喷射, 暴力破解, 目录服务, 私有化部署, 网络安全, 规避技术, 防御规避, 隐私保护