-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdll-injector.cpp
More file actions
87 lines (72 loc) · 2.58 KB
/
Copy pathdll-injector.cpp
File metadata and controls
87 lines (72 loc) · 2.58 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main(int argc, char *argv[]) {
if (argc < 3) {
printf("Usage: %s <PID> <DLL_PATH>\n", argv[0]);
return 1;
}
char* end; DWORD pid = strtoul(argv[1], &end, 10);
if (*end != '\0' || pid == 0) {
printf("Invalid PID\n");
return 1;
}
wchar_t dllPath[MAX_PATH];
if (mbstowcs(dllPath, argv[2], MAX_PATH) == (size_t)-1) {
printf("Invalid DLL path\n");
return 1;
}
HANDLE processHandle = OpenProcess(
PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION | PROCESS_VM_WRITE, FALSE, pid
);
if (processHandle == NULL) {
printf("func(OpenProcess failed). Reason: %lu\n", GetLastError());
return 1;
}
SIZE_T pathSize = (wcslen(dllPath) + 1) * sizeof(wchar_t);
LPVOID remoteBuffer = VirtualAllocEx(
processHandle, NULL, pathSize, MEM_COMMIT, PAGE_READWRITE
);
if (remoteBuffer == NULL) {
printf("func(VirtualAllocEx) failed. Reason: %lu\n", GetLastError());
CloseHandle(processHandle);
return 1;
}
if (!WriteProcessMemory(
processHandle, remoteBuffer, dllPath, pathSize, NULL
)) {
printf("func(WriteProcessMemory) failed. Reason: %lu\n", GetLastError());
VirtualFreeEx(processHandle, remoteBuffer, 0, MEM_RELEASE);
CloseHandle(processHandle);
return 1;
}
HMODULE kernel32 = GetModuleHandleW(L"Kernel32");
if (kernel32 == NULL) {
printf("func(GetModuleHandle) failed. Reason: %lu\n", GetLastError());
VirtualFreeEx(processHandle, remoteBuffer, 0, MEM_RELEASE);
CloseHandle(processHandle);
return 1;
}
PTHREAD_START_ROUTINE loadLibraryAddr = (PTHREAD_START_ROUTINE)
GetProcAddress(kernel32, "LoadLibraryW");
if (loadLibraryAddr == NULL) {
printf("func(GetProcAddress) failed. Reason: %lu\n", GetLastError());
VirtualFreeEx(processHandle, remoteBuffer, 0, MEM_RELEASE);
CloseHandle(processHandle);
return 1;
}
HANDLE threadHandle = CreateRemoteThread(
processHandle, NULL, 0, loadLibraryAddr, remoteBuffer, 0, NULL
);
if (threadHandle == NULL) {
printf("func(CreateRemoteThread) failed. Reason: %lu\n", GetLastError());
VirtualFreeEx(processHandle, remoteBuffer, 0, MEM_RELEASE);
CloseHandle(processHandle);
return 1;
}
WaitForSingleObject(threadHandle, INFINITE);
VirtualFreeEx(processHandle, remoteBuffer, 0, MEM_RELEASE);
CloseHandle(threadHandle);
CloseHandle(processHandle);
return 0;
}