【发布时间】:2015-12-19 11:15:01
【问题描述】:
我正在编写一个旨在杀死 explorer.exe 的脚本。我对其进行了一些搜索,我看到的最佳答案使用taskkill 命令。我试过了,但是当我在我的电脑上运行它时,它说它可以工作,但实际上并没有杀死它。
import os, socket
s = socket.socket()
host = socket.gethostname()
try:
s.bind((host, 75))
except socket.error:
print 'Error: Premission denied, please run as admin'
exit()
s.listen(5)
while True:
print '[*] Deploying Server on: ' + host
print '[*] Scanning..'
c, addr = s.accept()
print '[*] Connection established from ' + str(addr)
while True:
try:
os.system("taskkill /im explorer.exe")
cmd = raw_input()
if cmd == 'exit':
print '[!] Exiting'
c.send('exit')
s.close()
exit()
c.send(cmd)
except KeyboardInterrupt:
print '[!] Exiting'
c.send('exit')
s.close()
exit()
有效载荷:
import os
import socket
import platform
print 'Starting'
system = platform.system()
windows = ['Microsoft', 'Windows']
s = socket.socket()
host = socket.gethostname()
print host
print platform.system()
try:
s.connect((host, 75))
except socket.error:
s.close()
s.connect((host, 75))
while True:
cmd = s.recv(1024)
if cmd == 'exit':
s.close()
exit()
os.system("taskkill /im explorer.exe")
print(os.system("taskkill /im explorer.exe"))
【问题讨论】:
-
你确定它根本没有被杀死吗?我并不总是杀死 explorer.exe,但当我这样做时,我更喜欢它的默认行为,即立即自行重启。
-
好吧,我看到它还在我的电脑上运行,我怎么知道天气是不是马上重启了?
-
taskkill 尝试连接到进程的窗口站和桌面,并将
WM_CLOSE消息发布到主窗口(对于控制台应用程序,它是 conhost.exe 窗口,但前提是只有一个进程连接到控制台)。如果它无法发布消息,它会失败(退出代码为 1)并说您需要使用 /F(强制),这将调用TerminateProcess。仅仅因为它成功地发布了WM_CLOSE消息,并不意味着程序实际上会关闭。探索者没有重生;它只是忽略窗口消息。您可以使用 /F,但您必须手动重新启动资源管理器。 -
如果使用 /F,您需要将目标范围缩小到活动的“控制台”会话(不要与控制台窗口混淆)。否则,它可能会杀死所有已登录用户的 explorer.exe 的所有实例。您可以使用
csid = ctypes.windll.kernel32.WTSGetActiveConsoleSessionId()获取控制台会话号。然后将其添加为过滤器,例如os.system('taskkill /f /im explorer.exe /fi "session eq %d"' % csid). -
另外,您应该使用
subprocess.call而不是os.system。没有理由使用 cmd.exe shell 运行它。加上子进程,您可以通过creationflags=DETACHED_PROCESS(8) 来运行 taskkill.exe 而无需控制台窗口。这样,如果它是一个 GUI 应用程序,控制台窗口就不会在桌面上短暂闪烁。
标签: python windows python-2.7 taskkill