FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ShellcodeCompiler/ShellcodeCompiler/ShellcodeCompiler.cpp at master · d4nnyk/ShellcodeCompiler · GitHub
d4nnyk
ShellcodeCompiler
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
ShellcodeCompiler
/
ShellcodeCompiler
/
ShellcodeCompiler.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
1448 lines (1115 loc) · 36.3 KB
Breadcrumbs
ShellcodeCompiler
/
ShellcodeCompiler
/
ShellcodeCompiler.cpp
Copy path
File metadata and controls
1448 lines (1115 loc) · 36.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
Shellcode Test.cpp : Defines the entry point for the console application.
//
#
include
"
stdafx.h
"
#
include
<
cstdio
>
#
include
<
string
>
#
include
<
sstream
>
#
include
<
iostream
>
#
include
<
vector
>
#
include
<
windows.h
>
using
namespace
std
;
//
Program version
#
define
PROGRAM_VERSION
"
v0.1 Alpha
"
//
Special characters
#
define
CHR_READ_ALPHANUMSTR
'
\x01
'
#
define
CHR_READ_NUMBER
'
\x02
'
//
Transform struct
struct
State
;
struct
Transform
{
char
Character;
State* NextState;
};
//
State struct
struct
State
{
string Data =
"
"
;
bool
Process =
false
;
vector<Transform> Transforms;
string Name;
void
(*ProcessStateData)(string);
};
//
Vector with declared functions
struct
DeclaredFunction
{
string Name;
string
DLL
;
};
size_t
AllDeclaredFunctionsNr =
0
;
vector<DeclaredFunction> AllDeclaredFunctions;
//
Add function name
void
AddFunctionName
(string p_sFunctionName)
{
DeclaredFunction Function;
Function.
Name
= p_sFunctionName;
AllDeclaredFunctions.
push_back
(Function);
}
//
Add function DLL name
void
AddFunctionDLLName
(string p_sDLLName)
{
AllDeclaredFunctions[AllDeclaredFunctionsNr].
DLL
= p_sDLLName;
AllDeclaredFunctionsNr++;
}
#
define
PARAMETER_TYPE_INT
0
#
define
PARAMETER_TYPE_STRING
1
//
Struct used to save a parameter
struct
Parameter
{
size_t
Type;
size_t
IntValue;
string StringValue;
};
//
Struct to save data for a function call
struct
FunctionCall
{
string Name;
vector<Parameter> Parameters;
};
//
All function calls and index
size_t
AllFunctionCallsNr =
0
;
vector<FunctionCall> AllFunctionCalls;
bool
FirstFunctionCall =
true
;
//
Add function call name
void
AddFunctionCallName
(string p_sFunctionName)
{
FunctionCall Function;
Function.
Name
= p_sFunctionName;
AllFunctionCalls.
push_back
(Function);
if
(FirstFunctionCall) FirstFunctionCall =
false
;
else
AllFunctionCallsNr++;
}
//
Add function call int parameter
void
AddFunctionCallIntParameter
(string p_sIntParameter)
{
Parameter Param;
Param.
Type
=
PARAMETER_TYPE_INT
;
Param.
IntValue
= (
size_t
)
stoll
(p_sIntParameter);
AllFunctionCalls[AllFunctionCallsNr].
Parameters
.
push_back
(Param);
}
//
Add function call string parameter
void
AddFunctionCallStringParameter
(string p_sStringParameter)
{
Parameter Param;
Param.
Type
=
PARAMETER_TYPE_STRING
;
Param.
StringValue
= p_sStringParameter;
AllFunctionCalls[AllFunctionCallsNr].
Parameters
.
push_back
(Param);
}
//
Check if a function exists
bool
FunctionExists
(string p_sFunctionName)
{
for
(
size_t
i =
0
; i < AllDeclaredFunctions.
size
(); i++)
if
(AllDeclaredFunctions[i].
Name
.
compare
(p_sFunctionName) ==
0
)
return
true
;
return
false
;
}
//
Dump all data - debug purposes
void
DumpAllData
()
{
cout << endl;
for
(
size_t
i =
0
; i < AllDeclaredFunctions.
size
(); i++)
{
cout <<
"
Declared:
"
<< AllDeclaredFunctions[i].
Name
<<
"
@
"
<< AllDeclaredFunctions[i].
DLL
<< endl;
}
cout << endl;
for
(
size_t
i =
0
; i < AllFunctionCalls.
size
(); i++)
{
cout <<
"
Call function:
"
<< AllFunctionCalls[i].
Name
<< endl;
for
(
size_t
j =
0
; j < AllFunctionCalls[i].
Parameters
.
size
(); j++)
{
cout << ((AllFunctionCalls[i].
Parameters
[j].
Type
==
PARAMETER_TYPE_STRING
) ?
"
String
"
:
"
Int
"
) <<
"
parameter:
"
;
if
(AllFunctionCalls[i].
Parameters
[j].
Type
==
PARAMETER_TYPE_STRING
) cout << AllFunctionCalls[i].
Parameters
[j].
StringValue
<< endl;
else
cout << AllFunctionCalls[i].
Parameters
[j].
IntValue
<< endl;
}
}
cout << endl;
}
//
Write output to file
void
WriteToFile
(string p_sOutputFile, string p_sData)
{
FILE
* f =
fopen
(p_sOutputFile.
c_str
(),
"
ab
"
);
//
Write
fwrite
(p_sData.
c_str
(), p_sData.
length
(),
sizeof
(
char
), f);
//
Close
fclose
(f);
}
//
Return hex string for a char
string
CharToHexString
(
char
p_cChar)
{
stringstream ss;
ss << hex << (
int
)p_cChar;
string mystr = ss.
str
();
return
mystr;
}
//
Return ASM header -> PEB, GetProcAddress, LoadLibrary
string
GetASMHeader
()
{
string
sContent
=
"
; Shellcode generated using Shellcode Compiler
\r\n
"
"
; https://github.com/NytroRST/ShellcodeCompiler
\r\n\r\n
"
"
BITS 32
\r\n
"
"
SECTION .text
\r\n
"
"
global main
\r\n
"
"
main:
\r\n\r\n
"
"
xor ecx, ecx
\r\n
"
"
mov eax, [fs:ecx + 0x30] ; EAX = PEB
\r\n
"
"
mov eax, [eax + 0xc] ; EAX = PEB->Ldr
\r\n
"
"
mov esi, [eax + 0x14] ; ESI = PEB->Ldr.InMemOrder
\r\n
"
"
lodsd ; EAX = Second module
\r\n
"
"
xchg eax, esi ; EAX = ESI, ESI = EAX
\r\n
"
"
lodsd ; EAX = Third(kernel32)
\r\n
"
"
mov ebx, [eax + 0x10] ; EBX = Base address
\r\n
"
"
mov edx, [ebx + 0x3c] ; EDX = DOS->e_lfanew
\r\n
"
"
add edx, ebx ; EDX = PE Header
\r\n
"
"
mov edx, [edx + 0x78] ; EDX = Offset export table
\r\n
"
"
add edx, ebx ; EDX = Export table
\r\n
"
"
mov esi, [edx + 0x20] ; ESI = Offset namestable
\r\n
"
"
add esi, ebx ; ESI = Names table
\r\n
"
"
xor ecx, ecx ; EXC = 0
\r\n\r\n
"
"
Get_Function:
\r\n\r\n
"
"
inc ecx ; Increment the ordinal
\r\n
"
"
lodsd ; Get name offset
\r\n
"
"
add eax, ebx ; Get function name
\r\n
"
"
cmp dword [eax], 0x50746547 ; GetP
\r\n
"
"
jnz Get_Function
\r\n
"
"
cmp dword [eax + 0x4], 0x41636f72 ; rocA
\r\n
"
"
jnz Get_Function
\r\n
"
"
cmp dword [eax + 0x8], 0x65726464 ; ddre
\r\n
"
"
jnz Get_Function
\r\n
"
"
mov esi, [edx + 0x24] ; ESI = Offset ordinals
\r\n
"
"
add esi, ebx ; ESI = Ordinals table
\r\n
"
"
mov cx, [esi + ecx * 2] ; Number of function
\r\n
"
"
dec ecx
\r\n
"
"
mov esi, [edx + 0x1c] ; Offset address table
\r\n
"
"
add esi, ebx ; ESI = Address table
\r\n
"
"
mov edx, [esi + ecx * 4] ; EDX = Pointer(offset)
\r\n
"
"
add edx, ebx ; EDX = GetProcAddress
\r\n\r\n
"
"
xor ecx, ecx ; ECX = 0
\r\n
"
"
push ebx ; Kernel32 base address
\r\n
"
"
push edx ; GetProcAddress
\r\n
"
"
push ecx ; 0
\r\n
"
"
push 0x41797261 ; aryA
\r\n
"
"
push 0x7262694c ; Libr
\r\n
"
"
push 0x64616f4c ; Load
\r\n
"
"
push esp ; LoadLibrary
\r\n
"
"
push ebx ; Kernel32 base address
\r\n
"
"
call edx ; GetProcAddress(LL)
\r\n\r\n
"
"
add esp, 0xc ; pop LoadLibrary
\r\n
"
"
pop ecx ; ECX = 0
\r\n
"
"
push eax ; EAX = LoadLibrary
\r\n\r\n
"
;
return
sContent
;
}
//
Struct to save DLL base offset address on the stack
struct
DLLBase
{
string Name;
size_t
Offset;
};
vector<DLLBase> DLLOffsets;
size_t
CurrentDLLIOffset =
4
;
//
Create kernel32 DLL base index
void
InitDLLBase
()
{
DLLBase kernel;
kernel.
Name
=
"
kernel32.dll
"
;
kernel.
Offset
=
1
;
DLLOffsets.
push_back
(kernel);
}
//
Check if a DLL base index already exists
bool
DLLBaseExists
(string p_sDLLName)
{
for
(
size_t
i =
0
; i < DLLOffsets.
size
(); i++)
if
(DLLOffsets[i].
Name
.
compare
(p_sDLLName) ==
0
)
return
true
;
return
false
;
}
//
Add a DLL base index
void
AddDLLBase
(string p_sDLLName)
{
DLLBase dll;
//
Check if DLL already exists
if
(
DLLBaseExists
(p_sDLLName))
return
;
dll.
Name
= p_sDLLName;
dll.
Offset
= CurrentDLLIOffset;
DLLOffsets.
push_back
(dll);
CurrentDLLIOffset++;
}
//
Get DLL base index
size_t
GetDLLBase
(string p_sDLLName)
{
for
(
size_t
i =
0
; i < DLLOffsets.
size
(); i++)
if
(DLLOffsets[i].
Name
.
compare
(p_sDLLName) ==
0
)
return
DLLOffsets[i].
Offset
;
cout <<
"
Error: Cannot find DLL base index for
"
<< p_sDLLName << endl;
return
0
;
}
//
Struct to save function address offset on the stack
struct
FunctionOffset
{
string Name;
size_t
Offset;
};
vector<FunctionOffset> FunctionOffsets;
size_t
CurrentFunctionOffset =
1
;
//
Check if a function offset already exists
bool
FunctionOffsetExists
(string p_sFunctionName)
{
for
(
size_t
i =
0
; i < FunctionOffsets.
size
(); i++)
if
(FunctionOffsets[i].
Name
.
compare
(p_sFunctionName) ==
0
)
return
true
;
return
false
;
}
//
Add a function offset
void
AddFunctionOffset
(string p_sFunctionName)
{
FunctionOffset func;
//
Check if DLL already exists
if
(
FunctionOffsetExists
(p_sFunctionName))
return
;
func.
Name
= p_sFunctionName;
func.
Offset
= CurrentFunctionOffset;
FunctionOffsets.
push_back
(func);
CurrentFunctionOffset++;
}
//
Get function offset
size_t
GetFunctionOffset
(string p_sFunctionName)
{
for
(
size_t
i =
0
; i < FunctionOffsets.
size
(); i++)
if
(FunctionOffsets[i].
Name
.
compare
(p_sFunctionName) ==
0
)
return
FunctionOffsets[i].
Offset
;
cout <<
"
Error: Cannot find function offset for
"
<< p_sFunctionName << endl;
return
0
;
}
//
Struct to save strings (for function calls) on the stack
struct
StringOffset
{
string Name;
size_t
Offset;
};
vector<StringOffset> StringOffsets;
size_t
CurrentStringOffset =
0
;
//
Check if a string offset already exists
bool
StringOffsetExists
(string p_sString)
{
for
(
size_t
i =
0
; i < StringOffsets.
size
(); i++)
if
(StringOffsets[i].
Name
.
compare
(p_sString) ==
0
)
return
true
;
return
false
;
}
//
Add a string offset
void
AddStringOffset
(string p_sString)
{
StringOffset str;
//
Check if string already exists
if
(
StringOffsetExists
(p_sString))
return
;
str.
Name
= p_sString;
str.
Offset
= CurrentStringOffset;
StringOffsets.
push_back
(str);
}
//
Get string offset
size_t
GetStringOffset
(string p_sString)
{
for
(
size_t
i =
0
; i < StringOffsets.
size
(); i++)
if
(StringOffsets[i].
Name
.
compare
(p_sString) ==
0
)
return
StringOffsets[i].
Offset
;
cout <<
"
Error: Cannot find string offset for
"
<< p_sString << endl;
return
0
;
}
//
Convert int to hex
string
IntToHexString
(
size_t
p_iNumber)
{
std::stringstream stream;
stream << std::hex << p_iNumber;
std::string
result
(stream.
str
());
return
result;
}
//
Generate a call to LoadLibrary("string")
size_t
NrBasesToStack =
0
;
string
GenerateLoadLibraryCall
(string p_sDLLName)
{
string
sContent
=
"
"
;
size_t
Len = p_sDLLName.
length
();
//
Check if the DLL was already loades
if
(
DLLBaseExists
(p_sDLLName))
return
"
"
;
if
(Len %
4
==
0
)
{
sContent
=
"
xor eax, eax ; EAX = 0
\r\n
"
;
sContent
+=
"
push eax ; NULL on the stack
\r\n
"
;
}
else
if
(Len %
4
==
1
)
{
sContent
=
"
xor eax, eax
\r\n
"
;
sContent
+=
"
mov al, 0x
"
;
sContent
+=
CharToHexString
(p_sDLLName[Len -
1
]);
sContent
+=
"
\r\n
"
;
sContent
+=
"
push eax
\r\n
"
;
}
else
if
(Len %
4
==
2
)
{
sContent
=
"
xor eax, eax
\r\n
"
;
sContent
+=
"
mov ax, 0x
"
;
sContent
+=
CharToHexString
(p_sDLLName[Len -
1
]);
sContent
+=
CharToHexString
(p_sDLLName[Len -
2
]);
sContent
+=
"
\r\n
"
;
sContent
+=
"
push eax
\r\n
"
;
}
else
if
(Len %
4
==
3
)
{
sContent
=
"
xor eax, eax
\r\n
"
;
sContent
+=
"
mov eax, 0x23
"
;
sContent
+=
CharToHexString
(p_sDLLName[Len -
1
]);
sContent
+=
CharToHexString
(p_sDLLName[Len -
2
]);
sContent
+=
CharToHexString
(p_sDLLName[Len -
3
]);
sContent
+=
"
\r\n
"
;
sContent
+=
"
push eax
\r\n
"
;
sContent
+=
"
sub dword [esp + 3], 0x23
\r\n
"
;
}
else
cout <<
"
Imaginary number?
"
<< endl;
//
Put the string as hex data pushes on the stack
size_t
Times = Len /
4
;
for
(
size_t
i = Times; i >
0
; i--)
{
sContent
+=
"
push 0x
"
;
for
(
size_t
j =
4
; j >
0
; j--)
{
sContent
+=
CharToHexString
(p_sDLLName[i *
4
-
4
+ j -
1
]);
}
sContent
+=
"
\r\n
"
;
}
//
LoadLibrary function call
sContent
+=
"
push esp ; String on the stack
\r\n
"
;
sContent
+=
"
call dword [esp +
"
;
sContent
+=
to_string
(((Times +
2
) *
4
) + NrBasesToStack *
4
);
sContent
+=
"
]
\r\n
"
;
sContent
+=
"
add esp,
"
;
sContent
+=
to_string
(((Times +
1
) *
4
));
sContent
+=
"
\r\n
"
;
sContent
+=
"
push eax ; DLL base on the stack
\r\n\r\n
"
;
//
Add DLL to base list and increment bases on stack number
AddDLLBase
(p_sDLLName);
NrBasesToStack++;
return
sContent
;
}
//
Generate a call to GetProcAddress(DLLBase, "FunctionName")
size_t
NrFunctionsToStack =
0
;
string
GenerateGetProcAddressCall
(string p_sDLLName, string p_sFunctionName)
{
string
sContent
=
"
"
;
size_t
Len = p_sFunctionName.
length
();
//
Check if the function address was already found
if
(
FunctionOffsetExists
(p_sFunctionName))
return
"
"
;
if
(Len %
4
==
0
)
{
sContent
=
"
xor eax, eax ; EAX = 0
\r\n
"
;
sContent
+=
"
push eax ; NULL on the stack
\r\n
"
;
}
else
if
(Len %
4
==
1
)
{
sContent
=
"
xor eax, eax
\r\n
"
;
sContent
+=
"
mov al, 0x
"
;
sContent
+=
CharToHexString
(p_sFunctionName[Len -
1
]);
sContent
+=
"
\r\n
"
;
sContent
+=
"
push eax
\r\n
"
;
}
else
if
(Len %
4
==
2
)
{
sContent
=
"
xor eax, eax
\r\n
"
;
sContent
+=
"
mov ax, 0x
"
;
sContent
+=
CharToHexString
(p_sFunctionName[Len -
1
]);
sContent
+=
CharToHexString
(p_sFunctionName[Len -
2
]);
sContent
+=
"
\r\n
"
;
sContent
+=
"
push eax
\r\n
"
;
}
else
if
(Len %
4
==
3
)
{
sContent
=
"
xor eax, eax
\r\n
"
;
sContent
+=
"
mov eax, 0x23
"
;
sContent
+=
CharToHexString
(p_sFunctionName[Len -
1
]);
sContent
+=
CharToHexString
(p_sFunctionName[Len -
2
]);
sContent
+=
CharToHexString
(p_sFunctionName[Len -
3
]);
sContent
+=
"
\r\n
"
;
sContent
+=
"
push eax
\r\n
"
;
sContent
+=
"
sub dword [esp + 3], 0x23
\r\n
"
;
}
else
cout <<
"
Imaginary number?
"
<< endl;
//
Put the string as hex data pushes on the stack
size_t
Times = Len /
4
;
for
(
size_t
i = Times; i >
0
; i--)
{
sContent
+=
"
push 0x
"
;
for
(
size_t
j =
4
; j >
0
; j--)
{
sContent
+=
CharToHexString
(p_sFunctionName[i *
4
-
4
+ j -
1
]);
}
sContent
+=
"
\r\n
"
;
}
//
LoadLibrary function call
sContent
+=
"
push esp ; String on the stack
\r\n
"
;
sContent
+=
"
push dword [esp +
"
;
sContent
+=
to_string
((NrFunctionsToStack *
4
) + ((NrBasesToStack +
3
-
GetDLLBase
(p_sDLLName)) *
4
) + ((Times +
2
) *
4
));
sContent
+=
"
]
\r\n
"
;
//
Call GetProcAddress
sContent
+=
"
call dword [esp +
"
;
sContent
+=
to_string
(((NrFunctionsToStack +
1
) *
4
) + ((NrBasesToStack +
1
) *
4
) + ((Times +
2
) *
4
));
sContent
+=
"
]
\r\n
"
;
sContent
+=
"
add esp,
"
;
sContent
+=
to_string
(((Times +
1
) *
4
));
sContent
+=
"
\r\n
"
;
sContent
+=
"
push eax ; Function address on the stack
\r\n\r\n
"
;
//
Add function address to the list and increment nr of functions on stack number
AddFunctionOffset
(p_sFunctionName);
NrFunctionsToStack++;
//
Do it!
return
sContent
;
}
//
Put a string (function parameter) on the stack
string
GeneratePutStringToStack
(string p_sString)
{
string
sContent
=
"
"
;
size_t
Len = p_sString.
length
();
//
Check if the string was already on the stack
if
(
StringOffsetExists
(p_sString))
return
"
"
;
if
(Len %
4
==
0
)
{
sContent
=
"
xor eax, eax ; EAX = 0
\r\n
"
;
sContent
+=
"
push eax ; NULL on the stack
\r\n
"
;
}
else
if
(Len %
4
==
1
)
{
sContent
=
"
xor eax, eax
\r\n
"
;
sContent
+=
"
mov al, 0x
"
;
sContent
+=
CharToHexString
(p_sString[Len -
1
]);
sContent
+=
"
\r\n
"
;
sContent
+=
"
push eax
\r\n
"
;
}
else
if
(Len %
4
==
2
)
{
sContent
=
"
xor eax, eax
\r\n
"
;
sContent
+=
"
mov ax, 0x
"
;
sContent
+=
CharToHexString
(p_sString[Len -
1
]);
sContent
+=
CharToHexString
(p_sString[Len -
2
]);
sContent
+=
"
\r\n
"
;
sContent
+=
"
push eax
\r\n
"
;
}
else
if
(Len %
4
==
3
)
{
sContent
=
"
xor eax, eax
\r\n
"
;
sContent
+=
"
mov eax, 0x23
"
;
sContent
+=
CharToHexString
(p_sString[Len -
1
]);
sContent
+=
CharToHexString
(p_sString[Len -
2
]);
sContent
+=
CharToHexString
(p_sString[Len -
3
]);
sContent
+=
"
\r\n
"
;
sContent
+=
"
push eax
\r\n
"
;
sContent
+=
"
sub dword [esp + 3], 0x23
\r\n
"
;
}
else
cout <<
"
Imaginary number?
"
<< endl;
//
Put the string as hex data pushes on the stack
size_t
Times = Len /
4
;
for
(
size_t
i = Times; i >
0
; i--)
{
sContent
+=
"
push 0x
"
;
for
(
size_t
j =
4
; j >
0
; j--)
{
sContent
+=
CharToHexString
(p_sString[i *
4
-
4
+ j -
1
]);
}
sContent
+=
"
\r\n
"
;
}
//
Add dtring offset
sContent
+=
"
push esp
\r\n\r\n
"
;
CurrentStringOffset = CurrentStringOffset + Times +
2
;
AddStringOffset
(p_sString);
return
sContent
;
}
//
Generate a function call
string
GenerateFunctionCall
(FunctionCall p_oFunctionCall)
{
string
sContent
=
"
"
;
size_t
NrParam = p_oFunctionCall.
Parameters
.
size
();
size_t
CurrentParamNr =
1
;
//
First, put all string parameters on the stack
for
(
size_t
i =
0
; i < p_oFunctionCall.
Parameters
.
size
(); i++)
if
(p_oFunctionCall.
Parameters
[i].
Type
==
PARAMETER_TYPE_STRING
)
sContent
+=
GeneratePutStringToStack
(p_oFunctionCall.
Parameters
[i].
StringValue
);
//
Parse all arguments (from the end)
for
(
size_t
i = NrParam -
1
; ; i--)
{
if
(p_oFunctionCall.
Parameters
[i].
Type
==
PARAMETER_TYPE_STRING
)
{
sContent
+=
"
push dword [ESP +
"
;
sContent
+=
to_string
(((CurrentParamNr -
1
) *
4
) + ((CurrentStringOffset -
GetStringOffset
(p_oFunctionCall.
Parameters
[i].
StringValue
)) *
4
));
sContent
+=
"
]
\r\n
"
;
}
else
if
(p_oFunctionCall.
Parameters
[i].
Type
==
PARAMETER_TYPE_INT
)
{
//
If int parameter is 0, avoid NULL
if
(p_oFunctionCall.
Parameters
[i].
IntValue
==
0
)
{
sContent
+=
"
xor eax, eax
\r\n
"
;
sContent
+=
"
push eax
\r\n
"
;
}
else
{
sContent
+=
"
push 0x
"
;
sContent
+=
IntToHexString
(p_oFunctionCall.
Parameters
[i].
IntValue
);
sContent
+=
"
\r\n
"
;
}
}
else
cout <<
"
Error: Undefined parameter type!
"
<< endl;
CurrentParamNr++;
if
(i ==
0
)
break
;
}
//
Call function
sContent
+=
"
call dword [ESP +
"
;
sContent
+=
to_string
(((NrParam -
1
) *
4
) + (CurrentStringOffset *
4
) + ((CurrentFunctionOffset -
GetFunctionOffset
(p_oFunctionCall.
Name
)) *
4
));
sContent
+=
"
]
\r\n
"
;
//
Clean string parameters
if
(CurrentStringOffset >
0
)
{
sContent
+=
"
add ESP,
"
;
sContent
+=
to_string
(CurrentStringOffset *
4
);
sContent
+=
"
\r\n
"
;
}
//
Clean global strings data
CurrentStringOffset =
0
;
StringOffsets.
clear
();
return
sContent
;
}
//
Declare functions states
State DF_function;
State DF_FunctionName;
State DF_FunctionDLLStr;
State DF_ReadStringInQuotes;
State DF_GotStrInQuotes;
State DF_FinishStrInQuotes;
//
Call functions states
State CF_FunctionCall;
State CF_GetFirstParam;
State CF_ReadStrInQuotes;
State CF_GotStrInQuotes;
State CF_GetNextArg;
State CF_GetNumber;
State CF_GotAllArgs;
//
Process data for FUNCTION NAME
void
ProcessStateData_FunctionName
(string p_sString)
{
AddFunctionName
(p_sString);
}
//
Process data for FUNCTION DLL NAME
void
ProcessStateData_FunctionDLLName
(string p_sString)
{
AddFunctionDLLName
(p_sString);
}
//
Process data for FUNCTION STRING PARAMETER
void
ProcessStateData_FunctionCallStringArg
(string p_sString)
{
AddFunctionCallStringParameter
(p_sString);
}
//
Process data for FUNCTION INT PARAMETER
void
ProcessStateData_FunctionCallIntArg
(string p_sString)
{
AddFunctionCallIntParameter
(p_sString);
}
//
Function used to check if a character is "string": [a-z][A-Z][0-9][Specail]
bool
IsString
(
char
p_cCharacter)
{
return
(p_cCharacter >=
32
&& p_cCharacter <=
126
&&
p_cCharacter !=
'
"
'
&& p_cCharacter !=
'
)
'
&& p_cCharacter !=
'
(
'
&& p_cCharacter !=
'
,
'
);
}
//
Get TEMP folder
string
GetTemp
()
{
char
buffer[
1024
];
string
sContent
=
"
"
;
DWORD
r =
GetTempPath
(
1024
, buffer);
if
(r ==
0
)
return
"
"
;
sContent
= buffer;
return
sContent
;
}
//
Get current working directory
string
GetCurrentDir
()
{
char
buffer[
1024
];
string
sContent
=
"
"
;
DWORD
r =
GetCurrentDirectory
(
1024
, buffer);
if
(r ==
0
)
return
"
"
;
sContent
= buffer;
return
sContent
;
}
//
Check if a file exists
bool
FileExists
(string p_sPath)
{
DWORD
dwAttrib =
GetFileAttributes
(p_sPath.
c_str
());
return
(dwAttrib !=
INVALID_FILE_ATTRIBUTES
);
}
//
Delete a file
bool
DeleteSourceFile
(string p_sFile)
{
return
DeleteFile
(p_sFile.
c_str
());
}
//
Function used to read a file
string
ReadSourceFile
(string p_sFilename)
{
string contents =
"
"
;
FILE
* f =
fopen
(p_sFilename.
c_str
(),
"
rb
"
);
//
Get file size
fseek
(f,
0
,
SEEK_END
);
size_t
size =
ftell
(f);
char
* cdata =
new
char
[size +
1
];
cdata[size] =
'
\0
'
;
//
Read file
rewind
(f);
fread
(cdata,
sizeof
(
char
), size, f);
fclose
(f);
//
Return data
contents = cdata;
delete[]
cdata;
return
contents;
}
//
Get the size of a file
size_t
GetSize
(string p_sFilename)
{
size_t
size;
FILE
* f =
fopen
(p_sFilename.
c_str
(),
"
rb
"
);
//
Get file size
fseek
(f,
0
,
SEEK_END
);
size =
ftell
(f);
rewind
(f);
fclose
(f);
return
size;
}
//
Read binary file
unsigned
char
*
ReadBinaryFile
(string p_sFilename,
size_t
*p_rSize)
{
unsigned
char
*p =
NULL
;
FILE
* f =
NULL
;
size_t
res =
0
;
if
(!
FileExists
(p_sFilename))
{
cout <<
"
Binary file does not exists!
"
<< endl;
return
NULL
;
}
//
Get size and allocate space
*p_rSize =
GetSize
(p_sFilename);
if
(*p_rSize ==
0
)
return
NULL
;
f =
fopen
(p_sFilename.
c_str
(),
"
rb
"
);
p =
new
unsigned
char
[*p_rSize];
//
Read file
rewind
(f);
res =
fread
(p,
sizeof
(
unsigned
char
), *p_rSize, f);
fclose
(f);
if
(res ==
0
)
{
delete[]
p;
return
NULL
;
}
return
p;
}
//
Test the generated shellcode
void
TestShellcode
(string p_sFilename)
{
unsigned
char
*p =
NULL
;
size_t
size =
0
;
p =
ReadBinaryFile
(p_sFilename, &size);
//
Check if successful read
if
(size ==
0
|| p ==
NULL
)
{
cout <<
"
Error: Cannot read shellcode file!
"
<< endl;
return
;
}
//
Get space for shellcode
void
*sc =
VirtualAlloc
(
0
, size,
MEM_RESERVE
|
MEM_COMMIT
,
PAGE_EXECUTE_READWRITE
);
if
(sc ==
NULL
)
{
cout <<
"
Error: Cannot allocate space for shellcode!
"
<< endl;
return
;
}
//
Copy shellcode and execute it
memcpy
(sc, p, size);
(*(
int
(*)()) sc)();
}
//
Function used to parse data
bool
ParseFile
(string p_sFileData)
{
bool
result =
true
;
string
sReadString
=
"
"
;
State CurrentState;
for
(
size_t
i =
0
; i < p_sFileData.
length
(); i++)
{
//
Check for a string
if
(
isalnum
(p_sFileData[i]))
{
sReadString
+= p_sFileData[i];
}
//
If it is not an alphanumeric character
else
{
//
We have a "function" definition
if
(
sReadString
.
compare
(
"
function
"
) ==
0
)
{
CurrentState = DF_function;
//
PROCESS "function" - Declare a function
while
(
true
)
{
//
Parse states transforms
for
(
size_t
s =
0
; s < CurrentState.
Transforms
.
size
(); s++)
{
if
(CurrentState.
Transforms
[s].
Character
== p_sFileData[i])
{
//
cout << "We found a transform (" << i << "): " << p_sFileData[i] << " [" << (int)p_sFileData[i] << "] - Current state: " << CurrentState.Name << endl;
//
Process state
if
(CurrentState.
Process
)
{
//
Do stuff
CurrentState.
ProcessStateData
(CurrentState.
Data
);
CurrentState.
Data
=
"
"
;
}
//
Go to next state
CurrentState = *(CurrentState.
Transforms
[s].
NextState
);
//
cout << "Moved to state: " << CurrentState.Name << endl;
break
;
}
else
if
(CurrentState.
Transforms
[s].
Character
==
CHR_READ_ALPHANUMSTR
&&
IsString
(p_sFileData[i]))
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL