python 使用WinRM连接远程windows

一、什么是WinRM服务

WinRM是很早之前微软推出的一种新式的方便远程管理的服务Windows Remote Management的简称,相比RDP远程桌面协议,WinRM这种服务更具备轻量、低宽带的特性,WinRM与WinRS(Windows Remote Shell)的使用可以让远程连接的运维人员拥有CMDShell环境,通过命令执行对服务器与服务器数据进行管理。

随着运维人员的任务量的增大,RDP的图形化界面让服务器管理更轻松,WinRM在日常的使用中逐渐被淡化,在开启该服务时,防火墙默认放行5985端口,所以在安全测试人员中,WinRM服务也成为常见的渗透利用点之一。

但在 Win7、Win10、Win11 这些单机系统默认存在但没有正常启用WinRM服务,仍需要在本地进行命令执行进行配置,而Server 2008/2008R2版本启用了WinRM服务,但服务配置不完整,仍需要对其进行快速配置才能正常使用。Server 2012及之后的版本才能直接正常使用。

二、服务器(被控端 windows)

1、检查是否开启

winrm e winrm/config/listener
 
# 成功开启
Listener
    Address = *
    Transport = HTTP
    Port = 5985
    Hostname
    Enabled = true
    URLPrefix = wsman
    CertificateThumbprint
    ListeningOn = 10.3.0.233, 127.0.0.1, ::1, fe80::5efe:10.3.0.233%13, fe80::b17d:a13d:4859:66cb%12

2、如果没有开启

以管理员权限打开 CMD 命令窗口,输入下面命令启动 winrm 服务
 

# 启动winrm服务
winrm quickconfig -q
winrm quickconfig

3、检查 winrm 服务监听状态

继续在命令行输入下面命令,查看 winrm 服务的状态

PS:注意这里的端口号 Port 值后面连接会用到

# 查看winrm服务的状态
winrm e winrm/config/listener

# 结果
Listener
    Address = *
    Transport = HTTP
    Port = 5985
    Hostname
    Enabled = true
    URLPrefix = wsman
    CertificateThumbprint
    ListeningOn = **

4、查看 winrm 配置信息(可选)

通过以下命令可以查看 winrm 全部配置信息、client 客户端配置信息、service 服务端配置信息

# 全部
winrm get winrm/config

# Client
winrm get winrm/config/client

# Service
winrm get winrm/config/service

5.配置 winrm client

# 配置winrm client
winrm set winrm/config/client @{AllowUnencrypted="true"}

winrm set winrm/config/client @{TrustedHosts="*"}

winrm set winrm/config/client/auth @{Basic="true"}

6. 配置 winrm service

在配置完 winrm service 和 winrm client 后,我们通过通过步骤 1-3 查看配置文件,确保配置文件已生效

# 配置winrm service
winrm set winrm/config/service @{AllowUnencrypted="true"}

winrm set winrm/config/service/auth @{Basic="true"}

三、客户端(控制端)

在控制端,比如:Mac OSX、Linux,我们只需要安装「 pywinrm 」依赖包即可

1、使用 winrs 连接

winrs -r:http://10.3.0.233:5985 -u:administrator -p:P@ssw0rd cmd
 
Microsoft Windows [版本 6.3.9600]
(c) 2013 Microsoft Corporation。保留所有权利。
 
# 连接成功
C:\Users\Administrator>

2、使用 Enter-PSSession 连接

Enter-PSSession -Computer 10.3.0.233 -Credential administrator
 
# 成功进入
[10.3.0.233]: PS C:\Users\Administrator\Documents>

输入密码后,成功进入

python 使用WinRM连接远程windows_第1张图片

3.使用python 使用winrm连接远程

1.要在Python中使用WinRM,需要安装pywinrm库。您可以使用pip命令进行安装:

# 控制端安装依赖包
pip install pywinrm

2.安装完成后,您可以使用以下代码来连接远程Windows机器并执行命令:

# 连接windows
import winrm


# ip地址:端口号
# winrm server端口号
# auth:用户名和密码
session = winrm.Session("192.168.164.133:5985", auth=('admin', 'love-520'), transport='ntlm')
res = session.run_cmd('dir C:\\Users')
# res = session.run_cmd('ipconfig')
print(res)
print(res.status_code)
print(res.std_out.decode('gbk'))

这样,我们就可以通过对象的「 run_cmd 」和「 run_ps 」函数模拟 CMD、PowerShell 输入命令了

3.案例1:

这里以查看 Windows 某个硬盘目录下的日志文件为例

# 连接windows
import winrm
import codecs
...
 def exec_cmd(self, cmd):
        """
        执行cmd命令,获取返回值
        :param cmd:
        :return:
        """
        # CMD
        result = self.session.run_cmd(cmd)
        # powerShell
        # result = self.session.run_ps(cmd)
        # 返回码
        # code为0代表调用成功
        code = result.status_code

        # 根据返回码,获取响应内容(bytes)
        content = result.std_out if code == 0 else result.std_err

        # 转为字符串(尝试通过UTF8、GBK进行解码)
        # result = content.decode("utf8")
        # result = codecs.decode(content,'UTF-8')
        try:
            result = content.decode("utf8")
        except:
            result = content.decode("GBK")

        print(result)
        return result
...
# 打开文件D:/py/log/trade.log
# windows使用type命令,查看文件内容
result = self.exec_cmd('D: &cd py\\log &type trade.log')

# 查看结果
print(result)

4.案例2:

import winrm

# 创建一个WinRM客户端实例
session = winrm.Session("192.168.164.133:5985", auth=('admin', 'love-520'), transport='ntlm')

# 下载解压
session.run_ps('Invoke-WebRequest -Uri "https://dl.360safe.com/360zip_setup.exe" -OutFile "c:/360zip_setup.exe"')
session.run_ps(f'Expand-Archive -Path "C:\\php.zip" -DestinationPath "C:\\"')


# 执行PowerShell命令
r = session.run_ps(ps_cmd)

# 输出结果
print(r.std_out.strip())

5. 总结

除了可以远程查看 Windows 的文件外,还可以执行 bat 批处理文件,又或者是模拟命令行输入,根据返回值进行其他骚操作

你可能感兴趣的:(Python,桌面运维,python,windows,开发语言)