Windows有多种远程命令执行的方法,今天一起学习下WinRM。
图片来源于https://us.informatiweb-pro.net/system-admin/win-server/ws-2012-2012-r2-manage-multiple-servers-remotely-securely.html
#0x00 WinRM服务介绍
Web服务管理协议(WS-MAN,WS-Management,Web Services-Management)是一种基于标准简单对象访问协议(SOAP)的DMTF开放标准,允许不同供应商的硬件和操作系统相互操作,用于对服务器等网络设备以及各种Web应用程序进行管理。
WinRM(Windows Remote Management)是Windows对WS-Management的实现,WinRM允许远程用户使用工具和脚本对Windows服务器进行管理并获取数据。
winrs.exe:基于命令行的工具,此工具作为客户端使用,用于远程连接运行WinRM的服务器并执行大多数的cmd命令。
WinRM服务在Windows Vista和Windows Server 2008已经默认安装,但windows 7 和 windows server 2008需要手动启动,从Windows Server 2008 r2开始WinRM自动启动,监听5985端口(HTTP)和5986端口(HTTPS)。
在Windows中,除了WinRM本身,其他一些工具的实现借助了WinRM所提供的功能。例如:
PowerShell:PowerShell自2.0开始引入了Remoting技术,即远程执行PowerShell命令,此技术基于WinRM服务实现。
Ansible:基于Python的开源IT自动化平台,使用pywinrm库远程管理Windows服务器,基于WinRM服务。
#0x01 直接开启WinRM服务
这里以Win7为例介绍WinRM的启动方法。
1、在命令行中执行winrm quickconfig对WinRM进行首次(默认)配置
winrm quickconfig -q
此时,WinRM服务已经开始监听5985/TCP(WinRM2.0开始,WinRM服务的HTTP默认监听端口由原来的80/TCP变更为5985/TCP端口并等待远程主机进行访问,通过winrm enumerate winrm/config/listener查看WinRM服务当前的配置情况。
winrm enumerate winrm/config/listener
以此配置为例,此时远程主机已经可以通过WS-Management协议访问http://x.x.x.x/wsman连接当前服务器的WinRM服务。
2、添加白名单
WinRM只允许当前域用户或者处于本机TrustedHosts列表中的远程主机进行访问。因此在连接之前,还需要确保发起连接的主机与当前服务器处于同一域或者两台主机的WinRM服务TrustedHosts中必须存在对方主机的IP或主机名,这里类似于一个白名单机制。我们可以执行winrm set winrm/config/client @{TrustedHosts=”*“}手动配置当前服务器允许被任意主机连接。
winrm set winrm/config/client @{TrustedHosts="*"}
在本地Windows主机上也进行相同的设置,允许连接任意Windows主机。
3、配置访问策略
设置LocalAccountTokenFilterPolicy指为1,使本地管理员组中的非build-in管理员可以远程执行命令
配置LocalAccountTokenFilterPolicy
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /v LocalAccountTokenFilterPolicy /t REG_DWORD /d 1 /f
查看配置结果
reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /v LocalAccountTokenFilterPolicy
4、开启WinRM服务的脚本 把需要执行命令写个脚本,内容如下:
winrm quickconfig -q
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
winrm set winrm/config/service/auth '@{Basic="true"}'
winrm set winrm/config/Client @{TrustedHosts="*"}
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /v LocalAccountTokenFilterPolicy /t REG_DWORD /d 1 /f
各命令的作用如下
winrm quickconfig -q 主要执行的过程包括:启动winrm服务;设置winrm服务自启动;开启监听5985端口;添加防火墙策略,允许5985/5986
winrm set winrm/config/service '@{AllowUnencrypted="true"}' 允许流量不加密
winrm set winrm/config/service/auth '@{Basic="true"}' 允许明文密码认证
winrm set winrm/config/Client @{TrustedHosts="*"} 允许任意ip地址连接(WinRM只允许当前域用户或者处于本机TrustedHosts列表中的远程主机进行访问)
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /v LocalAccountTokenFilterPolicy /t REG_DWORD /d 1 /f 使本地管理员组中的非build-in管理员可以远程执行命令
#0x02 通过PSRemoting开启WinRm服务
Powershell的Remoting技术是通过WinRM服务实现的,通过开启PSRemoting服务可以自动开启WinRM服务,另外,可以直接使用PSRemoting获取交互式powershll。
在powershell中执行Enable-PSRemoting -Force即可开启WinRM服务。
Enable-PSRemoting -Force
Enable-PSRemoting该指令执行的过程包括
启动winrm服务;
设置winrm服务自启动;
开启监听5985端口;
添加防火墙策略,允许5985/5986;
设置LocalAccountTokenFilterPolicy指为1,使本地管理员组中的非build-in管理员可以远程执行命令
若目标机器的网络为public时,无法添加防火墙策略,需要修改网络为domain或private。win7或2008R2修改网络的方法为:其中0 - Public;1 - Private;2 - Domain
$networkListManager = [Activator]::CreateInstance([Type]::GetTypeFromCLSID([Guid]"{DCB00C01-570F-4A9B-8D69-199FDBA5723B}"))
$connections = $networkListManager.GetNetworkConnections()
# Set network location to Private for all networks
$connections | % {$_.GetNetwork().SetCategory(1)}
Windows 2012 and up系统在powershell中执行
Get-NetConnectionProfile | Set-NetConnectionProfile -NetworkCategory Private
PSRemoting技术与WinRM的关系如下
#0x03 远程连接WinRM服务
1、使用windows自带的winrs连接WinRM执行命令
使用winrs客户端连接这台Windows服务器即可直接执行系统命令,例如运行winrs -r:http://x.x.x.x:5985 -u:administrator -p:123456 ipconfig得到网络配置信息。
2、使用winrs连接WinRM服务获得cmd交互shell
winrs -r:x.x.x.x -u:administrator -p:123456 cmd
除了windows提供的winrs工具之外,也可以使用github上的相关工具,优点是可以运行在linux平台上,不需要本机开启winrm服务,也不需要设置本机winrm白名单。(其实没发现特别好的工具)
3、使用PowerShell远程连接主机
使用PowerShell执行Enter-PSSession -ComputerName x.x.x.x -Credential administrator即可连接至这台Windows服务器,在弹框中输入密码,获得这台Windows服务器的交互式PowerShell
4、使用evil-winrm工具来获取交互式powershell
./evil-winrm.rb -i 192.168.81.185 -u administrator -p 123456
./evil-winrm.rb -i 192.168.81.185 -u administrator@DomainName -p 123456
./evil-winrm.rb -i 192.168.81.185 -u administrator@DomainName -H ntlm-hash
#0x04 WinRM相关命令
WinRM状态查询
PS C:\Users\Administrator> Get-WmiObject -Class win32_service | Where-Object {$_.name -like "WinRM"}
WinRM服务开启
winrm quickconfig -q #这条命令运行后会自动添加防火墙例外规则,放行5985端口。
Enable-PSRemoting -Force
修改默认端口以实现端口复用,对于部署了WEB服务的目标来说是一个非常好的后门
winrm set winrm/config/Listener?Address=*+Transport=HTTP @{Port="80"}
winrm set winrm/config/service @{EnableCompatibilityHttpListener="true"}
查看WinRM配置
winrm get winrm/config
允许所有IP连接
winrm set winrm/config/Client @{TrustedHosts="*"}
winrm e winrm/config/listener #查看监听地址和端口
远程命令执行
C:\Users\Administrator>winrs -r:192.168.86.114 -u:192.168.86.114\administrator -p:123456 whoami
获取交互shell
C:\Users\Administrator>winrs -r:192.168.86.114 -u:192.168.86.114\administrator -p:123456 cmd
参考
- 内网横移之WinRM
-
[内网渗透 基于winRM的横向移动](https://www.se7ensec.cn/2020/08/26/%E5%86%85%E7%BD%91%E6%B8%97%E9%80%8F-%E5%9F%BA%E4%BA%8Ewinrm%E7%9A%84%E6%A8%AA%E5%90%91%E7%A7%BB%E5%8A%A8/) - Disable PowerShell remoting: Disable-PSRemoting, WinRM, listener, firewall, LocalAccountTokenFilterPolicy