PowerShell P/Invoke API Chain for Process Injection
This brief details detection of PowerShell scripts leveraging P/Invoke API calls to perform process injection, covering techniques like self-injection, remote thread injection, APC injection, thread-context hijacking, process hollowing, section-map injection, reflective DLL loading, and DLL injection.
This brief focuses on the detection of PowerShell scripts utilizing Platform Invoke (P/Invoke) to perform process injection. P/Invoke allows managed code (PowerShell) to call native, unmanaged code (Windows API functions). Adversaries leverage this capability to inject malicious code into other processes, bypassing traditional defenses. This activity is identified through PowerShell script block logging (Event ID 4104). The detection strategy covers both the compile phase (detecting inline .NET class definitions with DllImport declarations) and the execution phase (detecting static method invocation patterns using ::MethodName syntax with execution context indicators). This ensures broad coverage, even when pre-compiled assemblies are loaded. The techniques detected cover a wide range of process injection methods, increasing the likelihood of detection against various attack vectors.
Attack Chain
- The attacker executes a PowerShell script containing malicious code designed for process injection.
- The script uses
Add-Type -TypeDefinitionto define a .NET class inline, embedding C# source code that includes[DllImport]declarations for Windows API functions. - The
DllImportattribute specifies the native DLL (e.g., kernel32.dll, ntdll.dll) and the function name to import. - The script declares external functions like
VirtualAlloc,WriteProcessMemory,CreateRemoteThread,NtCreateSection, andNtMapViewOfSectionusingextern <ReturnType> <FunctionName>. - The script uses static method invocation (e.g.,
[IntPtr]::Zero,[Marshal]::Copy) to call the declared functions. - The script allocates memory in the target process using
VirtualAllocExorNtAllocateVirtualMemory. - The malicious code (shellcode or DLL) is written to the allocated memory using
WriteProcessMemory. - A new thread is created in the target process to execute the injected code using
CreateRemoteThreadorRtlCreateUserThread. Alternatively, APC injection usesQueueUserAPCto queue an Asynchronous Procedure Call in the target process.
Impact
Successful process injection allows attackers to execute arbitrary code within the context of a legitimate process. This can lead to privilege escalation, credential theft, and persistence. Process injection can also be used to bypass security software and gain unauthorized access to sensitive data. This technique has been observed in malware campaigns associated with VIP Keylogger and similar threats, leading to data exfiltration and system compromise.
Recommendation
- Enable PowerShell script block logging (Event ID 4104) to capture the necessary data for detection.
- Deploy the provided Sigma rules to your SIEM to detect malicious PowerShell scripts using P/Invoke for process injection.
- Investigate any alerts generated by the Sigma rules, focusing on processes that exhibit suspicious API call patterns.
- Review and tune the Sigma rules based on your environment to minimize false positives and ensure accurate detection.
Detection coverage 3
PowerShell Add-Type DllImport Definition
mediumDetects PowerShell scripts that use Add-Type to define .NET classes with DllImport attributes, indicating potential P/Invoke usage for process injection.
PowerShell PInvoke API Call Sequence
highDetects PowerShell scripts that invoke a sequence of API calls commonly used for process injection, such as VirtualAlloc, WriteProcessMemory, and CreateRemoteThread.
PowerShell Static Method Invocation with IntPtr Zero
mediumDetects PowerShell scripts utilizing static method invocation patterns with IntPtr::Zero, often associated with P/Invoke and process injection techniques.
Detection queries are kept inside the platform. Get full rules →