Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent strcpy() being optimised

Tags:

c

gcc

assembly

I'm following an example in the book Hacking: The Art of Exploitation and I am getting different results to the book.

It seems that the strcpy() function in the example compiles to an instruction like:

0x802384c5 <main+27>: call 0x80482C4 <strcpy@plt>

whereas when I compile the same program it removes the call to the library and replaces it with a series of mov instructions:

0x8048475 <main+25>: mov    DWORD PTR [eax],0x6c6c6548
0x804847b <main+31>: mov    DWORD PTR [eax+0x4],0x6f57206f
0x8048482 <main+38>: mov    DWORD PTR [eax+0x8],0x21646c72
0x8048489 <main+45>: mov    WORD PTR [eax+0xc],0xa

I understand that the compiler can make various optimizations, but even though it's the default I've even tried compiling it with -O0 which is supposed to prevent optimisations.

How can I compile the code so it references the external library?

I've not done any C or assembly since uni, so be gentle with me :)

like image 743
starskythehutch Avatar asked Feb 18 '23 17:02

starskythehutch


1 Answers

With GCC, you can use the -fno-builtin (disable all builtins) or -fno-builtin-strcpy (just disable builtin strcpy).

like image 193
R.. GitHub STOP HELPING ICE Avatar answered Feb 21 '23 06:02

R.. GitHub STOP HELPING ICE