Shellcode Injection

Apr 4, 2023

Shellcode injection consists of four basic steps which are:

  1. Attach to/create a process.

  2. Allocate some memory within that process.

  3. Write the memory to the process.

  4. Finally, create a thread to execute the code we injected into the process's memory.

Shellcode Injection flow chart

Win32 API Calls

Documentation for the following API calls can be found by clicking the functions header.

Setup

Let's start by implementing step 1. The OpenProcess function returns a handle to a specified process. In case of failure, it will return NULL and we can get the error by calling GetLastError(). Therefore we can implement a simple check to handle errors:

Shellcode

We will use msfvenom in order to generate a simple payload that will run the calc.exe application.

Setup II

Injection

Now let's compile our script

Add an exclusion to Microsoft Defender

Great! Our injector is ready. For demonstration purposes, we will inject into notepad.exe

For the sake of demonstration and logging, I added some prints in the code.

Successful shellcode injection.

🎉 And we did it! Congratulations 😄

In the next writeup we will be talking about DLL Injections.

Last updated