-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
460 lines (400 loc) · 21.8 KB
/
main.cpp
File metadata and controls
460 lines (400 loc) · 21.8 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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <windows.h>
#include <shellapi.h>
#include "Obfusk8Core.hpp"
#include "ObfuscationOptions.hpp"
// Opaque seed is provided by Obfusk8Core.hpp when needed
using namespace std;
std::vector<char> readBinaryFile(const std::string& filename) {
std::ifstream file(filename, std::ios::binary);
if (!file) {
throw std::runtime_error(OBFUSCATE_STRING("Cannot open input file: ") + filename);
}
return std::vector<char>((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
}
void writeBinaryFile(const std::string& filename, const std::vector<char>& data) {
std::ofstream file(filename, std::ios::binary);
if (!file) {
throw std::runtime_error(OBFUSCATE_STRING("Cannot open output file: ") + filename);
}
file.write(data.data(), data.size());
}
void obfuscate(std::vector<char>& data, char key, const ObfuscationOptions& opts) {
// MINIMAL OBFUSCATION: Only safe techniques that won't break execution
// Preserve PE file structure - only obfuscate code and data sections
// Find PE header and sections
if (data.size() < sizeof(IMAGE_DOS_HEADER)) {
std::cout << "File too small for PE header" << std::endl;
return;
}
IMAGE_DOS_HEADER* dosHeader = reinterpret_cast<IMAGE_DOS_HEADER*>(data.data());
if (dosHeader->e_magic != IMAGE_DOS_SIGNATURE) {
std::cout << "Not a valid PE file (DOS signature)" << std::endl;
return; // Not a valid PE file
}
IMAGE_NT_HEADERS* ntHeader = reinterpret_cast<IMAGE_NT_HEADERS*>(
data.data() + dosHeader->e_lfanew);
if (ntHeader->Signature != IMAGE_NT_SIGNATURE) {
std::cout << "Not a valid PE file (NT signature)" << std::endl;
return;
}
// Get section headers - handle both 32-bit and 64-bit PE files
IMAGE_SECTION_HEADER* sectionHeaders;
if (ntHeader->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
// 64-bit PE
sectionHeaders = reinterpret_cast<IMAGE_SECTION_HEADER*>(
reinterpret_cast<char*>(ntHeader) + sizeof(IMAGE_NT_HEADERS64));
} else {
// 32-bit PE
sectionHeaders = reinterpret_cast<IMAGE_SECTION_HEADER*>(
reinterpret_cast<char*>(ntHeader) + sizeof(IMAGE_NT_HEADERS32));
}
std::cout << "Found " << ntHeader->FileHeader.NumberOfSections << " sections" << std::endl;
// SAFE CODE MUTATION: Only mutate NOP instructions and padding areas
// This avoids corrupting actual executable code
for (int i = 0; i < ntHeader->FileHeader.NumberOfSections; ++i) {
IMAGE_SECTION_HEADER& section = sectionHeaders[i];
std::string sectionName(reinterpret_cast<char*>(section.Name), 8);
// Remove null padding for display
size_t nullPos = sectionName.find('\0');
if (nullPos != std::string::npos) sectionName = sectionName.substr(0, nullPos);
// Show raw section name bytes for debugging
std::cout << "Section " << i << ": '";
for (int j = 0; j < 8; ++j) {
char c = section.Name[j];
if (c >= 32 && c <= 126) {
std::cout << c;
} else if (c == 0) {
break;
} else {
std::cout << "?";
}
}
std::cout << "' (size: " << section.SizeOfRawData << " bytes, offset: " << section.PointerToRawData << ")" << std::endl;
// Only mutate .text section with safe code mutations
if (sectionName.find(".text") == 0) {
size_t sectionStart = section.PointerToRawData;
size_t sectionSize = section.SizeOfRawData;
std::cout << "Applying safe code mutation to .text section at offset " << sectionStart << " size " << sectionSize << std::endl;
if (sectionStart + sectionSize <= data.size()) {
int mutationsApplied = 0;
if (opts.mutate_nop_pairs) {
for (size_t j = sectionStart; j < sectionStart + sectionSize - 2; ++j) {
if (data[j] == 0x90 && data[j+1] == 0x90) {
data[j] = 0x89;
data[j+1] = 0xC0;
mutationsApplied++;
j++;
}
}
}
if (opts.mutate_single_nops) {
for (size_t j = sectionStart; j < sectionStart + sectionSize; ++j) {
if ((unsigned char)data[j] == 0x90) {
if (j + 2 < sectionStart + sectionSize) {
data[j] = 0x8D;
data[j+1] = 0x40;
data[j+2] = 0x00;
mutationsApplied++;
j += 2;
}
}
}
}
if (opts.padding_obfuscation) {
for (size_t j = sectionStart + sectionSize - 100; j < sectionStart + sectionSize; ++j) {
if (j < data.size() && data[j] == 0x00) {
data[j] = (char)(0xCC ^ (j & 0xFF));
mutationsApplied++;
}
}
}
if (opts.dead_code_insertion) {
size_t startScanDC = (sectionSize > 256) ? (sectionStart + sectionSize - 256) : sectionStart;
for (size_t o = startScanDC; o + 10 <= sectionStart + sectionSize; ++o) {
if (data[o] == 0x00 && data[o+1] == 0x00 && data[o+2] == 0x00 && data[o+3] == 0x00 && data[o+4] == 0x00 && data[o+5] == 0x00 && data[o+6] == 0x00 && data[o+7] == 0x00 && data[o+8] == 0x00 && data[o+9] == 0x00) {
data[o] = 0x3D; data[o+1] = 0xEF; data[o+2] = 0xBE; data[o+3] = 0xAD; data[o+4] = 0xDE; data[o+5] = 0x75; data[o+6] = 0x00; data[o+7] = 0x31; data[o+8] = 0xC0; data[o+9] = 0x90;
mutationsApplied += 10;
break;
}
}
}
if (opts.loop_unrolling) {
for (size_t j = sectionStart; j < sectionStart + sectionSize - 10; ++j) {
if ((unsigned char)data[j] == 0x40 && (unsigned char)data[j+1] == 0x83 && (unsigned char)data[j+2] == 0xF8) {
if (j + 15 < sectionStart + sectionSize) {
data[j] = 0x40;
data[j+1] = 0x40;
data[j+2] = 0x40;
data[j+3] = 0x90;
data[j+4] = 0x90;
mutationsApplied += 5;
j += 4;
}
}
}
}
if (opts.control_flow_flattening) {
for (size_t j = sectionStart; j + 6 <= sectionStart + sectionSize; ++j) {
if ((unsigned char)data[j] == 0x90 && (unsigned char)data[j+1] == 0x90 && (unsigned char)data[j+2] == 0x90 && (unsigned char)data[j+3] == 0x90 && (unsigned char)data[j+4] == 0x90 && (unsigned char)data[j+5] == 0x90) {
data[j] = 0x85; data[j+1] = 0xC0; data[j+2] = 0x75; data[j+3] = 0x05; data[j+4] = 0x31; data[j+5] = 0xC0;
mutationsApplied += 6;
j += 5;
}
}
}
std::cout << "Applied " << mutationsApplied << " safe code mutations" << std::endl;
} else {
std::cout << "Section out of bounds!" << std::endl;
}
}
}
if (opts.string_encryption) {
IMAGE_SECTION_HEADER* rdataSec = nullptr;
IMAGE_SECTION_HEADER* textSec = nullptr;
for (int i = 0; i < ntHeader->FileHeader.NumberOfSections; ++i) {
IMAGE_SECTION_HEADER& s = sectionHeaders[i];
std::string nm(reinterpret_cast<char*>(s.Name), 8); size_t p = nm.find('\0'); if (p != std::string::npos) nm = nm.substr(0, p);
if (nm.find(".rdata") == 0) rdataSec = &s;
if (nm.find(".text") == 0) textSec = &s;
}
std::vector<unsigned char> protectedMap;
if (rdataSec && textSec) {
protectedMap.assign(rdataSec->SizeOfRawData, 0);
size_t tStart = textSec->PointerToRawData, tEnd = tStart + textSec->SizeOfRawData;
size_t tRvaBase = textSec->VirtualAddress;
size_t rVA = rdataSec->VirtualAddress, rVEnd = rVA + rdataSec->Misc.VirtualSize;
size_t rStart = rdataSec->PointerToRawData, rEnd = rStart + rdataSec->SizeOfRawData;
if (ntHeader->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
for (size_t pos = tStart; pos + 4 <= tEnd; pos += 4) {
uint32_t val = *reinterpret_cast<uint32_t*>(&data[pos]);
if (val >= rVA && val < rVEnd) {
size_t off = rStart + (val - rVA);
if (off < rEnd) {
size_t k = off;
while (k < rEnd && data[k] != '\0') { protectedMap[k - rStart] = 1; ++k; }
}
}
}
} else if (ntHeader->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
for (size_t pos = tStart; pos + 7 <= tEnd; ++pos) {
unsigned char b = static_cast<unsigned char>(data[pos]);
size_t i = pos;
bool hasRex = (b >= 0x40 && b <= 0x4F);
if (hasRex) { ++i; if (i >= tEnd) break; }
unsigned char op = static_cast<unsigned char>(data[i]);
if (op == 0x8D || op == 0x8B) {
if (i + 6 <= tEnd) {
unsigned char modrm = static_cast<unsigned char>(data[i+1]);
if ((modrm & 0xC7) == 0x05) {
int32_t disp = *reinterpret_cast<int32_t*>(&data[i+2]);
size_t instrLen = (hasRex ? 1 : 0) + 1 + 1 + 4;
size_t instrRva = tRvaBase + (i - tStart);
int64_t tgtRvaSigned = static_cast<int64_t>(instrRva) + static_cast<int64_t>(instrLen) + static_cast<int64_t>(disp);
if (tgtRvaSigned >= 0) {
size_t tgtRva = static_cast<size_t>(tgtRvaSigned);
if (tgtRva >= rVA && tgtRva < rVEnd) {
size_t off = rStart + (tgtRva - rVA);
if (off < rEnd) {
size_t k = off;
while (k < rEnd && data[k] != '\0') { protectedMap[k - rStart] = 1; ++k; }
}
}
}
}
}
}
}
}
}
for (int i = 0; i < ntHeader->FileHeader.NumberOfSections; ++i) {
IMAGE_SECTION_HEADER& section = sectionHeaders[i];
std::string sectionName(reinterpret_cast<char*>(section.Name), 8);
if (sectionName.find(".rdata") == 0) {
size_t sectionStart = section.PointerToRawData;
size_t sectionSize = section.SizeOfRawData;
if (sectionStart + sectionSize <= data.size()) {
std::cout << "Encrypting strings in .rdata (excluding referenced)" << std::endl;
for (size_t j = sectionStart; j + 4 < sectionStart + sectionSize; ++j) {
if (!protectedMap.empty()) { if (protectedMap[j - sectionStart]) continue; } else { if (!(j >= sectionStart + 2 && data[j-1] == 0x00 && data[j-2] == 0x00)) continue; }
if (isprint((unsigned char)data[j]) && isprint((unsigned char)data[j+1]) && isprint((unsigned char)data[j+2]) && isprint((unsigned char)data[j+3])) {
size_t stringStart = j;
while (j < sectionStart + sectionSize && isprint((unsigned char)data[j])) {
data[j] ^= 0xAA;
j++;
}
std::cout << "Encrypted string at offset " << stringStart << std::endl;
if (j - stringStart > 0) {
std::cout << "Last byte offset: " << (j - 1) << std::endl;
}
j--;
}
}
}
}
}
}
if (ntHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress != 0) {
size_t importRVA = ntHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
size_t importSize = ntHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size;
std::cout << "Found import table at RVA " << importRVA << " size " << importSize << std::endl;
// Find the section containing import table
for (int i = 0; i < ntHeader->FileHeader.NumberOfSections; ++i) {
IMAGE_SECTION_HEADER& section = sectionHeaders[i];
if (importRVA >= section.VirtualAddress &&
importRVA < section.VirtualAddress + section.Misc.VirtualSize) {
size_t importOffset = section.PointerToRawData + (importRVA - section.VirtualAddress);
std::cout << "Import table in section " << i << " at file offset " << importOffset << std::endl;
if (importOffset + importSize <= data.size()) {
std::cout << "Encrypting import function names" << std::endl;
// Walk through import descriptors
size_t currentOffset = importOffset;
while (currentOffset + sizeof(IMAGE_IMPORT_DESCRIPTOR) <= importOffset + importSize) {
IMAGE_IMPORT_DESCRIPTOR* importDesc = reinterpret_cast<IMAGE_IMPORT_DESCRIPTOR*>(&data[currentOffset]);
// Check if this is a valid descriptor (Name RVA != 0)
if (importDesc->Name == 0) break;
if (opts.dll_name_encryption) {
importDesc->TimeDateStamp ^= 0xBB;
}
if (opts.function_name_encryption &&
importDesc->OriginalFirstThunk >= section.VirtualAddress &&
importDesc->OriginalFirstThunk < section.VirtualAddress + section.Misc.VirtualSize) {
size_t thunkOffset = section.PointerToRawData + (importDesc->OriginalFirstThunk - section.VirtualAddress);
size_t thunkIndex = 0;
while (thunkOffset + thunkIndex * 8 + 8 <= data.size()) {
uint64_t thunkValue = *reinterpret_cast<uint64_t*>(&data[thunkOffset + thunkIndex * 8]);
if (thunkValue == 0) break; // End of thunks
// If it's an import by name (high bit not set)
if ((thunkValue & 0x8000000000000000ULL) == 0) {
size_t nameRVA = (size_t)(thunkValue & 0xFFFFFFFFULL);
if (nameRVA >= section.VirtualAddress &&
nameRVA < section.VirtualAddress + section.Misc.VirtualSize) {
size_t hintOffset = section.PointerToRawData + (nameRVA - section.VirtualAddress);
if (hintOffset + 2 <= data.size()) {
data[hintOffset] ^= 0xCC;
data[hintOffset + 1] ^= 0xCC;
}
}
}
thunkIndex++;
if (thunkIndex > 1000) break; // Safety limit
}
}
if (opts.function_name_encryption &&
importDesc->FirstThunk >= section.VirtualAddress &&
importDesc->FirstThunk < section.VirtualAddress + section.Misc.VirtualSize) {
size_t ftOffset = section.PointerToRawData + (importDesc->FirstThunk - section.VirtualAddress);
if (ntHeader->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
size_t idx = 0;
while (ftOffset + idx * 8 + 8 <= data.size()) {
uint64_t v = *reinterpret_cast<uint64_t*>(&data[ftOffset + idx * 8]);
if (v == 0) break;
v ^= 0x5A5A5A5A5A5A5A5AULL;
*reinterpret_cast<uint64_t*>(&data[ftOffset + idx * 8]) = v;
++idx; if (idx > 1000) break;
}
} else {
size_t idx = 0;
while (ftOffset + idx * 4 + 4 <= data.size()) {
uint32_t v = *reinterpret_cast<uint32_t*>(&data[ftOffset + idx * 4]);
if (v == 0) break;
v ^= 0x5A5A5A5AU;
*reinterpret_cast<uint32_t*>(&data[ftOffset + idx * 4]) = v;
++idx; if (idx > 2000) break;
}
}
}
currentOffset += sizeof(IMAGE_IMPORT_DESCRIPTOR);
}
std::cout << "Import names encrypted" << std::endl;
}
break;
}
}
}
if (opts.debug_stripping && ntHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress != 0) {
size_t debugRVA = ntHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress;
// Find the section containing debug info
for (int i = 0; i < ntHeader->FileHeader.NumberOfSections; ++i) {
IMAGE_SECTION_HEADER& section = sectionHeaders[i];
if (debugRVA >= section.VirtualAddress &&
debugRVA < section.VirtualAddress + section.Misc.VirtualSize) {
size_t debugOffset = section.PointerToRawData + (debugRVA - section.VirtualAddress);
size_t debugSize = ntHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].Size;
if (debugOffset + debugSize <= data.size()) {
// Zero out debug information
std::fill(data.begin() + debugOffset, data.begin() + debugOffset + debugSize, 0);
std::cout << "Debug information stripped" << std::endl;
}
break;
}
}
}
std::cout << "Minimal obfuscation completed successfully" << std::endl;
}
int main(int argc, char* argv[]) {
// Check command line arguments for drag-and-drop or terminal mode
if (argc > 1) {
// Drag-and-drop mode
for (int i = 1; i < argc; ++i) {
string inputFile = argv[i];
if (inputFile.find(".exe") != string::npos) {
string outputFile = inputFile.substr(0, inputFile.find_last_of('.')) + "_obfuscated.exe";
char key = 0x42; // Default key
try {
cout << "Processing: " << inputFile << endl;
vector<char> data = readBinaryFile(inputFile);
ObfuscationOptions opts;
obfuscate(data, key, opts);
writeBinaryFile(outputFile, data);
cout << "Obfuscated: " << outputFile << endl;
} catch (const exception& e) {
cerr << "Error processing " << inputFile << ": " << e.what() << endl;
}
}
}
cout << "Press Enter to exit..." << endl;
cin.get();
return 0;
}
// Terminal-based binary obfuscator
cout << "Binary Obfuscator - An0n" << endl;
cout << "===================================" << endl;
cout << "Drag and drop .exe files onto this executable," << endl;
cout << "or enter file paths manually below." << endl;
cout << endl;
while (true) {
string inputFile;
cout << "Enter input .exe file path (or 'quit' to exit): ";
getline(cin, inputFile);
if (inputFile == "quit" || inputFile == "exit") {
break;
}
if (inputFile.empty()) {
cout << "No input file specified." << endl;
continue;
}
string outputFile = inputFile.substr(0, inputFile.find_last_of('.')) + "_obfuscated.exe";
char key = 0x42; // Default key
try {
cout << "Reading input file..." << endl;
vector<char> data = readBinaryFile(inputFile);
cout << "Applying obfuscation..." << endl;
ObfuscationOptions opts;
obfuscate(data, key, opts);
cout << "Writing output file..." << endl;
writeBinaryFile(outputFile, data);
cout << "Obfuscation complete!" << endl;
cout << "Output: " << outputFile << endl;
cout << endl;
} catch (const exception& e) {
cerr << "Error: " << e.what() << endl;
cout << endl;
}
}
return 0;
}
// Wrapper not needed; calls provide ObfuscationOptions explicitly