Understanding Buffer Overflow Vulnerabilities in C Programming Part 2

Continuing from the example in the last post, let's try to understand how we can implement this and achieve privilege escalation.

You sit by yourself and try to guess the password in order to get into the debug mode. So you try a few shitty passwords, you try entering "password." Well, that's not working. Then maybe "youwontguessthisone." But you're also denied. You start thinking about what you can do, and then you remember reading the last post about buffer overflow. You decide to use this information to bypass the password check and go straight into the debug mode.

We will start by compiling the main.c file using the following command with gcc:

No alt text provided for this image

Notice a few things. First, we are using a couple of flags to help us remove protections that gcc has. Nevertheless, even though gcc has protections, it doesn't mean that our code is not vulnerable! We can see the warning after running gcc, which tells us that "the 'gets' function is dangerous and should not be used."

After this, we can run the code and attempt to overflow the buffer to trigger a segmentation fault:

No alt text provided for this image

Now let's check where the program crashed by using the following command:

No alt text provided for this image

The command sudo dmesg | tail -n 2 shows the last two lines of the kernel message buffer. It's useful for quickly checking recent system messages. The sudo part gives you administrative privileges, dmesg displays the kernel messages, and tail -n 2 filters and shows only the last two lines of that output.

We can see that we got a segfault at the address 41414141. To convert this to bytes, we can open Python3 and run the command:

No alt text provided for this image

We get A*4, so let's take 20 characters (which is what we defined in the buffer) and after this, we put as the password a quad-set of different characters to trigger a new segfault and repeat the whole process we just did.

No alt text provided for this image

We got quad F, meaning that 'AAAAAAAAAAAAAAAAAAAABBBBCCCCEEEE' comes after the last 'E', which is the place in the stack where we need to override and return to our debug function.

Let's find the address of the debug function.

The address is determined at compile time, so we need to inspect the compiled binary using the following command in the terminal:

objdump -d ./prog | less

After running this command, type "/debug" and you will see the name of the function along with the address on the left:

No alt text provided for this image

Next, let's create a Python file named exploit_main and write the following code inside:

No alt text provided for this image
payload = b"AAAAAAAAAAAAAAAAAAAABBBBCCCCEEEE": This line initializes a byte string variable named payload. It starts with a sequence of 20 'A' characters, followed by 'BBB', 'CCC', and 'EEEE'. payload += b"\x08\x04\x91\xa6"[::-1]: This line appends a little-endian encoded address to the payload byte string. The specific address "\x08\x04\x91\xa6" is represented in hexadecimal notation and is reversed using the [::-1] slice notation.

Now all we have to do is write this command in the terminal and press enter twice:

(python3 exploit_main.py; cat) | ./prog

No alt text provided for this image

Well, our program has been hacked quite severely.

Yoni Ankri

Deep Learning & Computer Vision Embedded Software Developer @ Mobileye | CS & Computational Neuroscience Student @ HUJI

1 年

Wow, amazing ??

回复
Nitay Shoshana

Software Team Lead

1 年

????! ?????? ?

回复
Meitar De Garc'ea

B.Sc. in electrical engineering and computer science student

1 年

nice, thanks

回复
Matan George

Software developer at Wix.com

1 年

Thanks Geva ??

回复
Shetu Ayalew

Team Lead at Israel Defense Forces

1 年

Cool thanks

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了