forked from NytroRST/ShellcodeCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompile.cpp
More file actions
186 lines (140 loc) · 5.03 KB
/
Compile.cpp
File metadata and controls
186 lines (140 loc) · 5.03 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
#include "Compile.h"
// Function used to parse data
bool Compile::ParseFile(string p_sFileData)
{
bool result = true;
string sReadString = "";
DeclaredStates::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 = DeclaredFunctionsStates::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 && Utils::IsString(p_sFileData[i]))
{
CurrentState.Data = CurrentState.Data + p_sFileData[i];
// Do not change current state
}
else if (CurrentState.Transforms[s].Character == CHR_READ_ALPHANUMSTR && Utils::IsString(p_sFileData[i]))
{
CurrentState.Data = CurrentState.Data + p_sFileData[i];
// Do not change current state
}
//else cout << "Stuff: " << p_sFileData[i] << " on " << CurrentState.Name << endl;
}
i++;
if (p_sFileData[i] == ';' || i == p_sFileData.length()) break;
}
}
else if (FunctionCalls::FunctionExists(sReadString) || LinuxSyscalls::SyscallExists(sReadString))
{
FunctionCalls::AddFunctionCallName(sReadString);
CurrentState = CallFunctionsStates::CF_FunctionCall;
// 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.clear();
}
// 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_NUMBER && isdigit(p_sFileData[i]))
{
if (CurrentState.Name.compare((CurrentState.Transforms[s].NextState)->Name) != 0) (CurrentState.Transforms[s].NextState)->Data = "";
(CurrentState.Transforms[s].NextState)->Data += p_sFileData[i];
CurrentState = *(CurrentState.Transforms[s].NextState);
}
else if (CurrentState.Transforms[s].Character == CHR_READ_ALPHANUMSTR && Utils::IsString(p_sFileData[i]))
{
CurrentState.Data = CurrentState.Data + p_sFileData[i];
// Do not change current state
}
//else cout << "Stuff: " << p_sFileData[i] << " on " << CurrentState.Name << endl;
}
i++;
if (p_sFileData[i] == ';' || i == p_sFileData.length()) break;
}
}
// Reset string
sReadString = "";
}
}
return result;
}
// Compile all parsed data into ASM file
string Compile::CompileAllData()
{
string sOutput = "";
// Compile for Windows
if (Platform::GetPlatform() == PLATFORM_TYPE_LINUX_X86 || Platform::GetPlatform() == PLATFORM_TYPE_LINUX_X64)
{
// Generate system calls for all system calls
for (size_t i = 0; i < FunctionCalls::AllFunctionCalls.size(); i++)
{
sOutput += FunctionCalls::GenerateFunctionCall(FunctionCalls::AllFunctionCalls[i]);
}
}
else
{
sOutput += ASMHeader::GetASMHeader();
// Generate LoadLibrary for all DLLs (from declared functions)
for (size_t i = 0; i < DeclaredFunctions::AllDeclaredFunctions.size(); i++)
{
sOutput += DeclaredFunctions::GenerateLoadLibraryCall(DeclaredFunctions::AllDeclaredFunctions[i].DLL);
}
// Generate GetProcAddress for all declared functions
for (size_t i = 0; i < DeclaredFunctions::AllDeclaredFunctions.size(); i++)
{
sOutput += DeclaredFunctions::GenerateGetProcAddressCall(DeclaredFunctions::AllDeclaredFunctions[i].DLL, DeclaredFunctions::AllDeclaredFunctions[i].Name);
}
// Generate function calls for all function calls
for (size_t i = 0; i < FunctionCalls::AllFunctionCalls.size(); i++)
{
sOutput += FunctionCalls::GenerateFunctionCall(FunctionCalls::AllFunctionCalls[i]);
}
}
return sOutput;
}