-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.cpp
More file actions
141 lines (119 loc) · 3.74 KB
/
loader.cpp
File metadata and controls
141 lines (119 loc) · 3.74 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <windows.h>
#include <tlhelp32.h>
#include <string>
#include "Log.h"
static const char* kDefaultProcess = "Pirates_Online.exe";
static const char* kDefaultDll = "injector.dll";
static DWORD FindProcessId(const char* name)
{
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snapshot == INVALID_HANDLE_VALUE)
return 0;
PROCESSENTRY32 entry = {0};
entry.dwSize = sizeof(entry);
DWORD pid = 0;
if (Process32First(snapshot, &entry))
{
do
{
if (_stricmp(entry.szExeFile, name) == 0)
{
pid = entry.th32ProcessID;
break;
}
} while (Process32Next(snapshot, &entry));
}
CloseHandle(snapshot);
return pid;
}
static bool Inject(DWORD pid, const std::string& dllPath)
{
HANDLE process = OpenProcess(
PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION |
PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ,
FALSE, pid);
if (!process)
{
tlog::error("OpenProcess failed (" + std::to_string(GetLastError()) +
"). Try running as admin.");
return false;
}
bool ok = false;
const SIZE_T size = dllPath.size() + 1;
LPVOID remote = VirtualAllocEx(process, nullptr, size,
MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (!remote)
{
tlog::error("VirtualAllocEx failed (" +
std::to_string(GetLastError()) + ").");
}
else if (!WriteProcessMemory(process, remote, dllPath.c_str(), size,
nullptr))
{
tlog::error("WriteProcessMemory failed (" +
std::to_string(GetLastError()) + ").");
}
else
{
LPVOID loadLib = (LPVOID)GetProcAddress(
GetModuleHandleA("kernel32.dll"), "LoadLibraryA");
HANDLE thread = CreateRemoteThread(
process, nullptr, 0, (LPTHREAD_START_ROUTINE)loadLib,
remote, 0, nullptr);
if (!thread)
{
tlog::error("CreateRemoteThread failed (" +
std::to_string(GetLastError()) + ").");
}
else
{
WaitForSingleObject(thread, INFINITE);
DWORD exitCode = 0;
GetExitCodeThread(thread, &exitCode);
if (exitCode == 0)
tlog::error("LoadLibraryA returned NULL inside the target.");
else
ok = true;
CloseHandle(thread);
}
}
if (remote)
VirtualFreeEx(process, remote, 0, MEM_RELEASE);
CloseHandle(process);
return ok;
}
static std::string ResolveDllPath(const char* arg)
{
char full[MAX_PATH] = {0};
if (GetFullPathNameA(arg, MAX_PATH, full, nullptr) == 0)
return std::string(arg);
return std::string(full);
}
int main(int argc, char** argv)
{
const char* processName = (argc > 1) ? argv[1] : kDefaultProcess;
const char* dllName = (argc > 2) ? argv[2] : kDefaultDll;
const std::string dllPath = ResolveDllPath(dllName);
if (GetFileAttributesA(dllPath.c_str()) == INVALID_FILE_ATTRIBUTES)
{
tlog::fatal("DLL not found: " + dllPath);
return 1;
}
tlog::info("Target: " + std::string(processName));
tlog::info("Payload: " + dllPath);
DWORD pid = FindProcessId(processName);
if (!pid)
{
tlog::fatal("Process not found: " + std::string(processName) +
". Is the game running?");
return 1;
}
tlog::info("PID: " + std::to_string(pid));
if (!Inject(pid, dllPath))
{
tlog::fatal("Injection failed.");
return 1;
}
tlog::success("Injected successfully. Press Insert in-game to toggle.");
return 0;
}