Blog | G5 Cyber Security

MIPS Buffer Overflow: Redirect to Address

TL;DR

This guide shows you how to exploit a simple buffer overflow in MIPS assembly to redirect execution to an arbitrary address (0x0040xxxx). We’ll cover identifying the vulnerability, crafting the payload, and running it.

Prerequisites

1. Identify the Vulnerability

The core idea is to overwrite the return address on the stack with your target address. This usually happens when a program copies user input into a buffer without checking its size.

2. Crafting the Payload

The payload consists of a series of bytes designed to overwrite the return address with your target address.

Step 1: Determine the Offset

You need to know how many bytes you need to write before reaching the return address on the stack. Use debugging tools (MARS’s debugger is suitable) to find this offset. Fill the buffer with a known pattern (e.g., ‘A’ characters) and observe where the return address gets overwritten.

Step 2: Construct the Payload

The payload will have the following structure:

[Padding] + [Target Address]

For example, if the offset is 20 bytes and your target address is 0x00401000, the payload might look like this (assuming little-endian MIPS):

"A" * 20 + "x00x10x40x00"

3. Running the Exploit

Step 1: Provide Input

Feed the crafted payload as input to the vulnerable program.

Step 2: Debugging (MARS)

4. Example Scenario

Let’s assume a simple program with a buffer overflow vulnerability:

.text
.globl main
main:
  # Allocate space on the stack for the buffer
buffer: .space 64

  # Copy user input into the buffer (vulnerable function)
  la $a0, buffer
  li $a1, 128 # Input size - larger than buffer!
  jal gets_wrapper

  # Return from main
jr $ra

.text
gets_wrapper:
  sw $fp, 0($sp)
  move $fp, $sp
  subu $sp, $sp, 4
  sw $ra, 0($sp)
  la $a0, buffer
  li $a1, 128
  jal gets
  lw $ra, 0($sp)
  move $sp, $fp
  lw $fp, 0($sp)
  jr $ra

In this example, the gets_wrapper function calls gets with a size larger than the buffer allocated on the stack. This creates a classic buffer overflow vulnerability.

5. Important Considerations

Exit mobile version