Here's our example program that we'll debug.
When we run it it crashes and dumps core. The bug is obvious looking at the code. The string being copied into the buf array is much larger than it can hold.
The problem with memory overruns on the stack is that we can't rely of the frame pointers in our core dump as they are commonly trashed:
Looks hopeless doesn't it? While the above error is obvious when your looking at it. How do we find it when our core dump is screwed up. Well first let's find out where our program code is.
Our program code is found within the range of 0x8048320-0x804843c How did I know to look for those symbol names? Well you find that out using the objdump utility on your executable.
Why do we need to know that? Well we can look at the registers in our core and look at a few meaningful ones. We can also get a sense of which are valid knowing the range our program is loaded at and where the stack usually lives.
Let's see what's on the stack pointed to by ESP. Display the contents of ESP in strings.
Okay assuming we still didn't have clue.. lets display the stack in words
As we go up the stack we're hoping to find some data that looks valid. Bingo 0x08048320 looks like the last valid EIP of our program that is on the stack.
It's a shame as that happens to be _start which doesn't narrow things down. Had we had a much bigger program with many more functions nested a might deeper that might have been helpful. Merc muds don't nest too deeply anyways so depending on how bad your overflow was that might not help. Learn to use gdb, explore, play around and how the real code works under the covers of the high level language. I'm certain there are quicker approaches to solving this problem. |
Here's our example program that we'll debug.
When we run it it crashes and dumps core. The bug is obvious looking at the code. The string being copied into the buf array is much larger than it can hold.
The problem with memory overruns on the stack is that we can't rely on the frame pointers in our core dump as they are commonly trashed:
Looks hopeless doesn't it as gdb has lost it's way? While the above error is obvious when your looking right at it. How do we find the offensive source code in a large program when our core dump is screwed up. Well first let's find out where our program code is.
Our program code is found within the range of 0x8048320-0x804843c How did I know to look for those symbol names? Well you find that out using the objdump utility on your executable.
Why do we need to know that? Well we can look at the registers in our core and look at a few meaningful ones. We can also get a sense of which are valid knowing the range our program is loaded at and where the stack usually lives.
Let's see what's on the stack pointed to by ESP. Display the contents of ESP in strings.
Okay assuming we still didn't have clue.. lets display the stack in words
As we go up the stack we're hoping to find some data that looks valid. Bingo 0x08048320 looks like the last valid EIP of our program that is on the stack.
It's a shame as that happens to be _start which doesn't narrow things down. Had we had a much bigger program with many more functions nested a might deeper that might have been helpful. Merc muds don't nest too deeply anyways so depending on how bad your overflow was that might not help. Learn to use gdb, explore, play around and how the real code works under the covers of the high level language. I'm certain there are quicker approaches to solving this problem. |