<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Pinvoke — CraftedSignal Threat Feed</title><link>https://feed.craftedsignal.io/tags/pinvoke/</link><description>Trending threats, MITRE ATT&amp;CK coverage, and detection metadata — refreshed continuously.</description><generator>Hugo</generator><language>en</language><managingEditor>hello@craftedsignal.io</managingEditor><webMaster>hello@craftedsignal.io</webMaster><lastBuildDate>Wed, 03 Jan 2024 18:23:00 +0000</lastBuildDate><atom:link href="https://feed.craftedsignal.io/tags/pinvoke/feed.xml" rel="self" type="application/rss+xml"/><item><title>PowerShell P/Invoke Process Injection API Chain Detection</title><link>https://feed.craftedsignal.io/briefs/2024-01-powershell-pinvoke-process-injection/</link><pubDate>Wed, 03 Jan 2024 18:23:00 +0000</pubDate><author>hello@craftedsignal.io</author><guid isPermaLink="true">https://feed.craftedsignal.io/briefs/2024-01-powershell-pinvoke-process-injection/</guid><description>This analytic detects PowerShell code that uses P/Invoke to call Windows API functions associated with process injection, such as VirtualAlloc, WriteProcessMemory, and CreateRemoteThread, indicating potential malicious activity.</description><content:encoded><![CDATA[<p>This detection identifies PowerShell scripts leveraging the P/Invoke (Platform Invoke) technology to perform process injection. P/Invoke allows managed code (like PowerShell) to call unmanaged functions exported from DLLs, including critical Windows API functions. Attackers use this to inject malicious code into legitimate processes for evasion and persistence. The detection focuses on identifying specific API chains commonly used in process injection techniques, such as allocating memory in a target process (VirtualAlloc), writing malicious code into the allocated memory (WriteProcessMemory), and executing the injected code (CreateRemoteThread). This activity is often associated with malware deployment, privilege escalation, and defense evasion. The detection logic is designed to identify these API chains either at the compile phase using Add-Type or during the execution phase, alerting on suspicious PowerShell behavior.</p>
<h2 id="attack-chain">Attack Chain</h2>
<ol>
<li>Attacker gains initial access to the system, potentially through phishing or exploiting a vulnerability.</li>
<li>PowerShell is invoked to execute a malicious script.</li>
<li>The PowerShell script uses Add-Type and DllImport to declare external functions from Windows DLLs, including kernel32.dll and ntdll.dll.</li>
<li>The script uses functions such as OpenProcess to gain a handle to a target process.</li>
<li>VirtualAllocEx is called to allocate memory within the target process.</li>
<li>WriteProcessMemory is used to write malicious code into the allocated memory region of the target process.</li>
<li>CreateRemoteThread is called to create a new thread within the target process, pointing to the injected code.</li>
<li>The injected code executes within the context of the target process, achieving code execution and potential privilege escalation.</li>
</ol>
<h2 id="impact">Impact</h2>
<p>Successful process injection allows attackers to execute arbitrary code within the context of a trusted process, bypassing security controls and potentially gaining elevated privileges. This can lead to data theft, system compromise, or further propagation within the network. The use of PowerShell and P/Invoke makes detection more challenging, as the activity can blend in with legitimate system administration tasks. A successful attack could lead to the deployment of a VIP Keylogger or other malware, as noted in the provided references.</p>
<h2 id="recommendation">Recommendation</h2>
<ul>
<li>Enable PowerShell Script Block Logging (Event ID 4104) to provide the necessary data for detection (data_source).</li>
<li>Deploy the Sigma rule <code>PowerShell PInvoke Process Injection</code> to your SIEM and tune the rule to your environment (rules).</li>
<li>Investigate any alerts generated by the Sigma rule, focusing on the specific API chains identified in the <code>detection</code> section of the rule.</li>
<li>Review PowerShell execution policies and restrict the execution of unsigned scripts to reduce the attack surface.</li>
</ul>
]]></content:encoded><category domain="severity">high</category><category domain="type">advisory</category><category>process-injection</category><category>powershell</category><category>pinvoke</category><category>defense-evasion</category></item><item><title>PowerShell P/Invoke API Chain for Process Injection</title><link>https://feed.craftedsignal.io/briefs/2024-01-powershell-pinvoke-injection/</link><pubDate>Wed, 03 Jan 2024 12:00:00 +0000</pubDate><author>hello@craftedsignal.io</author><guid isPermaLink="true">https://feed.craftedsignal.io/briefs/2024-01-powershell-pinvoke-injection/</guid><description>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.</description><content:encoded><![CDATA[<p>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.</p>
<h2 id="attack-chain">Attack Chain</h2>
<ol>
<li>The attacker executes a PowerShell script containing malicious code designed for process injection.</li>
<li>The script uses <code>Add-Type -TypeDefinition</code> to define a .NET class inline, embedding C# source code that includes <code>[DllImport]</code> declarations for Windows API functions.</li>
<li>The <code>DllImport</code> attribute specifies the native DLL (e.g., kernel32.dll, ntdll.dll) and the function name to import.</li>
<li>The script declares external functions like <code>VirtualAlloc</code>, <code>WriteProcessMemory</code>, <code>CreateRemoteThread</code>, <code>NtCreateSection</code>, and <code>NtMapViewOfSection</code> using <code>extern &lt;ReturnType&gt; &lt;FunctionName&gt;</code>.</li>
<li>The script uses static method invocation (e.g., <code>[IntPtr]::Zero</code>, <code>[Marshal]::Copy</code>) to call the declared functions.</li>
<li>The script allocates memory in the target process using <code>VirtualAllocEx</code> or <code>NtAllocateVirtualMemory</code>.</li>
<li>The malicious code (shellcode or DLL) is written to the allocated memory using <code>WriteProcessMemory</code>.</li>
<li>A new thread is created in the target process to execute the injected code using <code>CreateRemoteThread</code> or <code>RtlCreateUserThread</code>. Alternatively, APC injection uses <code>QueueUserAPC</code> to queue an Asynchronous Procedure Call in the target process.</li>
</ol>
<h2 id="impact">Impact</h2>
<p>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.</p>
<h2 id="recommendation">Recommendation</h2>
<ul>
<li>Enable PowerShell script block logging (Event ID 4104) to capture the necessary data for detection.</li>
<li>Deploy the provided Sigma rules to your SIEM to detect malicious PowerShell scripts using P/Invoke for process injection.</li>
<li>Investigate any alerts generated by the Sigma rules, focusing on processes that exhibit suspicious API call patterns.</li>
<li>Review and tune the Sigma rules based on your environment to minimize false positives and ensure accurate detection.</li>
</ul>
]]></content:encoded><category domain="severity">high</category><category domain="type">advisory</category><category>process-injection</category><category>powershell</category><category>pinvoke</category></item></channel></rss>