-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
168 lines (139 loc) · 4.86 KB
/
Copy pathmain.cpp
File metadata and controls
168 lines (139 loc) · 4.86 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "ixwebsocket/IXWebSocket.h"
#include <windows.h>
#include <tlhelp32.h>
#include <psapi.h>
#include <winsock2.h>
#include <iostream>
#include <thread>
#include <chrono>
#include <filesystem>
std::string managed_folder_path;
std::string game_window_name;
void find_managed_folder(const std::string &mono_dll_path) {
const auto start = std::filesystem::path(mono_dll_path).parent_path().parent_path().parent_path();
for (auto &entry : std::filesystem::recursive_directory_iterator(start)) {
if (entry.is_regular_file() && entry.path().filename() == "Assembly-CSharp.dll") {
managed_folder_path = entry.path().parent_path().string();
return;
}
}
}
void find_game_window(const DWORD pid) {
EnumWindows([](HWND hwnd, const LPARAM l_param) -> BOOL __stdcall {
DWORD window_pid = 0;
GetWindowThreadProcessId(hwnd, &window_pid);
if (window_pid != l_param) return true;
if (!IsWindowVisible(hwnd)) return true;
const auto window_text_len = GetWindowTextLengthA(hwnd);
std::vector<char> buf(window_text_len + 1);
GetWindowTextA(hwnd, buf.data(), window_text_len + 1);
game_window_name.assign(buf.data());
return false;
}, pid);
}
bool is_mono(const DWORD pid) {
const auto snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid);
MODULEENTRY32 entry;
entry.dwSize = sizeof(MODULEENTRY32);
if (Module32First(snapshot, &entry)) {
do {
if (strcmp(entry.szModule, "mono-2.0-bdwgc.dll") == 0) {
find_managed_folder(entry.szExePath);
return true;
}
} while (Module32Next(snapshot, &entry));
}
return false;
}
int main() {
WSADATA wsa_data;
WSAStartup(MAKEWORD(2, 2), &wsa_data);
ix::WebSocket ws;
ws.setUrl("ws://127.0.0.1:7717/Echo");
bool conected = false;
ws.setOnMessageCallback([&](const ix::WebSocketMessagePtr &msg) {
if (msg->type == ix::WebSocketMessageType::Open) {
conected = true;
}
if (msg->type == ix::WebSocketMessageType::Error) {
ws.close(0, "Error");
}
});
ws.start();
int count = 0;
while (!conected) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
count++;
if (count > 60) {
ws.close(0, "Timed out");
return 3;
}
}
const auto path = (std::filesystem::current_path()).generic_string();
const auto dll_str = path + "/ciplasplas.dll";
const auto csharp_helper_dll = path + "/ClassLibrary1.dll";
const auto dll_path = dll_str.c_str();
if (!std::filesystem::exists(std::filesystem::path(csharp_helper_dll))) {
std::cout << "Failed to find the C# helper DLL in the current directory";
return 1;
}
DWORD pid = 0;
PROCESSENTRY32 pe;
pe.dwSize = sizeof(PROCESSENTRY32);
const auto snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (Process32First(snapshot, &pe)) {
do {
if (is_mono(pe.th32ProcessID)) {
pid = pe.th32ProcessID;
CloseHandle(snapshot);
}
} while (Process32Next(snapshot, &pe));
}
CloseHandle(snapshot);
find_game_window(pid);
std::cout << game_window_name << "\n";
// std::cout << pid << "\n";
if (managed_folder_path.empty()) {
std::cout << "Failed to find the folder path\n";
return 1;
}
if (!std::filesystem::exists(std::filesystem::path(managed_folder_path + "/ClassLibrary1.dll"))) {
std::cout << "Did not find helper C# assembly in game directory, adding it" << "\n";
std::filesystem::copy(std::filesystem::path(csharp_helper_dll), std::filesystem::path(managed_folder_path));
}
const auto h_proc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
if (!h_proc)
return 1;
const auto path_len = strlen(dll_path) + 1;
const auto remoteMem = VirtualAllocEx(h_proc, nullptr, path_len, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (!remoteMem) {
CloseHandle(h_proc);
return 1;
}
if (!WriteProcessMemory(h_proc, remoteMem, dll_path, path_len, nullptr)) {
VirtualFreeEx(h_proc, remoteMem, 0, MEM_RELEASE);
CloseHandle(h_proc);
return 1;
}
const auto h_thread = CreateRemoteThread(
h_proc,
nullptr,
0,
reinterpret_cast<LPTHREAD_START_ROUTINE>(LoadLibraryA),
remoteMem,
0,
nullptr
);
if (!h_thread) {
VirtualFreeEx(h_proc, remoteMem, 0, MEM_RELEASE);
CloseHandle(h_proc);
return 1;
}
WaitForSingleObject(h_thread, INFINITE);
VirtualFreeEx(h_proc, remoteMem, 0, MEM_RELEASE);
CloseHandle(h_thread);
CloseHandle(h_proc);
std::cout << "Me & twin locked in\n";
ws.close(0, "Success");
return 0;
}