Load Resource Data From .Rsrc
Last updated
Last updated
#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>
#include "resource.h"
#include <string.h>
PBYTE GetResourceData(SIZE_T* resourceSize) {
HRSRC hRsrc = NULL;
HGLOBAL hGlobal = NULL;
PVOID pPayloadAddr = NULL;
printf("[*] Searching for resource...\n");
hRsrc = FindResource(NULL, MAKEINTRESOURCE(IDR_RCDATA1), RT_RCDATA);
if (hRsrc == NULL) {
printf("[-] Failed to find resource. Error: %lu\n", GetLastError());
return NULL;
}
printf("[+] Resource found successfully.\n");
printf("[*] Loading resource...\n");
hGlobal = LoadResource(NULL, hRsrc);
if (hGlobal == NULL) {
printf("[-] Failed to load resource. Error: %lu\n", GetLastError());
return NULL;
}
printf("[+] Resource loaded successfully.\n");
printf("[*] Locking resource...\n");
pPayloadAddr = LockResource(hGlobal);
if (pPayloadAddr == NULL) {
printf("[-] Failed to lock resource. Error: %lu\n", GetLastError());
return NULL;
}
printf("[+] Resource locked successfully. Payload address: %p\n", pPayloadAddr);
printf("[*] Getting resource size...\n");
*resourceSize = SizeofResource(NULL, hRsrc);
if (*resourceSize == 0) {
printf("[-] Failed to get resource size. Error: %lu\n", GetLastError());
return NULL;
}
printf("[+] Resource size: %zu bytes.\n", *resourceSize);
printf("[*] Allocating memory for writable payload...\n");
PBYTE pWritablePayload = (PBYTE)malloc(*resourceSize);
if (pWritablePayload == NULL) {
printf("[-] Failed to allocate memory for writable payload.\n");
return NULL;
}
printf("[+] Memory allocated successfully at address: %p\n", pWritablePayload);
printf("[*] Copying resource data to allocated memory...\n");
memcpy(pWritablePayload, pPayloadAddr, *resourceSize);
printf("[+] Resource data copied successfully.\n");
return pWritablePayload;
}
int main() {
SIZE_T sPayloadSize = 0;
printf("[#] Calling GetResourceData...\n");
PBYTE pWritablePayload = GetResourceData(&sPayloadSize);
if (pWritablePayload == NULL) {
printf("[-] Failed to get resource data.\n");
return 1;
}
printf("[+] Successfully retrieved resource data.\n");
printf("[+] Payload Size: %zu bytes\n", sPayloadSize);
free(pWritablePayload);
printf("[#] Memory freed successfully.\n");
return 0;
}