Building Python for Windows with Wine

03 Aug 2022 - GRX6

Wine

Wine is a compatibility layer to run Windows applications in POSIX systems like Linux.

One great example is the possibility to create .exe files using Python.

Please note that in the recent versions of Windows such as Windows 11, Microsoft is forcing you to sign the .exe files in order to be able to run them.

Install wine in your machine

It can be installed with your package manager.

sudo apt-get install wine

Note that it’s necessary to have x86 arch also enabled, so maybe you have to run this:

sudo dpkg --add-architecture i386 && sudo apt-get update && sudo apt-get install wine32

Install Python

To install Python you must download the version that you want to run. In this case we are using 2.7

Download-python-msi

sudo wine msiexec /i /home/kali/Downloads/python-2.7.18.msi

It will be installed in /root/.wine/drive_c/Python27

Install Pip dependencies

Once it is installed, you would see that you have the /root/.wine/ folder. Let’s install pip.

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
sudo wine /root/.wine/drive_c/Python27/python.exe get-pip.py
sudo wine /root/.wine/drive_c/Python27/python.exe -m pip help

To install dependencies and use in the executable,

sudo wine /root/.wine/drive_c/Python27/python.exe -m pip install <dependency>

Build the executable

To compile the executable

sudo wine /root/.wine/drive_c/Python27/Scripts/pyinstaller.exe --onefile --noconsole <python_file.py>

Python 2.7 reached end of its life on January 1st, 2020. Please upgrade your Python!!

Obfuscate and Windows Defender

In the recent versions of Windows your .exe file will be identified by Windows Defender. So long I wasn’t able to avoid this using alternative packages to pyinstaller or using obfuscation. Let me know if there is some workaround to signing the files!