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

p3-loader

> 编程语言
Open source

P³-Shellcode Loader is a loader that implements a code injection technique which leverages the Process Parameters structure as an execution and staging location

192 stars0 likes0 views
WebsiteGitHub

About

P³-Shellcode Loader is a loader that implements a code injection technique which leverages the Process Parameters structure as an execution and staging location

P³-Shellcode Loader - Process Parameter Poisoning

Authors: Max Hirschberger & Ogulcan Ugur


Contents

  1. Introduction
  2. Typical Process Injection and Involved System APIs
  3. Technical Foundation of Required Windows Internals
    • 3.1 Process Creation API and Startup Parameters
    • 3.2 The Process Environment Block (PEB)
  4. Process Parameter Poisoning (P³)
    • 4.1 Starting a Process with a Poisoned Parameter
    • 4.2 Locating the Injected Data in the New Process
    • 4.3 Executing the Injected Code
    • 4.4 Implemented Payload Injections
  5. Passing Arbitrary Shellcode in a String
    • 5.1 Lower Level Helper Methods Used by the Shellcode Generator
    • 5.2 Implementation of Higher Level Operations
  6. Advantages of Detection Avoidance by this Technique
  7. Detection Approach
  8. Conclusion
  9. References

1. Introduction

P³-Shellcode Loader is a loader that implements a code injection technique which leverages the Process Parameters structure (Process Parameter Poisoning) as an execution and staging location for shellcode injection into remote processes, without triggering common detection mechanisms.

A similar concept was described by the security researcher modexp, who demonstrated that the arguments passed to the CreateProcess-API can be leveraged for this purpose [1].


2. Typical Process Injection and Involved System APIs

Attackers want to make their activities look less suspicious. With process injection, the attackers are able to perform their activities from a different process that is more trusted or expected to be performing the specific activity, thus lowering suspicion.

The following are the typical steps required to inject code into another process:

  1. The attacker searches for and opens a target process or starts a new process (via OpenProcess / NtOpenProcess or CreateProcess / NtCreateProcess).
  2. Memory for the malicious code is allocated in the target process (via VirtualAllocEx or NtAllocateVirtualMemory).
  3. The malicious code is written to the new allocation (via WriteProcessMemory / NtWriteVirtualMemory).
  4. Memory access protection is configured to allow executing the malicious code (via VirtualProtectEx / NtProtectVirtualMemory).
  5. A new thread is started in the target process that runs the malicious code (via CreateRemoteThread bzw. NtCreateThreadEx).

Additional injection techniques include but are not limited to the following:

  • Thread Hijacking: Instead of creating a new thread, an existing one is redirected (via NtSetContextThread)
  • Early-Bird APC-Injection: Utilizes Asynchronous Procedure Calls (APCs) to redirect the execution of an existing thread (via NtQueueApcThread)
  • Dirty Vanity: Abuses the Windows API RtlCreateProcessReflection, that implements process forking. In our testing, we observed that most EDRs focus on specific telemetry to detect process injection. EDRs primarily monitor the usage of WriteProcessMemory and VirtualAllocEx, as well as their underlying kernel system calls NtWriteVirtualMemory, NtAllocateVirtualMemory and NtAllocateVirtualMemoryEx.

3. Technical Foundation of Required Windows Internals

3.1 Process Creation API and Startup Parameters

Windows provides the API function CreateProcessW for creating new processes, shown in Listing 1. The first three of its parameters lpCommandLine, lpEnvironment and lpStartupInfo are relevant for the described injection technique, since they are used to transfer data to the new process.

…

Listing 1: Definition of CreateProcessW Windows API Function

The lpCommandLine parameter specifies the command line for the new process. It is limited to a maximum of 32,767 unicode characters, including the unicode null-terminator. For the unicode variant, it is necessary to provide a string that the function can write to. If a constant string is supplied, any write attempts made by the API function result in a memory access violation. If the value is NULL, the process's command line will be taken from the lpApplicationName parameter. If lpApplicationName is NULL, it has to be provided in the lpCommandLine field and is limited to MAX_PATH characters.

The lpEnvironment parameter provides a list of environment variables to the process. If the value is NULL, the environment of the creating process will be used. The list of environment variables consists of successive null-terminated strings of the format NAME=VALUE with another null-terminator at the end.

The lpStartupInfo parameter is a structure shown in Listing 2 with fields such as window station, desktop, standard input and output handles as well as fields that configure the main window of the new process. According to Microsoft documentation, the field lpReserved is reserved for internal use with no further documentation. Through analysis with the WinDbg debugger, it was possible to relate this parameter to the ShellInfo variable of the type UNICODE_STRING in the new process.

c
typedef struct _STARTUPINFOW {
    DWORD  cb;
    LPWSTR lpReserved;   // Copied to ShellInfo (UNICODE_STRING)
    LPWSTR lpDesktop;
    LPWSTR lpTitle;
    DWORD  dwX;
    DWORD  dwY;
    DWORD  dwXSize;
    // (...) additional fields
    WORD   wShowWindow;
    WORD   cbReserved2;
    LPBYTE lpReserved2;
    HANDLE hStdInput;
    // (...) additional fields
} STARTUPINFOW, *LPSTARTUPINFOW;

Listing 2: Layout of STARTUPINFOW data structure

3.2 The Process Environment Block (PEB)

At the creation of a new process, all supplied process parameters are written to the Process Environment Block (PEB). The PEB is a data structure that is present in all processes and unique to each process. The parameters can be accessed within the ProcessParameters member of the type RTL_USER_PROCESS_PARAMETERS. Besides the process parameters, this structure also includes further runtime information, such as a list of loaded modules. The structure of the PEB and relevant process parameters in the RTL_USER_PROCESS_PARAMETERS is shown in Listing 3 and Listing 4.

c
typedef struct _PEB
{
    BOOLEAN InheritedAddressSpace;
    BOOLEAN ReadImageFileExecOptions;
    BOOLEAN BeingDebugged;
    // (...) additional fields
    PVOID ImageBaseAddress;
    PPEB_LDR_DATA Ldr;
    PRTL_USER_PROCESS_PARAMETERS ProcessParameters; // Poisonable
    PVOID SubSystemData;
    PVOID ProcessHeap;
    PRTL_CRITICAL_SECTION FastPebLock;
    // (...) additional fields
} PEB, *PPEB;

Listing 3: Layout of PEB data structure

…

Listing 4: Layout of RTL_USER_PROCESS_PARAMETERS data structure

Figure 1 shows the ShellInfo parameter with the poison value supplied in the lpReserved field for the STARTUPINFOW structure. Furthermore, Figure 2 shows the controlled command line within the System Informer tool.

Figure 1: Poisoned ShellInfo Parameter

Figure 2: Poisoned Command Line


4. Process Parameter Poisoning (P³)

4.1 Starting a Process with a Poisoned Parameter

Since there are multiple parameters that may be used to copy the malicious code, the wrapper function in Listing 5 creates a process with the poison argument supplied to the chosen process parameter. When running the implemented injector, this choice can be made as shown in Figure 3. Furthermore, any value can be provided for the target application that will be used in lpApplication, with the user prompt shown in Figure 4.

…

Listing 5: Implementation of CreateProcessWithPoison

This function implements three distinct parameter choices:

  1. ShellInfo Injection: Places the poison in the lpReserved field of the lpStartupInfo parameter that will be copied to the ShellInfo variable in the PEB
  2. Environment Injection: Places the poison in the lpEnvironment parameter with the CREATE_UNICODE_ENVIRONMENT flag
  3. CommandLine Injection: Places the poison in the lpCommandLine parameter of CreateProcessW

Figure 3: Selection of Poisonable Parameter

Figure 4: Selection of the Targeted Application Executable

4.2 Locating the Injected Data in the New Process

After successful process creation, the injected data can be found via the PEB structure. The following three steps are required to locate the poison in the new process.

First up, the starting address of the PEB structure is determined by calling NtQueryInformationProcess. NtQueryInformationProcess retrieves the data structure PROCESS_BASIC_INFORMATION when called with the information class ProcessBasicInformation. And PROCESS_BASIC_INFORMATION contains the address of the PEB in the PebBaseAddress field. This step is shown in Listing 6.

cpp
WinApiResolver winapi = WinApiResolver::GetInstance();
PROCESS_BASIC_INFORMATION pbi = { 0 };
ULONG retLen;
winapi.NtQueryInformationProcess(
    pi.hProcess,             // Handle of the new process
    ProcessBasicInformation, // Query PROCESS_BASIC_INFORMATION
    &pbi,                    // Destination
    sizeof(pbi),             // Size of the destination
    &retLen                  // Resulting size of what was read
);

Listing 6: First Step of Locating the Injected Data

In the second step, the PEB structure is read by calling NtReadVirtualMemoryEx with the starting address of the structure that was retrieved in the first step. The implementation of the second step is shown in Listing 7.

cpp
PEB pebLocal = { 0 };
SIZE_T bytesRead;
NTSTATUS status = winapi.NtReadVirtualMemoryEx(
    pi.hProcess,        // Handle of the new process
    pbi.PebBaseAddress, // Starting address of the PEB
    &pebLocal,          // Destination / Local copy of the PEB
    sizeof(pebLocal),   // Size to be read
    &bytesRead,         // Resulting size of what was read
    0                   // Reserved parameter
);

Listing 7: Second Step of Locating the Injected Data

After reading the PEB, the field ProcessParameters contains the starting address of the RTL_USER_PROCESS_PARAMETERS structure in the new process. In the third step, this structure is also read from the new process. The pointers to the injected data are within this structure. The third step is shown in Listing 8.

cpp
RTL_USER_PROCESS_PARAMETERS parameters = { 0 };
status = winapi.NtReadVirtualMemoryEx(
    pi.hProcess,                // Handle of target process
    pebLocal.ProcessParameters, // ProcessParameters address in target
    &parameters,                // Output buffer / Local copy
    sizeof(parameters),         // Size to be read
    &bytesRead,                 // Resulting size of what was read
    0                           // Reserved parameter
);

Listing 8: Third Step of Locating the Injected Data

This approach only uses memory read APIs and no write or allocation API that the EDRs focus on. However, there is one limitation imposed by the parameters. Since these parameters are null-terminated strings, only shellcode with no null-terminator can be fully transferred. A solution to overcome this limitation is given in Section 5.

4.3 Executing the Injected Code

After transferring the code, the process's execution still needs to be d

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 推出的简洁高效系统语言