Malware payloads
1. Placement
Malware payload can be stored in one of the following PE sections:
.data
.rdata
.text
.rsrc
NOTE: The compiler might decide to store global variables and constants in
.text
section anyway. There is no 100% sure method to determine where the data will be finally stored.
1.1. .data section
To store payload in .data
section initialize global variable. This section is readable and writable.
1.2. .rdata section
To store payload in .rdata
section initialize global constant. This section in read-only.
1.3. .text section
To store data in .text
section, one must explicitly instruct the compiler to do this. This section is executable. It's good for small payloads.
1.4. .rsrc section
Visual Studio has an option to embed binary resources (icons, etc.) into PE .rsrc
section. This section is read-only. The payload cannot be accessed directly at runtime. Instead, several WinAPI functions (especially from libloaderapi.h
) must be used to access it.
2. Encryption
Encryption of the payload can help evade signature-based detection. It is always necessary against modern security solutions. Raw payloads are very well detected. It may not be effective against runtime and heuristic analysis.
NOTE: The more encrypted data is embedded in the malicious file, the higher its entropy is. It might be suspicious for security solutions. Entropy should be as normal as possible.
- XOR - the easiest and the stealthiest method. It doesn't require to use any external modules. The same function can be used to encryption and decryption. This encryption is very easy to spot and reversed by an analyst. Some security solutions are able to even brute-force weak encryption keys.
- RC4 - efficient bidirectional encryption algorithm. Its implementation is pretty short and can be easily embedded in the source code of malware. Even NTAPI has some efficient and small implementations of the algorithm (
SystemFunction032
andSystemFunction033
exported fromadvapi32.dll
). - AES - advanced, secure and pretty complicated encryption algorithm. It operates on blocks of data which are 16-bytes long. It usually requires usage of some external library which can be already signatured by security vendors or produces a lot of WinAPI calls.
3. Obfuscation
When it comes to payload obfuscation, the sky is the limit. You can rotate bytes, take every second byte, reverse string, split it across multiple variables and so on. There are, however, some real-life examples of very interesting obfuscation techniques that can help not only evade security solution but even slow down the process of manual analysis. Additionally, obfuscation usually doesn't increase an entropy of the file which might be important. Obfuscation techniques usually try to hide the payload in a form of a bunch of innocent data.
- IPFuscation - 1 byte = 1 octet in a IP address. Or every byte in a seperate address.
- MACFuscation - same with MAC addresses.
- UUIDFuscation - same with UUID.
- GeoFuscation - same with geo coordinates.
NOTE: Both, encryption and obfuscation, are intended to evade signature-based detection.
4. Execution
4.1. External DLL
A payload can be stored and fired in an external DLL file. There are four different DLL events that can be observed. Look at the following snippet:
LoadLibrary
WinAPI function is used to load a DLL into the current process memory. During that process the DLL_PROCESS_ATTACH
event is executed. There might be a hidden piece of malicious code.
4.2. Local thread
NOTE: This technique is not stealthy in a confrontation with EDRs and more advanced security solutions!
Steps to perform a local thread shellcode execution: