Malware Evasion through Injection pt1
Malware use Injection as a Common Evasion Tactic as it allows to hide and execute Malicious Code inside legitimate processes or inside malware's own process hence evading common AV detection. Injection also allows malware to gain access to other processes, e.g Banking Trojans gaining access to browsers to steal credentials, Or Malware gaining access to crucial Windows Services for Persistence.
There are a number of ways Malwares use to inject itself or inject a Malicious Payload in other Processes, this post will only focus on Memory | Process Manipulation where a Sample directly injects an executable into memory rather than loading it as a DLL as in the old DLL Injection technique, So the Sample will have it's Malicious Payload Packed inside itself rather than loading it from disk. which of course is more stealthier than regular DLL Injection which is not effective anymore. This is mostly called Dropping a Malicious Payload, as the binary used to perform this is called a Dropper. Droppers must be stealthy as much as possible and in some cases they delete themselves after Dropping their accomplices.
Self-Injection
Commonly used in Packers, where a packed executable has a region of memory containing the Code responsible for unpacking the executable to its actual/real form, This Code is called The Unpacking Stub, the Unpacking stub is responsible for decrypting Shellcode that is in turn responsible for decrypting/unpacking not necessarily the final payload and overwriting the executable with it.
For a Closer look into Packers, Check Malware Unpacking Episode in the REM-Essentials Series.
=> Spotting Self-Injection
This is fairly straight-forward, The packed binary will allocate a region of memory in it's own process, first to write the Unpacking Stub which will allocate another regions of memory, and by watching these allocated regions of memory, at some point an executable will be written into it. Next the executable will either call
the Unpacked Payload's Original Entry Point, or jmp
to it.
Helpful APIs:
Practical Example for Self-Injection: Unpacking Dridex
PE-Injection
PE-Injection Involves Injecting the Malicious Payload into a Remote Process, there are Multiple ways to achieve this with different levels of Stealth. Almost all Straightforward PE-Injection Techniques are now detectable by Anti-Virtus Scanners, yet as we will encounter later on, Malware can make a Mix of these techniques to make it harder to detect it injecting into legitimate binaries, they could use lower-level NTDLL
calls or twisting a Legitimate Windows Development Components into their own use.
We will discuss these three main PE-Injection Techniques:
-> Regular PE-Injection with WriteProcessMemory
.
-> Process Hollowing with NtMapViewOfSection
.
-> Process Dopple-ganging with CreateFileTranscatedW
.
=> PE-Injection w/ WriteProcessMemory
WriteProcessMemory
The idea behind a regular PE Injection is to Simply allocate a region of memory in a remote process, write an Executable or Shellcode in this allocated region of Memory and Continue Executing from there. This technique is very easy to spot by AV as it involves direct calls to WriteProcessMemory
. This Technique is very similar to DLL Injection which we discuss in another post but instead of loading the Malicious DLL, the sample will be writing the Malicious Executable directly into Memory.
=> Spotting PE-Injection
The Sample might start with Spawning another process could be a legitimate process or even it's own process as a child in a suspended state, then it will allocate a region of Memory with the Size of the Malicious Executable or Size of Shellcode then it will write it's malicious code into the allocated region of memory, if the Malicious Code is an Executable, the Sample has to Map it into Memory i.e. replicate the functions of the Windows Loader, Then the Packed Sample will have to Overwrite/Patch the entry point of the Remote Process, Setup the Process/ Thread Objects' Parameters to execute either the Injected Shellcode or the Malicious Executable.
=> Implementing Regular PE-Injection:
Helpful APIs
Practical Example: Unpacking Parallax
=> Process Hollowing w/ NtUnMapViewOfSection
:
NtUnMapViewOfSection
:Process Hollowing is meant to be more stealthier than a regular use of WriteProcessMemory
, Since it uses low-level NTDLL calls to perform the Injection into the Remote Process. After spawning the target process, the Sample will query the binary to figure out it's offsets, then it will deallocate
/unmap
Memory Blocks inside the target process that it literally Hollows the binary so it has a space to write it's Malicious Code into, then it will allocate regions of memory inside the Hollowed binary with the size of the Malicious Payload either an Executable or ShellCode and with low level calls to NtWriteVirtualMemory
it will write the Malicious Payload into the reallocated regions of memory. With an Executable written, this region of memory must be mapped correctly in order to execute, so again the Sample will emulate the Windows Loader and update the PEB
with the Patched entry point. As for an Analyst looking for a regular WriteProcessMemory
implementation, it would be easy to be tricked.
=> Spotting Process Hollowing:
Although this Technique meant to be more stealthier than regular PE-Injection it's still very Noisy and easy to detect with AVs. The Sample starts with spawning the target process in a suspended state, then it queries the process's memory blocks to find offsets, the Sample will call NtUmapViewOfSection
over the legitimate binary's memory block to wipe it out, then a call to VirtualAllocEx
with the size of the Malicious Payload to reallocate Memory into the Target Process's Memory Space, then the Malicious Payload is written with NtWriteVirtualMemory
that is also used to redirect the entry point of the target process, and we end up with a Trojaned Windows Binary.
Helpful APIs:
NtUnMapViewOfSection() / NtMapViewOfSection() : Functions to Map/Unmap sepcified part of Section into/off Process Memory.
NtWriteVirtualMemory() : Low-Level API to write data into a speficied process memory.
VirtualAllocEx()
Practical Example: Unpacking SmokeLoader
=> Process Dopple-ganging w/ CreateFileTransactedW
CreateFileTransactedW
Process Doppleganging is a Process Injection Method that was Published by Security Researchers in BlackHat conference, this techniques elevates the use of NTFS Transactions to inject a Malicious Binary into a Remote Process without ever touching the disk, so it's a Fileless injection technique that was Undetectable back then when Anti-Virus Scanners didn't know how to catch it. It is not Pretty Common in the World of Malware to use Process Doppleganging, but one of the Interesting Cases was the Dropper for The Osiris Banking Trojan which is the Practical Example for this Injection Method. The Malware makes use of Creating a Transaction in Memory to write a file Malicious File into_,_ in this case and Out of the Transaction, a Section is created in the Process Memory and a Process is Spawned off of the Created Section. For Legitimate Windows Development, Applications use NTFS Transactions to make changes in the File System without breaking the Integrity of the Data, So it Increases the Application Reliability by protecting the data on disk from unexpected errors, Unless the changes made in the Transaction is Committed, no changes will be witnessed on Disk.
Typical NTFS Transaction Implementation
For Malware the goal is to be stealthy, so a clear difference in the API Implementation to create a Transaction is noted, No Commits will be made, but rather a Rollback to wipe any changes and not leave a detectable Malicious File on disk, So the Malicious Binary only exists in Memory with the Illusion that it was executed from a non-existent File/ non-executable File or even a total innocent File.
=> Malicious Implementation of NTFS-Transaction
=> Process Doppleganging/ Process Hollowing Hybrid:
This Typical Process Doppleganging technique is not really common in the wild, but for our Practical Example, The Dropper for the Osiris Banking Trojan used a Hybrid of Process Hollowing and Process Doppleganging, in which the Sample created a non exe file inside a transaction and injected the Malicious Payload inside it, with a Section made off of the Transacted File, Osiris used Process Hollowing to Map that Section inside a legitimate windows process that is spawned as a remote process, The Dropper then sets up the process parameters to start executing the thread containing The Malicious Payload.
=> Spotting Process Doppleganging
The existence of a TmTx/TxF is enough, but usually the sample will do a good job in using unhooked APIs as in Osiris Sample where the Dropper mapped it's own copies of NTDLL
to evade AV hooks, So eventually tracing into the execution would be the final resort to identify custom implementations of important functions like CreateFileTransactedW
.
Helpful APIs:
NtCreateProcessEx: Create Process from a Section containing an Executable.
Practical Example: Unpacking Osiris
Malware leverages code injection to perform actions from within the context of another process. By doing so, the malware can force a legitimate process to perform actions on its behalf, such as downloading additional Trojans or stealing information from the system. Attackers can inject code into a process in many ways, such as writing to the remote process’s memory directly or adding a registry key that makes new processes load a DLL of the attacker’s choice.
Malware Analysts Cookbook
Last updated