-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDLLjector.cpp
More file actions
70 lines (54 loc) · 1.82 KB
/
Copy pathDLLjector.cpp
File metadata and controls
70 lines (54 loc) · 1.82 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
#include <iostream>
#include <Windows.h>
#include <TlHelp32.h>
DWORD GetPID(const char* windowName)
{
// Find pid of process through window name
HWND windowHandle = FindWindow(NULL, windowName);
DWORD* pid = new DWORD;
if (!GetWindowThreadProcessId(windowHandle, pid))
return NULL;
std::cout << windowName << " PID: " << *pid << std::endl;
return *pid;
}
int main(int argc, char **argv)
{
if (argc < 3)
{
std::cout << "Usage: DLLjector.exe \"[window name]\" [path of dll]" << std::endl;
return 1;
}
const char* windowName = argv[1];
const char* dllPath = argv[2];
const DWORD pid = GetPID(windowName);
if (!pid)
{
std::cout << "Unable to find window..." << std::endl;
return 1;
}
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
if (hProc && hProc != INVALID_HANDLE_VALUE)
{
// Allocate memory in process for dll
void* allocAddr = VirtualAllocEx(hProc, 0, MAX_PATH, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
std::cout << "Allocated " << MAX_PATH << " bytes at 0x" << std::hex << allocAddr << std::endl;
if (!allocAddr)
{
std::cout << "Invalid allocated address..." << std::endl;
return 1;
}
// Inject dll path into allocated mem
WriteProcessMemory(hProc, allocAddr, dllPath, strlen(dllPath) + 1, nullptr);
std::cout << "Wrote " << strlen(dllPath) + 1 << " bytes into allocated address" << std::endl;
// Call LoadLibrary on injected dll and create child thread
// from parent process, running the injected DLL as a result
HANDLE hThread = CreateRemoteThread(hProc, 0, 0, (LPTHREAD_START_ROUTINE)LoadLibraryA, allocAddr, 0, 0);
std::cout << "Loaded DLL library into remote thread" << std::endl;
// Cleanup
if (hThread)
CloseHandle(hThread);
CloseHandle(hProc);
std::cout << "Successfully injected " << dllPath << " into " << windowName << std::endl;
}
return 0;
}