-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhack_civilization.cpp
More file actions
326 lines (273 loc) · 10.9 KB
/
hack_civilization.cpp
File metadata and controls
326 lines (273 loc) · 10.9 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <BlackBone/Process/Process.h>
#include <BlackBone/Patterns/PatternSearch.h>
#include <BlackBone/Process/RPC/RemoteFunction.hpp>
#include <BlackBone/Syscalls/Syscall.h>
#include "inject_civilization.h"
#include <thread>
using namespace blackbone;
void showMessageBox(const char* title, const char* text);
void print_debug(const char* msg);
namespace CivilizationVI {
std::vector<uint8_t> hook_code(
{
0x42, 0x8D, 0x04, 0x02, 0x49, 0x8B, 0xC9, 0x48,
0x8D, 0x54, 0x24, 0x30, 0x89, 0x44, 0x24, 0x30,
}
);
struct HackStateException : public std::exception {
std::string caption;
std::string text;
HackStateException(const char* caption, const char* text) {
this->caption = caption;
this->text = text;
}
HackStateException(std::string caption, std::string text) {
this->caption = caption;
this->text = text;
}
/*const char* what() const throw () {
return "C++ Exception";
}*/
};
class HackState
{
public:
bool hack(std::vector<std::pair<uint64_t, uint32_t>>& pointers, OverwriteMode mode, uint64_t selected_address);
~HackState();
private:
bool attachProcess();
MemBlock getGoldSetter();
ModuleDataPtr getDll(bool inject);
bool writePayload();
void syncDllConfig(bool push);
bool resetProcessCode();
bool update_hack_state(std::vector<std::pair<uint64_t, uint32_t>>& pointers, OverwriteMode mode, uint64_t selected_address);
std::unique_ptr<Process> process;
bool gold_adding_overwritten = false;
DWORD pid = -1;
ptr_t getGoldSetter_address = NULL;
struct DllConfig dll_config;
};
HackState::~HackState() {
resetProcessCode();
}
bool HackState::attachProcess() {
// Attach to SpeedRunners.exe (excluding self)
auto pids = Process::EnumByName(L"CivilizationVI.exe");
// This process maybe have the same name -> remove it from the list
pids.erase(std::remove(pids.begin(), pids.end(), GetCurrentProcessId()), pids.end());
if (pids.size() != 1) {
throw HackStateException("Error", "Found " + std::to_string(pids.size()) + " CivilizationVI.exe processes, expected 1!");
} else {
// Assume: same process name and pid -> same process instance
if (pid != pids.front() /*|| !process->valid()*/) {
process.reset(); // replace with new Process object, but first delete old object
process.reset(new Process());
gold_adding_overwritten = false;
} else {
// Only attach again
}
pid = pids.front();
if (NT_SUCCESS(process->Attach(pid))) {
return true;
} else {
throw HackStateException("Process attach", "Error: Could not attach to SpeedRunners.exe process!");
}
}
}
MemBlock HackState::getGoldSetter() {
// Inject or search custom dll
ptr_t hook_ptr;
ptr_t static_offset = 0;
// Pattern cannot be found after overwritting: store it
// When application is closed but game keeps running this address cannot be found by pattern matching
// because it will be overwritten!
// Set static_offset to disable pattern search
if (getGoldSetter_address) {
hook_ptr = getGoldSetter_address;
} else {
auto& modules = process->modules();
auto gamecore_module = modules.GetModule(L"GameCore_Base_FinalRelease.dll");
if (static_offset) {
hook_ptr = gamecore_module->baseAddress + static_offset;
printf("Warning: Using static hook address\n");
} else {
std::vector<ptr_t> results;
PatternSearch ps(hook_code);
ps.SearchRemote(*process, gamecore_module->baseAddress, gamecore_module->size, results);
// Pattern is expected to be found once or twice. If found twice, it's the higher memory address
if (results.size() == 0 || results.size() > 2) {
throw HackStateException("Pattern Search", "Error: Could not find code pattern in getGoldSetter(), results.size() == " + std::to_string(results.size()) + ", expected 1 or 2! Try to restart the target process");
}
// Choose the correct result with higher memory location. If results contains one element, this does nothing
hook_ptr = max(results.front(), results.back());
print_debug(("GoldSetter offset found: " + std::to_string(hook_ptr - gamecore_module->baseAddress)).c_str());
}
}
getGoldSetter_address = hook_ptr;
return MemBlock(&process->memory(), hook_ptr, true);
}
ModuleDataPtr HackState::getDll(bool inject) {
// Inject or search custom dll
auto& modules = process->modules();
auto inject_civilization = modules.GetModule(L"inject_civilization.dll");
if (!inject_civilization && inject) {
std::wstring dll_dir = Utils::GetExeDirectory() + L"\\inject_civilization.dll";
auto result = modules.Inject(dll_dir);
if (!result.success()) {
throw HackStateException("Error", "Injection of inject_civilization.dll failed!");
} else {
inject_civilization = result.result();
}
}
return inject_civilization;
}
bool HackState::writePayload() {
ptr_t dll_callback;
ptr_t target_dll_config;
{
auto& modules = process->modules();
auto inject_civilization = getDll(true);
auto result = modules.GetExport(inject_civilization, "dll_callback");
if (!result.success()) {
throw HackStateException("Error", "Export symbol dll_callback not found!");
}
dll_callback = result.result().procAddress;
result = modules.GetExport(inject_civilization, "dll_config");
if (!result.success()) {
throw HackStateException("Error", "Export symbol dll_config not found!");
}
target_dll_config = result.result().procAddress;
}
// JIT Assembler
#define code_offset hook_code.size()
auto asmPtr = AsmFactory::GetAssembler(process->core().isWow64());
if (asmPtr) {
auto& a = *asmPtr;
a->setBaseAddress(getGoldSetter().ptr());
//a->jmp(noskipConditional);
a->mov(asmjit::x86::rax, dll_callback);
a->call(asmjit::x86::rax);
while (a->getCodeSize() < code_offset)
a->nop();
assert(a->getCodeSize() == code_offset);
size_t code_size = a->getCodeSize();
auto code = a->make();
printf("Writing to 0x%llx\n", getGoldSetter().ptr());
for (int i = 0; i < code_size; ++i) {
printf("\\x%x", 0xFF & (uint32_t)((uint8_t*)code)[i]);
}
printf("\n");
// TODO: Return statements here do not free "code"
if (NTSTATUS status = getGoldSetter().Write(0, code_size, code); !NT_SUCCESS(status)) {
throw HackStateException("Error", "Block writing 2 failed! Status: " + std::to_string(status) + "!");
return false;
}
gold_adding_overwritten = true;
a->getRuntime()->release(code);
return true;
} else {
throw HackStateException("Error", "Assembler init failed!");
return false;
}
}
void HackState::syncDllConfig(bool push) {
ptr_t target_dll_config;
{
auto& modules = process->modules();
auto inject_civilization = getDll(true);
auto result = modules.GetExport(inject_civilization, "dll_config");
if (!result.success()) {
throw HackStateException("Error", "Export symbol dll_config not found!");
}
target_dll_config = result.result().procAddress;
}
if (push) {
if (NTSTATUS status = MemBlock(&process->memory(), target_dll_config, true).Write(0, sizeof(dll_config), &dll_config); !NT_SUCCESS(status)) {
throw HackStateException("Error", "Block writing dll_config failed! Status: " + std::to_string(status) + "!");
}
} else {
size_t val1 = sizeof(dll_config);
if (NTSTATUS status = MemBlock(&process->memory(), target_dll_config, true).Read(0, val1, &dll_config); !NT_SUCCESS(status)) {
throw HackStateException("Error", "Block read dll_config failed! Status: " + std::to_string(status) + "!");
}
}
}
bool HackState::resetProcessCode() {
try {
// Attach process and THEN check if *this* process instance was patched
if (attachProcess()) {
if (gold_adding_overwritten) {
MemBlock gold_setter = getGoldSetter();
gold_setter.Write(0, hook_code.size(), hook_code.data());
gold_adding_overwritten = false;
}
// Module unloading crashes process
/*if (auto inject_civilization = getDll(false); inject_civilization) {
auto& modules = process->modules();
modules.Unload(inject_civilization);
}*/
process->Detach();
return true;
}
}
catch (const HackStateException& e) {
showMessageBox(e.caption.c_str(), e.text.c_str());
process->Detach();
return false;
}
return false;
}
bool HackState::hack(std::vector<std::pair<uint64_t, uint32_t>>& pointers, OverwriteMode mode, uint64_t selected_address) {
try {
if (!attachProcess())
throw HackStateException("Failed", "attachProcess()");
if (!writePayload())
throw HackStateException("Failed", "writePayload()");
if (!update_hack_state(pointers, mode, selected_address))
throw HackStateException("Failed", "update_hack_state()");
}
catch (const HackStateException& e) {
showMessageBox(e.caption.c_str(), e.text.c_str());
if (process)
process->Detach();
return false;
}
// Always detach from process
process->Detach();
return true;
}
std::optional<HackState> hackstate;
bool HackState::update_hack_state(std::vector<std::pair<uint64_t, uint32_t>>& pointers, OverwriteMode mode, uint64_t selected_address) {
syncDllConfig(false);
dll_config.overwrite_mode = mode;
dll_config.overwrite_pValue = selected_address;
uint32_t gold_value = 1000;
dll_config.overwrite_value = gold_value << 8; // fixed decimal point integer
dll_config.keep_overwrite_mode = true;
syncDllConfig(true);
pointers.clear();
for (int i = 0; i < sizeof(dll_config.pValue) / sizeof(dll_config.pValue[0]); ++i) {
std::pair<uint64_t, uint32_t> data_pair = std::make_pair(dll_config.pValue[i], dll_config.value[i]);
pointers.push_back(data_pair);
}
return true;
}
bool hack(std::vector<std::pair<uint64_t, uint32_t>>& pointers, OverwriteMode mode, uint64_t selected_address) {
if (!hackstate && mode == OverwriteMode::Init)
hackstate.emplace();
if (hackstate) {
if (hackstate->hack(pointers, mode, selected_address)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
bool deinit_hack() {
hackstate.reset();
return true;
}
}