P³-Shellcode Loader 是一个加载器,它实现了一种代码注入技术,该技术利用 Process Parameters 结构作为执行和准备位置。
P³-Shellcode Loader 是一个加载器,它实现了一种代码注入技术,该技术利用 Process Parameters 结构作为执行和准备位置。
Authors: Max Hirschberger & Ogulcan Ugur
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].
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:
OpenProcess / NtOpenProcess or CreateProcess / NtCreateProcess).VirtualAllocEx or NtAllocateVirtualMemory).WriteProcessMemory / NtWriteVirtualMemory).VirtualProtectEx / NtProtectVirtualMemory).CreateRemoteThread bzw. NtCreateThreadEx).Additional injection techniques include but are not limited to the following:
NtSetContextThread)NtQueueApcThread)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.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.
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
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.
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
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:
lpReserved field of the lpStartupInfo parameter that will be copied to the ShellInfo variable in the PEBlpEnvironment parameter with the CREATE_UNICODE_ENVIRONMENT flaglpCommandLine parameter of CreateProcessWFigure 3: Selection of Poisonable Parameter
Figure 4: Selection of the Targeted Application Executable
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.
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.
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.
RTL_USER_PROCESS_PARAMETERS parameters = { 0 };
status = winapi.NtReadVirtualMemoryEx(
pi.hProcess, // Handle of target process
pebLocal.ProcessParameters, // ProcessParameters address in target
¶meters, // 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.
After transferring the code, the process's execution still needs to be d
暂无开放 Issues,或尚未同步最近议题。