DejaBlue: Analyzing a RDP Heap Overflow

In August 2019 Microsoft announced it had patched a collection of RDP bugs, two of which were wormable. The wormable bugs, CVE-2019-1181 & CVE-2019-1182 affect every OS from Windows 7 to Windows 10. There is some confusion about which CVE is which, though it’s possible both refer to the same bug. The vulnerable code exist in both the RDP client and server, making it possible to exploit in either direction.

Patch Diff

A BinDiff or RDPCoreTS.dll (the only RDP related file changed on Windows 7).

From my previous work with RDP, I believe the DecodeFormatData and SendFormatDataRequest functions to only be callable post-authentication; leaving just the Decompress function.

A comparison between the unpatched and patched decompress function.

The original code (left) is a bit of a mess because it has been optimized into a single if statement. The updated code (right) is slightly less of a mess, and has some additional validation.

Line 24 adds code which checks if v11 + 0x2000 is smaller than v11. If the condition is true an error code is set, which later results in the function aborting. You may be asking how the check could possibly fail. How can adding 0x2000 to an integer make it smaller than before? The answer is: via an integer overflow.

An unsigned integer is 4 bytes and can hold any value between 0 and 0xFFFFFFFF (4,294,967,295‬). If an integer is incremented past its max value, it loops around to zero. So, if v11 was 0xFFFFFFFF, adding 0x2000 to it would result in a final value of 0x1FFF, which is smaller than the original.

v11 is used as the size for a heap allocation performed via the “new” operator. The assumption here would be that if we can overflow v11, it’d cause the allocation to be smaller than the decompressed data, leading to a heap overflow.

Some Reversing Required

To get a better understanding of how to exploit the integer overflow, I needed to know what values I control and how to call the vulnerable function. To do this, I xref’d DecompressUnchopper::DecompressUnchopper (the initializer function for the DecompressUnchopper class). Next, I continued to use “xrefs to” to walk backwards until I found the function which utilizes this class. Eventually, I came to CRdpDynVCMgr::HandleIncomingDvcData.

From my previous work with RDP (hours of reading docs), I knew that DynVC was short for “Dynamic Virtual Channel”. The DVC interface allows for communication between modules on the client and server, and supports both raw and compressed data. It’s likely the vulnerable “decompress” function can be hit by sending compressed data.

Already having a custom RDP client written for exploiting BlueKeep, I added code to open the DVC channel and send some test data.

Sending the string “MalwareTech123” to the DVC channel.

After attaching the debugger to the RDP server process, I set a breakpoint on DecompressUnchopper::Decompress and ran my code.

The breakpoint on DecompressUnchopper::Decompress.

rdx (2nd argument) is the test data I sent, and r8 (3nd argument) is the data length. Now knowing the function parameters, I was able to clean up the decompiled code.

The cleaned up decompression code.

Now, it’s time to see if I can trigger a crash.

Overflowing The Heap

In order to test the vulnerability by crashing the RDP server, I crafted a malicious DVC packet.

Building the malicious DVC packet.

I set the uncompressedSize field to 1 – 0x2000 (0xFFFFE001‬), so that when 0x2000 is added it will loop around to 1. Then I set the compressed data to contain the letter ‘A’ repeated 0x200 times, which should result in the heap buffer being overflowed by 0x1FF bytes.

Awesome, now I can write arbitrary data of arbitrary size to neighboring heap allocations! This bug is powerful because object instances are stored on the same heap, making it possible to overwrite them.

For example, I changed an object’s VTable pointer so that the application would perform a ptr call via the address 0x1337133713371337.

Vulnerability Research
BlueKeep: A Journey from DoS to RCE (CVE-2019-0708)

Due to the serious risk of a BlueKeep based worm, I’ve held back this write-up to avoid advancing the timeline. Now that a proof-of-concept for RCE (remote code execution) has been release as part of Metasploit, i feel it’s now safe for me to post this. This article will be …

Vulnerability Research
Analysis of CVE-2019-0708 (BlueKeep)

I held back this write-up until a proof of concept (PoC) was publicly available, as not to cause any harm. Now that there are multiple denial-of-service PoC on github, I’m posting my analysis. Binary Diffing As always, I started with a BinDiff of the binaries modified by the patch (in …

Vulnerability Research
Analysis of a VB Script Heap Overflow (CVE-2019-0666)

Anyone who uses RegEx knows how easy it is to shoot yourself in the foot; but, is it possible to write RegEx so badly that it can lead to RCE? With VB Script, the answer is yes! In this article I’ll be writing about what I assume to be CVE-2019-0666. …