-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
534 lines (458 loc) · 10.8 KB
/
main.cpp
File metadata and controls
534 lines (458 loc) · 10.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
// NOTE():
/*
This was inspired by pe_to_shell code created by hasherezade for Windows. I decided to transport to Linux because I'm mostly a linux user and I wanted to understand how this works.
// Stub64 was created by hasherezade (https://github.com/hasherezade/pe_to_shellcode/tree/master/hldr64 ).
*/
//TODO():
// - 32bit Executables
#include <stdio.h>
#include "required/intrinsic.h"
#include "required/nix.h"
#include "required/memory.h"
#include "required/platform.h"
#include "required/assert.h"
#include "marray.h"
#include "stringz.h"
enum WIN_SUBSYSTEM
{
SUBSYSTEM_UNKNOWN,
SUBSYSTEM_NATIVE,
SUBSYSTEM_WINDOWS_GUI,
SUBSYSTEM_WINDOWS_CUI,
SUBSYSTEM_OS2_CUI,
SUBSYSTEM_POSIX_CUI,
SUBSYSTEM_NATIVE_WINDOWS,
SUBSYSTEM_WINDOWS_CE_GUI,
SUBSYSTEM_EFI_APPLICATION,
SUBSYSTEM_EFI_SERVICE_DRIVER,
SUBSYSTEM_EFI_RUNTIME_DRIVER,
SUBSYSTEM_EFI_ROM,
SUBSYSTEM_WINDOWS_BOOT_APP
};
enum IMAGE_NT_OP_HDR_MAGIC
{
HDR32_MAGIC = 0x10b,
HDR64_MAGIC = 0x20b,
IMAGE_HDR_MAGIC = 0x107
};
enum IMAGE_DIRECTORY
{
DIRECTORY_ENTRY_EXPORT,
DIRECTORY_ENTRY_IMPORT,
DIRECTORY_ENTRY_RESOURCES,
DIRECTORY_ENTRY_EXPECTION,
DIRECTORY_ENTRY_SECURITY,
DIRECTORY_ENTRY_BASELOG,
DIRECTORY_ENTRY_DEBUG,
DIRECTORY_ENTRY_ARCHITECTURE,
DIRECTORY_ENTRY_GLOBAL_PTR,
DIRECTORY_ENTRY_TS,
DIRECTORY_ENTRY_LOAD_CONFIG,
DIRECTORY_ENTRY_BOUND_IMPORT,
DIRECTORY_ENTRY_IAT,
DIRECTORY_ENTRY_DELAY_IMPORT,
DIRECTORY_ENTRY_TLS,
DIRECTORY_ENTRY_COM_DESCRIPTOR,
};
struct MS_DOS_HEADER
{
ui16 magic;
ui16 LastPageBytes;
ui16 PagesInFile;
ui16 Relocations;
ui16 SizeInHeader;
ui16 MinParagraph;
ui16 MaxParagraph;
ui16 SSValue;
ui16 SPValue;
ui16 CheckSum;
ui16 IPValue;
ui16 CSVALUE;
ui16 FileAddressOf;
ui16 OverlayNumber;
ui16 ReservedWordsFour[4];
ui16 OEMID;
ui16 OEMInfo;
ui16 ReservedWordsTen[10];
ui32 PEOffset;
};
struct PE_HEADER
{
ui32 Sign;
ui16 Machine;
ui16 NumOfSections;
ui32 TimeDateStamp;
ui32 PointerToSymbolTable;
ui32 NumberOfSymbols;
ui16 SizeOfOptionalHeader;
ui16 Characteristics;
};
struct data_directory
{
ui32 VirtualAddress;
ui32 size;
};
struct PE_OP_HEADER
{
ui16 magic;
};
struct PE_OP_HEADER64
{
ui16 magic;
uchar minorLinkVer;
uchar majorLinkVer;
ui32 sizeOfInitData;
ui32 sizeOfUnInitData;
ui32 addrOfEntryPoint;
ui32 baseOfCode;
ui64 ImageBase;
ui32 sectionAlign;
ui32 fileAlign;
ui16 majorOSVer;
ui16 minorOSVer;
ui16 majorImageVer;
ui16 minorImageVer;
ui16 majorSubVer;
ui16 minorSubVer;
ui32 win32VersionValue;
ui32 sizeOfImage;
ui32 sizeOfHeader;
ui32 checkSum;
ui16 subSystem;
ui16 DLLChar;
ui64 sizeOfStackReserve;
ui64 sizeOfStackCommit;
ui64 sizeOfHeapReserve;
ui64 sizeOfHeapCommit;
ui32 loaderFlag;
ui32 numberOfRvaAndSize;
data_directory DataDirectory[16];
};
struct PE_OP_HEADER32
{
ui16 magic;
uchar minorLinkVer;
uchar majorLinkVer;
ui32 sizeOfInitData;
ui32 sizeOfUnInitData;
ui32 addrOfEntryPoint;
ui32 baseOfCode;
ui32 ImageBase;
ui32 sectionAlign;
ui32 fileAlign;
ui16 majorOSVer;
ui16 minorOSVer;
ui16 majorImageVer;
ui16 minorImageVer;
ui16 majorSubVer;
ui16 minorSubVer;
ui32 win32VersionValue;
ui32 sizeOfImage;
ui32 sizeOfHeader;
ui32 checkSum;
ui16 subSystem;
ui16 DLLChar;
ui32 sizeOfStackReserve;
ui32 sizeOfStackCommit;
ui32 sizeOfHeapReserve;
ui32 sizeOfHeapCommit;
ui32 loaderFlag;
ui32 numberOfRvaAndSize;
data_directory DataDirectory[16];
};
bool PEIs64Bit(ui8 *appBytes)
{
bool result = false;
struct MS_DOS_HEADER *dosHeader = NULL;
struct PE_HEADER *peHeader = NULL;
struct PE_OP_HEADER *peOpHeader=NULL;
dosHeader = (MS_DOS_HEADER*) appBytes;
peHeader = (PE_HEADER *) ( ( appBytes) + dosHeader->PEOffset);
peOpHeader = (PE_OP_HEADER *) ( ( appBytes) + dosHeader->PEOffset+sizeof(PE_HEADER));
if (peOpHeader->magic == HDR64_MAGIC)
{
result = true;
}
return result;
}
//NOTE(): This code was taken from PE_TO_SHELLCODE https://github.com/hasherezade/pe_to_shellcode/
bool overwrite_hdr(ui8 *my_exe, size_t exe_size, ui32 raw)
{
ui8 redir_code[] = "\x4D" //dec ebp
"\x5A" //pop edx
"\x45" //inc ebp
"\x52" //push edx
"\xE8\x00\x00\x00\x00" //call <next_line>
"\x5B" // pop ebx
"\x48\x83\xEB\x09" // sub ebx,9
"\x53" // push ebx (Image Base)
"\x48\x81\xC3" // add ebx,
"\x59\x04\x00\x00" // value
"\xFF\xD3" // call ebx
"\xc3"; // ret
size_t offset = sizeof(redir_code) - 8;
memcpy(redir_code + offset, &raw, sizeof(ui32));
memcpy(my_exe, redir_code, sizeof(redir_code));
return true;
}
//NOTE(): This code was taken from PE_TO_SHELLCODE https://github.com/hasherezade/pe_to_shellcode/
ui8 * shellcodify(ui8 *my_exe, size_t exe_size, size_t &out_size, bool is64b)
{
out_size = 0;
size_t stub_size = 0;
FILE *fileStub = NULL;
size_t fileSize = 0;
size_t ext_size = 0;
ui8 *stub = NULL;
ui8 *ext_buf = NULL;
s32* stubsFile = NULL;
if (is64b)
{
stubsFile = S32("stub64.bin");
} else {
stubsFile = S32("stub32.bin");
}
fileStub = fopen(stubsFile, "rb");
if (fileStub)
{
fseek(fileStub,0,SEEK_END);
stub_size = ftell(fileStub);
rewind(fileStub);
stub = (ui8*) MemoryRaw(stub_size);
memset(stub,0,stub_size);
fread(stub,1,stub_size,fileStub);
ext_size = exe_size + stub_size;
ext_buf = (ui8*) MemoryRaw(ext_size);
memset(ext_buf,0,ext_size);
memcpy(ext_buf, my_exe, exe_size);
memcpy(ext_buf + exe_size, stub, stub_size);
ui32 raw_addr = exe_size;
overwrite_hdr(ext_buf, ext_size, raw_addr);
out_size = ext_size;
fclose(fileStub);
} else {
printf("Can not open stub file\n");
}
if (stub)
{
Free(stub);
stub = NULL;
}
if (stubsFile)
{
Free(stubsFile);
stubsFile=NULL;
}
return ext_buf;
}
void DisplayHelp()
{
printf("\n");
printf("-f <execute path> - Specify the exe to convert to shell code.\n");
printf("-o <output name> - Specify output shell code.\n");
printf("-x <string to xor> - String to XOR the shellcode to evade.\n");
printf("-l <length of xor string> - Specify length of the string XOR.\n");
printf("-h Display this\n");
}
int main(int argc, char *args[])
{
bool32 isArgValid = true;
bool32 isPEValid = true;
FILE *app = NULL;
ui8 *appBytes = NULL;
size_t fileSize = 0;
size_t outSize = 0;
ui8 *code = NULL;
struct MS_DOS_HEADER *dosHeader = NULL;
struct PE_HEADER *peHeader = NULL;
struct PE_OP_HEADER *peOpHeader=NULL;
struct PE_OP_HEADER64 *peOpHeader64=NULL;
struct PE_OP_HEADER32 *peOpHeader32=NULL;
ui8 *payload = NULL;
FILE *outFile = NULL;
s32 *filename = NULL;
s32 *outputFilename = NULL;
s32 *xorKey = NULL;
int xorLen = 0;
bool Is64Bits = false;
printf("------ Win32 EXE to Shellcode -----\n");
for (int argIndex = 0; argIndex < argc; argIndex++)
{
if (StrCmp(args[argIndex], "-f"))
{
if (argIndex+1 < argc)
{
filename = S32(args[argIndex+1]);
}
}
if (StrCmp(args[argIndex], "-o"))
{
if (argIndex+1 < argc)
{
outputFilename = S32(args[argIndex+1]);
}
}
if (StrCmp(args[argIndex], "-x"))
{
if (argIndex+1 < argc)
{
xorKey = S32(args[argIndex+1]);
}
}
if (StrCmp(args[argIndex], "-l"))
{
if (argIndex+1 < argc)
{
xorLen = SToI(args[argIndex+1]);
}
}
if (StrCmp(args[argIndex], "-h"))
{
isArgValid = false;
}
}
if (outputFilename == NULL)
{
outputFilename = S32("shellcode.sc");
}
if (isArgValid)
{
app = fopen(filename, "rb");
if (app)
{
fseek(app,0,SEEK_END);
fileSize = ftell(app);
rewind(app);
appBytes = (uint8*) MemoryRaw(fileSize);
fread(appBytes,1,fileSize,app);
fclose(app);
dosHeader = (MS_DOS_HEADER*) appBytes;
peHeader = (PE_HEADER *) ( ( appBytes) + dosHeader->PEOffset);
peOpHeader = (PE_OP_HEADER *) ( ( appBytes) + dosHeader->PEOffset+sizeof(PE_HEADER));
printf("Header magic %x\n", dosHeader->magic);
printf("Filename: %s\n", filename);
printf("Output name: %s\n", outputFilename);
if (dosHeader->magic == 0x4d5a || dosHeader->magic == 0x5a4d)
{
printf("Appication Type: ");
Is64Bits = PEIs64Bit(appBytes);
//NOTES(): This is prob not the best way to do this, but since I'm not using templates, I have no choice unless I can figure out a another good way to do this.
if (Is64Bits)
{
peOpHeader64 = (PE_OP_HEADER64 *) ( ( appBytes) + dosHeader->PEOffset+sizeof(PE_HEADER));
printf("64 Bits ");
if (peOpHeader64->subSystem == SUBSYSTEM_WINDOWS_GUI)
{
printf("Windows GUI Appication.\n");
} else
if (peOpHeader64->subSystem == SUBSYSTEM_WINDOWS_CUI)
{
printf("Console Application.\n");
} else {
printf("Application is not supportive.\n");
isPEValid = false;
}
data_directory dd = peOpHeader64->DataDirectory[DIRECTORY_ENTRY_COM_DESCRIPTOR];
if (dd.VirtualAddress != 0)
{
printf("Dot net appicaltion are not supportive.\n");
isPEValid = false;
}
data_directory tlsdd = peOpHeader64->DataDirectory[DIRECTORY_ENTRY_TLS];
if (tlsdd.VirtualAddress != 0)
{
printf("TLS Callbacks are not supportive!\n");
isPEValid = false;
}
} else {
peOpHeader32 = (PE_OP_HEADER32 *) ( ( appBytes) + dosHeader->PEOffset+sizeof(PE_HEADER));
printf("32 Bits ");
data_directory dd = peOpHeader32->DataDirectory[DIRECTORY_ENTRY_COM_DESCRIPTOR];
if (dd.VirtualAddress != 0)
{
printf("Dot net appicaltion are not supportive.\n");
isPEValid = false;
}
data_directory tlsdd = peOpHeader32->DataDirectory[DIRECTORY_ENTRY_TLS];
if (tlsdd.VirtualAddress != 0)
{
printf("TLS Callbacks are not supportive!\n");
isPEValid = false;
}
}
} else {
printf("Invalid Header!\n");
isPEValid = false;
}
} else {
printf("Can't not open executable file!\n");
isPEValid = false;
}
if (isPEValid)
{
printf("Creating Shell Code.\n");
payload = shellcodify(appBytes, fileSize, outSize,Is64Bits);
if (payload)
{
int xorCount = 0;
if (xorKey)
{
if (xorLen <= 0)
{
xorLen = Strlen(xorKey);
}
for (int xorKeyIndex = 0; xorKeyIndex < outSize; xorKeyIndex++)
{
payload[xorKeyIndex] ^= xorKey[xorCount];
if (xorCount > xorLen-1)
{
xorCount = 0;
} else {
xorCount++;
}
}
}
outFile = fopen(outputFilename,"wb");
if (outFile)
{
printf("Writing to file...%s\n",outputFilename);
fwrite(payload,1,outSize,outFile);
printf("Done...\n");
fclose(outFile);
} else {
printf("Can't not create file.");
}
if (payload)
{
Free(payload);
payload = NULL;
}
}
} else {
DisplayHelp();
}
} else {
DisplayHelp();
}
if (xorKey)
{
Free(xorKey);
xorKey=NULL;
}
if (outputFilename)
{
Free(outputFilename);
outputFilename=NULL;
}
if (filename)
{
Free(filename);
filename = NULL;
}
if (appBytes)
{
Free(appBytes);
appBytes=NULL;
}
MemoryResults();
}