-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInjector32.cpp
More file actions
81 lines (67 loc) · 2.24 KB
/
Copy pathInjector32.cpp
File metadata and controls
81 lines (67 loc) · 2.24 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
// Injector32.cpp
// 编译选项: Release, x86, 静态链接 CRT (/MT)
#include <windows.h>
#include <iostream>
#include <string>
#include <tlhelp32.h>
// 简单的日志输出 方便调试(实际使用可去除)
void Log(const wchar_t* msg) {
// OutputDebugStringW(msg);
}
bool Inject(DWORD pid, const std::wstring& dllPath) {
Log(L"Injector32: Opening process...");
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
if (!hProcess) return false;
// 获取 LoadLibraryW 地址
// 因为这是 32位 进程 GetModuleHandle 获取的是 32位 kernel32
HMODULE hKernel32 = GetModuleHandleW(L"kernel32.dll");
if (!hKernel32) {
CloseHandle(hProcess);
return false;
}
LPVOID pLoadLibrary = (LPVOID)GetProcAddress(hKernel32, "LoadLibraryW");
if (!pLoadLibrary) {
CloseHandle(hProcess);
return false;
}
// 分配内存
size_t pathSize = (dllPath.length() + 1) * sizeof(wchar_t);
LPVOID pRemoteMem = VirtualAllocEx(hProcess, NULL, pathSize, MEM_COMMIT, PAGE_READWRITE);
if (!pRemoteMem) {
CloseHandle(hProcess);
return false;
}
// 写入路径
if (!WriteProcessMemory(hProcess, pRemoteMem, dllPath.c_str(), pathSize, NULL)) {
VirtualFreeEx(hProcess, pRemoteMem, 0, MEM_RELEASE);
CloseHandle(hProcess);
return false;
}
// 创建远程线程
Log(L"Injector32: Creating remote thread...");
HANDLE hThread = CreateRemoteThread(hProcess, NULL, 0,
(LPTHREAD_START_ROUTINE)pLoadLibrary, pRemoteMem, 0, NULL);
if (!hThread) {
VirtualFreeEx(hProcess, pRemoteMem, 0, MEM_RELEASE);
CloseHandle(hProcess);
return false;
}
// 等待注入完成
WaitForSingleObject(hThread, INFINITE);
DWORD exitCode = 0;
GetExitCodeThread(hThread, &exitCode);
CloseHandle(hThread);
VirtualFreeEx(hProcess, pRemoteMem, 0, MEM_RELEASE);
CloseHandle(hProcess);
return (exitCode != 0); // LoadLibrary 返回非零表示成功
}
int wmain(int argc, wchar_t* argv[]) {
if (argc < 3) return 1;
DWORD pid = _wtoi(argv[1]);
std::wstring dllPath = argv[2];
if (Inject(pid, dllPath)) {
return 0; // 成功
} else {
return 1; // 失败
}
}