h3xduck@blog:~#

Stack-based Buffer Overflow - Part 1

by h3xduck

23 May 2021

There exists lots of tutorials online about the all-time classic buffer overflow, but on this post I am trying to offer a new perspective - more visual and providing all the background needed for the understanding of the technique.

Review on memory architecture

Before going on with the technique, it is important to understand how memory works. Feel free to skip this part if you already know about it.

Memory works as a finite list of entries (called words) which can be accessed (either for reading or writing) via a memory address. We will be using a Intel x86 architecture, so the memory addresses are of the form 0xAAAAAAAA, where the “0x” means it is an hexadecimal number. Memory addresses go from 0x00000000 up to 0xFFFFFFFF.

Memory is partitioned so that elements are grouped into sections belonging to a common component:

Apart from this, we should also highlight the most important registers on IA-32:

Apart from those, there also exist other registers such as EAX, EBX, ECX… which are general purpose registers.

The Stack

Having seen how memory works on Intel x86, let’s now study what we are most interested in: the stack. In order to exploit something we always need to deeply understand how it works.

The figure above shows how the stack grows when calling a subroutine. For example, in a program invoking a function f1(int a,int b,int c); we would have the following events (mapped to the figures shown in the scheme above):

In Part 2 of this post we will apply all the knowledge we have got about memory architecture and IA-32, and see how it can be used for understading how a buffer overflow works and how it can be taken advantage by an attacker.

See you there!

h3xduck

tags: stack - overflow - buffer - exploitation - shellcode - assembly - IA-32 - Intelx86 - memory - registers - code - execution

Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.