/
filter.cpp
367 lines (315 loc) · 8.81 KB
/
filter.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*/
* Project: Wireless Proximity Analyzer
*
* Repository: https://github.com/ForensicTools/WirelessProximityMonitor_474-2135_Pittner-Sirianni-Swerling
*
* Authors:
* Joe Sirianni
* Cal Pittner
* Ross Swerling
*
* License: Apache v2
/*/
#include "filter.h"
//Static lists of user input strings
static const std::string days[7] =
{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
static const std::string weekEnds[2] = {"Sat", "Sun"};
static const std::string weekDays[5] =
{"Mon", "Tue", "Wed", "Thu", "Fri"};
static const std::string months[12] =
{"Jan", "Feb", "Mar", "Apr", "May","Jun",
"Jul","Aug","Sep", "Oct", "Nov", "Dec"};
static const std::string filterCommands[6] =
{"time", "date", "days", "months", "dbm", "uniqMac"};
FILTER::FILTER() {
clearFilter();
}
FILTER::~FILTER() {
}
bool FILTER::filterPacket(struct packet_structure packet,
std::list<packet_structure> *packetList) {
//Default answer is don't filter packet
bool answer = false;
//Structures for date and time
time_t epoch_time = packet.epoch_time;
struct tm *ptm = gmtime (&epoch_time);
if(filterTime == true) {
std::stringstream timeSs;
timeSs << std::setfill('0');
//Time in readable format
timeSs << std::setw(2) << static_cast<unsigned>(ptm->tm_hour);
timeSs << ":";
timeSs << std::setw(2) << static_cast<unsigned>(ptm->tm_min);
timeSs << ":";
timeSs << std::setw(2) << static_cast<unsigned>(ptm->tm_sec);
//Checks if packet time within specified filter range
if(startTime.compare(endTime) < 0)
if((timeSs.str()).compare(startTime) < 0 ||
(timeSs.str()).compare(endTime) > 0)
answer = true;
//If start time is greater than end
//It must extend into next day
else if(startTime.compare(endTime) > 0)
if((timeSs.str()).compare(startTime) > 0 ||
(timeSs.str()).compare(endTime) < 0)
answer = true;
else {
if((timeSs.str()).compare(startTime) != 0)
answer = true;
}
}
if(filterDate == true && answer == false) {
std::stringstream dateSs;
dateSs << std::setfill('0');
//Date in readable format
dateSs << std::setw(4) << static_cast<unsigned>(ptm->tm_year+1900);
dateSs << "/";
dateSs << std::setw(2) << static_cast<unsigned>(ptm->tm_mon+1);
dateSs << "/";
dateSs << std::setw(2) << static_cast<unsigned>(ptm->tm_mday);
//Checks if it is with in the allowable filter date
if(startDate.compare(endDate) < 0)
if((dateSs.str()).compare(startDate) < 0 ||
(dateSs.str()).compare(endDate) > 0)
answer = true;
else if(startDate.compare(endDate) > 0)
if((dateSs.str()).compare(startDate) > 0 ||
(dateSs.str()).compare(endDate) < 0)
answer = true;
else
if((dateSs.str()).compare(startDate) != 0)
answer = true;
}
if(filterDay == true && answer == false) {
//Sets fiter to true
answer = true;
for(int i = 0; i <= (matchDays.size() - 1); i++) {
//If packet day matches an allowed day
//Filter is set back to false
if(matchDays[i] == days[ptm->tm_wday]) {
answer = false;
break;
}
}
}
if(filterMonth == true && answer == false) {
//Sets filter to true
answer = true;
for(int i = 0; i <= (matchMonths.size()-1); i++) {
//If packet month matches an allowed month
//Filter is set back to true
if(matchMonths[i] == months[ptm->tm_mon]) {
answer = false;
break;
}
}
}
if(filterDbm == true && answer == false) {
//Checks if dBm is within allowable filter rnage
if(minDbm < packet.dbm || maxDbm > packet.dbm)
answer=true;
}
if(uniqMac == true && answer == false) {
//Checks packet MAC against all packets in the display filter list
for(std::list<packet_structure>::iterator it = packetList->begin();
it != packetList->end(); it++) {
if((it->mac).compare(packet.mac) == 0) {
answer = true;
break;
}
}
}
if( (blackList == true || whiteList == true) && answer == false) {
if(whiteList == true)
answer = true;
//Checks packet MAC against all packets in the MAC list
for(std::list<packet_structure>::iterator it = macList.begin();
it != macList.end(); it++) {
if((it->mac).compare(packet.mac) == 0) {
answer = !answer; //Tricky thinking
break;
}
}
}
if( macMono == true && answer == false) {
if((macAddr).compare(packet.mac) != 0)
answer = true;
}
return answer;
}
bool FILTER::setFilter(std::string filter) {
//Clears variables before setting them
clearFilter();
//Initializes Syntax as good
bool syntaxCheck = true;
std::stringstream ss(filter); //Separates commands by spaces
std::istringstream intConv; //Used for converting string numbers to type int
std::string buff; //General string buffer
std::string args; //Holds arguments for commandsf
std::vector<std::string> commands; //Holds all commands
//Separates commands by spaces and inputs them into list
while(ss >> buff) {
commands.push_back(buff);
}
//Parse each command
if(commands.size() != 0)
for(int i = 0; i <= (commands.size() - 1); i++) {
buff.clear();
args.clear();
//Initial syntax check for '='; All commands should have this
if((commands[i]).find_first_of("=") == std::string::npos) {
syntaxCheck = false;
break;
}
else //Split command into filter type and value
for(size_t q = 0; q <= commands[i].find_first_of("=") - 1; q++)
buff += commands[i].at(q);
for(int q = commands[i].find_first_of("=") + 1;
q <= ((commands[i]).size()-1); q++)
args += commands[i].at(q);
//Input values into variables
if(buff.compare("time" ) == 0) {
//Synatx check
if(args.find_first_of("-")==std::string::npos) {
syntaxCheck = false;
break;
}
else { //Split start and end time
filterTime = true;
for(int q = 0; q <= args.find_first_of("-") - 1; q++)
startTime += args.at(q);
for(int q = args.find_first_of("-") + 1;
q <= (args.size()-1); q++)
endTime += args.at(q);
}
}
else if(buff.compare("date") == 0) {
//Syntax check
if(args.find_first_of("-")==std::string::npos) {
syntaxCheck = false;
break;
}
else { //Split start and end date
filterDate = true;
for(int q = 0; q <= args.find_first_of("-"); q++)
startDate += args.at(q);
for(int q = args.find_first_of("-") + 1;
q <= (args.size()-1); q++)
endDate += args.at(q);
}
}
else if(buff.compare("days") == 0) {
filterDay = true;
//Relplaces commas with spaces
for(size_t q = args.find_first_of(",");
q != std::string::npos; q = args.find_first_of(",", ++q)) {
args[q] = ' ';
}
buff.clear();
ss.clear();
ss.str(args);
//Puts days into vector based on spaces
while(ss >> buff)
matchDays.push_back(buff);
}
else if(buff.compare("months") == 0) {
filterMonth = true;
//Replaces commas with spaces
for(size_t q = args.find_first_of(",");
q != std::string::npos; q = args.find_first_of(",", ++q)) {
args[q] = ' ';
}
buff.clear();
ss.clear();
ss.str(args);
//Puts days into vector based on spaces
while(ss >> buff)
matchMonths.push_back(buff);
}
else if(buff.compare("dbm") == 0) {
//Inital syntax check
if(args.find_first_of("-") == std::string::npos) {
syntaxCheck = false;
break;
}
else {
buff.clear();
//Gets Min dBm value
for(int q = 0; q <= args.find_first_of("-") - 1; q++)
buff += args.at(q);
intConv.clear();
intConv.str(buff);
intConv >> minDbm; //Convert to int
buff.clear();
//Gets Max dBm value
for(int q = args.find_first_of("-") + 1;
q <= (args.size()-1); q++)
buff += args.at(q);
intConv.clear();
intConv.str(buff);
intConv >> maxDbm; //Convert to int
}
//Syntax check for Min lt Max
if(minDbm > maxDbm)
syntaxCheck = false;
else { //Converts numbers to negative
minDbm *= - 1;
maxDbm *= - 1;
filterDbm = true;
}
}
else if(buff.compare("uniqMac") == 0) {
//Syntax checks
if(args.compare("true") == 0)
uniqMac = true;
else if(args.compare("false") != 0) {
syntaxCheck = false;
break;
}
}
else if(buff.compare("blackList") == 0 || buff.compare("whiteList") == 0) {
ReadWrite open;
open.readFromFile(args.c_str(), &macList);
if( buff.compare("blackList") == 0 )
blackList=true;
else
whiteList=true;
if( blackList == true && whiteList == true) {
syntaxCheck = false;
break;
}
}
else if(buff.compare("mac") == 0) {
macMono = true;
macAddr = args.c_str();
}
else {
syntaxCheck = false;
break;
}
}
return syntaxCheck;
}
void FILTER::clearFilter() {
//Clear variables & sets filter checks to false
filterTime = false;
startTime.clear();
endTime.clear();
filterDate = false;
startDate.clear();
endDate.clear();
filterDay = false;
matchDays.clear();
filterMonth = false;
matchMonths.clear();
filterDbm = false;
maxDbm = 0;
minDbm = 0;
uniqMac = false;
macMono = false;
macAddr.clear();
blackList = false;
whiteList = false;
macList.clear();
}