A Single-Instruction Micropatch For a Critical Remote Execution Issue
by Mitja Kolsek, 0patch Team
Last week, Microsoft issued an update resolving (among others) a critical remote code execution issue in VBScript Engine named CVE-2018-8174, exploit for which has previously been detected in the wild.
Unfortunately, Microsoft’s update breaks networking on some computers (details below), prompting users to avoid their application. Windows updates are “all or nothing” these days, so users can’t just remove a defective KB and enjoy the protection provided by other KBs issued on the same Patch Tuesday. Fortunately, micropatching provided by 0patch is the exact opposite of that, addressing each vulnerability individually. Let’s start with the exploit and the underlying vulnerability.
It appears the exploit has been caught by at least two security firms, Kaspersky and 360, and they both issued detailed analyses of the infection chain and, more importantly for us, the vulnerability itself [Kaspersky’s analysis, 360’s analysis]
The vulnerability is triggered by this simple proof-of-concept provided by Kaspersky:
Private Sub Class_Terminate()
Set ArrB(0) = ArrA(0)
ArrA(0) = 31337
Set ArrA(0) = New ClassVuln
By simply saving the above code to poc.vbs and double-clicking it on a Windows computer without May 2018 updates installed, we trigger the vulnerability and get wscript.exe to crash in oleaut32.dll’s VariantClear function .
|Access Violation in OLEAUT32.DLL’s VariantClear due to accessing an unallocated memory address|
Let’s see what goes on here. We create two fixed-size arrays ArrA and ArrB with one element each.
A ClassVuln object is created and assigned to the first element of the ArrA array, then the ArrA array is erased. There is just one element in the array, namely the ClassVuln object, and Erase sets ArrA(0) to Nothing, which removes all references to the object and triggers its destruction.
Our object has a weird method called Class_Terminate. This is a class event that’s not officially supported in VBScript anymore but apparently still works and gets called upon object’s destruction. (If you’re wondering why unsupported functionality still works it’s probably because removing it would break an unknown number of production scripts that are using it, and cause much more headache than keeping it and removing it from documentation.)
In our Class_Terminate, the first element of ArrB is set to ArrA(0) - but wait, ArrA(0) is the very object being destroyed, and you can imagine where this is heading. This assignment increases the reference (“lock”) count for the object, but the following instruction (ArrA(0) = 31337) decreases the same reference count, which will lead to the object being actually destroyed.
However, after being destroyed, there will still be a reference to this object in ArrB(0), which is called a “dangling pointer,” i.e., a pointer to a memory block that has already been freed. In this case the object was allocated on the heap, so now ArrB(0) has a reference to some location on the heap, and those versed in exploiting “use after free” issues have a nice starting point to make use of this reference.
We’re not interested in exploiting this issue, however, but in patching it. To do that, we first need to understand the vulnerability.
Kaspersky researchers pointed their fingers at the VBScriptClass::Release function, which only checks the object reference count before calling VBScriptClass::TerminateClass (this executes our Class_Terminate code), and doesn’t account for the case where Class_Terminate would increase the object reference count. So even though the reference count was increased (by Set ArrB(0) = ArrA(0)), the object still gets destroyed.
We’re not sure that this is the (entire) root cause, although we would probably try to fix the issue using this avenue if we didn’t have Microsoft’s official patch that contains information on how they fixed it.
After applying the May 2018 Windows updates, executing the PoC has a different result:
|Patched Windows show a runtime error|
The error states: “Object required: ‘ArrA(…)’” in line 6, indicating that the assignment of ArrA(0) is not possible as ArrA is not an object (anymore).
First we decided to compare code execution between the patched and the unpatched version using WinDbg’s wt (trace and watch data) command. Fortunately symbols are available for oleaut32.dll so the traces looked fairly identical up to the point where the patched version “derailed” from the unpatched version’s path towards crashing. It turned out the fork happened in function vbscript!AssignVar, executed as a result of assigning ArrA(0) to ArrB(0). In this function (which hasn’t been modified by the patch), a test is being made on the vt (variant type) member of a VARIANT structure, which in our case denotes the ArrA(0) element. And it turns out on the unpatched computer, vt is 9 (VT_DISPATCH) while on the patched computer, it is 0 (VT_EMPTY).
This means someone must have changed the value of vt from 9 to 0 along the way. In order to find out who did that, we placed an access breakpoint to the address of vt sometime earlier in the execution where it was still 9 on the patched computer.
Bingo! We found a “mov word [rdi], bp” instruction in our old friend, the VariantClear function, which sets our vt to 0. (rdi points to the VARIANT structure, and ebp is 0 throughout the function.)
It was time for BinDiff. We compared the patched and the last unpatched version of oleaut32.dll (which is where the crash occurs), and looked at the modified functions. Unsurprisingly, VariantClear was slightly modified in several places (orange blocks on the image below). Three of these blocks set the vt (variant type) member of a VARIANT structure to 0, which means VT_EMPTY, or an empty object.
|Left: unpatched code; Right: patched code|
Our PoC executes the middle orange block, and that block contains - in its patched version - the instruction that sets vt to 0.
So we created a micropatch that injects a logically identical instruction to the vulnerable code. Since the vulnerable code uses rbx as the pointer to the VARIANT, this is what we needed to inject:
mov word [rbx], 0
And this is the source code for our micropatch:
mov word [rbx], 0 ; Set VARIANT’s vt to 0 (VT_EMPTY)
Sure enough, applying this micropatch stopped the wscript.exe from crashing and made its behavior identical to the officially patched version:
|The old version with our micropatch has an identical reaction to our PoC as the new version.|
Our micropatches for this vulnerability have been labeled ZP-320 and ZP-321 for 32-bit and 64-bit version of oleaut32.dll respectively, and are applicable on Windows 7 and Windows 2008 Server updated up to April 2018 Windows updates. Why only these versions? Well, updates KB4103718 and KB4103712 are reportedly causing networking problems on some computers, which prompts users to delay their application - remaining vulnerable to the issue we have micropatched here. We have reports of Windows 7 and Windows 2008 Servers being affected, but if you or someone you know is experiencing this issue on other Windows versions, just ping us and we’ll quickly port these micropatches.
As always, if you have 0patch Agent installed, the above-mentioned micropatches should already be present and applied on your system. If not, you can download a free copy of 0patch Agent to protect your system from CVE-2018-8174 at least until Microsoft resolves the functional problems with their updates.