Skip to content
Memory corruption Write-up Advanced

Exploiting Stack Buffer Overflows To Get A Privileged Shell

A local program copied user input into a fixed 140-byte buffer without checking length, so a long input overwrote the saved return address. The author redirected execution to injected shellcode, first calling setreuid to switch to another user's ID, and read a file only that user could access.

Medusa Follow

Published on systemweakness.com

A program would usually comprise multiple functions and there needs to be a way of tracking which function has been called, and which data is passed from one function to another. The stack is a region of contiguous memory addresses and it is used to make it easy to transfer control and data between functions.

Medusa, on systemweakness.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the program copied user input into a fixed-size buffer without checking its length, allowing the saved return address on the stack to be overwritten.

Impact

An attacker with local access to run the vulnerable program could execute arbitrary code as another user, including root, and read files restricted to that user.

What the attacker needed

Local access to run the program; a vulnerable binary with a fixed buffer

How it was found

  1. Run the program under gdb with a long string of 'A' characters to confirm the buffer overflows and crashes.
  2. Replace the junk before the shellcode with NOP instructions so the exact start address does not matter.
  3. Dump memory to find an address inside the NOP sled.
  4. Convert that address to little-endian byte order.
  5. Generate shellcode that calls setreuid for the target user ID and prepends it to the /bin/sh shellcode.
  6. Pass the final payload to the program to obtain a shell as the other user and read the restricted file.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

A program keeps a small reserved space in memory for input. If it copies in more than fits, the extra data spills over into the place that records where to return after the function ends. By controlling that spilled value, an attacker can make the program run their own instructions instead.

What is Memory corruption?

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.

More by Medusa

View all

10 more disclosed reports, best first.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.