Shelltropy: hiding malicious shellcode via Shannon encoding
A technique of hiding malicious shellcode based on low-entropy via Shannon encoding.
Entropy is the measure of the randomness in a set of data (here: shellcode). The higher the entropy, the more random the data is. Shannon Entropy is an algorithm that will produce a result between 0 and 8, where 8 means there is no pattern in the data, thereby it’s very random and 0 means data follows a pattern.
Note: Check out my blog article for a more detailed explanation of my research, and a simple Vanilla Shellcode Injection PoC as well.
How the PoC works
The concept is to divide the array into chunks and insert a low-entropy pattern of bytes between each chunk. When the sample is run, we must reconstruct the original payload in memory, bypassing the static detection of the high entropy code at this stage. It’s also worth noting that the low-entropy code to be inserted can follow a variety of patterns, and the amount of insertions can vary, thus it can be used to circumvent static signature detection. The second step is to combine the high entropy chunks of bytes with the low entropy chunks. Because, after all, we need to restore the obfuscated code to what it was initially in order to proceed to the de-obfuscation step, the third task will restore the original array of bytes by deleting the low entropy patterns.
Clone this repository:
git clone https://github.com/kleiton0x00/Shelltropy.git
For the encoder:
Create a new Visual Studio project and import both Entropy.h and shannonEncode.cpp and simply build it.
For the Shellcode Injection PoC (with Syscalls):
Open the Visual Studio project file located in /Shelltropy/SyscallsExample/SyscallsExample/SyscallsExample.vcxproj and simply build it.
Note: If you want a simple Shellcode Injection PoC with WinAPI check out my blog
For the decoder (optional since it’s included on the Shellcode Injection PoC):
Create a new Visual Studio project and import shannonDecode.cpp and simply build it.
How to use
- Launch Cobalt Strike and generate a Windows Executable (Stageless or non-stageless, up to you). Make sure the output is Raw and save it as payload.bin
- Execute shellcodeFormatter.py and copy the output
- Replace the Shellcode in shannonEncoder.cpp (line 10) with the shellcode you generated from the python file.
- You can use the generated low-entropy shellcode in the Visual Studio project
Copyright (C) 2022 kleiton0x00