JIT Spray is a popular exploitation technique first appeared in 2010. It embeds shellcode as immediate value into the executable code the JIT compiler generates. Currently, all major JIT engine, including Chakra, already have many mitigations in place against this technique, such as random NOP instruction insertion, constant blinding, etc.
This article points out two weaknesses in Chakra’s JIT Spray mitigation (in Windows 8.1 and older operating systems, and Windows 10, respectively), allowing attackers to use JIT Spray to execute shellcode, bypassing DEP. I will also discuss a method to bypass CFG using Chakra’s JIT compiler.
0x01 Constant Blinding
Constant Blinding is the most important mitigation strategy against JIT Spray. Chakra engine use a randomly generated key to XOR every user inputted immediate value that is not 0x0000 or 0xFFFF, and decrypts it on the fly. For example, the following JavaScript:
1 | ... |
Generates machine code like this:
1 | ... |
The immediate value in the resulting machine code is unpredictable, thus shellcode embedding is not possible.
0x02 Bypass Chakra’s Constant Blinding on Windows 8.1 or Older Operating Systems
Internally, for integer n, it is stored as n*2+1 by Chakra engine. When evaluating the expression n=n+m, it is not necessary to restore the original value of n before adding m, its result can be obtained by directly adding m*2 to n*2+1. Chakra engine on Windows 8.1 and older operating systems treat m*2 as self-generated data rather than user input, so constant blinding does not apply. For the following JavaScript code:
1 | ... |
When some conditions are met, could generate machine code like this:
1 | ... |
If we could make each instruction in our shellcode not larger than 2 bytes, it could be embedded in the immediate value. The actual immediate value is 2 times of the value in JavaScript, so the first byte must be an even number if we use a 2-byte instruction, which is not very hard to satisfy.
0x03 Bypass Chakra’s Constant Blinding on Windows 10
Chakra engine on Windows 10 does not suffer from this issue. But in order to generate highly optimized code, when writing to an integer array, the following JavaScript code:
1 | var ar = new Uint16Array(0x10000); |
Generates the following machine code:
1 | ... |
To mitigate against JIT Spray, Chakra only allows user to control at most 2 bytes of immediate value. But in this specific situation, the array index and the value being written appear in one instruction. Now we can control 4 bytes instead of 2 bytes of data.
Previously discussed 2-byte shellcode can also be used here. Due to the additional 2-byte 0x00 (which will be interpreted as “add byte ptr[eax], al”), we need to make the eax point to a writable location in the first two instruction.
0x04 Using Chakra Engine to Bypass CFG
By using previously discussed methods, we can do a JIT Spray to bypass DEP, but the shellcode entry point address embedded in the JIT’d code obviously cannot pass the CFG check. But actually, there are implementation flaws in Chakra engine itself that can be exploited to bypass CFG.
There is a fixed entry point function that always gets generated regardless of the need of JIT of the currently executing JavaScript code:
1 | 0:017> uf 4ff0000 |
This function address can pass the CFG check. Also, before jmp ecx, there is no CFG check of the target address. This can be used as a trampoline for jumping to arbitrary address. We will call it “cfgJumper” hereafter.
0x05 Locating JIT Memory and cfgJumper
Locating the JIT compiled code and the cfgJumper are needed if we want to use JIT Spray to bypass DEP and use cfgJumper to bypass CFG. Interestingly, the method of locating both are almost identical.
Every JavaScript function has a corresponding Js::ScriptFunction object. Every Js::ScriptFunction object also includes a Js::FunctionBody object. Inside this Js::FunctionObject object, a function pointer to the actual function entry point is stored.
If a function is never called, this function pointer points to Js::InterpreterStackFrame::DelayDynamicInterpreterThunk:
1 | 0:002> dc 0b89de70 l 8 |
If a function has been called before, but never compiled into JIT’d code, this function pointer points to cfgJumper:
1 | 0:002> dc 0b89de70 l 8 |
If a function is regularly called and Chakra compiles it into JIT’d code, this function pointer points to the actual code:
1 | 0:002> d 0b89de70 l8 |
With understandings of the internal structure of Js::ScriptFunction and Js::FunctionBody, we could precisely locate the JIT’d code and the cfgJumper.
0x06 Avoiding Randomly Inserted NOP instructions
Other than constant blinding, Chakra engine also employs randomized NOP instruction insertion to mitigate JIT Spray. But the density of the insertion is rather low. Testing code combines 29 16-bit number to form a shellcode, only 29 x86 instructions are generated on Windows 10, with virtually no NOP instruction inserted in between. But in the exploitation method used on Windows 8.1 and older operating systems, about 200 x86 instruction are generated, and highly likely to contain NOP instructions.
To solve this problem:
- Create a new script tag, put in a JavaScript function that contains JIT shellcode.
- Call this function in a loop to trigger JIT compilation.
- Read in compiled code to determine if there is any NOP instruction inserted.
- If any, destroy the script tag and repeat this procedure.
Testing environments are Windows 8.1 with all updates till May 2015 and Windows 10 TP 9926.
Microsoft informed me that it has been fixed in September 2015.