-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPatternV.cpp
More file actions
417 lines (345 loc) · 12.5 KB
/
PatternV.cpp
File metadata and controls
417 lines (345 loc) · 12.5 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include <iostream>
#include <filesystem>
#include <string>
#include <fstream>
#include <regex>
#include <chrono>
#include <thread>
#include <mutex>
#include <vector>
#include <future>
#include <semaphore>
#include <cstring>
bool useColors = true;
bool hideTime = false;
bool minifiedOutput = false;
#define RED (useColors ? "\033[31m" : "")
#define GREEN (useColors ? "\033[32m" : "")
#define YELLOW (useColors ? "\033[33m" : "")
#define RESET (useColors ? "\033[0m" : "")
namespace fs = std::filesystem;
constexpr auto TARGET_EXTENSION_EXE = ".exe";
constexpr auto TARGET_EXTENSION_TEXT = ".text";
struct ResultLine {
int build;
std::string line;
};
struct SectionInfo {
size_t rawOffset;
size_t rawSize;
};
std::counting_semaphore<> sem(std::thread::hardware_concurrency());
std::vector<std::optional<uint8_t>> parseBytePattern(const std::string& input)
{
std::vector<std::optional<uint8_t>> pattern;
std::istringstream stream(input);
std::string byteStr;
while(stream >> byteStr)
{
if (byteStr == "?" || byteStr == "??")
{
pattern.push_back(std::nullopt);
}
else
{
try
{
uint8_t byte = static_cast<uint8_t>(std::stoul(byteStr, nullptr, 16));
pattern.push_back(byte);
}
catch (...)
{
std::cerr << "Invalid byte: " << byteStr << "\n";
}
}
}
return pattern;
}
std::vector<size_t> searchAllPatternOffsets(const uint8_t* data, size_t size, const std::vector<std::optional<uint8_t>>& pattern) {
std::vector<size_t> matches;
if (size < pattern.size()) return matches;
for (size_t i = 0; i <= size - pattern.size(); ++i) {
bool matched = true;
for (size_t j = 0; j < pattern.size(); ++j) {
if (pattern[j].has_value() && data[i + j] != pattern[j].value()) {
matched = false;
break;
}
}
if (matched) matches.push_back(i);
}
return matches;
}
std::vector<uint8_t> readFile(const fs::path& filepath) {
FILE* file = nullptr;
#ifdef _WIN32
if (fopen_s(&file, filepath.string().c_str(), "rb") != 0 || !file) {
std::cerr << "Failed to open: " << filepath << '\n';
return {};
}
#else
file = fopen(filepath.string().c_str(), "rb");
if (!file) {
std::cerr << "Failed to open: " << filepath << '\n';
return {};
}
#endif
if (std::fseek(file, 0, SEEK_END) != 0) {
fclose(file);
std::cerr << "fseek() failed on: " << filepath << '\n';
return {};
}
long size = std::ftell(file);
if (size <= 0) {
fclose(file);
std::cerr << "Empty or invalid file: " << filepath << '\n';
return {};
}
rewind(file);
std::vector<uint8_t> buffer(size);
if (std::fread(buffer.data(), 1, size, file) != static_cast<size_t>(size)) {
fclose(file);
std::cerr << "Failed to read: " << filepath << '\n';
return {};
}
fclose(file);
return buffer;
}
std::string extractGameName(const std::string& filename) {
std::string nameOnly = filename.substr(0, filename.find_last_of('.'));
size_t dashPos = nameOnly.find('-');
size_t underscorePos = nameOnly.find('_');
size_t sepPos = std::min(
dashPos == std::string::npos ? nameOnly.size() : dashPos,
underscorePos == std::string::npos ? nameOnly.size() : underscorePos
);
if (sepPos == std::string::npos) return nameOnly;
return nameOnly.substr(0, sepPos);
}
std::optional<std::string> extractBuildNumber(const std::string& filename) {
std::regex pattern(R"((\d{4}))");
std::smatch match;
if (std::regex_search(filename, match, pattern)) {
return match[1];
}
return std::nullopt;
}
std::optional<SectionInfo> getTextSection(const std::vector<uint8_t>& buffer) {
if (buffer.size() < 0x1000) return std::nullopt;
const uint32_t dosSignature = *reinterpret_cast<const uint16_t*>(&buffer[0x00]);
if (dosSignature != 0x5A4D) return std::nullopt; // MZ
const uint32_t peOffset = *reinterpret_cast<const uint32_t*>(&buffer[0x3C]);
if (peOffset + 0x18 >= buffer.size()) return std::nullopt;
const uint32_t peSignature = *reinterpret_cast<const uint32_t*>(&buffer[peOffset]);
if (peSignature != 0x00004550) return std::nullopt; // PE\0\0
const uint16_t numberOfSections = *reinterpret_cast<const uint16_t*>(&buffer[peOffset + 6]);
const uint16_t sizeOfOptionalHeader = *reinterpret_cast<const uint16_t*>(&buffer[peOffset + 20]);
size_t sectionTableOffset = peOffset + 24 + sizeOfOptionalHeader;
for (int i = 0; i < numberOfSections; ++i) {
if (sectionTableOffset + 40 > buffer.size()) break;
const char* name = reinterpret_cast<const char*>(&buffer[sectionTableOffset]);
if (std::strncmp(name, ".text", 5) == 0) {
const uint32_t rawDataPtr = *reinterpret_cast<const uint32_t*>(&buffer[sectionTableOffset + 20]);
const uint32_t rawSize = *reinterpret_cast<const uint32_t*>(&buffer[sectionTableOffset + 16]);
if (rawDataPtr + rawSize <= buffer.size()) {
return SectionInfo{ rawDataPtr, rawSize };
}
}
sectionTableOffset += 40;
}
return std::nullopt;
}
void scanFile(const fs::path& filePath, const std::vector<std::optional<uint8_t>>& pattern,
std::mutex& outputMutex, std::vector<ResultLine>& outputBuffer)
{
const auto filename = filePath.filename().string();
const auto buffer = readFile(filePath);
if (buffer.empty()) return;
const uint8_t* textSegment = nullptr;
size_t textSize = 0;
if (filePath.extension() == TARGET_EXTENSION_TEXT) {
textSegment = buffer.data();
textSize = buffer.size();
} else {
auto textSection = getTextSection(buffer);
if (!textSection.has_value()) {
std::lock_guard lock(outputMutex);
std::cerr << RED << "[-] .text section not found in: " << filename << RESET << '\n';
return;
}
textSegment = buffer.data() + textSection->rawOffset;
textSize = textSection->rawSize;
}
const auto gameName = extractGameName(filename);
const auto build = extractBuildNumber(filename).value_or(filename);
const auto matches = searchAllPatternOffsets(textSegment, textSize, pattern);
std::ostringstream oss;
if (!matches.empty()) {
if (minifiedOutput) {
oss << GREEN << "[+] " << RESET << gameName << "_" << build << " (" << matches.size() << " matches): ";
for (size_t i = 0; i < matches.size(); ++i) {
oss << "0x" << std::hex << std::uppercase << matches[i];
if (i != matches.size() - 1)
oss << ", ";
}
} else {
oss << GREEN << "[+]" << RESET << " Pattern found in " << gameName << " v" << YELLOW << build
<< RESET << " (" << matches.size() << " matches): ";
for (size_t i = 0; i < matches.size(); ++i) {
oss << YELLOW << "0x" << std::hex << std::uppercase << matches[i] << RESET;
if (i != matches.size() - 1)
oss << ", ";
}
}
} else {
if (minifiedOutput) {
oss << RED << "[-] " << RESET << gameName << "_" << build << " (0 matches)";
} else {
oss << RED << "[-]" << RESET << " Pattern not found in " << gameName << " v" << YELLOW << build << RESET;
}
}
{
std::lock_guard lock(outputMutex);
try {
int buildNum = std::stoi(build);
outputBuffer.push_back({ buildNum, oss.str() });
} catch (...) {
outputBuffer.push_back({ 0, oss.str() });
}
}
}
void scanFileLimited(const fs::path& filePath, const std::vector<std::optional<uint8_t>>& pattern, std::mutex& outputMutex, std::vector<ResultLine>& outputBuffer)
{
sem.acquire();
scanFile(filePath, pattern, outputMutex, outputBuffer);
sem.release();
}
bool scanDirectory(const fs::path& folderPath, const std::vector<std::optional<uint8_t>>& pattern) {
using namespace std::chrono;
const auto start = high_resolution_clock::now();
std::vector<ResultLine> outputBuffer;
std::mutex outputMutex;
std::vector<std::future<void>> futures;
std::vector<fs::path> buildFiles;
for (const auto& entry : fs::directory_iterator(folderPath)) {
if (entry.is_regular_file()) {
auto ext = entry.path().extension().string();
if (ext == TARGET_EXTENSION_EXE || ext == TARGET_EXTENSION_TEXT) {
buildFiles.push_back(entry.path());
}
}
}
for (const auto& path : buildFiles) {
futures.push_back(std::async(std::launch::async, scanFileLimited,
path, std::cref(pattern),
std::ref(outputMutex), std::ref(outputBuffer)));
}
for (auto& f : futures) f.get();
bool allFound = true;
{
std::lock_guard lock(outputMutex);
std::sort(outputBuffer.begin(), outputBuffer.end(),
[](const ResultLine& a, const ResultLine& b) {
return a.build < b.build;
});
for (const auto& result : outputBuffer) {
std::cout << result.line << '\n';
if (result.line.find("Pattern not found") != std::string::npos) {
allFound = false;
}
}
}
const auto end = high_resolution_clock::now();
if (!hideTime) {
std::cout << "\n[~] Scan completed in "
<< duration_cast<milliseconds>(end - start).count()
<< " ms\n";
}
return allFound;
}
void extractTextSections(const fs::path& folderPath) {
for (const auto& entry : fs::directory_iterator(folderPath)) {
if (!entry.is_regular_file() || entry.path().extension() != TARGET_EXTENSION_EXE)
continue;
const auto buffer = readFile(entry.path());
if (buffer.empty())
continue;
auto textSection = getTextSection(buffer);
if (!textSection.has_value()) {
std::cerr << RED << "[-] .text section not found in: "
<< entry.path().filename().string() << RESET << '\n';
continue;
}
fs::path outPath = entry.path();
outPath += ".text";
std::ofstream outFile(outPath, std::ios::binary);
if (!outFile) {
std::cerr << RED << "[-] Failed to create: " << outPath << RESET << '\n';
continue;
}
outFile.write(
reinterpret_cast<const char*>(buffer.data() + textSection->rawOffset),
textSection->rawSize
);
std::cout << GREEN << "[+]" << RESET << " Extracted .text from "
<< entry.path().filename().string()
<< " -> " << outPath.filename().string()
<< " (" << textSection->rawSize << " bytes)\n";
}
}
int main(int argc, char* argv[])
{
fs::path folderPath = "Builds/";
std::string argPattern;
bool extractMode = false;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "--no-color") {
useColors = false;
} else if (arg == "--extract-text") {
extractMode = true;
} else if (arg == "--hide-time") {
hideTime = true;
} else if (arg == "--minified") {
minifiedOutput = true;
} else if (folderPath == "Builds/") {
folderPath = arg;
} else if (argPattern.empty()) {
argPattern = arg;
}
}
if (extractMode) {
extractTextSections(folderPath);
return 0;
}
if (!argPattern.empty())
{
auto pattern = parseBytePattern(argPattern);
if (pattern.empty())
{
std::cerr << "Invalid pattern provided as argument.\n";
return 1;
}
bool ok = scanDirectory(folderPath, pattern);
return ok ? 0 : 2;
}
if (!fs::exists(folderPath) || !fs::is_directory(folderPath)) {
std::cerr << "Can't find the builds path at: " << folderPath << ".\n";
return 1;
}
while(true)
{
std::cout << "> ";
std::string input;
std::getline(std::cin, input);
auto pattern = parseBytePattern(input);
if(pattern.empty())
{
std::cout << "Invalid pattern.\n";
break;
}
scanDirectory(folderPath, pattern);
std::cout << "\n";
}
return 0;
}