-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServer.cpp
More file actions
287 lines (236 loc) · 7.04 KB
/
Server.cpp
File metadata and controls
287 lines (236 loc) · 7.04 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
#include <iostream>
#include <string>
#include <map>
#include <ostream>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#include <istream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <boost/algorithm/string.hpp>
#include <boost/interprocess/file_mapping.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/tokenizer.hpp>
#include <boost/foreach.hpp>
#include <iostream>
#include "ACEPattern.h"
#include "ACE.cpp"
using namespace std;
vector<string> keywords;
typedef unsigned char byte;
typedef unsigned int uint;
typedef unsigned long ulong;
class Pattern {
public:
string HexString;
string BinaryString;
string GUID;
};
map<string, Pattern> patterns;
//AhoCorasick ahocorasick;
ACE *ace = new ACE();
std::string char_to_hex(unsigned char input){
static const char* const lut = "0123456789abcdef";
std::string output;
output.push_back(lut[input >> 4]);
output.push_back(lut[input & 15]);
return output;
}
std::string string_to_hex(const std::string& input)
{
static const char* const lut = "0123456789abcdef";
size_t len = input.length();
std::string output;
output.reserve(2 * len);
for (size_t i = 0; i < len; ++i)
{
const unsigned char c = input[i];
output.push_back(lut[c >> 4]);
output.push_back(lut[c & 15]);
output += " ";
}
return output;
}
#include <algorithm>
#include <stdexcept>
std::string hex_to_string(const std::string& input)
{
string s = input;
std::transform(s.begin(), s.end(), s.begin(), ::toupper);
static const char* const lut = "0123456789ABCDEF";
size_t len = s.length();
if (len & 1) throw std::invalid_argument("odd length");
std::string output;
output.reserve(len / 2);
for (size_t i = 0; i < len; i += 2)
{
char a = s[i];
const char* p = std::lower_bound(lut, lut + 16, a);
if (*p != a) throw std::invalid_argument("not a hex digit");
char b = s[i + 1];
const char* q = std::lower_bound(lut, lut + 16, b);
if (*q != b) throw std::invalid_argument("not a hex digit");
output.push_back(((p - lut) << 4) | (q - lut));
}
return output;
}
#include <fstream>
#include <string>
#include <cerrno>
std::string get_file_contents(const char *filename)
{
std::ifstream in(filename, std::ios::in | std::ios::binary);
if (in)
{
std::string contents;
in.seekg(0, std::ios::end);
contents.resize(in.tellg());
in.seekg(0, std::ios::beg);
in.read(&contents[0], contents.size());
in.close();
return(contents);
}
throw(errno);
}
bool addPattern(string line){
Pattern p;
size_t dotPosition = line.find('.');
if( dotPosition == -1 )
return false;
p.HexString = line.substr( 0, dotPosition);
p.GUID = line.substr( line.find('{') + 1, line.find('}') - line.find('{') - 1 );
if( p.HexString.length() < 10 )
return false;
string s = hex_to_string(p.HexString);
patterns[s] = p;
ACEPattern *aceP = new ACEPattern((byte*)s.c_str(), s.size(), true);
ace->AddPattern(aceP);
//keywords.push_back( s );
return true;
}
string loadPaternsFile(string patternFilePath){
if( ace != NULL )
delete ace;
ace = new ACE();
patterns.clear();
keywords.clear();
ifstream patternsFile;
string line;
string result = "Pattern file "+patternFilePath+" loaded successfully.";
patternsFile.open( patternFilePath.c_str() );
if( patternsFile.is_open() ){
int counter = 0;
while( patternsFile.good() ){
std::getline( patternsFile, line );
if( line.size() < 5 ){
continue;
}
if( addPattern(line) == false ){
continue;
}
counter++;
}
cout << "Read ";
cout << counter;
cout << " patterns from file" <<endl;
patternsFile.close();
}else{
return "Failed to open pattern file: "+patternFilePath;
}
ace->Compile();
//ahocorasick.initializeMachine(keywords);
return result;
}
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
int main(int argc, char **argv) {
string patternsFilePath;
if(argv[1]==NULL){
patternsFilePath = "/home/ntufar/projects/Scanner/doc/patterns100";
}else{
patternsFilePath = argv[1];
}
string loadPatternsResult = loadPaternsFile(patternsFilePath);
cout << loadPatternsResult << endl;
std::string userInput;
while(std::cin){
cout << "OVER" << endl;
getline (cin, userInput);
vector<string> tokens;
//boost::split(tokens, userInput, boost::is_any_of("\t \r\n"));
//boost::tokenizer<boost::escaped_list_separator<char> > t(userInput, boost::escaped_list_separator<char>("\\", ",", "\""));
boost::tokenizer<boost::escaped_list_separator<char> > t(userInput, boost::escaped_list_separator<char>("\\", " ", "\""));
BOOST_FOREACH(string s, t)
tokens.push_back(s);
if( tokens.size() < 1 )
continue;
if(tokens[0] == "quit"){
cout << "Godbye" <<endl;
exit(0);
}
if(tokens[0] == "loadpatterns"){
if( tokens.size() < 2 ){
cout << "Please provide file name"<<endl;
continue;
}
string loadpatternsresult = loadPaternsFile(tokens[1]);
cout << loadpatternsresult << endl;
continue;
}
if(tokens[0] == "scanfile"){
void *filecontents;
string scanFileName = tokens[1];
try{
boost::interprocess::file_mapping m_file(scanFileName.c_str(), boost::interprocess::read_only);
boost::interprocess::mapped_region region(m_file, boost::interprocess::read_only);
ace->Search((byte*)region.get_address(), region.get_size());
//results = ahocorasick.query((unsigned char*)region.get_address(), region.get_size());
}catch(boost::interprocess::interprocess_exception &e){
cout << e.what()<<endl;
}
for(std::vector<string>::iterator it = ace->results.begin(); it != ace->results.end(); ++it) {
string s = *it;
Pattern p = patterns[s];
cout << p.GUID << endl;
}
continue;
}
if(tokens[0] == "scanarray"){
string charArray = tokens[1];
//cout <<tokens[1] <<endl;
string binArray = hex_to_string(charArray);
//cout << string_to_hex(binArray)<<endl;
ace->Search((byte*)binArray.c_str(), binArray.size());
//vector<string> results = ahocorasick.query(binArray);
for(std::vector<string>::iterator it = ace->results.begin(); it != ace->results.end(); ++it) {
string s = *it;
Pattern p = patterns[s];
cout << p.GUID << endl;
}
continue;
}
cout << "Unrecognized command."<<endl;
}
cout << "Godbye" <<endl;
exit(0);
return 0;
}