By Waseem Akram on 10/1/2024
Today we’re gonna see a simple malware development technique, Shellcode injection via CreateRemoteThread in Golang...
Hello hackers!
Today we’re gonna see a simple malware development technique, Shellcode injection via CreateRemoteThread in Golang. The most complex malwares use different shellcode injection techniques to bypass common AV vendors as the shellcode is executed in memory.
Executing shellcode Let’s test this with a simple metasploit reverse shell, to generate it use msfvenom like this:
In this post we’ll be using the raw format, however in future posts we’ll see how we can combine encryption/decryption functions to avoid static scanning and much more
As every Golang program we start defining the package name and which packages we want to import
To perform this technique we have to use some Windows API calls and to use them in Golang you must import them from their DLLs like this:
This isn’t the only way to do this but it’s the most used and it’s easy to implement. Other option is using CGO to use C code
We also should have in mind that all the API calls return 3 values but we just will use the first and the third of them. Once we know how to access the Windows API in Golang we can continue.
The first step to inject shellcode is to get a handle to the desired process:
Notice that you must have the right permissisons to get process handle
Now we allocate the memory buffer so then we can write our malicious bytes. To do this we use VirtualAllocEx
Note the “Ex” at the end of the function which means that it can allocate memory on remote processes
Then we use WriteProcessMemory
call to write the shellcode into RW allocated process memory space
At this point we use CreateRemoteThreadEx
call to create a new thread in the especified process which will finally execute the shellcode:
And finally we close the process handle using CloseHandle
call:
Now we put all pieces together and add more output info to create a more readable program
This is the final result of the program. Now compile the program in your attacker machine:
Command for linux
Command for windows
Now let’s see how it works!
And if I check my netcat listener…
As you can see I’ve catched the reverse shell and if you open the Process Explorer you will see that there is the notepad.exe process but no new process was created
Let’s upload the generated .exe to VirusTotal and antiscan.me to see the results (I use VirusTotal as it’s just for testing purposes, not for real malware because it gets burned out)
As you can see 7/69 detections isn’t bad at all, but it occurs because the program receives the arguments via CLI so when VirusTotal analyze the file it just gives an error and exits, and the shellcode isn’t hardcoded on the source code so it really helps to bypass AVs
This technique is simple but really useful to see how we can leverage the Windows API to execute malicious code. In the following posts I’ll show you different techniques to bypass AVs and much more.
Source code here - GitHub repository with all the code from this post.
Thanks for reading this post, if you like my work you can support by Become a Patron!. Read other posts
This course is designed to be hands-on and beginner-friendly, so even if you’re new to the world of network security, you’ll be able to follow along with ease. By the end, you'll have a
Read MoreToday we’re going to see how real malware protect themselves from being analyzed using a technique called Api Hashing. First of all we should...
Read MoreToday we’ll dump LSASS.EXE process memory to obtain credentials and we also will be using some evasion techniques. During red team...
Read MoreToday we’re gonna see an effective technique to mantain access in Windows systems during red team operations just by modifying a registry key...
Read MoreToday we’ll learn an advanced shellcode injection technique used by Lazarus group which uses UuidFromStringA API call. In this technique, the malware..
Read MoreInstahack is a security tool officially designed to test the password strength of Instagram accounts using termux and kali with a brute force attack...
Read More