Windows远程执行进程工具psexec和wmiexec介绍

在自动化测试或者自动化工具开发中,通常需要向其它电脑或者服务器发送指令,比如Windows发送命令到Linux服务器开启某个服务进程,或者读取状态信息,我们可以使用ssh协议实现。

如果Windows主机需要发送命令到局域网内的其它Windows电脑要如何实现呢?在网络安全中称为横向移动的内网渗透方法可实现远程执行命令,横向移动工具有很多,本文介绍psexec和wmiexec这两个工具如何实现远程执行进程。

PsExec

PsExec是 Windows 资源工具包PsTools中提供的一个远程执行进程的工具,使用服务控制管理器(SCM)远程启动一个服务,并通过服务进程进行远程管理。可用于在其他windows系统上执行进程,并提供交互式控制。

我使用PsExec没有成功启动远程电脑进程,推荐使用后面介绍的psexec.py脚本。

PsTools下载地址:https://learn.microsoft.com/zh-cn/sysinternals/downloads/psexec

在这里插入图片描述

使用前需要先退出目标电脑的杀毒软件,并关闭防火墙。

命令格式:

$ psexec \\hostIp -u [username] -p [password] cmd

psexec.py

psexec.py是 Python impacket 包提供的远程执行脚本。

安装命令:

$ python3 -m pip install impacket

安装完成后,可在python安装路径下的Scripts目录中找到psexec.py脚本。

命令格式:

Python psexec.py <username>:"<password>"@<IP address> cmd

示例:

1、进⼊半交互式shell

python psexec.py Administrator:admin@192.168.0.93
Impacket v0.10.0 - Copyright 2022 SecureAuth Corporation

[*] Requesting shares on 192.168.0.93.....
[*] Found writable share ADMIN$
[*] Uploading file BIlvtqil.exe
[*] Opening SVCManager on 192.168.0.93.....
[*] Creating service BVDe on 192.168.0.93.....
[*] Starting service BVDe.....
[!] Press help for extra shell commands
[-] Decoding error detected, consider running chcp.com at the target,
map the result with https://docs.python.org/3/library/codecs.html#standard-encodings
and then execute smbexec.py again with -codec and the corresponding codec
Microsoft Windows [�汾 6.1.7601]

[-] Decoding error detected, consider running chcp.com at the target,
map the result with https://docs.python.org/3/library/codecs.html#standard-encodings
and then execute smbexec.py again with -codec and the corresponding codec
��Ȩ���� (c) 2009 Microsoft Corporation����������Ȩ����


C:\Windows\system32>

2、执行脚本

编写一个Python脚本demo.py:

print("hello world")

放到远程电脑的D盘根目录下,然后在本地电脑打开cmd窗口执行:python psexec.py Administrator:admin@192.168.0.93 "python D:/demo.py"

python psexec.py Administrator:admin@192.168.0.93 "python D:/demo.py"
Impacket v0.10.0 - Copyright 2022 SecureAuth Corporation

[*] Requesting shares on 192.168.0.93.....
[*] Found writable share ADMIN$
[*] Uploading file pnZHQfBI.exe
[*] Opening SVCManager on 192.168.0.93.....
[*] Creating service SFff on 192.168.0.93.....
[*] Starting service SFff.....
[!] Press help for extra shell commands
hello world
[*] Process python D:/demo.py finished with ErrorCode: 0, ReturnCode: 0
[*] Opening SVCManager on 192.168.0.93.....
[*] Stopping service SFff.....
[*] Removing service SFff.....
[*] Removing file pnZHQfBI.exe.....
[-] Error performing the uninstallation, cleaning up

其它示例:

# 获取远程电脑主机名
python psexec.py Administrator:admin@192.168.0.93 "hostname" 
# 启动远程电脑的IxiaEndpoint服务
python psexec.py Administrator:admin@192.168.0.93 "net start IxiaEndpoint"

3、使用Pythonsubprocess库来读取进程返回结果

import subprocess

command = 'python D:/attrobot3/Scripts/psexec.py Administrator:admin@192.168.0.93 "python D:/demo.py"'
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE).stdout
print(p.read().decode('utf-8'))

wmiexec.py

Wmiexec也是一种横向移动工具,它利用了Windows Management Instrumentation (WMI) 接口来远程管理Windows系统。WMI是一个Windows操作系统提供的系统管理工具,它提供了一套API接口,可以对本地和远程Windows系统进行管理。如查看进程列表、执行命令、上传和下载文件等。

在使用Wmiexec之前,需要先在目标Windows系统上启用WMI服务,并且确保防火墙不会阻止WMI的通信。

wmiexec.py也来自于impacket工具包,支持的参数可执行 python wmiexec.py 命令查看,下面介绍几种用法。

1、进⼊半交互式shell

$ python wmiexec.py Administrator:admin@192.168.0.93
Impacket v0.10.0 - Copyright 2022 SecureAuth Corporation

[*] SMBv2.1 dialect used
[!] Launching semi-interactive shell - Careful what you execute
[!] Press help for extra shell commands
C:\>

2、执行脚本

$ python wmiexec.py Administrator:admin@192.168.168.93 "python D:/demo.py"
Impacket v0.10.0 - Copyright 2022 SecureAuth Corporation

[*] SMBv2.1 dialect used
hello world

Psexec和Wmiexec之间的差异

以下内容采用ChatGPT生成:

Wmiexec和Psexec是用于在Windows系统上执行命令和脚本的工具,它们各自的特点如下:

Wmiexec的特点:

  • 使用WMI接口远程连接到Windows系统,可以执行更高级别的管理操作,如注册表操作和事件订阅等。
  • 可以编写自定义WMI脚本来扩展其功能。
  • 使用WMI接口时,对防火墙没有太大的依赖。

Psexec的特点:

  • 使用服务控制管理器(SCM)远程启动一个服务,并通过服务进程进行远程管理。
  • 支持Windows NT及以上版本的操作系统。
  • 执行速度较快,比Wmiexec更快。
  • 可以隐藏服务进程的图标和窗口。

总的来说,Wmiexec适用于基于WMI的管理和远程控制,可以进行更高级别的操作,但执行速度较慢。Psexec则适合用于执行命令和脚本等任务,执行速度快,但不能进行高级别的操作。选择哪种工具取决于具体的使用场景和需求。

参考文档

  1. impacket包:https://github.com/fortra/impacket
  2. PsTools下载地址:https://learn.microsoft.com/zh-cn/sysinternals/downloads/psexec
  3. PowerShell远程: https://www.anyviewer.com/how-to/remotely-run-programs-on-another-computer-2578.html
--THE END--
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
包含以下工具 accesschk.exe accesschk64.exe AccessEnum.exe AdExplorer.chm ADExplorer.exe ADInsight.chm ADInsight.exe adrestore.exe Autologon.exe autoruns.chm Autoruns.exe Autoruns64.exe autorunsc.exe autorunsc64.exe Bginfo.exe Cacheset.exe Clockres.exe Clockres64.exe Contig.exe Contig64.exe Coreinfo.exe ctrl2cap.amd.sys ctrl2cap.exe ctrl2cap.nt4.sys ctrl2cap.nt5.sys dbgview.chm Dbgview.exe Desktops.exe Disk2vhd.chm disk2vhd.exe diskext.exe diskext64.exe Diskmon.exe DISKMON.HLP DiskView.exe DMON.SYS du.exe du64.exe efsdump.exe Eula.txt FindLinks.exe FindLinks64.exe handle.exe handle64.exe hex2dec.exe hex2dec64.exe junction.exe junction64.exe ldmdump.exe Listdlls.exe Listdlls64.exe livekd.exe LoadOrd.exe LoadOrd64.exe LoadOrdC.exe LoadOrdC64.exe logonsessions.exe logonsessions64.exe movefile.exe movefile64.exe notmyfault.exe notmyfault64.exe notmyfaultc.exe notmyfaultc64.exe ntfsinfo.exe ntfsinfo64.exe pagedfrg.exe pagedfrg.hlp pendmoves.exe pendmoves64.exe pipelist.exe pipelist64.exe PORTMON.CNT portmon.exe PORTMON.HLP procdump.exe procdump64.exe procexp.chm procexp.exe procmon.chm Procmon.exe PsExec.exe PsExec64.exe psfile.exe psfile64.exe PsGetsid.exe PsGetsid64.exe PsInfo.exe PsInfo64.exe pskill.exe pskill64.exe pslist.exe pslist64.exe PsLoggedon.exe PsLoggedon64.exe psloglist.exe pspasswd.exe pspasswd64.exe psping.exe psping64.exe PsService.exe PsService64.exe psshutdown.exe pssuspend.exe pssuspend64.exe Pstools.chm psversion.txt RAMMap.exe readme.txt RegDelNull.exe RegDelNull64.exe regjump.exe RootkitRevealer.chm RootkitRevealer.exe ru.exe ru64.exe sdelete.exe sdelete64.exe ShareEnum.exe ShellRunas.exe sigcheck.exe sigcheck64.exe streams.exe streams64.exe strings.exe strings64.exe sync.exe sync64.exe Sysmon.exe Sysmon64.exe Tcpvcon.exe tcpview.chm Tcpview.exe TCPVIEW.HLP Testlimit.exe Testlimit64.exe Vmmap.chm vmmap.exe Volumeid.exe Volumeid64.exe whois.exe whois64.exe Winobj.exe WINOBJ.HLP ZoomIt.exe
运行system权限工具psexec 用法:psexec [\\computer[,computer2[,...] | @file][-u user [-p psswd]][-n s][-l][-s|-e][-x][-i [session]][-c [-f|-v]][-w directory][-d][-<priority>][-a n,n,...] cmd [arguments] computer 指示 PsExec 在指定的一台或多台计算机上运行应用程序。如果省略计算机名称,则 PsExec 将在本地系统上运行应用程序;如果输入计算机名称“\\*”,则 PsExec 将在当前域中的所有计算机上运行应用程序。 @file 指示 PsExec 在指定的文本文件中列出的每台计算机上运行命令。 -a 用逗号分隔可以运行应用程序的处理器,CPU 编号最小为 1。例如,要在 CPU 2 和 CPU 4 上运行应用程序,请输入:“-a 2,4” -c 将指定的程序复制到远程系统以便执行。如果省略此选项,则应用程序必须位于远程系统上的系统路径中。 -d 不等待应用程序终止。请只对非交互式应用程序使用此选项。 -e 不加载指定帐户的配置文件。 -f 将指定的程序复制到远程系统,即使远程系统中已存在该文件。 -i 运行程序,以便它与远程系统中指定会话的桌面进行交互。如果未指定会话,则进程将在控制台会话中运行。 -l 以受限用户身份(去除 Administrators 组的权限,并且只允许使用分配给 Users 组的权限)运行进程。在 Windows Vista 上,此进程将以“低完整性”运行。 -n 指定与远程计算机连接的超时(秒)。 -p 指定用户名的密码(可选)。如果省略此选项,系统将提示您输入隐藏密码。 -s 在系统帐户中运行远程进程。 -u 指定用于登录远程计算机的可选用户名。 -v 仅在指定文件具有更高版本号或该文件比远程系统上的文件新时复制该文件。 -w 设置进程的工作目录(相对于远程计算机)。 -x 在 Winlogon 桌面上显示 UI(仅限于本地系统)。 -priority 指定 –low、-belownormal、-abovenormal、-high 或 -realtime 按不同优先级运行进程。 program 要执行的程序的名称。 arguments 要传递的参数(请注意,文件路径必须是目标系统中的绝对路径) 对于其名称中含有空格的应用程序,可以在其两侧加引号,例如,psexec \\marklap "c:\long name\app.exe"。按下 Enter 键时,仅将输入内容传递到远程系统。键入 Ctrl-C 可终止远程进程。 如果省略用户名,则远程进程将以执行 PsExec 时所使用的相同帐户运行,但由于远程进程以模仿方式运行,因此它无权访问远程系统上的网络资源。指定用户名时,远程进程将以指定的帐户执行,并可访问该帐户有权访问的任何网络资源。请注意,密码是以明文形式传递到远程系统的。 当目标系统是本地系统时,由于 PsExec 不需要您具有管理员权限,因此您可以使用当前版本的 PsExec 来取代 Runas。 以下命令可在 \\marklap 上启动交互式命令提示窗口: psexec \\marklap cmd 此命令通过 /all 开关在远程系统上执行 IpConfig,并在本地显示输出结果: psexec \\marklap ipconfig /all 此命令将程序 test.exe 复制到远程系统,并以交互方式执行此程序: psexec \\marklap -c test.exe 如果远程系统中已经安装的程序不在系统路径中,请指定该程序的完整路径: psexec \\marklap c:\bin\test.exe 在系统帐户中以交互方式运行 Regedit,以便查看 SAM 和 SECURITY 注册表项的内容: psexec -i -d -s c:\windows\regedit.exe 要以受限用户权限运行 Internet Explorer,请使用此命令: psexec -l -d "c:\program files\internet explorer\iexplore.exe"

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值