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
Remember to make an exception in your antivirus software.
Using the Win32 API like that will immediately get the program flagged by your AV.
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.