Fotis’ blog

Blooooooog

Playing with the return address

Posted by Fotis on June 7, 2006

Try this

#include <stdio.h>
#include <stdlib.h>

void lala(void)
{
printf(”Hello world!\n”);
exit(0);
}

int main(void)
{
asm(”movl %%eax, 4(%%ebp)” : : “a” (lala));
}

Isn’t it nice?
What we’re doing here is very simple, just overwritting the return address from main() and changing it to the address of lala(). main() then returns to lala() which calls printf. Nice, isn’t it? Btw, this only works for the x86 platform!

4 Responses to “Playing with the return address”

  1. pthreat Says:

    IIRC this is the mechanism, buffer overflow exploits are base on. Nice piece of code! Btw, I didnt know this type of asm syntax :)

  2. Fotis Says:

    Yes, most buffer overflow exploits work like this. But there are other ways to exploit a bug like this, e.g. overwritting a function pointer with the address of another “interesting” function like “system”, or overwritting a pointer to the location of the argument of a function like “system” again :)

  3. Aggelos Orfanakos Says:

    What does asm() do? I can see the first part is:

    movl %eax, 4(%ebp)

    but what are the : : etc after that? Would you mind explaining what goes under the hood?

  4. Maximus Says:

    I would like to see a continuation of the topic

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>