Dissecting and Exploiting TCP/IP RCE Vulnerability “EvilESP”
A one-byte out-of-bounds write in the Windows TCP/IP driver occurs when an IPv6 fragmented datagram is sent inside IPsec ESP packets, because the Next Header field of the ESP packet is placed after the encrypted payload rather than inside the extension header. An attacker could trigger this to cause a denial of service, and the write primitive could potentially be developed into remote code execution.
Published on securityintelligence.com
September’s Patch Tuesday unveiled a critical remote vulnerability in tcpip.sys, CVE-2022-34718. The advisory from Microsoft reads: “An unauthenticated attacker could send a specially crafted IPv6 packet to a Windows node where IPsec is enabled, which could enable a remote code execution exploitation on that machine.”
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
September's Patch Tuesday unveiled a critical remote vulnerability in tcpip.sys, CVE-2022-34718. Microsoft's advisory describes it as follows: "An unauthenticated attacker could send a specially crafted IPv6 packet to a Windows node where IPsec is enabled, which could enable a remote code execution exploitation on that machine."
Pure remote bugs tend to attract attention, yet more than a month after the patch nothing beyond the advisory had been published. That gap is what prompted this work: a binary patch diff, a root cause analysis, and a proof of concept. An exploit demo and root cause analysis were posted on 21 October, and shortly afterwards Numen Cyber Labs published a blog post and PoC using a different exploitation method. This article is the follow-up to the demo video, explaining the reverse engineering in depth and correcting inaccuracies found in the Numen Cyber Labs post.
The path covered here is: diffing the patch, understanding the affected protocols, identifying the bug, reproducing it, writing an exploit that triggers a Denial of Service, and then looking at the primitives that would be needed to reach remote code execution.
Setup
The advisory gives only two facts: the bug lives in the TCP/IP driver and it requires IPsec to be enabled. Everything else has to come out of the binaries.
- Obtain two versions of
tcpip.sysfrom Winbindex, one immediately before the patch and one immediately after, both for the same version of Windows. Sequential versions matter: binaries even a few updates apart introduce noise from unrelated differences and waste analysis time. Winbindex makes this easy because it can supply any Windows binary from Windows 10 onwards. - Load both files in Ghidra, apply the Program Database (pdb) files, and run auto analysis. Checking the aggressive instruction finder works best.
- Export both files into BinExport format using the BinExport for Ghidra extension.
- Load the exports into BinDiff to produce a diff and start analysing the differences.
BinDiff matches functions between the two binaries using various algorithms. Because Microsoft's function symbol information was applied, every function can be matched by name.
Finding it
The BinDiff summary compares the pre- and post-patch binaries, and the list of matched functions sorted by similarity shows only two functions with a similarity below 100%: IppReceiveEsp and Ipv6pReassembleDatagram. Those are the two functions the patch changed.
From previous research, Ipv6pReassembleDatagram is known to handle the reassembly of IPv6 fragmented packets. The name IppReceiveEsp indicates it handles the receiving of IPsec ESP packets.
Before reading the patch, two pieces of background are needed.
IPv6 fragmentation
An IPv6 packet can be divided into fragments, each sent as a separate packet. Once all fragments reach the destination, the receiver reassembles them into the original packet. Per RFC 8200, fragmentation is implemented through an extension header called the Fragment header. Its Next Header field holds the type of header present in the fragmented data.
IPsec ESP
IPsec is a group of protocols used together to set up encrypted connections, commonly for VPNs. The Encapsulating Security Payload (ESP) protocol encrypts (encapsulates) a packet's contents. There are two modes: in tunnel mode a copy of the IP header sits inside the encrypted payload; in transport mode only the transport layer portion is encrypted. Like IPv6 fragmentation, ESP is implemented as an extension header. Per RFC 4303, an ESP packet contains the Security Parameters Index (SPI) and Sequence Number fields, which together form the ESP extension header, and the fields between and including Payload Data and Next Header are encrypted. The Next Header field describes the header contained in Payload Data.
Root cause
The Ipv6pReassembleDatagram patch
A side-by-side comparison of the function graphs shows a single new code block in the patched function. The block compares two unsigned integers in registers EAX and EDX and jumps if one value is less than the other. The destination block makes an unconditional call to IppDeleteFromReassemblySet, which by its name looks like error handling. The new code is therefore a bounds check with a goto error inserted for the failure case.
With that insight, static analysis moves to a decompiler. Structure definitions for the undocumented Packet_t and Reassembly_t objects, plus a couple of crucial local variable assignments, were filled in from 0vercl0k's earlier blog post on a different IPv6 vulnerability (CVE-2021-24086) and from additional reverse engineering.
In the decompiled output, the new code added by the patch is the bounds check. Reassembly->nextheader_offset holds the byte offset of the next_header field in the IPv6 fragmentation header. The check compares next_header_offset against the length of the header buffer. HeaderBufferLen is used to allocate a buffer, and Reassembly->nextheader_offset is then used to index into and copy into that allocated buffer.
Because the check had to be added, a condition must exist that lets nextheader_offset exceed the header buffer length. The second patched function provides the rest of the answer.
The IppReceiveEsp patch
A side-by-side comparison of the function graphs in the BinDiff workspace shows several new code blocks in the patched function. In the decompiled output, the new check examines the Next Header field of the ESP packet, which identifies the header of the decrypted ESP packet. A Next Header value can correspond to an upper layer protocol such as TCP or UDP, or to an extension header such as a fragmentation header or routing header. If NextHeader is 0, 0x2B or 0x2C, IppDiscardReceivedPackets is called and the error code is set to STATUS_DATA_NOT_ACCEPTED. Those values correspond to IPv6 Hop-by-Hop Option, Routing Header for IPv6 and Fragment Header for IPv6 respectively.
RFC 4303 states that in the IPv6 context ESP is viewed as an end-to-end payload and should appear after hop-by-hop, routing and fragmentation extension headers. If a header of one of those types is contained within an ESP payload, the packet violates the protocol's RFC and is discarded.
Putting the two together
In Ipv6pReassembleDatagram the fix addresses a buffer overflow. The size of the victim buffer is calculated as the size of the extension headers plus the size of an IPv6 header. The inserted check compares Reassembly->nextheader_offset, the offset of the Next Header value in the buffer holding the fragment's data, against the buffer length.
In an ESP packet, the Next Header field comes after Payload Data. That means Reassembly->nextheader_offset includes the size of the Payload Data, which is controlled by the size of the data and can be much greater than the size of the extension headers. The expected location of the Next Header field is inside an extension header or the IPv6 header. In an ESP packet it is not inside the header at all, because it is contained in the encrypted portion of the packet.
The out-of-bounds write happens where Reassembly->nextheader_offset is used to index and copy into the allocated buffer: a one-byte out-of-bounds write, whose size and value come from NextHeader.
Exploitation
The bug can be triggered by sending an IPv6 fragmented datagram via IPsec ESP packets. The next question is how the victim is able to decrypt those ESP packets.
Step 1: confirm the vulnerable path is reachable
Sending packets to a victim with an ESP header containing junk data and setting a breakpoint on the vulnerable IppReceiveEsp function showed the breakpoint was hit, but the internal function believed to do the decrypting, IppReceiveEspNbl, returned an error, so the vulnerable code was never reached. Further reverse engineering of IppReceiveEspNbl located the point of failure: to decrypt an ESP packet successfully, a security association must be established.
A security association is a shared state, primarily cryptographic keys and parameters, maintained between two endpoints to secure traffic between them. It defines how a host encrypts, decrypts and authenticates traffic to and from another host, and can be established via the Internet Key Exchange (IKE) or the Authenticated IP Protocol. In practice, the attacker needs a way to establish a security association with the victim so the victim knows how to decrypt the incoming data.
Step 2: create the security association on the victim
Rather than implement IKE, the security association was created manually on the victim using the Windows Filtering Platform WinAPI (WFP). Numen's blog post stated that WFP cannot be used for secret key management; that is incorrect. By modifying Microsoft's sample code for manual SA keying, it is possible to set a symmetric key that the victim will use to decrypt ESP packets coming from the attacker IP.
Step 3: build and send malformed encrypted ESP packets
With the victim able to decrypt ESP traffic from the attacker, malformed encrypted ESP packets are built with scapy, which can send packets at the IP layer. The process is simple: create a set of fragmented packets from an ICMPv6 Echo request, then encrypt each fragment into an ESP layer before sending.
The primitive
The primitive is an out-of-bounds write at:
offset = sizeof(Payload Data) + sizeof(Padding) + sizeof(Padding Length)
The value written is controllable through the value of the Next Header field; in the exploit it was set to 0x41.
Denial of Service
Corrupting a single byte at a random offset in the NetIoProtocolHeader2 pool, where the target buffer is allocated, usually does not cause an immediate crash. A reliable crash comes from inserting additional headers within the fragmented message for the target to parse, or from repeatedly pinging the target after corrupting a large portion of the pool.
Limits on the way to RCE
The offset is attacker controlled, but per the ESP RFC padding is required so that the Integrity Check Value (ICV) field, if present, is aligned on a 4-byte boundary. Since
sizeof(Padding Length) = sizeof(Next Header) = 1,
sizeof(Payload Data) + sizeof(Padding) + 2 must be 4-byte aligned, and therefore:
offset = 4n - 1
where n is any positive integer, constrained by the fact that the payload data and padding must fit within a single packet and are therefore limited by the MTU (frame size). The consequence is that full pointers cannot be overwritten. That is limiting but not necessarily prohibitive: the offset of an address in an object, a size, a reference counter and similar fields can still be overwritten. What is possible depends on which objects can be sprayed into the kernel pool where the victim headerBuff is allocated.
Heap grooming research
The victim out-of-bounds buffer is allocated in the NetIoProtocolHeader2 pool. The first steps in heap grooming research are to examine which types of objects are allocated in that pool, what they contain, how they are used, and how they are allocated and freed. That shows how the write primitive can be used to obtain a leak or build a stronger primitive. The work is not necessarily restricted to NetIoProtocolHeader2, but because the position of the victim out-of-bounds buffer cannot be predicted and the addresses of surrounding pools are randomised, targeting other pools looks challenging.
Impact
The primitive is a one-byte out-of-bounds write in kernel pool memory, with an attacker-controlled offset of the form 4n - 1 and an attacker-controlled value. In the demonstrated case it produces a Denial of Service: a single corrupted byte usually does not crash the target, but a reliable crash follows from adding headers inside the fragmented message or from repeatedly pinging the target after corrupting a large portion of the pool. The advisory describes the potential as remote code execution on a Windows node where IPsec is enabled, reachable by an unauthenticated attacker sending a specially crafted IPv6 packet. The RCE step is not demonstrated here; the article sets out the primitives and the next steps needed to get there, with the 4-byte alignment constraint meaning full pointers cannot be overwritten.
Fix
Microsoft's patch changes two functions. In Ipv6pReassembleDatagram it adds a bounds check comparing Reassembly->nextheader_offset against the length of the header buffer, and routes a failed comparison to IppDeleteFromReassemblySet as error handling. In IppReceiveEsp it adds a check on the ESP Next Header field: values 0, 0x2B and 0x2C (IPv6 Hop-by-Hop Option, Routing Header for IPv6 and Fragment Header for IPv6) cause IppDiscardReceivedPackets to be called with the error code set to STATUS_DATA_NOT_ACCEPTED, because such headers inside an ESP payload violate RFC 4303.
Variants to test
- Diff other
tcpip.sysfunctions that handle extension headers after decryption. A positive result is a function whose post-patch graph gains a bounds check or a discard path that the pre-patch version lacks. - Look for other places where an offset into a reassembly buffer is derived from a field whose position depends on the encapsulation. A positive result is an offset that can be made larger than the buffer by choosing a different encapsulation, as with ESP here.
- Check other Next Header values that the patched
IppReceiveEspdoes not reject. A positive result is a header type that still reaches reassembly from inside an ESP payload. - Test whether other pools besides
NetIoProtocolHeader2can be reached by the same write. A positive result is a controllable object allocated adjacent to the victim buffer whose offset, size or reference count can be corrupted with a 4n - 1 offset. - Re-examine published analyses of the same bug against the binaries. A positive result is a claim in a third-party write-up that the decompiled code contradicts, as happened with the claim that WFP cannot manage secret keys.
What happened, in plain words
In September, Microsoft released its monthly batch of security fixes, which people in the industry call "Patch Tuesday" because it usually lands on a Tuesday. That batch unveiled a serious hole in a file called tcpip.sys. That file is part of Windows itself: it is the piece of the operating system that handles network traffic, the code that reads the packets arriving on a network card and decides what to do with them. The hole was given the identifier CVE-2022-34718. A CVE is simply a public catalogue number for a known security flaw, so that everyone talking about the same bug can refer to it by the same name.
Microsoft's own description of the bug was short. It said that an unauthenticated attacker, meaning someone who has not logged in and has no account on the target machine, could send a specially crafted IPv6 packet to a Windows machine where IPsec is enabled, and that this could allow remote code execution on that machine. Remote code execution, usually shortened to RCE, means the attacker can make the victim's computer run code of the attacker's choosing, which is about as bad as a network bug gets. IPv6 is the newer version of the addressing system the internet uses to route traffic between machines. IPsec is a set of rules for encrypting network traffic, commonly used to build VPNs, the private tunnels that connect offices or remote workers over the public internet.
That description is all Microsoft published. Bugs that can be attacked purely over the network, with no need to trick a user into clicking anything, usually attract a lot of attention from security researchers. Yet more than a month after the patch, nothing beyond the advisory had appeared. That silence is what prompted this work: a binary patch diff, a root cause analysis, and a proof of concept. A proof of concept, or PoC, is a small piece of code that demonstrates the bug actually works, rather than just describing it on paper.
An exploit demo and root cause analysis were posted on 21 October. Shortly afterwards, Numen Cyber Labs published a blog post and PoC using a different exploitation method. This article is the follow-up to the demo video: it explains the reverse engineering in depth and corrects inaccuracies found in the Numen Cyber Labs post.
The path covered here is: diffing the patch, understanding the affected protocols, identifying the bug, reproducing it, writing an exploit that triggers a Denial of Service, and then looking at the primitives that would be needed to reach remote code execution. A Denial of Service, or DoS, is an attack that makes a machine or service stop working, for example by crashing it, without necessarily giving the attacker control of it. A primitive, in this context, is a small, reliable capability an attacker has, such as "I can write one byte somewhere I should not be able to"; bigger attacks are built by combining primitives.
Setup
The advisory gives only two facts: the bug lives in the TCP/IP driver and it requires IPsec to be enabled. Everything else has to come out of the binaries. A binary is the compiled file that the computer actually runs, made of machine instructions rather than readable source code. Reading a binary to work out what it does is called reverse engineering.
- Obtain two versions of
tcpip.sysfrom Winbindex, one immediately before the patch and one immediately after, both for the same version of Windows. Sequential versions matter: binaries even a few updates apart introduce noise from unrelated differences and waste analysis time. Winbindex makes this easy because it can supply any Windows binary from Windows 10 onwards. - Load both files in Ghidra, apply the Program Database (pdb) files, and run auto analysis. Checking the aggressive instruction finder works best.
- Export both files into BinExport format using the BinExport for Ghidra extension.
- Load the exports into BinDiff to produce a diff and start analysing the differences.
Why each step: Winbindex is a website that can supply Windows binaries from Windows 10 onwards, so you can download the file from just before and just after a fix. Comparing those two files is the whole idea of patch diffing: the fix is the difference between them, so whatever changed is a strong hint about where the bug is. Ghidra is a free tool that turns machine instructions back into something a human can read, a process called disassembly and decompilation. A pdb file is a companion file that lists the names Microsoft gave to functions and variables, so the tool can label things instead of showing anonymous addresses. BinExport and BinDiff are tools that compare two binaries function by function and score how similar each pair is.
BinDiff matches functions between the two binaries using various algorithms. Because Microsoft's function symbol information was applied, every function can be matched by name. A function is a named block of code that does one job, like a recipe with a name; matching by name means the tool knows that the function called IppReceiveEsp in the old file is the same one as in the new file, so it can compare them directly.
Finding it
The BinDiff summary compares the pre- and post-patch binaries, and the list of matched functions sorted by similarity shows only two functions with a similarity below 100%: IppReceiveEsp and Ipv6pReassembleDatagram. Those are the two functions the patch changed. In other words, only these two functions came out of the comparison as changed by Microsoft's fix.
From previous research, Ipv6pReassembleDatagram is known to handle the reassembly of IPv6 fragmented packets. The name IppReceiveEsp indicates it handles the receiving of IPsec ESP packets.
Before reading the patch, two pieces of background are needed.
IPv6 fragmentation
An IPv6 packet can be divided into fragments, each sent as a separate packet. Once all fragments reach the destination, the receiver reassembles them into the original packet. Think of sending a long letter by cutting it into several postcards: each postcard travels on its own, and the recipient puts them back in order using the numbering written on each one. Per RFC 8200, fragmentation is implemented through an extension header called the Fragment header. Its Next Header field holds the type of header present in the fragmented data. An RFC is a document published by the Internet Engineering Task Force that defines how internet protocols must behave; it is the rulebook everyone agrees to follow. A header is the labelled section at the start of a packet that tells the receiver what kind of data follows, much like the label on a parcel.
IPsec ESP
IPsec is a group of protocols used together to set up encrypted connections, commonly for VPNs. The Encapsulating Security Payload (ESP) protocol encrypts (encapsulates) a packet's contents. There are two modes: in tunnel mode a copy of the IP header sits inside the encrypted payload; in transport mode only the transport layer portion is encrypted. Like IPv6 fragmentation, ESP is implemented as an extension header. Per RFC 4303, an ESP packet contains the Security Parameters Index (SPI) and Sequence Number fields, which together form the ESP extension header, and the fields between and including Payload Data and Next Header are encrypted. The Next Header field describes the header contained in Payload Data. The SPI is a number that tells the receiver which set of keys to use to decrypt this particular packet, and the sequence number helps the receiver detect replayed or out-of-order packets.
Root cause
The Ipv6pReassembleDatagram patch
A side-by-side comparison of the function graphs shows a single new code block in the patched function. A function graph is a picture of a function's logic: boxes are chunks of instructions and arrows show which chunk runs next, so a new box with new arrows is easy to spot. The block compares two unsigned integers in registers EAX and EDX and jumps if one value is less than the other. A register is a tiny, very fast storage slot inside the processor; EAX and EDX are the names of two of them on Intel-style processors. An unsigned integer is a whole number that cannot be negative. The destination block makes an unconditional call to IppDeleteFromReassemblySet, which by its name looks like error handling. The new code is therefore a bounds check with a goto error inserted for the failure case. A bounds check is code that verifies a number is within an allowed range before using it, like a bouncer checking that a ticket number is valid before letting someone in.
With that insight, static analysis moves to a decompiler. Static analysis means studying code without running it, as opposed to dynamic analysis, which means running it and watching what happens. A decompiler is the part of Ghidra that turns low-level machine instructions back into something resembling a high-level programming language, which is far easier to read. Structure definitions for the undocumented Packet_t and Reassembly_t objects, plus a couple of crucial local variable assignments, were filled in from 0vercl0k's earlier blog post on a different IPv6 vulnerability (CVE-2021-24086) and from additional reverse engineering. A structure is a bundle of related values stored together, like a form with several fields; naming those fields correctly is what makes decompiled code readable.
In the decompiled output, the new code added by the patch is the bounds check. Reassembly->nextheader_offset holds the byte offset of the next_header field in the IPv6 fragmentation header. An offset is a distance from the start of something, measured in bytes; if a buffer starts at position zero, offset 10 means the eleventh byte. The check compares next_header_offset against the length of the header buffer. HeaderBufferLen is used to allocate a buffer, and Reassembly->nextheader_offset is then used to index into and copy into that allocated buffer. A buffer is a reserved chunk of memory used to hold data temporarily, like a tray you put papers on while you sort them. To allocate means to ask the operating system for such a chunk.
Because the check had to be added, a condition must exist that lets nextheader_offset exceed the header buffer length. If the value could never be too large, there would be nothing to check. The second patched function provides the rest of the answer.
The IppReceiveEsp patch
A side-by-side comparison of the function graphs in the BinDiff workspace shows several new code blocks in the patched function. In the decompiled output, the new check examines the Next Header field of the ESP packet, which identifies the header of the decrypted ESP packet. A Next Header value can correspond to an upper layer protocol such as TCP or UDP, or to an extension header such as a fragmentation header or routing header. TCP and UDP are the two most common protocols carried inside IP packets: TCP is used for reliable connections such as web pages, UDP for simpler fire-and-forget messages. If NextHeader is 0, 0x2B or 0x2C, IppDiscardReceivedPackets is called and the error code is set to STATUS_DATA_NOT_ACCEPTED. Those values correspond to IPv6 Hop-by-Hop Option, Routing Header for IPv6 and Fragment Header for IPv6 respectively. The 0x prefix simply means the number is written in hexadecimal, a base-16 counting system programmers use because it maps neatly onto bytes.
RFC 4303 states that in the IPv6 context ESP is viewed as an end-to-end payload and should appear after hop-by-hop, routing and fragmentation extension headers. If a header of one of those types is contained within an ESP payload, the packet violates the protocol's RFC and is discarded. In plain terms: those three header types are supposed to sit outside the encrypted part, in front of it, so finding one inside the encrypted part means the packet is malformed and should be thrown away.
Putting the two together
In Ipv6pReassembleDatagram the fix addresses a buffer overflow. A buffer overflow is when code writes more data into a buffer than it can hold, so the extra data spills into neighbouring memory. That is dangerous because the neighbouring memory holds other data the program relies on, and an attacker who controls the spill can corrupt it deliberately. The size of the victim buffer is calculated as the size of the extension headers plus the size of an IPv6 header. The inserted check compares Reassembly->nextheader_offset, the offset of the Next Header value in the buffer holding the fragment's data, against the buffer length.
In an ESP packet, the Next Header field comes after Payload Data. That means Reassembly->nextheader_offset includes the size of the Payload Data, which is controlled by the size of the data and can be much greater than the size of the extension headers. The expected location of the Next Header field is inside an extension header or the IPv6 header. In an ESP packet it is not inside the header at all, because it is contained in the encrypted portion of the packet. So the code assumes the Next Header sits near the start of the data, but with ESP it sits far away, past all the encrypted content, and the offset used to find it can point beyond the end of the buffer.
The out-of-bounds write happens where Reassembly->nextheader_offset is used to index and copy into the allocated buffer: a one-byte out-of-bounds write, whose size and value come from NextHeader. An out-of-bounds write means writing to a memory location outside the area the code was supposed to touch, like writing on the wall next to the form instead of inside its boxes.
Exploitation
The bug can be triggered by sending an IPv6 fragmented datagram via IPsec ESP packets. The next question is how the victim is able to decrypt those ESP packets. This matters because ESP is encrypted by design: if the victim cannot decrypt the packet, it never gets as far as the vulnerable code.
Step 1: confirm the vulnerable path is reachable
Sending packets to a victim with an ESP header containing junk data and setting a breakpoint on the vulnerable IppReceiveEsp function showed the breakpoint was hit, but the internal function believed to do the decrypting, IppReceiveEspNbl, returned an error, so the vulnerable code was never reached. A breakpoint is a debugging feature that pauses the program at a chosen line so you can inspect what is happening, like putting a bookmark in a book and stopping there every time you reach it. Further reverse engineering of IppReceiveEspNbl located the point of failure: to decrypt an ESP packet successfully, a security association must be established.
A security association is a shared state, primarily cryptographic keys and parameters, maintained between two endpoints to secure traffic between them. It defines how a host encrypts, decrypts and authenticates traffic to and from another host, and can be established via the Internet Key Exchange (IKE) or the Authenticated IP Protocol. A cryptographic key is a secret value used by an encryption algorithm; the same key that locks the data is needed to unlock it. In practice, the attacker needs a way to establish a security association with the victim so the victim knows how to decrypt the incoming data.
Step 2: create the security association on the victim
Rather than implement IKE, the security association was created manually on the victim using the Windows Filtering Platform WinAPI (WFP). An API is a documented set of functions that one program offers so other programs can ask it to do things; a WinAPI is simply an API provided by Windows. The Windows Filtering Platform is the part of Windows that lets software inspect and control network traffic. Numen's blog post stated that WFP cannot be used for secret key management; that is incorrect. By modifying Microsoft's sample code for manual SA keying, it is possible to set a symmetric key that the victim will use to decrypt ESP packets coming from the attacker IP. A symmetric key is one single secret used both to encrypt and to decrypt, as opposed to a pair of different keys. Doing this manually is a shortcut for testing: for reproducing the bug it is enough to tell the victim the key directly.
Step 3: build and send malformed encrypted ESP packets
With the victim able to decrypt ESP traffic from the attacker, malformed encrypted ESP packets are built with scapy, which can send packets at the IP layer. Scapy is a free Python library for constructing and sending network packets by describing their layers in code, which is much easier than assembling raw bytes by hand. The process is simple: create a set of fragmented packets from an ICMPv6 Echo request, then encrypt each fragment into an ESP layer before sending. An ICMPv6 Echo request is the IPv6 equivalent of the ping you may have typed in a terminal: a small message that asks the other machine to reply, used to check whether it is reachable.
The primitive
The primitive is an out-of-bounds write at:
offset = sizeof(Payload Data) + sizeof(Padding) + sizeof(Padding Length)
Line by line: sizeof(Payload Data) is the number of bytes of actual encrypted content in the ESP packet. sizeof(Padding) is the number of extra filler bytes ESP adds so that the encrypted section lines up on a tidy boundary. sizeof(Padding Length) is the single byte that records how much padding was used. Adding the three gives the distance from the start of the data to the Next Header field, which is exactly the offset the vulnerable code uses. Because the attacker chooses how much payload to send, the attacker chooses this offset.
The value written is controllable through the value of the Next Header field; in the exploit it was set to 0x41. In other words, the attacker controls both where the stray byte lands and what that byte contains.
Denial of Service
Corrupting a single byte at a random offset in the NetIoProtocolHeader2 pool, where the target buffer is allocated, usually does not cause an immediate crash. A pool is a region of memory the operating system hands out in small pieces to whoever asks, and NetIoProtocolHeader2 is the name of one such region used for network data. A reliable crash comes from inserting additional headers within the fragmented message for the target to parse, or from repeatedly pinging the target after corrupting a large portion of the pool. To parse means to read through structured data and interpret each field; adding more headers gives the corrupted memory more chances to be used in a way that breaks something.
Limits on the way to RCE
The offset is attacker controlled, but per the ESP RFC padding is required so that the Integrity Check Value (ICV) field, if present, is aligned on a 4-byte boundary. The ICV is a checksum-like value that lets the receiver verify the packet was not tampered with in transit. Aligned on a 4-byte boundary means the field starts at a position that is a multiple of four, which some processors handle faster and which the protocol mandates. Since
sizeof(Padding Length) = sizeof(Next Header) = 1,
Line by line: both the Padding Length field and the Next Header field are exactly one byte long. That is why the arithmetic below works out the way it does.
sizeof(Payload Data) + sizeof(Padding) + 2 must be 4-byte aligned, and therefore:
offset = 4n - 1
Line by line: the payload plus padding plus the two one-byte fields must add up to a multiple of four. Since the offset is the payload plus padding plus one of those bytes, it must be one less than a multiple of four. So the offset can be 3, 7, 11, 15 and so on, but never 4, 8 or 12. n is any positive integer, constrained by the fact that the payload data and padding must fit within a single packet and are therefore limited by the MTU (frame size). MTU stands for Maximum Transmission Unit, the largest packet a network link will carry in one piece. The consequence is that full pointers cannot be overwritten. A pointer is a memory address stored in a variable, telling the program where to find something; a single-byte write cannot replace a full pointer. That is limiting but not necessarily prohibitive: the offset of an address in an object, a size, a reference counter and similar fields can still be overwritten. A reference counter is a number that tracks how many parts of a program are using an object, so that memory can be freed when the last user is done; corrupting it can cause memory to be freed while still in use. What is possible depends on which objects can be sprayed into the kernel pool where the victim headerBuff is allocated. Spraying means deliberately creating many objects of a chosen type so that they fill memory and end up next to the target, giving the attacker some control over what gets corrupted.
Heap grooming research
The victim out-of-bounds buffer is allocated in the NetIoProtocolHeader2 pool. The first steps in heap grooming research are to examine which types of objects are allocated in that pool, what they contain, how they are used, and how they are allocated and freed. Heap grooming is the craft of arranging memory allocations so that a victim object lands next to something worth corrupting, the way you might rearrange boxes in a cupboard so the fragile one sits next to the one you want to reach. That shows how the write primitive can be used to obtain a leak or build a stronger primitive. A leak means getting the program to reveal a memory address or secret value it should keep hidden, which is often the first step towards a full exploit. The work is not necessarily restricted to NetIoProtocolHeader2, but because the position of the victim out-of-bounds buffer cannot be predicted and the addresses of surrounding pools are randomised, targeting other pools looks challenging. Randomised addresses are a deliberate defence: if the attacker cannot predict where things are in memory, corrupting a specific target becomes much harder.
Impact
The primitive is a one-byte out-of-bounds write in kernel pool memory, with an attacker-controlled offset of the form 4n - 1 and an attacker-controlled value. Kernel memory is the memory used by the core of the operating system, which has far more power than ordinary applications, so corrupting it is especially serious. In the demonstrated case it produces a Denial of Service: a single corrupted byte usually does not crash the target, but a reliable crash follows from adding headers inside the fragmented message or from repeatedly pinging the target after corrupting a large portion of the pool. The advisory describes the potential as remote code execution on a Windows node where IPsec is enabled, reachable by an unauthenticated attacker sending a specially crafted IPv6 packet. The RCE step is not demonstrated here; the article sets out the primitives and the next steps needed to get there, with the 4-byte alignment constraint meaning full pointers cannot be overwritten.
Fix
Microsoft's patch changes two functions. In Ipv6pReassembleDatagram it adds a bounds check comparing Reassembly->nextheader_offset against the length of the header buffer, and routes a failed comparison to IppDeleteFromReassemblySet as error handling. In IppReceiveEsp it adds a check on the ESP Next Header field: values 0, 0x2B and 0x2C (IPv6 Hop-by-Hop Option, Routing Header for IPv6 and Fragment Header for IPv6) cause IppDiscardReceivedPackets to be called with the error code set to STATUS_DATA_NOT_ACCEPTED, because such headers inside an ESP payload violate RFC 4303.
Variants to test
- Diff other
tcpip.sysfunctions that handle extension headers after decryption. A positive result is a function whose post-patch graph gains a bounds check or a discard path that the pre-patch version lacks. - Look for other places where an offset into a reassembly buffer is derived from a field whose position depends on the encapsulation. A positive result is an offset that can be made larger than the buffer by choosing a different encapsulation, as with ESP here.
- Check other Next Header values that the patched
IppReceiveEspdoes not reject. A positive result is a header type that still reaches reassembly from inside an ESP payload. - Test whether other pools besides
NetIoProtocolHeader2can be reached by the same write. A positive result is a controllable object allocated adjacent to the victim buffer whose offset, size or reference count can be corrupted with a 4n - 1 offset. - Re-examine published analyses of the same bug against the binaries. A positive result is a claim in a third-party write-up that the decompiled code contradicts, as happened with the claim that WFP cannot manage secret keys.
Glossary
- API: a documented set of functions one program offers so other programs can ask it to do things.
- Binary: the compiled file a computer actually runs, made of machine instructions rather than readable source code.
- BinDiff: a tool that compares two binaries function by function and scores their similarity.
- BinExport: a file format, produced by an extension for Ghidra, that BinDiff can read.
- Bounds check: code that verifies a number is within an allowed range before using it.
- Breakpoint: a debugging feature that pauses a program at a chosen point so you can inspect it.
- Buffer: a reserved chunk of memory used to hold data temporarily.
- Buffer overflow: writing more data into a buffer than it can hold, so the extra data spills into neighbouring memory.
- CVE: a public catalogue number for a known security vulnerability.
- Decompiler: a tool that turns low-level machine instructions back into something resembling high-level source code.
- Denial of Service (DoS): an attack that makes a machine or service stop working, without necessarily giving the attacker control of it.
- ESP (Encapsulating Security Payload): the IPsec protocol that encrypts a packet's contents.
- Function: a named block of code that performs one job.
- Function graph: a picture of a function's logic, with boxes for chunks of instructions and arrows for the flow between them.
- Ghidra: a free reverse engineering tool that disassembles and decompiles binaries.
- Header: the labelled section at the start of a packet that tells the receiver what kind of data follows.
- Heap grooming: arranging memory allocations so that a victim object lands next to something worth corrupting.
- Hexadecimal: a base-16 way of writing numbers, prefixed with
0x, commonly used for bytes. - ICMPv6 Echo request: the IPv6 equivalent of a ping, a small message asking another machine to reply.
- ICV (Integrity Check Value): a value that lets the receiver verify a packet was not tampered with in transit.
- IKE (Internet Key Exchange): a protocol for automatically negotiating the keys used by IPsec.
- IPsec: a group of protocols used together to set up encrypted connections, commonly for VPNs.
- IPv6: the newer version of the internet's addressing system for routing traffic between machines.
- Kernel memory: memory used by the core of the operating system, which has more power than ordinary applications.
- Leak: getting a program to reveal a memory address or secret value it should keep hidden.
- MTU (Maximum Transmission Unit): the largest packet a network link will carry in one piece.
- Offset: a distance from the start of something, measured in bytes.
- Out-of-bounds write: writing to a memory location outside the area the code was supposed to touch.
- Parse: to read through structured data and interpret each field.
- Patch diffing: comparing the binary before and after a security fix to find what changed and therefore where the bug is.
- pdb (Program Database): a companion file listing the names Microsoft gave to functions and variables in a binary.
- Pointer: a memory address stored in a variable, telling the program where to find something.
- Pool: a region of memory the operating system hands out in small pieces to whoever asks.
- Primitive: a small, reliable capability an attacker has, used as a building block for a bigger attack.
- Proof of concept (PoC): a small piece of code demonstrating that a bug actually works.
- Reference counter: a number tracking how many parts of a program are using an object, so memory can be freed when the last user is done.
- Register: a tiny, very fast storage slot inside the processor.
- Remote code execution (RCE): an attack that makes the victim's computer run code of the attacker's choosing.
- Reverse engineering: reading a binary to work out what it does.
- RFC: a document published by the Internet Engineering Task Force defining how an internet protocol must behave.
- Scapy: a free Python library for constructing and sending network packets by describing their layers in code.
- Security association: a shared state, primarily cryptographic keys and parameters, maintained between two endpoints to secure traffic between them.
- Spraying: deliberately creating many objects of a chosen type so they fill memory and end up next to a target.
- Static analysis: studying code without running it.
- Structure: a bundle of related values stored together, like a form with several fields.
- Symmetric key: one single secret used both to encrypt and to decrypt.
- TCP / UDP: the two most common protocols carried inside IP packets; TCP for reliable connections, UDP for simpler messages.
- Unsigned integer: a whole number that cannot be negative.
- VPN: a private encrypted tunnel that connects networks or remote workers over the public internet.
- WFP (Windows Filtering Platform): the part of Windows that lets software inspect and control network traffic.
- Winbindex: a website that keeps copies of Windows system files as shipped in each update.
- WinAPI: an API provided by Windows.