1

I'm trying to run through a buffer overflow exercise, here is the code:

#include <stdio.h>

int badfunction() {
  char buffer[8];
  gets(buffer);
  puts(buffer);
}

int cantrun() {

  printf("This function cant run because it is never called");

}

int main() {

  badfunction();

}

This is a simple piece of code. The objective is to overflow the buffer in badfunction()and override the return address having it point to the memory address of the function cantrun().

Step 1: Find the offset of the return address (in this case it's 12bytes, 8 for the buffer and 4 for the base pointer).

Step 2: Find the memory location of cantrun(), gdb say it's 0x0804849a.

When I run the program printf "%012x\x9a\x84\x04\x08" | ./vuln, I get the error "illegal instruction". This suggests to me that I have correctly overwritten the EIP, but that the memory location of cantrun() is incorrect.

I am using Kali Linux, Kernel 3.14, I have ASLR turned off and I am using execstack to allow an executable stack. Am I doing something wrong?

UPDATE:

As a shot in the dark I tried to find the correct instruction by moving the address around and 0x0804849b does the trick. Why is this different than what GDB shows. When running GDB, 0x0804849a is the location of the prelude instruction push ebp and 0x0804849b is the prelude instruction mov ebp,esp.

a1ph4byte
  • 73
  • 5

1 Answers1

2

gdb doesn't do anything to change the locations of functions in the programs it executes. ASLR may matter, but by default gdb turns this off to enable simpler debugging.

It's hard to say why you are seeing the results you are. What does disassembling the function in gdb show?

Tom Tromey
  • 21,507
  • 2
  • 45
  • 63