Day 5 – Code Injection into Running Processes
Objective
The goal for today is to understand code injection and learn how to implement it in C. You’ll study the functions involved in accessing another process’s memory space, writing to it, and executing code within that process. By the end, you’ll complete a microproject to practice injecting code into a simple application running on your system.
Understanding Code Injection
Code Injection is the act of inserting and executing arbitrary code within the address space of another running process. It’s commonly used in various attack techniques, such as DLL injection or process hollowing. Here’s how it generally works in a Windows environment:
- Access the Target Process: First, you need to gain a handle to the target process (e.g., a running program) using OpenProcess().
- Allocate Memory in the Target Process: Allocate space in the target process’s memory using VirtualAllocEx().
- Write to the Allocated Memory: Copy the code you want to execute into the allocated memory using WriteProcessMemory().
- Execute the Injected Code: Create a new thread within the target process to execute the injected code using CreateRemoteThread().
This process allows you to inject and execute code covertly within another process, making it a common technique in malware development but also in red team testing and application testing.
Exploring Key Functions for Code Injection in Windows
To successfully perform code injection on Windows, you’ll need to understand these core functions:
OpenProcess():
- This function is used to open an existing process and obtain a handle to it.
- Common access rights include
PROCESS_VM_WRITE
(to write to the process memory),PROCESS_VM_OPERATION
(to perform memory operations like allocation), andPROCESS_CREATE_THREAD
(to create threads in the target process).
VirtualAllocEx():
Allocates memory in the target process, specifying MEM_COMMIT and PAGE_EXECUTE_READWRITE permissions to ensure the injected code can be executed.WriteProcessMemory(): Writes the actual shellcode or executable code into the allocated memory of the target process.
CreateRemoteThread():Starts a new thread in the target process at the address where the shellcode has been written, executing the code.
With these four functions, you have the basic tools required to inject and execute code within another process in Windows.
Microproject
Let’s go through a practical example where we inject code into a target process on Windows. For simplicity, we’ll inject code into a process that simply opens the calculator (calc.exe), which is easy to observe as it will become visible when the injection is successful.
Shellcode: Open Calculator (calc.exe)
The shellcode for opening calc.exe
on Windows is as follows:
This byte sequence will open the Windows calculator when executed in the memory of the target process.
Code Injection in Windows
This code demonstrates how to inject shellcode into a target process using the discussed functions.
Explanation of the Code
User Input for PID: We ask for the Process ID (PID) of the target process. You can find the PID in Task Manager under the “Processes” tab.
Process Handle: OpenProcess() is used to obtain a handle to the target process.
Memory Allocation: VirtualAllocEx() allocates executable memory in the target process for our shellcode.
Writing Shellcode: WriteProcessMemory() copies the shellcode (calculator opener) to the allocated memory in the target process.
Executing the Shellcode: CreateRemoteThread() runs the shellcode by starting a new thread at the location of our injected code.
Cleanup: After the shellcode runs, we release the allocated memory and close the handles to avoid resource leaks.
Running the Program
Compile the Code:
Run the Program with a Target Process ID:
- Open a simple process you wish to target (like Notepad).
- Find the PID of Notepad in Task Manager.
- Run the injector program and enter the PID when prompted.
If the injection is successful, calc.exe will open as a result of the injected shellcode, indicating that the injection worked.
Conclusion
We have learned the basics of code injection in Windows, including how to allocate memory in another process, write code into it, and execute it remotely. This technique is fundamental in malware development but also serves as a powerful tool for testing application resilience in security research.
Happy Hacking! ❤
No comments:
Post a Comment