Sandboxed Module 2.7: Memory Injections



Memory injection is a sophisticated technique used in cybersecurity attacks where malicious code is inserted directly into the memory of a running process on a target system. This tactic allows attackers to execute their payloads stealthily, often bypassing traditional security measures like antivirus software and file-based detection, because the malicious code doesn’t reside as a static file on disk.


What is Memory Injection?

At its core, memory injection is a method of code execution. Instead of writing malware to disk (which might trigger antivirus alerts), attackers load and run malicious code directly in memory—typically within the context of a legitimate, trusted process. This helps hide their activities, maintain persistence, and escalate privileges on the system.


How It Works (Step by Step)

  • Initial Access

    • The attacker first gains access to a system (via phishing, vulnerability exploitation, remote access tools, etc.).

  • Injection Preparation

    • The attacker chooses a target process (e.g., explorer.exe, svchost.exe, or a browser). These are often whitelisted or trusted by the OS and security software.

  • Code Injection

    • The attacker uses system calls or APIs like:

      • WriteProcessMemory

      • VirtualAllocEx

      • CreateRemoteThread

      • NtMapViewOfSection (used by more advanced techniques)

    • These allow them to allocate space in the memory of the target process and write malicious code into that space.

  • Execution

    • Once the malicious code is injected, the attacker uses a call like CreateRemoteThread to execute the code within the context of the legitimate process.

  • Stealth and Persistence

    • The injected code runs in memory only, leaving little or no trace on the filesystem.

    • It may set up command and control communication, dump credentials, escalate privileges, or disable security tools.


Common Memory Injection Techniques

Technique Description
DLL Injection Injects a malicious DLL into a process using functions like LoadLibrary.
Reflective DLL Injection Injects a DLL directly into memory without using Windows loader; often avoids detection.
Process Hollowing A legitimate process is started in a suspended state, its memory is replaced with malicious code, and then it's resumed.
Thread Hijacking Modifies the execution of an existing thread in a process to run malicious code.
APC Injection Queues code execution in another process by inserting into Asynchronous Procedure Calls.
Shellcode Injection Injects raw bytecode directly into memory for immediate execution.

Why It's Dangerous

  • Evades Detection: No files written to disk → hard for antivirus to detect.

  • Runs in Trusted Processes: Antivirus might ignore malicious behavior because it's running in a signed or trusted executable.

  • Used in APTs and Ransomware: Advanced Persistent Threats (APTs) often use memory injection to maintain stealthy long-term access.

  • Can Escalate Privileges: By injecting into higher-privilege processes, attackers can gain SYSTEM-level access.


Real-World Examples

  • Emotet: Used reflective DLL injection to load modules into memory.

  • Cobalt Strike: A red-team tool used by threat actors, known for using memory injection techniques.

  • TrickBot: Often uses process hollowing and injection to execute malicious tasks without detection.

  • Mimikatz: Uses memory techniques to extract credentials from memory (e.g., LSASS).


How to Defend Against It

  • Endpoint Detection and Response (EDR): Monitors process memory behaviors (e.g., thread creation, memory writes).

  • Memory Scanning: Tools like Windows Defender now include memory-based detection signatures.

  • Process Whitelisting: Only allow certain executables to run.

  • Behavior-Based Detection: Instead of scanning files, monitor system behavior patterns.

  • Privilege Management: Reduce admin privileges to limit what injected code can do.

  • Patch Management: Many injections start by exploiting vulnerabilities.



Post a Comment

Previous Post Next Post