WinRM
(Windows Remote Management) 是微软实现的一套远程管理协议,基于 WS-Management(WS-MAN) 标准,采用 SOAP/HTTP(S) 作为传输载体。WinRM
监听器发送 HTTP/HTTPS
的 SOAP
请求(Identify/Authenticate)。Creat
e 一个 shell/session
(持久化上下文),随后通过该 session Invoke
命令。shell
中执行命令,将标准输出/错误通过 SOAP
消息分片返回(可能会有流式或分块)。Delete shell
,释放资源。<host>
:测试目标主机 WinRM 服务是否可达并查看返回信息。<host>
-Credential <cred>
:交互式远程会话(像 SSH 的 shell)。<host>
-ScriptBlock { ... }:在远程主机执行命令或脚本(可在多主机上并行)。这些库一般封装了 WS-MAN 的 SOAP 细节,使用更方便。
认证方式 | 特点 | 安全性 | 使用场景 |
---|---|---|---|
Basic | 用户名和密码以明文 Base64 发送 | 低(必须配合 HTTPS,否则等于明文传输) | 简单快速,测试环境、自签名证书场景 |
Digest | 使用质询-响应机制,密码不会明文传输 | 中(比 Basic 安全,但已过时) | 很少使用,兼容性差 |
NTLM | Windows 内置的挑战/响应协议,不需要明文密码 | 中(比 Basic 强,但存在中继攻击风险) | 域外或工作组环境常用 |
Kerberos | 基于票据的认证(需 KDC/AD 域环境),无需发送密码 | 高(企业常用,强安全性) | 企业域环境,大规模管理 |
CredSSP | 支持凭据委派(客户端凭据转发到远端执行) | 高(可做二次跳转),但存在凭据泄露风险 | 需要跨跳远程执行时 |
Negotiate | Windows 自动选择 Kerberos 或 NTLM | 取决于底层选择 | 一般推荐(兼容性好) |
以 管理员身份 打开 PowerShell,执行:
bash# 1. 启用 WinRM 服务
winrm quickconfig -force
# 2. 启用 Basic 认证(方便跨平台调用,例如 Java、Python 客户端)
winrm set winrm/config/service/auth '@{Basic="true"}'
# 3. 允许未加密会话(仅测试用,生产环境建议配置 HTTPS)
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
# 4. 放行防火墙 5985 端口(HTTP)
netsh advfirewall firewall add rule name="WinRM HTTP" dir=in action=allow protocol=TCP localport=5985
xml<!-- https://mvnrepository.com/artifact/io.cloudsoft.windows/winrm4j -->
<dependency>
<groupId>io.cloudsoft.windows</groupId>
<artifactId>winrm4j</artifactId>
<version>0.12.3</version>
</dependency>
javaimport io.cloudsoft.winrm4j.winrm.WinRmTool;
import io.cloudsoft.winrm4j.winrm.WinRmToolResponse;
import org.apache.http.client.config.AuthSchemes;
import org.junit.jupiter.api.Test;
public class WinRMTest {
private String host = "xxx"; // 目标主机名或IP
private String user = "Administrator";
private String password = "xxx";
@Test
public void testWinRM() throws Exception {
WinRmTool tool = WinRmTool.Builder.builder(host, user, password)
.port(5985) // WinRM 默认端口: HTTP 5985 / HTTPS 5986
.useHttps(false).authenticationScheme(AuthSchemes.NTLM).disableCertificateChecks(true).build();
// 获取系统信息
String[] commands = { "wmic os get Caption,OSArchitecture",
"wmic cpu get Name,NumberOfCores,NumberOfLogicalProcessors",
"wmic memorychip get Capacity,Manufacturer" };
for (String cmd : commands) {
WinRmToolResponse response = tool.executeCommand(cmd);
System.out.println("命令: " + cmd);
System.out.println(response.getStdOut());
}
}
}
提示
实际使用中若涉及到中文需要注意中文乱码
本文作者:蒋固金
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!