I am trying to perform a very simple buffer overflow attack. I am pretty new to this. So if this question is stupid, excuse me :-)
The code:
#include<stdio.h> #include<stdlib.h> int i, n; void confused(int i) { printf("**Who called me? Why am I here?? *** %x\n ", i); } void shell_call(char *c) { printf(" ***Now calling \"%s\" shell command *** \n", c); system(c); } void victim_func() { int a[4]; printf("Enter n: "); scanf("%d",&n); printf("~~~~~~~~~~~~~ values and address of n locations ~~~~~~~~~~"); for (i = 0;i <n ;i++) printf ("\na[%d] = %x, address = %x", i, a[i], &a[i]); printf("\nEnter %d HEX Values \n", n); // Buffer Overflow vulnerability HERE! for (i=0;i<n;i++) scanf("%x",&a[i]); printf("Done reading junk numbers\n"); } int main() { victim_func(); printf("\n done"); return 0; }
When I use objdump to get function addresses, I have the following:
main(): 0x804854d Address of main() where printf() is called: 0x8048563 victim_func(): 0x8048455 confused(): 0x8048414
Now I want to go to the function 'confused ()' from victim_func (), overflowing the buffer there, and rewriting the return address to confused (). And I want to return from confused () to printf () to main and exit normally. So, I provide the following input
Enter n: 7 Enter 7 HEX values: 1 2 3 4 5 8048414 (This is to jump to confused) 8048563 (this is to jump to printf() in main)
Although the program prints "Done" from this printf statement, it jumps back to victim_func () and prints "Enter n:"
What am I doing wrong? Any help would be greatly appreciated!
PS: I am not sure whether the question was posed correctly. Please let me know if you need more information.
c objdump buffer-overflow
Ashwin Sep 08 2018-11-11T00: 00Z
source share