If you have been watching the Microsoft security bulletins lately, then you’ve likely noticed yesterday’s bulletin, MS08-067. This is a particularly nasty bug, as it doesn’t require authentication to exploit in the default configuration for Windows Server 2003 and earlier systems (assuming that an attacker can talk over port 139 or port 445 to your box).
The usual mitigation for this particular vulnerability is to block off TCP/139 (NetBIOS) and TCP/445 (Direct hosted SMB), thus cutting off remote access to the srvsvc pipe, a prerequisite for exploiting the vulnerability in question. In my case, however, I had a box that I really didn’t want to reboot immediately. In addition, for the box in question, I did not wish to leave SMB blocked off remotely.
Given that I didn’t want to assume that there’d be no possible way for an untrusted user to be able to establish a TCP/139 or TCP/445, this left me with limited options; either I could simply hope that there wasn’t a chance for the box to get compromised before I had a chance for it to be convenient to reboot, or I could see if I could come up with some form of alternative mitigation on my own. After all, a debugger is the software development equivalent of a swiss army knife and duct-tape; I figured that it would be worth a try seeing if I could cobble together some sort of mitigation by manually patching the vulnerable netapi32.dll. To do this, however, it would be necessary to gain enough information about the flaw in question in order to discern what the fix was, in the hope of creating some form of alternative countermeasure for the vulnerability.
The first stop for gaining more information about the bug in question would be the Microsoft advisory. As usual, however, the bulletin released for the MS08-067 issue was lacking in sufficiently detailed technical information as required to fully understand the flaw in question to the degree necessary down to the level of what functions were patched, aside from the fact that the vulnerability resided somewhere in netapi32.dll (the Microsoft rationale behind this policy is that providing that level of technical detail would simply aid the creation of exploits). However, as Pusscat presented at Blue Hat Fall ’07, reverse engineering most present-day Microsoft security patches is not particularly insurmountable.
The usual approach to the patch reverse engineering process is to use a program called bindiff (an IDA plugin) that analyzes two binaries in order to discover the differences between the two. In my case, however, I didn’t have a copy of bindiff handy (it’s fairly pricey). Fortunately (or unfortunately, depending on your persuasion), there already existed a public exploit for this bug, as well as some limited public information from persons who had already reverse engineered the patch to a degree. To this end, I had a particular function in the affected module (netapi32!NetpwPathCanonicalize) which I knew was related to the vulnerability in some form.
At this point, I brought up a copy of the unpatched netapi32.dll in IDA, as well as a patched copy of netapi32.dll, then started looking through and comparing disassembly one page at a time until an interesting difference popped up in a subfunction of netapi32!NetpwPathCanonicalize:
Unpatched code:
.text:000007FF7737AF90 movzx eax, word ptr [rcx] .text:000007FF7737AF93 xor r10d, r10d .text:000007FF7737AF96 xor r9d, r9d .text:000007FF7737AF99 cmp ax, 5Ch .text:000007FF7737AF9D mov r8, rcx .text:000007FF7737AFA0 jz loc_7FF7737515E
Patched code:
.text:000007FF7737AFA0 mov r8, rcx .text:000007FF7737AFA3 xor eax, eax .text:000007FF7737AFA5 mov [rsp+arg_10], rbx .text:000007FF7737AFAA mov [rsp+arg_18], rdi .text:000007FF7737AFAF jmp loc_7FF7738E5D6 .text:000007FF7738E5D6 mov rcx, 0FFFFFFFFFFFFFFFFh .text:000007FF7738E5E0 mov rdi, r8 .text:000007FF7738E5E3 repne scasw .text:000007FF7738E5E6 movzx eax, word ptr [r8] .text:000007FF7738E5EA xor r11d, r11d .text:000007FF7738E5ED not rcx .text:000007FF7738E5F0 xor r10d, r10d .text:000007FF7738E5F3 dec rcx .text:000007FF7738E5F6 cmp ax, 5Ch .text:000007FF7738E5FA lea rbx, [r8+rcx*2+2] .text:000007FF7738E5FF jnz loc_7FF7737AFB4
Now, without even really understanding what’s going on here on the function as a whole, it’s pretty obvious that here’s where (at least one) modification is being made; the new code involved the addition of an inline wcslen call. Typically, security fixes for buffer overrun conditions involve the creation of previously missing boundary checks, so a new call to a string-length function such as wcslen is a fairly reliable indicator that one’s found the site of the fix for the the vulnerability in question.
(The repne scasw instruction set scans a memory region two-bytes at a time until a particular value (in rax) is reached, or the maximum count (in rcx, typically initialized to (size_t)-1) is reached. Since we’re scanning two bytes at a time, and we’ve initialized rax to zero, we’re looking for an 0x0000 value in a string of two-byte quantities; in other words, an array of WCHARs (or a null terminated Unicode string). The resultant value on rcx after executing the repne scasw can be used to derive the length of the string, as it will have been decremented based on the number of WCHARs encountered before the 0x0000 WCHAR.)
My initial plan was, assuming that the fix was trivial, to simply perform a small opcode patch on the unpatched version of netapi32.dll in the Server service process on the box in question. In this particular instance, however, there were a number of other changes throughout the patched function that made use of the additional length check. As a result, a small opcode patch wasn’t ideal, as large parts of the function would need to be rewritten to take advantage of the extra length check.
Thus, plan B evolved, wherein the Microsoft-supplied patched version of netapi32.dll would be injected into an already-running Server service process. From there, the plan was to detour buggy_netapi32!NetpwPathCanonicalize to fixed_netapi32!NetpwPathCanonicalize.
As it turns out, netapi32!NetpwPathCanonicalize and all of its subfunctions are stateless with respect to global netapi32 variables (aside from the /GS cookie), which made this approach feasible. If the call tree involved a dependancy on netapi32 global state, then simply detouring the entire call tree wouldn’t have been a valid option, as the globals in the fixed netapi32.dll would be distinct from the globals in the buggy netapi32.dll.
This approach also makes the assumption that the only fixes made for the patch were in netapi32!NetpwPathCanonicalize and its call tree; as far as I know, this is the case, but this method is (of course) completely unsupported by Microsoft. Furthermore, as x64 binaries are built without hotpatch nop stubs at their prologue, the possibility for atomic patching in of a detour appeared to be out, so this approach has a chance of failing in the (unlikely) scenario where the first few instructions of netapi32!NetpwPathCanonicalize were being executed at the time of the detour.
Nonetheless, the worst case scenario would be that the box went down, in which case I’d be rebooting now instead of later. As the whole point of this exercise was to try and delay rebooting the system in question, I decided that this was an acceptable risk in my scenario, and elected to proceed. For the first step, I needed a program to inject a DLL into the target process (SDbgExt does not support !loaddll on 64-bit targets, sadly). The program that I came up with is certainly quick’n’dirty, as it fudges the thread start routine in terms of using kernel32!LoadLibraryA as the initial start address (which is a close enough analogue to LPTHREAD_START_ROUTINE to work), but it does the trick in this particular case.
The next step was to actually load the app into the svchost instance containing the Server service instance. To determine which svchost process this happens to be, one can use “tasklist /svc” from a cmd.exe console, which provides a nice formatted view of which services are hosted in which processes:
C:\WINDOWS\ms08-067-hotpatch>tasklist /svc
[…]
svchost.exe 840 AeLookupSvc, AudioSrv, BITS, Browser,
CryptSvc, dmserver, EventSystem, helpsvc,
HidServ, IAS, lanmanserver,
[…]
That being done, the next step was to inject the DLL into the process. Unfortunately, the default security descriptor on svchost.exe instances doesn’t allow Administrators the access required to inject a thread. One way to solve this problem would have been to write the code to enable the debug privilege in the injector app, but I elected to simply use the age-old trick of using the scheduler service (at.exe) to launch the program in question as LocalSystem (this, naturally, requires that you already be an administrator in order to succeed):
C:\WINDOWS\ms08-067-hotpatch>at 21:32 C:\windows\ms08-067-hotpatch\testapp.exe 840 C:\windows\ms08-067-hotpatch\netapi32.dll
Added a new job with job ID = 1
(21:32 was one minute from the time when I entered that command, minutes being the minimum granularity for the scheduler service.)
Roughly one minute later, the debugger (attached to the appropriate svchost instance) confirmed that the patched DLL was loaded successfully:
ModLoad: 00000000`04ff0000 00000000`05089000 C:\windows\ms08-067-hotpatch\netapi32.dll
Stepping back for a moment, attaching WinDbg to an svchost instance containing services in the symbol server lookup code path is risky business, as you can easily deadlock the debugger. Proceed with care!
Now that the patched netapi32.dll was loaded, it was time to detour the old netapi32.dll to refer to the new netapi32.dll. Unfortunately, WinDbg doesn’t support assembling amd64 instructions very well (64-bit addresses and references to the extended registers don’t work properly), so I had to use a separate assembler (HIEW, [Hacker’s vIEW]) and manually patch in the opcode bytes for the detour sequence (mov rax, <absolute addresss> ; jmp rax):
0:074> eb NETAPI32!NetpwPathCanonicalize 48 C7 C0 40 AD FF 04 FF E0 0:074> u NETAPI32!NetpwPathCanonicalize NETAPI32!NetpwPathCanonicalize: 000007ff`7737ad30 48c7c040adff04 mov rax,offset netapi32_4ff0000!NetpwPathCanonicalize 000007ff`7737ad37 ffe0 jmp rax 0:076> bp NETAPI32!NetpwPathCanonicalize 0:076> g
This said and done, all that remained was to set a breakpoint on netapi32!NetpwPathCanonicalize and give the proof of concept exploit a try against my hotpatched system (it survived). Mission accomplished!
The obvious disclaimer: This whole procedure is a complete hack, and not recommended for production use, for reasons that should be relatively obvious. Additionally, MS08-067 did not come built as officially “hotpatch-enabled” (i.e. using the Microsoft supported hotpatch mechanism); “hotpatch-enabled” patches do not entail such a sequence of hacks in the deployment process.
(Thanks to hdm for double-checking some assumptions for me.)
Tags: hack, hotpatching, ms08-067, windbg
Interesting look of MS08-067. I did also get some trouble with Windbg assembler for x64. It did not recognize 64 bits registers so I had to change code using ‘eb’ as you’ve done. Hope they will update it one day.
Btw, Nicolas Pouvesle released a free bindiff like plugin for IDA known as PatchDiff2 (http://cgi.tenablesecurity.com/tenable/patchdiff.php). For this specific patch, you see only two matched functions which are the vulnerable function and it’s caller (due to argument optimization).
Some example on MS08-067 diff:
http://mxatone.free.fr/patchdiff_MS08-067_1.jpg
http://mxatone.free.fr/patchdiff_MS08-067_2.jpg
I’m not sure why this one was not delivered as a hotpatch. AMD64 actually requires all functions to be hotpatchable. They must begin with an opcode bigger than two bytes (so sometimes you get a redundant REX prefix on pushreg instructions at the beginning of a function). Perhaps it takes too long to generate the hotpatch and test it properly relative to how quickly this bug was turned around from discovery to patch.
[…] Unfassbar… […]
You mentioned blocking inbound SMB on both TCP ports 139 and 445 “thus cutting off remote access to the srvsvc pipe, a prerequisite for exploiting the vulnerability”.
While your proposed remedy accomplishes the goal, I think it worthwhile to point out that the srvsvc pipe is not necessarily required to access the vulnerable function. Because of what appear to be well-known characteristics of Microsoft’s RPC implementation, srvsvc functionality is available over other RPC endpoints registered in-process. This is noteworthy because one endpoint is the BROWSER named pipe, which has a more permissive DACL than than the SRVSVC named pipe. As an example of the practical application of this, see the public Metasploit module that exploits this vulnerability.
Awesome post. No need to push off BinDiff because of price…
“For all you hardcore hax0rs, Skywing has put together a detailed post on using the AT service and hiew to to inject the updated code into svchost.exe and manually hot patch the running vulnerable service, avoiding a reboot. Fun reading, but not recommended.
In it he claims that he wasn’t able to use BinDiff to identify the patched code, but for those RE’s with a lack of funding, there is a limited trial version of v2.0 that worked great on netapi32 and helped id this problem as a stack overflow within a couple minutes of Microsoft’s patch release. You can see for yourself what a great tool Bindiff really is — google is your friend.”
http://blog.threatfire.com/2008/10/microsoft-is-releasing-out-of-cycle.html
Skywing, here is first ever humor book for debugging folks. Available for order on Amazon. I’ve been following your blog for quite long and so in one of the cartoons I’ve included you :) Hope you don’t mind :p
http://www.amazon.com/gp/product/1906717257?ie=UTF8&tag=idebug_in_windbg-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=1906717257
Is the bug exist in Windows Server 2008?
[…] Conficker worm (W32/Confick-A, W32/Confick-B, W32/Confick-C, W32/Confick-D), aka Downadup (W32.Downadup, W32.Downadup.B) is going around and is capable of spreading very fast on Windows XP/Vista machines. This worm exploits1 a remote code execution vulnerability in the Server service responsible for file/printer sharing functionalities. The vulnerability is apparently because of a buffer overrun bug in netapi32.dll call NetpwPathCanonicalize. […]
[…] reverse engineering most present-day Microsoft security patches is not particularly insurmountable. In detail Posted in Security | Leave a […]
[…] in mind that the update requires a reboot.For all you hardcore hax0rs, Skywing has put together a detailed post on using the AT service and hiew to to inject the updated code into svchost.exe and manually hot […]
.text:000007FF7738E5D6 mov rcx, 0FFFFFFFFFFFFFFFFh
.text:000007FF7738E5E0 mov rdi, r8
.text:000007FF7738E5E3 repne scasw
BTW, 65nm Core 2 has errata relating to executing REP SCAS/CMPS with values exceeding 0x100000000. Luckily, it is mentioned that Intel has a microcode update to fix this, which would be essential since it is code generated by a common compiler.