-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cpp
323 lines (267 loc) · 7.85 KB
/
parser.cpp
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
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <iomanip>
#include <sstream>
using namespace std;
//using namespace ofstream;
//конвертор hex байта string в unsigned char...
unsigned char hstob(char data)
{
if(data>=48 && data<=57)
return (unsigned char)(data-48);
if(data>=65 && data<=70)
return (unsigned char)(data-55);
if(data>=97 && data<=102)
return (unsigned char)(data-87);
return 0;
}
//распечатка сортировки...
void print_sort(map<unsigned int, vector<vector<unsigned char>>> & data)
{
for(pair plspair : data)
{
cout << "size " << plspair.first << " | contains objects " << plspair.second.size() << endl;
}
}
//печать заголовка, нумерация байт...
void print_head(unsigned int key)
{
for(unsigned int i=0;i<=key;i++)
cout << setw(2) << std::setfill('0') << i << " ";
cout << endl;
for(unsigned int i=0;i<=((key+1)*2)+key;i++)
cout << "-";
cout << endl;
}
//печать отсортированной группы по ключу...
void print_group(map<unsigned int, vector<vector<unsigned char>>> & data, unsigned int key)
{
print_head(key);
vector<vector<unsigned char>> & outer_vec = data[key];
// Вывод элементов векторов
for (const auto & inner_vec : outer_vec)
{
for (unsigned char value : inner_vec) {
cout << hex << setw(2) <<static_cast<int>(value) << " " << dec;
}
cout << endl;
}
}
void analitic(map<unsigned int, vector<vector<unsigned char>>> & data, unsigned char key)
{
cout << ">>>> analitic..." << endl;
vector<vector<unsigned char>> & outer_vec = data[key];
//vector<unsigned int> differences;
map<unsigned int,unsigned int> differences;
//ищем несовпадения...
for(unsigned int i=0;i<key;i++)
{
unsigned char temp = data[key][0][i];
for(vector<unsigned char> & inner_vec : outer_vec)
{
if(temp!=inner_vec[i])
{
if(!differences.count(temp))
{
differences.insert(make_pair(i,i));
}
}
}
}
if(differences.size())
{
cout << "found " << differences.size() << " differences" << endl;
cout << "are different ";
for(pair tmp : differences)
{
cout << dec << tmp.first << " ";
}
cout << "bytes" << endl;
}
else
{
cout << ">>>> payload equial!" << endl;
}
}
void print_help()
{
cout << endl << "<<< JSON parser + sort + find differences v0.1.1>>>" << endl << endl;
cout << "USING: parser <filename>" << endl << endl;
cout << "COMMANDs:..." << endl;
cout << " prints | ps - print result sort" << endl;
cout << " printg | pg <grp> - print group payload data lenght" << endl;
cout << "analitic | al <grp> - analysis differences in group" << endl;
cout << " clear | cls - clear console" << endl;
cout << " quit | q - exit programm" << endl << endl;
}
int main(int argc, char* argv[])
{
/*
* обработка аргументов
*/
if(argc<2)
{
print_help();
exit(0);
}
if(!memcmp(argv[1],"--help",6))
{
print_help();
exit(0);
}
string path = argv[1];
/*
* открываем файл
*/
ifstream fin;
fin.open(path);
if(fin.is_open())
{
cout << "file " << path << " is open" << endl;
}
else
{
cout << "file " << path << " not exist" << endl;
exit(-1);
}
vector<string> json_str;
vector<string> payload_str;
vector<vector<unsigned char>> payload_hex;
/*
* вычитываем строки из файла в вектор
*/
while(!fin.eof())
{
string strtmp = "";
getline(fin,strtmp);
json_str.push_back(strtmp);
}
cout << "lines readed " << json_str.size() << "..."<< endl;
int index = 0;
/*
* чистим строки
*/
for(string strtmp : json_str)
{
if(strtmp.find("udp.payload")!=string::npos)
{
index++;
strtmp.erase(0,26);
for(auto i=strtmp.begin();i!=strtmp.end();i++)
if(*i==':') strtmp.erase(i);
payload_str.push_back(strtmp);
//cout << strtmp << endl << endl;
}
}
cout << "find " << index << " string..." << endl;
/*
* конвертирование hex строки в число...
*/
index = 0;
for(string strtmp : payload_str)
{
index++;
vector<unsigned char> temp;
for(size_t i=0;i<strtmp.size();i+=2)
{
unsigned char _hex = (unsigned char)(hstob(strtmp[i])*16 + hstob(strtmp[i+1]));
temp.push_back(_hex);
}
payload_hex.push_back(temp);
}
cout << "convert " << index << " string..." << endl;
/*
* сортировка...
*/
map<unsigned int, vector<vector<unsigned char>>> payload_sort;
for(vector<unsigned char> uchtmp : payload_hex)
{
unsigned int size = (unsigned int)uchtmp.size()-1;
if(payload_sort.count(size))
{
payload_sort[size].push_back(uchtmp);
}
else
{
// Используем emplace для создания ключа и первоначального значения
//без make_pair!
payload_sort.emplace(size, vector<vector<unsigned char>>());
// Добавляем вектор в значение по ключу 1
payload_sort[size].push_back(uchtmp);
}
}
cout << "sorted " << payload_sort.size() << " uniq elements..." << endl;
/*
* командный блок...
*/
string cmd;
string arg1(10,'\0');
string arg2(10,'\0');
while(1)
{
cmd.clear();
getline(cin, cmd);
sscanf(cmd.c_str(),"%s %s",arg1.data(),arg2.data());
unsigned int key = (unsigned int)atoi(arg2.c_str());
//печать группы пакетов...
if(!memcmp(arg1.c_str(),"printg",6)||!memcmp(arg1.c_str(),"pg",2))
{
if(key>0)
{
if(payload_sort.count(key))
{
print_group(payload_sort,key);
continue;
}
else
{
cout << ">>>> illegal key!" << endl;
continue;
}
}
}
//анализ группы пакетов...
if(!memcmp(arg1.c_str(),"analitic",8)||!memcmp(arg1.c_str(),"al",2))
{
analitic(payload_sort,(unsigned char)key);
continue;
}
//печать списка сортировки...
if(cmd=="prints"||cmd=="ps")
{
print_sort(payload_sort);
continue;
}
//очистка консоли...
if(cmd=="clear"||cmd=="cls")
{
system("cls");
continue;
}
//пустая комманда...
if(cmd=="")
{
continue;
}
//выход...
if(cmd=="quit"||cmd=="q")
{
break;
}
cout << ">>>> illegal cmd!" << endl;
}
/*
* закрываем файл...
*/
fin.close();
}
// pair<const unsigned int, vector<unsigned char>> //для пары из map
/* for(unsigned char c : temp)
{
printf("%2x ",(unsigned char)c);
}
cout << endl; */