Payload Placement
Payloads can be stored in one of the following PE sections:
- .data Section
The .data section of a PE file is a section of a program's executable file that contains initialized global and static variables. This section is readable and writable ( the memory protection of the region is specified as RW which indicates it is a read-write region), making it suitable for an encrypted payload that requires decryption during runtime.
Example:
- .rdata Section
Variables that are specified using the const qualifier are written as constants. These types of variables are considered "read-only" data. The letter "r" in .rdata indicates this, and any attempt to change these variables will cause access violations.
Example (same code but the variable is now preceded by the const qualifier):
*Depending on the compiler and its settings, the .data and .rdata sections may be merged, or even merged into the .text section.
- .text Section
One must instruct the compiler to save it in the .text section, now is not just a matter of declaring a random variable.
The compiler must be told to place the Text_rawData variable in the .text section instead of the .rdata section.
The .text section is special in that it stores variables with executable memory permissions, allowing them to be executed directly without the need for editing the memory region permissions.
Useful for small payloads (less than 10 bytes)
Example:
- .rsrc Section
Saving the payload in the .rsrc section is one of the best options as this is where most real-world binaries save their data.
It is also a cleaner method for malware authors, since larger payloads cannot be stored in the .data or .rdata sections due to size limits.
To store a payload in the .rsrc section:
Inside Visual Studio, right-click on 'Resource files' then click Add > New Item.
Click on 'Resource File'.
This will generate a new sidebar, the Resource View. Right-click on the .rc file (Resource.rc is the default name), and select the 'Add Resource' option.
Click 'Import'.
Select the calc.ico file, which is the raw payload renamed to have the .ico extension.
A prompt will appear requesting the resource type. Enter "RCDATA" without the quotes.
After clicking OK, the payload should be displayed in raw binary format within the Visual Studio project
When exiting the Resource View, the "resource.h" header file should be visible and named according to the .rc file from Step 2. This file contains a define statement that refers to the payload's ID in the resource section (IDR_RCDATA1). This is important in order to be able to retrieve the payload from the resource section later.
Once compiled, the payload will now be stored in the .rsrc section, but it cannot be accessed directly. Instead, several WinAPIs must be used to access it.
Example of how to access it with explanation of the used APIs:
However, since the payload can't be edited directly from within the resource section, it must be moved to a temporary buffer.
To do so, memory is allocated the size of the payload using HeapAlloc and then the payload is moved from the resource section to the temporary buffer using memcpy, adding the following to the previous example:
Since pTmpBuffer now points to a writable memory region that is holding the payload, it's possible to decrypt the payload or perform any updates to it.
Last updated