h3xduck@blog:~#

EternalBlue Series Part 5: Vulnerability analysis

by h3xduck

21 August 2021

3. Comprehensive analysis of the vulnerability

Welcome to my second favorite section of this eternalblue series, where we will now join all pieces of the puzzle gathered until this point:

We know from the FEAs research that…

And from reverse-engineering we know that…

Now, to help with this study I present you the pseudocode of the SrvOs2FeaListToNt and SrvOs2FeaListSizeToNt functions in which we are interested (SrvOs2FeaListSizeToNt being the one we decompiled with Ghidra on part2, and you can also decompile SrvOs2FeaListToNt in case you want to obtain the pseudocode by yourself):

(This relates to the decompiled code of before, see how p0s2FeaList is our old param_1, and pCurrentOs2FeaRecord is piVar1).

Okay, focusing on the pseudocode above, let’s follow the execution of the function when it is called:

Element Real HIGH WORD Real LOW WORD Values expected
FEA List size in bytes 0000 fff5 0000ff5f
SizeOfListInBytes 0001 0000 00010000
Overwritten SizeOfListInBytes 0001 fff5 0000ff5f

Appreciate the difference between the actual real value and the value which was expected to be received, at least by the developers, when overwriting the value of SizeOfListInBytes. Since only the lower word of the dword which is SizeOfListInBytes is overwritten, then if an attacker purposely set the value of SizeOfListInBytes to one which does not correspond to the actual size of the FEAlist but in which the higher word has a value larger than 0 and in which the lower word is smaller than the lower word of the size of the FEAlist, then the value of SizeOfListInBytes will get larger when the server overwrites it instead of shrinking, which is what the server desired to do. Now, the value of SizeOfListInBytes is much larger than the one in NtFeaListSize and also larger to the one we returned to the main function as the size of the NT buffer to allocate!

Let’s then continue following the execution so that we can see what consequences this has:

There it is! We have found our buffer overflow, which is an excellent signal. Please revise all the previous and make sure you fully understand the vulnerability and how we reached this point because we are almost at the part when things get spicy with our exploit.

There is only one small aspect to take into consideration with this buffer overflow, which is that we need some way of “stopping” copying FEAs where we want, or otherwise we could hit some undesired heap memory. This is where the FEA flags play a role: Each FEA has a flag (we saw this when we saw the FEAs on part 4 of the series) which take a value either 0x00 or 0x80. The meaning of these flags does not actually matter for the vulnerability (they just indicate the importance of the FEA when transforming it into NT format) but what is relevant is that, as we see in instruction 10 above, we will stop copying the FEAs if notice some flag has an incorrect value. Therefore, we will put a FEA with a flag set different to 0x00 or 0x80 at the end of the overflowing buffer in order to stop the memcpy whenever we desire.

(As a curious note, the fact that the overflow ends by this incorrect flag makes the server return an error message to the client indicating it, which is in fact what many EternalBlue vulnerability checkers use to see if the system is exploitable, because if that error does not appear it means that the system was patched and the overflow is no longer possible thus not triggering that copy of the invalid FEA).

Going back to the vulnerability, some issues arise now, however: we are overflowing the NT buffer, but are we overwriting anything important because of that? Can we control what we overwrite? How do we take advantage of this overflow?. We will explore this matter and answer these questions. In the following sections, we will see some additional bugs which will help us exploit this vulnerability and, after that, I’ll present you with the full picture of eternalblue.

See you on the next part!

tags: stack - overflow - buffer - exploitation - shellcode - RCE - eternalblue - vulnerability - windows - eternalblue - SMB

Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.