How To: Convert Python Script to Exe

Convert Python Script to Exe

There are a lot of great tutorials on Null Byte using Python to create backdoors, reverse shells etc, so I thought it might be worthwhile making a tutorial on how to convert these python scripts to exe files for Windows victim machines.

This has a number of benefits:
1) The victim doesn't have to have python installed to be able to run your program

2) The victim cannot read the source code (though of course they could disassemble the exe that's probably not the type of person you're targeting ;) )

3) The exe will bundle third party python modules when it's compiled, meaning they don't have to be setup on the victim's system

Step 1: Download and Install Py2Exe for Windows

http://sourceforge.net/projects/py2exe/files/py2exe/0.6.9/Py2Exe
Unfortunately it's only available for windows :(

Step 2: Create the Python Program You Want to Compile

I called mine myScript.py

Step 3: Create setup.py

This is the python script that py2exe uses to create your exe. I think it's fairly self-explanatory.
Make sure you save it in the same directory as the file you want to compile.

Note, if you are using any third party python modules, you must specify them in this file.
For example, if myScript.py made use of the pandas module, I would specify it here like so:

Step 4: Compile!

Now go to your command prompt and compile your program, by running the following:

python setup.py py2exe

Step 5: Test Out Your Exe

Py2Exe will have created two folders: build and dist.
The dist folder contains your exe, and it's safe to delete the build folder.

Step 6: Happy Compiling!

Not a very exciting tutorial, but one that you'll hopefully find useful :)

Just updated your iPhone? You'll find new features for TV, Messages, News, and Shortcuts, as well as important bug fixes and security patches. Find out what's new and changed on your iPhone with the iOS 17.6 update.

Image credits: interskill.com.au

15 Comments

Kudos +1
Very useful concept!

I tried to do the same.
I get an import error while running from cmd.
But when I run the same script in idle . It shows no error and no output is displayed.

Hmm, that's odd. Do you have multiple versions of python installed on your computer?

y do you have to input ur bank details

I am using a package selenium in my python code and created a setup file as mentioned.
from distutils.core import setup
import py2exe
setup(
console = '<MY File Name>.py',
options = {
'py2exe' : {
'packages' : 'selenium'
}}).

I got an error as below.

File "C:\Users\1014983\AppData\Local\Programs\Python\Python36\lib\site-packages\py2exe\mf3.py", line 417, in scanopcodes

yield "store", (namesoparg,)
IndexError: tuple index out of range

i am also facing this error.
IndexError:tuple index out of range!
help please if any solution

me toooo

"tuple index out of range"

?????? what i doooo??????

I want to use standalone .exe in different directory , not in a dist folder. what should i do?

The online convertor works fine and easy. Thanks.

Hi, I've just read this tutorial and I'm wondering (once the python program has been converted to .exe) what if I modify some of my scripts and / or add a new script, is there a way to "compile" or "update" the .exe?

I've developed a tool that was distributed to some clients and I don't know if I can just "update" that tool with my new modules.

Thanks in advance

Is there a version which works for Python 3 ? Specifically 3.6.4 ? Thanks

it converts to exe but the meterpreter doesnt do anything. which means the exe file doesnt even start(or it does nothing)

Share Your Thoughts

  • Hot
  • Latest