-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAoBSwap.cpp
More file actions
234 lines (200 loc) · 10.9 KB
/
Copy pathAoBSwap.cpp
File metadata and controls
234 lines (200 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
#include "pch.h"
#include "AoBSwap.h"
#include "Logger.h"
#include "ScanMemory.h"
#include "Util.h"
const std::vector<BYTE> RETURN_OP = StringToByteVector("C3");
const std::vector<BYTE> ONE_BYTE_NOP = StringToByteVector("90");
const std::vector<BYTE> TWO_BYTE_NOP = StringToByteVector("66 90");
const std::vector<BYTE> THREE_BYTE_NOP = StringToByteVector("0F 1F 00");
const std::vector<BYTE> FOUR_BYTE_NOP = StringToByteVector("0F 1F 40 00");
const std::vector<BYTE> FIVE_BYTE_NOP = StringToByteVector("0F 1F 44 00 00");
const std::vector<BYTE> SIX_BYTE_NOP = StringToByteVector("66 0F 1F 44 00 00");
const std::vector<BYTE> SEVEN_BYTE_NOP = StringToByteVector("0F 1F 80 00 00 00 00");
const std::vector<BYTE> EIGHT_BYTE_NOP = StringToByteVector("0F 1F 84 00 00 00 00 00");
const std::vector<BYTE> NINE_BYTE_NOP = StringToByteVector("66 0F 1F 84 00 00 00 00 00");
const std::vector<BYTE> TEN_BYTE_NOP = StringToByteVector("66 66 0F 1F 84 00 00 00 00 00");
AoBSwap::AoBSwap(const std::string& targetModule, const std::vector<BYTE>& bytesToFind, const std::vector<BYTE>& bytesToReplace) {
this->targetModule_ = targetModule;
this->bytesToFind_ = bytesToFind;
this->bytesToReplace_ = bytesToReplace;
}
std::vector<const BYTE*> AoBSwap::ScanAndPatch() const {
auto addressesFound = ScanMemory(targetModule_, bytesToFind_);
for (auto address : addressesFound) {
auto size = bytesToReplace_.size();
DoWithProtect(const_cast<BYTE*>(address), size, [address, size, this]() {
memcpy(const_cast<BYTE*>(address), bytesToReplace_.data(), size);
});
}
return addressesFound;
}
std::vector<BYTE> IntToByteArray(const UINT64 value) {
auto buffer = std::vector<BYTE>(sizeof(UINT64));
for (UINT64 i = 0; i < sizeof(UINT64); i++) {
buffer[i] = value >> 8 * i & 0xFF;
}
return buffer;
}
std::vector<BYTE> IntToByteArray(const UINT32 value) {
auto buffer = std::vector<BYTE>(sizeof(UINT32));
for (UINT32 i = 0; i < sizeof(UINT32); i++) {
buffer[i] = value >> 8 * i & 0xFF;
}
return buffer;
}
std::vector<BYTE> CreateCallBytesToAddress(const BYTE* targetAddress, const BYTE* fromAddress) {
// First check if we're close enough to jump via an 8 byte offset.
const auto offset = static_cast<int>(targetAddress - (fromAddress + 5)); // +5 for the `call` op.
if (std::abs(offset) < INT32_MAX) {
// Small enough to do a relative call.
auto callBytes = std::vector<BYTE>{0xE8}; // call
for (auto byte : IntToByteArray(static_cast<UINT32>(offset))) {
callBytes.push_back(byte);
}
return callBytes;
}
// Create `call` bytes. e.g. FF15 02000000 EB08 30A08D2100000000 - call 218DA030
auto replacementBytes = std::vector<BYTE>{0xFF, 0x15, 0x02, 0x00, 0x00, 0x00, 0xEB, 0x08}; // x64 long call, add target address to the end (8 bytes).
for (auto byte : IntToByteArray(reinterpret_cast<UINT64>(targetAddress))) {
replacementBytes.push_back(byte);
}
return replacementBytes;
}
std::vector<BYTE> CreateJumpBytesToAddress(const BYTE* targetAddress, const BYTE* fromAddress, LogBuffer* logBuffer) {
// First check if we're close enough to jump via an 8 byte offset.
const auto offset = static_cast<int>(targetAddress - (fromAddress + 5)); // +5 for the `jmp` op.
if (std::abs(offset) >= INT32_MAX) {
LOG_BUFFER("Unable to create a `jmp` to an offset that exceeds INT32_MAX.");
return {};
}
// Small enough to do a relative jump.
auto callBytes = std::vector<BYTE>{0xE9}; // jmp
for (auto byte : IntToByteArray(static_cast<UINT32>(offset))) {
callBytes.push_back(byte);
}
return callBytes;
}
std::vector<std::string> SplitStringBySpace(const std::string& input) {
auto inStream = std::istringstream(input);
std::vector<std::string> outStrings;
std::string s;
while (getline(inStream, s, ' ')) {
outStrings.push_back(s);
}
return outStrings;
}
std::vector<BYTE> StringToByteVector(const std::string& input) {
const auto stringBytes = SplitStringBySpace(input);
std::vector<BYTE> bytes;
bytes.reserve(stringBytes.size());
for (const auto& s : stringBytes) {
bytes.push_back(static_cast<BYTE>(std::stoul(s, nullptr, 16)));
}
return bytes;
}
bool DoSimplePatch(const std::string& moduleName, const PTR_SIZE moduleAddress, const std::string& scanName, const std::string& scanBytes, const std::vector<BYTE>& newMemBytes, LogBuffer* logBuffer) {
ScanOptions scanOptions;
scanOptions.moduleName = &moduleName;
scanOptions.moduleAddress = moduleAddress;
scanOptions.patchType = PatchType::SIMPLE;
scanOptions.scanBytes = scanBytes;
scanOptions.newMemBytes = newMemBytes;
return DoPatch(scanName, scanOptions, logBuffer);
}
bool DoInjectPatch(const std::string& moduleName, const PTR_SIZE moduleAddress, const std::string& scanName, const std::string& scanBytes, const PTR_SIZE originalOpSize, AllocateMemory* allocator, const std::vector<BYTE>& newMemBytes, LogBuffer* logBuffer) {
ScanOptions scanOptions;
scanOptions.moduleName = &moduleName;
scanOptions.moduleAddress = moduleAddress;
scanOptions.patchType = PatchType::INJECT;
scanOptions.scanBytes = scanBytes;
scanOptions.newMemBytes = newMemBytes;
scanOptions.originalOpSize = originalOpSize;
scanOptions.allocator = allocator;
scanOptions.returnType = ReturnType::RETURN;
return DoPatch(scanName, scanOptions, logBuffer);
}
bool DoPatchInternal(const BYTE* const& injectAddress, ScanOptions scanOptions, LogBuffer* logBuffer) {
LOG_BUFFER("Inject address: " << PRINT_RELATIVE_ADDRESS(*scanOptions.moduleName, scanOptions.moduleAddress, injectAddress));
LOG_BUFFER("New mem bytes: " << BytesToString(scanOptions.newMemBytes));
switch (scanOptions.patchType) {
case PatchType::SIMPLE: //
DoWithProtect(const_cast<BYTE*>(injectAddress), scanOptions.newMemBytes.size(), [injectAddress, scanOptions] {
memcpy(const_cast<BYTE*>(injectAddress), scanOptions.newMemBytes.data(), scanOptions.newMemBytes.size()); // NOLINT(performance-no-int-to-ptr)
}, logBuffer);
return true;
case PatchType::INJECT: //
auto allocateSize = scanOptions.newMemBytes.size();
switch (scanOptions.returnType) {
case ReturnType::RETURN: allocateSize += 1;
break;
case ReturnType::CALL:
case ReturnType::JUMP: allocateSize += 5;
break;
}
const auto newMemStart = scanOptions.allocator->ReserveSpaceInAllocatedNewMem(allocateSize); // NOLINT(performance-no-int-to-ptr)
LOG_BUFFER("New mem address: " << std::uppercase << std::hex << reinterpret_cast<const UINT64>(newMemStart));
if (newMemStart == nullptr) {
LOG_BUFFER("Error: New mem address is 0, aborting.");
return false;
}
if (scanOptions.returnType == ReturnType::RETURN) {
scanOptions.newMemBytes.push_back(0xC3); // ret
} else if (scanOptions.returnType == ReturnType::JUMP || scanOptions.returnType == ReturnType::CALL) {
const auto jumpOrCall = scanOptions.returnType == ReturnType::JUMP ? "jump" : "call";
std::vector<BYTE> returnBytes;
if (scanOptions.returnType == ReturnType::JUMP) {
returnBytes = CreateJumpBytesToAddress(scanOptions.returnAddress, static_cast<BYTE*>(newMemStart) + scanOptions.newMemBytes.size(), logBuffer);
} else if (scanOptions.returnType == ReturnType::CALL) {
returnBytes = CreateCallBytesToAddress(scanOptions.returnAddress, static_cast<BYTE*>(newMemStart) + scanOptions.newMemBytes.size());
}
scanOptions.newMemBytes.insert(scanOptions.newMemBytes.end(), returnBytes.begin(), returnBytes.end());
LOG_BUFFER("Wrote return " << jumpOrCall << " to: " << PRINT_RELATIVE_ADDRESS(*scanOptions.moduleName, scanOptions.moduleAddress, scanOptions.returnAddress));
LOG_BUFFER("Return " << jumpOrCall << " bytes: " << BytesToString(returnBytes));
}
memcpy(newMemStart, scanOptions.newMemBytes.data(), scanOptions.newMemBytes.size());
const auto jumpOrCall = scanOptions.jumpType == JumpType::JUMP ? "jump" : "call";
std::vector<BYTE> callBytes; // Create a 'call' to inject to jump to our code.
if (scanOptions.jumpType == JumpType::JUMP) {
callBytes = CreateJumpBytesToAddress(static_cast<const BYTE*>(newMemStart), injectAddress, logBuffer);
} else if (scanOptions.jumpType == JumpType::CALL) {
callBytes = CreateCallBytesToAddress(static_cast<const BYTE*>(newMemStart), injectAddress);
}
if (callBytes.size() > scanOptions.originalOpSize) {
LOG_BUFFER("Error: Generated " << jumpOrCall << " bytes are too long, aborting.");
LOG_BUFFER("Generated " << jumpOrCall << " bytes: " << BytesToString(callBytes));
return false;
}
while (callBytes.size() < scanOptions.originalOpSize) {
callBytes.push_back(0x90); // nop
}
DoWithProtect(const_cast<BYTE*>(injectAddress), callBytes.size(), [injectAddress, callBytes] {
memcpy(const_cast<BYTE*>(injectAddress), callBytes.data(), callBytes.size()); // NOLINT(performance-no-int-to-ptr)
}, logBuffer);
LOG_BUFFER("Wrote " << jumpOrCall << " to inject mem: " << BytesToString(callBytes));
return true;
}
return false; // Not reachable.
}
bool DoPatch(const std::string& scanName, const ScanOptions& scanOptions, LogBuffer* logBuffer) {
LOG_BUFFER("");
LOG_BUFFER("Scanning for " << scanName << " bytes.");
const auto shortCircuit = scanOptions.scanType == ScanType::SINGLE;
const auto startAddress = scanOptions.startAddress == 0 ? nullptr : reinterpret_cast<BYTE*>(scanOptions.startAddress); // NOLINT(performance-no-int-to-ptr)
const auto addresses = ScanMemory(*scanOptions.moduleName, scanOptions.scanBytes, false, shortCircuit, startAddress, logBuffer);
LOG_BUFFER("Found " << addresses.size() << " match(es).");
if (addresses.empty()) {
LOG_BUFFER("AoB scan returned no results, aborting.");
return false;
}
switch (scanOptions.scanType) {
case ScanType::SINGLE: //
return DoPatchInternal(addresses[0] + scanOptions.injectOffset, scanOptions, logBuffer);
case ScanType::MULTIPLE: //
for (const auto& address : addresses) {
if (!DoPatchInternal(address + scanOptions.injectOffset, scanOptions, logBuffer)) return false;
}
return true;
}
return false; // Not reachable.
}