-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinjector_main.cpp
137 lines (115 loc) · 3.9 KB
/
injector_main.cpp
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
#include "injector.h"
#include <windows.h>
#include <detours.h>
#include <algorithm>
#include <filesystem>
#include <cstdio>
#include <string>
#include <vector>
#define ASIZE(V) (sizeof(V) / sizeof((V)[0]))
static const char* reference_sha256_hashes[] = {
"9028120f4e3fd84b790fed1cbc074d2d7a547fed49cfdaec60a6bb0f28c45de2",
"c6040203856b71e6a22d2a29053a1eadd1a2ab41bce97b6031d745079bc07bdf",
};
namespace fs = std::filesystem;
int CDECL main(int argc, char *argv[])
{
#if 0
auto workPathDir = fs::current_path();
#else
char exePath[512];
DWORD nLen = GetModuleFileNameA(nullptr, exePath, sizeof(exePath));
if (nLen == sizeof(exePath)) {
return 1;
}
exePath[nLen] = '\0';
auto workPathDir = fs::path(exePath).parent_path();
#endif
const auto workPathDirStr = workPathDir.generic_string();
const auto pluginsDirPath = workPathDir / "plugins";
const auto victimPath = (workPathDir / VICTIM).generic_string();
if (!fs::is_regular_file(victimPath)) {
fprintf(stderr, "%s does not exist.\n", victimPath.c_str());
return 1;
}
char *sha256 = hook_calculate_sha256(victimPath.c_str());
int valid = 0;
for (int i = 0; i < ASIZE(reference_sha256_hashes); i++) {
if (stricmp(sha256, reference_sha256_hashes[i]) == 0) {
valid = 1;
break;
}
}
if (!valid) {
fprintf(stderr, "SHA256 hash of \"%s\" is not in database (%s).\n", victimPath, sha256);
return 1;
}
free(sha256);
std::vector<std::string> pluginPaths;
printf("plugins:\n");
if (fs::is_directory(pluginsDirPath)) {
for (auto pluginItem: fs::directory_iterator(pluginsDirPath)) {
if (!pluginItem.exists() || !pluginItem.is_regular_file()) {
continue;
}
auto pluginPath = pluginItem.path();
if (pluginPath.extension() != ".dll") {
continue;
}
auto pluginPathStr = pluginPath.generic_string();
pluginPaths.push_back(pluginPathStr);
printf("- %s\n", pluginPaths.back().c_str());
}
}
//////////////////////////////////////////////////////////////////////////
STARTUPINFOA si;
PROCESS_INFORMATION pi;
std::string command;
ZeroMemory(&si, sizeof(si));
ZeroMemory(&pi, sizeof(pi));
si.cb = sizeof(si);
command = victimPath;
for (int i = 1; i < argc; i++) {
command += " ";
command += argv[i];
}
char* commandBuffer = new char[command.size() + 1];
command.copy(commandBuffer, command.size());
commandBuffer[command.size()] = '\0';
std::vector<const char*> pluginPathsCStrs;
pluginPathsCStrs.resize(pluginPaths.size());
std::transform(pluginPaths.begin(), pluginPaths.end(), pluginPathsCStrs.begin(), [](const auto &str) { return str.data(); });
printf(TARGET ": Starting: `%s'\n", command.c_str());
for (const auto& pluginPath : pluginPaths) {
printf(TARGET ": with `%s'\n", pluginPath.c_str());
}
fflush(stdout);
DWORD dwFlags = CREATE_DEFAULT_ERROR_MODE | CREATE_SUSPENDED;
SetLastError(0);
if (!DetourCreateProcessWithDllsA(
victimPath.c_str(),
commandBuffer,
nullptr,
nullptr,
TRUE,
dwFlags,
nullptr,
workPathDirStr.c_str(),
&si,
&pi,
pluginPathsCStrs.size(),
pluginPathsCStrs.data(),
nullptr)) {
DWORD dwError = GetLastError();
printf(TARGET ": DetourCreateProcessWithDllEx failed: %ld\n", dwError);
printf("ERROR");
ExitProcess(9009);
}
ResumeThread(pi.hThread);
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD dwResult = 0;
if (!GetExitCodeProcess(pi.hProcess, &dwResult)) {
printf(TARGET ": GetExitCodeProcess failed: %ld\n", GetLastError());
}
return dwResult;
}