-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinit.cpp
194 lines (150 loc) · 5.91 KB
/
init.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
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
#include "init.h"
#include "includes.h"
#include "global.h"
#include "log.h"
#include "utils.h"
CX86Disasm64 dis;
bool InitCapstone()
{
if (dis.GetError())
{
Log("Failed to initialize capstone disassembler");
return false;
}
dis.SetDetail(CS_OPT_ON);
dis.SetSyntax(CS_OPT_SYNTAX_INTEL);
return true;
}
bool FindPatchingTargets()
{
Log("Searching addresses to patch...");
for (const auto& targetStringRef : Global::patchingTargets)
{
const auto stringRef = FindStringReferenceA(targetStringRef);
if (!stringRef)
{
Log("Couldn't find string reference to {}", targetStringRef);
return false;
}
const auto followingRetIntInsn = PatternScanFromStartExact({ 0xC3, 0xCC }, stringRef, 0x10000);
const auto insns = dis.Disasm(stringRef, followingRetIntInsn - stringRef, reinterpret_cast<size_t>(stringRef));
std::vector<string> instructionSignature = { "mov", "lea", "call" };
bool foundAddr = false;
for (size_t i = 0; i < insns->Count - instructionSignature.size(); ++i)
{
const auto curInsn = insns->Instructions(i);
bool found = true;
for (size_t j = 0; j < instructionSignature.size(); ++j)
{
if (string(insns->Instructions(i + j)->mnemonic) == instructionSignature[j])
continue;
found = false;
break;
}
if (!found)
continue;
/* Patching mov dl, 2 (B2 02) opcodes to change errors into warnings */
std::vector<byte> patch = { 0xB2, 0x01 }; // mov dl, 1
const auto insn = insns->Instructions(i);
const auto insnAddr = reinterpret_cast<byte*>(insn->address);
if (!(*insnAddr == 0xB2 && *(insnAddr + 1) == 0x02))
{
Log("Unexpected opcodes 0x{:X} ({})", reinterpret_cast<uintptr_t>(insnAddr), targetStringRef);
return false;
}
const auto byteAmount = patch.size();
Log("Found address to patch at 0x{:X} ({})", reinterpret_cast<uintptr_t>(insnAddr), targetStringRef);
string fromBytes = "";
string toBytes = "";
for (auto i = 0; i < byteAmount; ++i)
{
auto addr = insnAddr + i;
fromBytes += std::format("{:02X}", *addr);
toBytes += std::format("{:02X}", patch.at(i));
const auto isLastByte = i == byteAmount - 1;
if (!isLastByte)
{
fromBytes += " ";
toBytes += " ";
}
}
Log("-> {} will be replaced with {}", fromBytes, toBytes);
for (auto i = 0; i < byteAmount; ++i)
{
auto addr = insnAddr + i;
Global::targetAddresses.try_emplace(addr, std::make_pair(*addr, patch.at(i)));
}
foundAddr = true;
break;
}
if (!foundAddr)
{
Log("Couldn't find address for {}", targetStringRef);
return false;
}
}
return true;
}
bool FindGlobals()
{
Log("Searching globals...");
{
const auto stringRef = FindStringReferenceA("$SB_ERRORBODY_DUPLICATION_EXCEEDED");
const auto previousRetIntInsn = PatternScanExactReverse({ 0xC3, 0xCC }, stringRef, 0x1000);
const auto insns = dis.Disasm(previousRetIntInsn, stringRef - previousRetIntInsn, reinterpret_cast<size_t>(previousRetIntInsn));
for (size_t i = insns->Count - 1; i > 0; --i)
{
const auto curInsn = insns->Instructions(i);
/* target is cmp insn with dword ptr */
const auto isTargetInsn =
string(curInsn->mnemonic) == "cmp"
&& string(curInsn->op_str).find("rip") != string::npos;
if (!isTargetInsn)
continue;
Global::maxShipModulesPtr = reinterpret_cast<int*>(curInsn->address + curInsn->size + curInsn->detail->x86.disp);
Log("Found maxShipModules @ 0x{:X}", reinterpret_cast<uintptr_t>(Global::maxShipModulesPtr));
return true;
}
Log("Couldn't find maxShipModules global");
}
return false;
}
bool FindFunctions()
{
Log("Searching function addresses...");
const auto stringRef = FindStringReferenceA("SaveLoadTester Step");
if (!stringRef)
{
Log("Failed to find target string reference");
return false;
}
const auto followingIntInsn = PatternScanFromStartExact({ 0xC3, 0xCC }, stringRef, 0x1000);
if (!followingIntInsn)
{
Log("Failed to find function end");
return false;
}
const auto insns = dis.Disasm(stringRef, followingIntInsn - stringRef, reinterpret_cast<size_t>(stringRef));
for (size_t i = 0; i < insns->Count; ++i)
{
const bool isTargetInsn =
string(insns->Instructions(i)->mnemonic) == "call"
&& string(insns->Instructions(i + 1)->mnemonic) == "mov"
&& string(insns->Instructions(i + 2)->mnemonic) == "mov"
&& string(insns->Instructions(i + 3)->mnemonic) == "call";
if (!isTargetInsn)
continue;
const auto scaleFormManagerPtrInsn = insns->Instructions(i + 2);
const auto executeCommandInsn = insns->Instructions(i + 3);
Global::scaleformManagerPtr = reinterpret_cast<void**>(scaleFormManagerPtrInsn->address + 7 + scaleFormManagerPtrInsn->detail->x86.disp);
Global::executeCommandPtr = reinterpret_cast<void*>(executeCommandInsn->detail->x86.operands[0].imm);
break;
}
if (!Global::scaleformManagerPtr || !Global::executeCommandPtr)
{
Log("Failed to find function address");
return false;
}
Log("Found ExecuteCommand @ 0x{:X}", reinterpret_cast<uintptr_t>(Global::executeCommandPtr));
return true;
}