如何在Kali中为低版本内核、i386架构的CentOS编译C代码?
Got it, let's walk through how to compile your C code on Kali x86_64 so it runs smoothly on that older i386 CentOS target without GCC. The key challenges here are cross-architecture compatibility (64-bit to 32-bit) and older kernel/glibc support—here's what you can do:
1. Install the i386 Cross-Compilation Toolchain
First, you need tools that can compile 32-bit x86 code on your 64-bit Kali system. Run these commands to set up the necessary packages:
sudo apt update sudo apt install gcc-multilib gcc-i686-linux-gnu
gcc-multilibenables your system to handle multiple architecture targetsgcc-i686-linux-gnuis the cross-compiler specifically for 32-bit Intel x86 systems (perfect for your i386 target)
2. Fix Older Kernel/GLIBC Compatibility
The biggest gotcha here is that Kali's default glibc is newer than what your old CentOS kernel expects. Directly compiled dynamic binaries will fail to run because of missing syscalls or incompatible library versions. Here are your best options:
Option A: Static Compilation (Simplest Approach)
Static compilation embeds all required libraries into the executable, so it doesn't rely on the target system's glibc at all. Use this command to compile your code:
i686-linux-gnu-gcc -static your_exploit.c -o exploit_i386
- Pros: No dependency issues, works on almost any i386 system
- Cons: The executable will be larger in size, and very rarely some exploits might have issues with static linking (but this is rare for most pen-test tools)
Option B: Use a Container to Match the Target Environment (Most Reliable)
If static compilation doesn't work (or you need a dynamic binary), spin up an old i386 CentOS container to compile directly in a matching environment. Here's how:
- Install Docker on Kali if you don't have it:
sudo apt install docker.io sudo systemctl start docker - Pull an old i386 CentOS image matching your target's kernel version (e.g., CentOS 6 for kernel 2.6.x):
sudo docker pull i386/centos:6 - Run the container, mounting your local code directory so you can access your C files:
sudo docker run -it --rm -v /path/to/your/local/code:/code i386/centos:6 - Inside the container, install GCC and compile your code:
yum install gcc -y cd /code gcc your_exploit.c -o exploit_i386 - Exit the container—your compiled binary will be in your local
/path/to/your/local/codedirectory, ready to transfer to the target.
Option C: Specify Kernel/GLIBC Flags (Advanced)
If you know the exact kernel and glibc version of your target, you can pass flags to the cross-compiler to target those versions. For example, if your target uses kernel 2.6.32 and glibc 2.12:
i686-linux-gnu-gcc -march=i686 -mtune=generic \ -Wl,--hash-style=gnu \ -Wl,--dynamic-linker=/lib/ld-linux.so.2 \ your_exploit.c -o exploit_i386
Note: This requires precise knowledge of the target's setup and is less reliable than the first two options.
3. Verify the Compiled Binary
Before transferring, confirm the binary is built for i386:
file exploit_i386
You should see output like:
ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, BuildID[sha1]=..., not stripped
If you want to test it locally (without the target), use qemu-i386:
qemu-i386 exploit_i386
4. Transfer and Execute on the Target
Use your preferred method (nc, scp, etc.) to send exploit_i386 to the CentOS machine. Then make it executable and run:
chmod +x exploit_i386 ./exploit_i386
内容的提问来源于stack exchange,提问作者frkntrn

