Notes on Exploiting Chrome JIT Spraying via Egghunters
Exploiting JIT spraying to execute large C2 payloads via egghunter assembly.
This blog post attempts to explain fundamental exploitation techniques applied to the Chrome JIT landscape. I faced issues when attempting to achieve a C2 beacon callback and used these methods to eventually succeed.
Chrome JIT Spraying
Chrome JIT spraying has been around for a while now, with various methods using it as a pivot point to acheive code execution. One old technique relied on TurboFan optimising a given function which stored IEEE-encoded shellcode. Once read/write primitives were achieved from a chrome exploit, you could overwrite the entry point stored within the function’s JIT-compiled Code object. With this completed, you’d have essentially redirected the program to execute your shellcode.
For more information about the topic, you can read the following (written by people much smarter than myself):
- https://mem2019.github.io/jekyll/update/2022/02/06/DiceCTF-Memory-Hole.html
- https://www.matteomalvica.com/blog/2024/06/05/intro-v8-exploitation-maglev/
- https://anvbis.au/posts/code-execution-in-chromiums-v8-heap-sandbox/
JIT Shellcode Limitations
There are some notable limitations with writing valid shellcode for this technique:
- Must be written in immediate numbers (such as floats)
- Must replicate the
mov reg, valinstructions - TurboFan deduplicates identical float constants, meaning that you cannot reuse lines
- You are restricted in your payload size due to TurboFan optimisation
I’ll explain how I dealt with these nuances.
Immediate numbers
Converting shellcode to immediate numbers can be done in a relatively simple manner by encoding it into IEEE floats. You can achieve this via this link or through a simple python script which converts each line of your shellcode. One thing to keep in mind is to ensure that your payload does not convert into NaN or infinity. This will cause the v8 engine to overwrite portions of the shellcode.
mov reg, val instructions
TurboFan compiles the IEEE shellcode into mov reg, val instructions. Each float constant becomes a 10-byte instruction sequence, followed by TurboFan specific instructions such as movq xmm0, r10.
Due to the limitations of this layout, we must essentially have 6 bytes of usable shellcode + a 2 byte jmp which avoids the TurboFan instructions. I found that with large paylods (20+ lines), the relative jmp displacement between blocks increases from 5 to 8 bytes. This may be a specific quirk of the chrome version I targeted. Another thing to be aware of is that the displacement also differs on Linux compared to Windows.
Identical float constants
As TurboFan is an optimisation engine, I’ve encountered cases where it has optimised specific shellcode lines as they were repeated. This breaks the jmp spacing. To prevent it, you can place NOP padding across identified lines to avoid TurboFan optimisation. Other single-byte NOP equivalents such as clc or stc can be used to create unique lines as well.
Payload size restrictions
Naturally due to everything we have covered thus far (and how TurboFan interacts with large shellcode itself), we can’t just encode our large C2 shellcode into IEEE floats and expect it to work. It’s here where we can employ the use of the classic technique: the egghunter. An example of a egghunter related to browsers is Forest Orr’s DoubleStar. In case you’re unfamiliar with his work, I highly recommend reading his blog.
Applying this to Chrome is pretty much the same. You will need to ensure you’re not hitting any of the above nuances, but generally the egghunter should look as follows:
- Enumerate PEB for
GetProcAddress - Obtain address of
VirtualQueryandVirtualProtect- Save address of each to general purpose registers
- Scan address space using
VirtualQuery- Skip
PAGE_NOACCESS,PAGE_GUARD, orMEM_IMAGEregions - Identify egg tag
- Save address to general purpose registers
- Skip
VirtualProtectRWX shellcode in memory- Jump and execute to shellcode
With this method, you can choose your desired way to get the C2 payload into the address space. I created a UInt8Array which held mine, but theoretically you could also host it and use fetch(). Important to note that your shellcode should align the stack by 16-bytes. If you’re unfamiliar with any of this, I suggest reading my blog post on creating position independent code.
Finally, if your array is only referenced as a local variable, it’s susceptible to v8’s GC either relocating it, or freeing it entirely. To avoid this, you can assign the array to a global object (which is a GC root). This ensures that the array will stay alive for the entirety of the process.