1r0BIT/WinSSHound

GitHub: 1r0BIT/WinSSHound

在 Active Directory 域环境中发现 Windows SSH 服务错误配置并映射横向移动路径,与 BloodHound CE 集成实现攻击路径可视化。

Stars: 85 | Forks: 11

# WinSSHound

Python 3.11+ Linux MIT License Version 0.2.0

**Windows SSH 错误配置发现工具** — 在 Active Directory 环境中,通过错误配置的 SSH 服务映射横向移动路径。 ## 问题背景 Windows 服务器和工作站可以运行 SSH 服务——无论是内置的 OpenSSH Server (Server 2019+)、Bitvise SSH Server、IBM 大型机 SSH 网桥,还是任何数量的第三方 SFTP 解决方案。当这些服务配置不当时(且默认配置往往非常宽松),导致的结果都是一样的: - **任何经过身份验证的域用户都可以登录。** 普通用户、服务账户、计算机账户。不需要任何特殊的组成员身份。 - 在默认的 Windows OpenSSH 安装中(Server 2025 之前),SSH daemon 会完全忽略 GPO 的“拒绝登录 (Deny Logon)”策略。 - SSH 登录事件使用类型 8 (NetworkCleartext),根据具体环境的不同,现有检测规则可能无法覆盖到此类事件。 这些 SSH 访问路径在标准的 BloodHound 收集模式下是不可见的。而 WinSSHound 可以找到它们。 ## WinSSHound 的功能 1. **发现** 启用了 SSH 的目标(通过 LDAP、手动指定目标或导入 nmap XML) 2. **测试** 在多个端口上使用提供的凭据进行 SSH 身份验证 3. **读取** `sshd_config` 以确定*谁*可以真正通过 SSH 登录(`--invasive` 模式) 4. **生成** 包含 `WinSSH_CanSSH` 边的 BloodHound CE OpenGraph JSON 数据 5. **上传** 发现结果到 BloodHound CE,用于攻击路径可视化 ## 快速开始 ``` # 安装 git clone https://github.com/1r0BIT/WinSSHound.git cd WinSSHound pip install -e . # 基础扫描 winsshound -u cloud.strife -p 'Buster$word97!' -d shinra.local -t reactor01.shinra.local # 通过 LDAP 自动发现目标,扫描多个端口 winsshound -u cloud.strife -p 'Buster$word97!' -d shinra.local \ --auto-targets --dc-ip 10.0.0.1 --port 22,2222,8022 # 侵入模式 — 读取 sshd_config 以确定实际访问策略 winsshound -u cloud.strife -p 'Buster$word97!' -d shinra.local \ --auto-targets --dc-ip 10.0.0.1 --invasive --no-confirm # 完整扫描并上传至 BloodHound CE winsshound -u cloud.strife -p 'Buster$word97!' -d shinra.local \ --auto-targets --dc-ip 10.0.0.1 --invasive --no-confirm \ --bh-url http://localhost:8080 --bh-username admin --bh-password 'bhpass' # 导入 nmap 结果(来自服务扫描的目标和端口) nmap -sV -p- --open -oX ssh_scan.xml 192.168.1.0/24 winsshound -u cloud.strife -p 'Buster$word97!' -d shinra.local --nmap-xml ssh_scan.xml ``` 需要 Python 3.11+ 以及 Linux 攻击机。 ## `--invasive` 模式 这是一个非常关键的标志。如果不使用它,你只能知道“该用户可以通过 SSH 登录到此处”。加上该标志后,WinSSHound 会从每个目标读取 `sshd_config` 并确定访问是受限制的还是完全开放的。边会根据以下情况相应地发生变化: - **默认 sshd_config(无限制)** -> 从 `Authenticated Users` 到计算机的边 - **配置了 AllowGroups** -> 从每个允许的组到计算机的边 - **配置了 AllowUsers** -> 从每个允许的用户到计算机的边 `--invasive` 会在目标上执行命令(读取配置文件),因此带有 OPSEC 警告和确认提示。在脚本中可以使用 `--no-confirm` 跳过该提示。 **策略解析仅涵盖 OpenSSH 语法的配置**(Microsoft 的 OpenSSH for Windows + Cygwin / copssh)。Bitvise 将其策略存储在注册表中;Tectia 使用自己的 `sshd2_config` 语法;像 Serv-U、MOVEit、GoAnywhere 和 Globalscape EFT 这样的 MFT 产品通过 GUI / 数据库进行管理。WinSSHound 仍然可以从 SSH 横幅中检测到这些产品,并使用 `ssh_product` 属性标记每个 CanSSH 边——但对于非 OpenSSH 产品,边会保持为 user→computer(即扫描用户的访问路径),并标记上 `policy_unparseable: true`,以便让你知道你是在盲目查看。有关完整的产品列表,请参阅 [docs/bloodhound-integration.md](docs/bloodhound-integration.md#ssh-server-product-detection)。 解析器不会处理 `Include` 指令 (Windows OpenSSH 9.5+) —— 如果真正的访问策略位于被包含的配置片段中,该工具会夸大访问权限。如果结果看起来过于宽松,请手动检查 `Include` 行。 ## 选择合适的账户 为了获得最准确的结果,请使用你拥有的最低权限账户进行扫描: - **普通域用户**(仅为 Domain Users 成员):如果此账户可以通过 SSH 登录,那么所有人都可以。 - **计算机账户**:如果 MachineAccountQuota > 0,请使用 `Impacket` 或 `PowerMad` 创建一个。计算机账户几乎是权限最低的了——除非 Domain Computers 被授予了额外的权限,如果是那样的话,客户面临的问题就比 SSH 严重得多了。 如果需要,可以使用单独的特权账户来进行 `--auto-targets` 的 LDAP 发现。 ## 文档 | 指南 | 描述 | |-------|-------------| | [BloodHound 集成](docs/bloodhound-integration.md) | 设置、`--invasive` 边语义、入门查询 | | [OpenGraph 节点和边](docs/opengraph-nodes-and-edges.md) | `WinSSH_CanSSH` 边参考 —— 滥用信息、修复方法、OPSEC | | [OPSEC 注意事项](docs/opsec.md) | 协议影响、检测痕迹、扫描配置文件 | | [入门查询](opengraph/queries/README.md) | BloodHound CE 的预构建 Cypher 查询 | | [攻击路径图](docs/opengraph-diagram.json) | 导入到 [arrows.app](https://arrows.app) | ## CLI 参考 ``` Usage: winsshound [-h] [-V] -u USERNAME -p PASSWORD -d DOMAIN [-t TARGET] [--targets-file FILE] [--nmap-xml FILE] [--auto-targets] [--dc-ip HOST] [--ldaps] [--resolve-hostnames] [--ns IP] [--port PORTS] [--timeout TIMEOUT] [--threads THREADS] [-v] [--invasive] [--no-confirm] [--bh-url URL] [--bh-username USER] [--bh-password PASS] [--bh-api-key KEY] [--bh-api-key-id ID] [--no-upload] [--allow-orphans] [--output FILE] [--json-only] Authentication: -u, --username Username for SSH authentication -p, --password Password for SSH authentication -d, --domain Domain name (e.g., corp.local) Targets: -t, --target Target(s): single IP, comma-separated, CIDR, or hostname --targets-file File containing targets (one per line) --nmap-xml Import targets from nmap XML (hosts with open SSH ports) --auto-targets Discover Windows computers via LDAP --dc-ip Domain controller IP for LDAP queries --ldaps Use LDAPS (port 636) instead of LDAP --resolve-hostnames Resolve IP addresses to hostnames via reverse DNS --ns DNS server for reverse lookups Options: --port SSH port(s), comma-separated (default: 22) --timeout Connection timeout in seconds (default: 5) --threads Number of parallel threads (default: 10) --invasive Read sshd_config to determine who can actually SSH in --no-confirm Skip confirmation prompts -v, --verbose Enable verbose output BloodHound Integration: --bh-url BloodHound CE URL (default: http://localhost:8080) --bh-username BloodHound username --bh-password BloodHound password --bh-api-key BloodHound API key --bh-api-key-id BloodHound API key ID --no-upload Generate JSON but don't upload --allow-orphans Include edges with placeholder SIDs Output Options: --output Write OpenGraph JSON to file --json-only Only output JSON, skip console table ``` ## 防御建议 1. **在 `sshd_config` 中添加 `AllowGroups` 或 `AllowUsers`** — 这是直接解决 SSH 默认开放访问的方法。适用于 OpenSSH;第三方 SSH 服务器有其自己的 ACL 机制。 2. **使用 OpenSSH Users 组 (Server 2025+)** — 验证组名是否与你的系统区域设置匹配。非英文版 Windows 会本地化组名,但配置文件引用的是英文组名。 3. **监控类型 8 登录事件**(Event ID 4624)— 这是 SSH 的登录类型。如果现有检测规则中尚未包含,建议添加进去。 4. **禁用未使用的 SSH 服务** — 适用于内置的 OpenSSH 和第三方 SSH/SFTP 服务器。 5. **网络隔离** — 无法攻击你无法到达的目标。限制不需要 SSH 连接的 VLAN 之间的 SSH 端口。 ## 参考 - [他们说你应该在 Windows 上使用 SSH...](https://it-pro-berlin.de/2024/07/use-ssh-on-windows-they-said/) — Evgenij Smirnov (第 1 部分) - [Windows Server 中的 SSH:有所改善,但仍然不够好](https://it-pro-berlin.de/2025/02/ssh-in-windows-server-better-still-not-good-enough/) — Evgenij Smirnov (第 2 部分) ## 许可证 MIT 许可证 — 详见 [LICENSE](LICENSE)。 ## 作者 **0xr0BIT** *WinSSHound 仅旨在用于授权的安全测试和研究。在测试前,请务必获取适当的授权。*
标签:Active Directory, AD域安全, BloodHound, CanSSH边缘, Checkov, Conpot, IP 地址批量处理, LDAP查询, Maven构建, OpenGraph, Plaso, Python, SMB, SSH安全, Windows安全, WinSSH, 企业安全, 关系图谱, 内存分配, 内部威胁, 协议分析, 基线检查, 威胁建模, 安全可视化, 插件系统, 攻击路径分析, 无后门, 无线安全, 权限提升, 横向移动, 活动目录, 编程规范, 网络发现, 网络渗透测试, 网络资产管理, 逆向工具, 错误配置检测