PDA

View Full Version : Shellcode in C


dontKnowYet
03-19-2009, 01:47 AM
Hi,
I'd like to generate a position independent flat binary file using c, not asm. I got some basic dummy code working but when adding more than one function gcc (which I could replace with something else btw.) needs a _GLOBAL_OFFSET_TABLE_. Is there a way to avoid this?

I only found examples that were not position independent but linked to a fixed address online like bootsectors/booloaders in c.

Any ideas?

Thanks for your help!

dELTA
03-19-2009, 11:42 AM
Make all functions inline? Will make the code very large if there are many function calls though...

tHE mUTABLE
03-20-2009, 05:19 PM
Try to compile with either one of these two options -fPIC, -fpic, after all it depends on the architecture; generally, the processors do not supports Program Counter -relative loads and stores, and that's why the compiler uses GOT technique.

dontKnowYet
03-22-2009, 07:06 AM
Thanks. I finally got some working code. The following article helped me a lot: http://kos.enix.org/pub/plainbin.pdf.gz I wasn't using objcopy, but now it works.

Code:

asm("jmp _start"; // needed if when more than one function is there
// because i don't know how to specify function order

void _start(){
while(1){
}
}


The binary is build using

Code:

gcc -c -Wall -fpic -Os Shellcode.c -o Shellcode.o
ld -N -Ttext 0x0 -e _start -Map Shellcode.map Shellcode.o -o Shellcode
objcopy -R .note -R .comment -S -O binary Shellcode Shellcode.bin