Cross-Compilation Mastery with MinGW-w64 (32-bit & 64-bit)
Written By Aryan Giri
π οΈ Cross-Compilation Mastery with MinGW-w64 (32-bit & 64-bit)
Build Windows binaries from Linux like a pro βοΈ
π Introduction
In modern offensive security and development workflows, you donβt always build on the target OS. Instead, you cross-compile β creating binaries for one platform while working on another.
One of the most powerful tools for this is MinGW-w64, which allows you to compile Windows executables directly from Linux systems like Kali.
π§ What is MinGW-w64?
MinGW-w64 is a cross-compilation toolchain based on GCC.
π It allows you to:
- Compile
.exefiles on Linux - Target Windows APIs
- Build both 32-bit and 64-bit binaries
βοΈ Installation (Kali / Ubuntu / Debian)
sudo apt update
sudo apt install mingw-w64
Once installed, you get multiple compilers:
| Compiler | Target |
|---|---|
x86_64-w64-mingw32-gcc |
64-bit Windows |
i686-w64-mingw32-gcc |
32-bit Windows |
π§ͺ Basic Example (Hello World)
#include <stdio.h>
int main() {
printf("Hello from Windows binary!\n");
return 0;
}
β‘ Compile for 64-bit Windows
x86_64-w64-mingw32-gcc hello.c -o hello64.exe
β‘ Compile for 32-bit Windows
i686-w64-mingw32-gcc hello.c -o hello32.exe
π Verify the Output
file hello64.exe
Expected output:
PE32+ executable (console) x86-64
For 32-bit:
PE32 executable (console) Intel 80386
π§© Key Difference: 32-bit vs 64-bit
| Feature | 32-bit | 64-bit |
|---|---|---|
| Compatibility | Works on old systems | Modern systems |
| Performance | Lower | Higher |
| Memory | Limited (~4GB) | Much higher |
π£ Linking Windows Libraries
Example (networking):
x86_64-w64-mingw32-gcc program.c -o app.exe -lws2_32
Common libraries:
-lws2_32β Networking (Winsock)-lkernel32β Core Windows API-luser32β GUI functions
π§ Static vs Dynamic Builds
πΉ Dynamic (default)
- Smaller file
- Depends on system DLLs
πΉ Static build
x86_64-w64-mingw32-gcc hello.c -o hello.exe -static
- Larger file π¦
- More portable
- Less dependency issues
π΅οΈββοΈ Security Perspective
MinGW-w64 is heavily used in:
π΄ Red Teaming
- Build payloads for Windows from Linux
- Avoid touching Windows during dev
- Automate payload pipelines
π΅ Blue Teaming
- Analyze binaries compiled with MinGW
- Detect suspicious PE patterns
- Study cross-compiled executables
β οΈ 2026 Detection Reality
Modern systems (Defender, EDRs) can detect:
- Suspicious API usage
- Unusual binary patterns
- Behavior (not just signatures)
π Meaning:
Simply compiling with MinGW β stealth
π₯ Practical Use Cases
- Cross-platform development
- Malware analysis labs
- CTF challenges
- Building Windows tools from Linux
π§ͺ Mini Lab
Compile both versions:
i686-w64-mingw32-gcc hello.c -o hello32.exe x86_64-w64-mingw32-gcc hello.c -o hello64.exeRun them in a Windows VM
Observe compatibility differences
π§ Final Insight
MinGW-w64 gives you power over platform boundaries.
You are no longer limited by OS β you control the build chain βοΈ