Repository navigation
Expand file tree
/
Copy pathSettings.cpp
More file actions
223 lines (202 loc) · 6.87 KB
/
Copy pathSettings.cpp
File metadata and controls
223 lines (202 loc) · 6.87 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
#include "Settings.h"
#include "Util.h"
#include <vector>
#include <fstream>
#include <sstream>
#define DELIM '='
#define KEY_FOLLOW_SHELLCODES "FOLLOW_SHELLCODES"
#define KEY_LOG_RTDSC "TRACE_RDTSC"
#define KEY_LOG_INT "TRACE_INT"
#define KEY_LOG_SYSCALL "TRACE_SYSCALL"
#define KEY_LOG_SECTIONS_TRANSITIONS "LOG_SECTIONS_TRANSITIONS"
#define KEY_LOG_SHELLCODES_TRANSITIONS "LOG_SHELLCODES_TRANSITIONS"
#define KEY_SHORT_LOGGING "ENABLE_SHORT_LOGGING"
#define HEXDUMP_SIZE "HEXDUMP_SIZE"
#define SLEEP_TIME "SLEEP_TIME"
#define HOOK_SLEEP "HOOK_SLEEP"
#define LOG_INDIRECT "LOG_INDIRECT_CALLS"
#define KEY_ANTIDEBUG "ANTIDEBUG"
#define KEY_ANTIVM "ANTIVM"
#define KEY_USE_DEBUG_SYMBOLS "USE_DEBUG_SYMBOLS"
t_shellc_options ConvertShcOption(int value)
{
if (value >= SHELLC_OPTIONS_COUNT) {
// choose the last option:
return t_shellc_options(SHELLC_OPTIONS_COUNT - 1);
}
return (t_shellc_options)value;
}
t_antidebug_options ConvertAntidebugOption(int value)
{
if (value >= ANTIDEBUG_OPTIONS_COUNT) {
// choose the last option:
return t_antidebug_options(ANTIDEBUG_OPTIONS_COUNT - 1);
}
return (t_antidebug_options)value;
}
//----
std::string SyscallsTable::getName(int syscallID)
{
auto found = syscallToFuncName.find(syscallID);
if (found == syscallToFuncName.end()) {
return "";
}
return found->second;
}
size_t SyscallsTable::load(const std::string& filename)
{
std::ifstream myfile(util::stripQuotes(filename).c_str());
if (!myfile.is_open()) {
return 0;
}
const size_t MAX_LINE = 300;
char line[MAX_LINE] = { 0 };
while (!myfile.eof()) {
myfile.getline(line, MAX_LINE);
std::string lineStr = line;
size_t found = lineStr.find_first_of(",");
if (found != std::string::npos) {
std::string numId = lineStr.substr(0, found);
std::string funcName = lineStr.substr(found + 1);
int syscallId = util::loadInt(numId, true);
syscallToFuncName[syscallId] = funcName;
}
}
myfile.close();
return syscallToFuncName.size();
}
//----
bool loadBoolean(const std::string &str, bool defaultVal)
{
if (util::iequals(str, "True") || util::iequals(str, "on") || util::iequals(str, "yes")) {
return true;
}
if (util::iequals(str, "False") || util::iequals(str, "off") || util::iequals(str, "no")) {
return false;
}
const int val = util::loadInt(str);
if (val == 0) return false;
return true;
}
bool fillSettings(Settings &s, std::string line)
{
std::vector<std::string> args;
util::splitList(line, DELIM, args);
if (args.size() < 2) {
return false;
}
bool isFilled = false;
std::string valName = args[0];
std::string valStr = args[1];
util::trim(valName);
util::trim(valStr);
if (util::iequals(valName, KEY_FOLLOW_SHELLCODES)) {
const int val = util::loadInt(valStr);
s.followShellcode = ConvertShcOption(val);
isFilled = true;
}
if (util::iequals(valName, KEY_LOG_RTDSC)) {
s.traceRDTSC = loadBoolean(valStr, s.traceRDTSC);
isFilled = true;
}
if (util::iequals(valName, KEY_LOG_INT)) {
s.traceINT = loadBoolean(valStr, s.traceINT);
isFilled = true;
}
if (util::iequals(valName, KEY_LOG_SYSCALL)) {
s.traceSYSCALL = loadBoolean(valStr, s.traceSYSCALL);
isFilled = true;
}
if (util::iequals(valName, KEY_LOG_SECTIONS_TRANSITIONS)) {
s.logSectTrans = loadBoolean(valStr, s.logSectTrans);
isFilled = true;
}
if (util::iequals(valName, KEY_LOG_SHELLCODES_TRANSITIONS)) {
s.logShelcTrans = loadBoolean(valStr, s.logShelcTrans);
isFilled = true;
}
if (util::iequals(valName, KEY_SHORT_LOGGING)) {
s.shortLogging = loadBoolean(valStr, s.shortLogging);
isFilled = true;
}
if (util::iequals(valName, HEXDUMP_SIZE)) {
s.hexdumpSize = util::loadInt(valStr);
isFilled = true;
}
if (util::iequals(valName, LOG_INDIRECT)) {
s.logIndirect = loadBoolean(valStr, s.logIndirect);
isFilled = true;
}
if (util::iequals(valName, HOOK_SLEEP)) {
s.hookSleep = loadBoolean(valStr, s.hookSleep);
isFilled = true;
}
if (util::iequals(valName, SLEEP_TIME)) {
s.sleepTime = util::loadInt(valStr);
isFilled = true;
}
if (util::iequals(valName, KEY_ANTIDEBUG)) {
const int val = util::loadInt(valStr);
s.antidebug = ConvertAntidebugOption(val);
isFilled = true;
}
if (util::iequals(valName, KEY_ANTIVM)) {
s.antivm = loadBoolean(valStr, s.antivm);
isFilled = true;
}
if (util::iequals(valName, KEY_USE_DEBUG_SYMBOLS)) {
s.useDebugSym = loadBoolean(valStr, s.useDebugSym);
isFilled = true;
}
return isFilled;
}
void Settings::stripComments(std::string &str)
{
size_t found = str.find_first_of(";#");
if (found != std::string::npos) {
str.resize(found);
}
}
bool Settings::saveINI(const std::string &filename)
{
std::ofstream myfile(filename.c_str());
if (!myfile.is_open()) {
return false;
}
myfile << KEY_FOLLOW_SHELLCODES << DELIM << this->followShellcode << "\r\n";
myfile << KEY_LOG_RTDSC << DELIM << this->traceRDTSC << "\r\n";
myfile << KEY_LOG_INT << DELIM << this->traceINT << "\r\n";
myfile << KEY_LOG_SYSCALL << DELIM << this->traceSYSCALL << "\r\n";
myfile << KEY_LOG_SECTIONS_TRANSITIONS << DELIM << this->logSectTrans << "\r\n";
myfile << KEY_LOG_SHELLCODES_TRANSITIONS << DELIM << this->logShelcTrans << "\r\n";
myfile << KEY_SHORT_LOGGING << DELIM << this->shortLogging << "\r\n";
myfile << KEY_USE_DEBUG_SYMBOLS << DELIM << this->useDebugSym << "\r\n";
myfile << HEXDUMP_SIZE << DELIM << this->hexdumpSize << "\r\n";
myfile << HOOK_SLEEP << DELIM << this->hookSleep << "\r\n";
myfile << SLEEP_TIME << DELIM << this->sleepTime << "\r\n";
myfile << LOG_INDIRECT << DELIM << this->logIndirect << "\r\n";
myfile << KEY_ANTIDEBUG << DELIM << this->antidebug << "\r\n";
myfile << KEY_ANTIVM << DELIM << this->antivm << "\r\n";
myfile.close();
return true;
}
bool Settings::loadINI(const std::string &filename)
{
std::ifstream myfile(filename.c_str());
if (!myfile.is_open()) {
return false;
}
const size_t MAX_LINE = 300;
char line[MAX_LINE] = { 0 };
bool filledAny = false;
while (!myfile.eof()) {
myfile.getline(line, MAX_LINE);
std::string lineStr = line;
stripComments(lineStr);
if (fillSettings(*this, lineStr)) {
filledAny = true;
}
}
myfile.close();
return filledAny;
}