-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathRebuild.cpp
More file actions
274 lines (239 loc) · 8.98 KB
/
Copy pathRebuild.cpp
File metadata and controls
274 lines (239 loc) · 8.98 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
#include "Rebuild.h"
#include "Logger.h"
#include <filesystem>
#include <fstream>
#include <random>
#include <thread>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
std::string Rebuild::random_suffix_; // Initialize static member
Rebuild::Rebuild() {
// Generate suffix once per run
if (random_suffix_.empty()) {
random_suffix_ = generateRandomString(8);
}
}
std::string Rebuild::generateRandomString(size_t length) {
static const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, sizeof(charset) - 2);
std::string result;
result.reserve(length);
for (size_t i = 0; i < length; ++i) {
result += charset[dis(gen)];
}
return result;
}
bool Rebuild::isValidExecutable(const std::string& filePath) {
std::ifstream file(filePath, std::ios::binary);
if (!file) {
Logger::logWarning(std::format("Cannot open file for validation: {}", filePath));
return false;
}
char header[2];
file.read(header, 2);
file.close();
bool isValid = (header[0] == 'M' && header[1] == 'Z');
if (!isValid) {
Logger::logWarning(std::format("File is not a valid PE executable: {}", filePath));
}
return isValid;
}
std::string Rebuild::getCurrentExeName() {
char exePath[MAX_PATH];
DWORD pathLength = GetModuleFileNameA(NULL, exePath, MAX_PATH);
if (pathLength == 0) {
Logger::logWarning(std::format("Failed to get module filename: {}", GetLastError()));
return "ErScripts.exe";
}
std::string fullPath(exePath, pathLength);
size_t lastSlash = fullPath.find_last_of("\\/");
if (lastSlash != std::string::npos) {
return fullPath.substr(lastSlash + 1);
}
return fullPath;
}
std::string Rebuild::getCurrentExePath() {
char exePath[MAX_PATH];
DWORD pathLength = GetModuleFileNameA(NULL, exePath, MAX_PATH);
if (pathLength == 0) {
Logger::logWarning(std::format("Failed to get module path: {}", GetLastError()));
return "ErScripts.exe";
}
return std::string(exePath, pathLength);
}
bool Rebuild::unpackIfNeeded() {
std::string current_exe = getCurrentExePath();
// Read binary
std::ifstream in_file(current_exe, std::ios::binary);
if (!in_file) {
Logger::logWarning(std::format("Failed to read binary for unpacking: {}", current_exe));
return false;
}
std::vector<char> binary_data((std::istreambuf_iterator<char>(in_file)), std::istreambuf_iterator<char>());
in_file.close();
// Check for marker (2 bytes: random byte + 0xFF)
if (binary_data.size() < 2 || binary_data[binary_data.size() - 1] != 0xFF) {
return true; // Not packed
}
// Remove random byte and marker
binary_data.pop_back(); // Marker (0xFF)
binary_data.pop_back(); // Random byte
// Write unpacked binary
for (int retry = 0; retry < 3; ++retry) {
std::ofstream out_file(current_exe, std::ios::binary);
if (out_file) {
out_file.write(binary_data.data(), binary_data.size());
out_file.close();
return true;
}
Logger::logWarning(std::format("Retry {}: Failed to write unpacked binary: {}", retry + 1, GetLastError()));
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
Logger::logWarning("Failed to unpack binary after retries");
return false;
}
bool Rebuild::createPolymorphicBinary(std::string& output_path) {
// Get TEMP directory
char temp_dir[MAX_PATH];
if (GetTempPathA(MAX_PATH, temp_dir) == 0) {
Logger::logWarning(std::format("Failed to get TEMP path: {}", GetLastError()));
return false;
}
// Use stored suffix
output_path = std::string(temp_dir) + "ErScripts_temp_" + random_suffix_ + ".exe";
// Check disk space (~10MB)
ULARGE_INTEGER freeBytes;
if (GetDiskFreeSpaceExA(temp_dir, &freeBytes, NULL, NULL)) {
if (freeBytes.QuadPart < 10 * 1024 * 1024) {
Logger::logWarning("Insufficient disk space for rebuild");
return false;
}
}
// Read current binary
std::string current_exe = getCurrentExePath();
std::ifstream in_file(current_exe, std::ios::binary);
if (!in_file) {
Logger::logWarning("Failed to read current binary for rebuild");
return false;
}
std::vector<char> binary_data((std::istreambuf_iterator<char>(in_file)), std::istreambuf_iterator<char>());
in_file.close();
// Append one random byte
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> byte_dis(0, 255);
binary_data.push_back(static_cast<char>(byte_dis(gen)));
// Append marker (0xFF)
binary_data.push_back(static_cast<char>(0xFF));
// Write temp binary
for (int retry = 0; retry < 3; ++retry) {
std::ofstream out_file(output_path, std::ios::binary);
if (out_file) {
out_file.write(binary_data.data(), binary_data.size());
out_file.close();
return true;
}
Logger::logWarning(std::format("Retry {}: Failed to write temp binary: {}", retry + 1, GetLastError()));
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
Logger::logWarning("Failed to write temp binary after retries");
return false;
}
bool Rebuild::replaceWithNewBinary(const std::string& newFilePath, const std::string& bat_path) {
if (!isValidExecutable(newFilePath)) {
Logger::logWarning("New binary is not a valid executable");
return false;
}
std::string current_exe = getCurrentExePath();
// Write batch file
for (int retry = 0; retry < 3; ++retry) {
std::ofstream bat_file(bat_path);
if (bat_file) {
bat_file << "@echo off\n";
bat_file << "timeout /t 1 >nul\n";
bat_file << "move /Y \"" << newFilePath << "\" \"" << current_exe << "\"\n";
bat_file << "if exist \"" << current_exe << "\" (\n";
bat_file << " start \"\" \"" << current_exe << "\" --run /requestuia\n";
bat_file << " del \"%~f0\"\n";
bat_file << ")\n";
bat_file.close();
break;
}
Logger::logWarning(std::format("Retry {}: Failed to create batch file: {}", retry + 1, GetLastError()));
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
if (!std::filesystem::exists(bat_path)) {
Logger::logWarning("Failed to create batch file after retries");
return false;
}
// Run batch file
SHELLEXECUTEINFOA sei = { sizeof(sei) };
sei.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_NO_CONSOLE;
sei.lpFile = bat_path.c_str();
sei.lpVerb = "open";
sei.nShow = SW_HIDE;
if (!ShellExecuteExA(&sei)) {
Logger::logWarning(std::format("Failed to execute batch file: {}", GetLastError()));
std::filesystem::remove(bat_path);
return false;
}
return true;
}
void Rebuild::cleanupTempFiles() {
char temp_dir[MAX_PATH];
if (GetTempPathA(MAX_PATH, temp_dir) == 0) {
Logger::logWarning(std::format("Failed to get TEMP path for cleanup: {}", GetLastError()));
return;
}
std::string temp_exe = std::string(temp_dir) + "ErScripts_temp_" + random_suffix_ + ".exe";
std::string temp_bat = std::string(temp_dir) + "rebuild_" + random_suffix_ + ".bat";
if (std::filesystem::exists(temp_exe)) {
std::filesystem::remove(temp_exe);
Logger::logInfo(std::format("Cleaned up temp file: {}", temp_exe));
}
if (std::filesystem::exists(temp_bat)) {
std::filesystem::remove(temp_bat);
Logger::logInfo(std::format("Cleaned up batch file: {}", temp_bat));
}
}
bool Rebuild::rebuildAndRelaunch(bool should_rebuild) {
if (!should_rebuild) {
// Unpack for --run
return unpackIfNeeded();
}
// Unpack before rebuild
if (!unpackIfNeeded()) {
Logger::logWarning("Failed to unpack binary before rebuild");
return false;
}
// Create new binary
std::string new_binary;
if (!createPolymorphicBinary(new_binary)) {
Logger::logWarning("Failed to create polymorphic binary");
return false;
}
// Create batch file in TEMP
char temp_dir[MAX_PATH];
if (GetTempPathA(MAX_PATH, temp_dir) == 0) {
Logger::logWarning(std::format("Failed to get TEMP path: {}", GetLastError()));
std::filesystem::remove(new_binary);
return false;
}
std::string bat_path = std::string(temp_dir) + "rebuild_" + random_suffix_ + ".bat";
// Replace and relaunch
if (replaceWithNewBinary(new_binary, bat_path)) {
Logger::logInfo("Rebuild initiated. Relaunching...");
return true;
}
else {
Logger::logWarning("Failed to replace binary for rebuild");
std::filesystem::remove(new_binary);
if (std::filesystem::exists(bat_path)) {
std::filesystem::remove(bat_path);
}
return false;
}
}