forked from kismetwireless/kismet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalertracker.h
407 lines (306 loc) · 12.8 KB
/
alertracker.h
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
This file is part of Kismet
Kismet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Kismet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Kismet; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __ALERTRACKER_H__
#define __ALERTRACKER_H__
#include "config.h"
#include <stdio.h>
#include <time.h>
#include <list>
#include <map>
#include <vector>
#include <algorithm>
#include <string>
#include "globalregistry.h"
#include "messagebus.h"
#include "packetchain.h"
#include "timetracker.h"
#include "kis_net_microhttpd.h"
#ifdef PRELUDE
#include <libprelude/prelude.hxx>
#define PRELUDE_ANALYZER_MODEL "Kismet"
#define PRELUDE_ANALYZER_CLASS "Wireless Monitor"
#define PRELUDE_ANALYZER_MANUFACTURER "https://www.kismetwireless.net/"
#endif
// TODO:
// - move packet_component to a tracked system & just use the converted
// kis_alert_info in the future...
// - Move alert_ref into a tracked component and return via rest
// - Add description to alerts
class kis_alert_info : public packet_component {
public:
kis_alert_info() {
tm.tv_sec = 0;
tm.tv_usec = 0;
channel = "0";
// We do NOT self-destruct because we get cached in the alertracker
// for playbacks. It's responsible for discarding us
self_destruct = 0;
}
uint64_t device_key;
string header;
int phy;
struct timeval tm;
mac_addr bssid;
mac_addr source;
mac_addr dest;
mac_addr other;
string channel;
string text;
};
class kis_alert_component : public packet_component {
public:
kis_alert_component() {
// We can self destruct because we won't clear out the vector
// of actual alert info
self_destruct = 1;
}
vector<kis_alert_info *> alert_vec;
};
class tracked_alert : public tracker_component {
public:
tracked_alert(GlobalRegistry *in_globalreg, int in_id) :
tracker_component(in_globalreg, in_id) {
register_fields();
reserve_fields(NULL);
}
tracked_alert(GlobalRegistry *in_globalreg, int in_id, SharedTrackerElement e) :
tracker_component(in_globalreg, in_id) {
register_fields();
reserve_fields(e);
}
virtual SharedTrackerElement clone_type() {
return SharedTrackerElement(new tracked_alert(globalreg, get_id()));
}
__Proxy(device_key, uint64_t, uint64_t, uint64_t, device_key);
__Proxy(header, string, string, string, header);
__Proxy(phy, uint32_t, uint32_t, uint32_t, phy);
__Proxy(timestamp, double, double, double, timestamp);
__Proxy(transmitter_mac, mac_addr, mac_addr, mac_addr, transmitter_mac);
__Proxy(source_mac, mac_addr, mac_addr, mac_addr, source_mac);
__Proxy(dest_mac, mac_addr, mac_addr, mac_addr, dest_mac);
__Proxy(other_mac, mac_addr, mac_addr, mac_addr, other_mac);
__Proxy(channel, string, string, string, channel);
__Proxy(frequency, double, double, double, frequency);
__Proxy(text, string, string, string, text);
void from_alert_info(kis_alert_info *info) {
set_device_key(info->device_key);
set_header(info->header);
set_phy(info->phy);
set_timestamp(ts_to_double(info->tm));
set_transmitter_mac(info->bssid);
set_source_mac(info->source);
set_dest_mac(info->dest);
set_other_mac(info->other);
set_channel(info->channel);
set_text(info->text);
}
protected:
virtual void register_fields() {
tracker_component::register_fields();
RegisterField("kismet.alert.device_key", TrackerUInt64,
"Device key of linked device", &device_key);
RegisterField("kismet.alert.header", TrackerString,
"Alert type", &header);
RegisterField("kismet.alert.phy_id", TrackerUInt32,
"ID of phy generating alert", &phy);
RegisterField("kismet.alert.timestamp", TrackerDouble,
"Timestamp (sec.ms)", ×tamp);
RegisterField("kismet.alert.transmitter_mac", TrackerMac,
"Transmitter MAC address", &transmitter_mac);
RegisterField("kismet.alert.source_mac", TrackerMac,
"Source MAC address", &source_mac);
RegisterField("kismet.alert.dest_mac", TrackerMac,
"Destination MAC address", &dest_mac);
RegisterField("kismet.alert.other_mac", TrackerMac,
"Other / Extra MAC address", &other_mac);
RegisterField("kismet.alert.channel", TrackerString,
"Phy-specific channel", &channel);
RegisterField("kismet.alert.frequency", TrackerDouble,
"Frequency (khz)", &frequency);
RegisterField("kismet.alert.text", TrackerString,
"Alert text", &text);
}
SharedTrackerElement device_key;
SharedTrackerElement header;
SharedTrackerElement phy;
SharedTrackerElement timestamp;
SharedTrackerElement transmitter_mac;
SharedTrackerElement source_mac;
SharedTrackerElement dest_mac;
SharedTrackerElement other_mac;
SharedTrackerElement channel;
SharedTrackerElement frequency;
SharedTrackerElement text;
};
static const int alert_time_unit_conv[] = {
1, 60, 3600, 86400
};
enum alert_time_unit {
sat_second, sat_minute, sat_hour, sat_day
};
class tracked_alert_definition : public tracker_component {
public:
tracked_alert_definition(GlobalRegistry *in_globalreg, int in_id) :
tracker_component(in_globalreg, in_id) {
register_fields();
reserve_fields(NULL);
}
tracked_alert_definition(GlobalRegistry *in_globalreg, int in_id,
SharedTrackerElement e) :
tracker_component(in_globalreg, in_id) {
register_fields();
reserve_fields(e);
}
virtual SharedTrackerElement clone_type() {
return SharedTrackerElement(new tracked_alert_definition(globalreg, get_id()));
}
__Proxy(header, string, string, string, header);
__Proxy(description, string, string, string, description);
__Proxy(phy, int64_t, int64_t, int64_t, phy);
__Proxy(limit_unit, uint64_t, alert_time_unit, alert_time_unit, limit_unit);
__Proxy(limit_rate, uint64_t, uint64_t, uint64_t, limit_rate);
__Proxy(burst_unit, uint64_t, alert_time_unit, alert_time_unit, burst_unit);
__Proxy(limit_burst, uint64_t, uint64_t, uint64_t, limit_burst);
__Proxy(burst_sent, uint64_t, uint64_t, uint64_t, burst_sent);
__ProxyIncDec(burst_sent, uint64_t, uint64_t, burst_sent);
__Proxy(total_sent, uint64_t, uint64_t, uint64_t, total_sent);
__ProxyIncDec(total_sent, uint64_t, uint64_t, total_sent);
__Proxy(time_last, double, double, double, time_last);
int get_alert_ref() { return alert_ref; }
void set_alert_ref(int in_ref) { alert_ref = in_ref; }
protected:
virtual void register_fields() {
tracker_component::register_fields();
RegisterField("kismet.alert.definition.header", TrackerString,
"Alert type", &header);
RegisterField("kismet.alert.definition.description", TrackerString,
"Alert description", &description);
RegisterField("kismet.alert.definition.phyid", TrackerInt64,
"Alert phy type", &phy);
RegisterField("kismet.alert.definition.limit_unit", TrackerUInt64,
"Alert limit time unit (defined in alertracker.h)", &limit_unit);
RegisterField("kismet.alert.definition.limit_rate", TrackerUInt64,
"Alert rate limit", &limit_rate);
RegisterField("kismet.alert.definition.burst_unit", TrackerUInt64,
"Burst limit time unit (defined in alertracker.h)", &burst_unit);
RegisterField("kismet.alert.definition.limit_burst", TrackerUInt64,
"Burst rate limit", &limit_burst);
RegisterField("kismet.alert.definition.burst_sent", TrackerUInt64,
"Alerts sent in burst", &burst_sent);
RegisterField("kismet.alert.definition.total_sent", TrackerUInt64,
"Total alerts sent", &total_sent);
RegisterField("kismet.alert.definition.time_last", TrackerDouble,
"Timestamp of last alert (sec.us)", &time_last);
}
// Non-exposed internal reference
int alert_ref;
// Alert type and description
SharedTrackerElement header, description;
// Phynum this is linked to
SharedTrackerElement phy;
// Units, rate limit, burst, and burst rate
SharedTrackerElement limit_unit, limit_rate, burst_unit, limit_burst;
// Number of burst and total alerts we've sent of this type
SharedTrackerElement burst_sent, total_sent;
// Timestamp of the last time
SharedTrackerElement time_last;
};
typedef shared_ptr<tracked_alert_definition> shared_alert_def;
class Alertracker : public Kis_Net_Httpd_CPPStream_Handler, public LifetimeGlobal {
public:
// Simple struct from reading config lines
struct alert_conf_rec {
string header;
alert_time_unit limit_unit;
int limit_rate;
alert_time_unit burst_unit;
int limit_burst;
};
static shared_ptr<Alertracker> create_alertracker(GlobalRegistry *in_globalreg) {
shared_ptr<Alertracker> mon(new Alertracker(in_globalreg));
in_globalreg->alertracker = mon.get();
in_globalreg->RegisterLifetimeGlobal(mon);
in_globalreg->InsertGlobal("ALERTTRACKER", mon);
return mon;
}
private:
Alertracker(GlobalRegistry *in_globalreg);
// Raise an Prelude alert (requires prelude support compiled in)
int RaisePreludeAlert(int in_ref, kis_packet *in_pack, mac_addr bssid, mac_addr source,
mac_addr dest, mac_addr other, string in_channel, string in_text);
// Initialize Prelude Client (requires prelude support compiled in)
void PreludeInitClient(const char *analyzer_name);
public:
virtual ~Alertracker();
// Register an alert and get an alert reference number back.
int RegisterAlert(string in_header, string in_desc,
alert_time_unit in_unit, int in_rate, alert_time_unit in_burstunit,
int in_burst, int in_phy);
// Find a reference from a name
int FetchAlertRef(string in_header);
// Will an alert succeed?
int PotentialAlert(int in_ref);
// Raise an alert ...
int RaiseAlert(int in_ref, kis_packet *in_pack,
mac_addr bssid, mac_addr source, mac_addr dest, mac_addr other,
string in_channel, string in_text);
// parse an alert config string
int ParseAlertStr(string alert_str, string *ret_name,
alert_time_unit *ret_limit_unit, int *ret_limit_rate,
alert_time_unit *ret_limit_burst, int *ret_burst_rate);
// Load alert rates from a config file
int ParseAlertConfig(ConfigFile *in_conf);
// Define an alert and limits
int DefineAlert(string name, alert_time_unit limit_unit, int limit_rate,
alert_time_unit limit_burst, int burst_rate);
// Activate a preconfigured alert from a file
int ActivateConfiguredAlert(string in_header, string in_desc);
int ActivateConfiguredAlert(string in_header, string in_desc, int in_phy);
// Find an activated alert
int FindActivatedAlert(string in_header);
virtual bool Httpd_VerifyPath(const char *path, const char *method);
virtual void Httpd_CreateStreamResponse(Kis_Net_Httpd *httpd,
Kis_Net_Httpd_Connection *connection,
const char *url, const char *method, const char *upload_data,
size_t *upload_data_size, std::stringstream &stream);
virtual int Httpd_PostComplete(Kis_Net_Httpd_Connection *concls);
protected:
pthread_mutex_t alert_mutex;
shared_ptr<Packetchain> packetchain;
shared_ptr<EntryTracker> entrytracker;
int alert_vec_id, alert_entry_id, alert_timestamp_id, alert_def_id;
// Check and age times
int CheckTimes(shared_alert_def arec);
// Parse a foo/bar rate/unit option
int ParseRateUnit(string in_ru, alert_time_unit *ret_unit, int *ret_rate);
GlobalRegistry *globalreg;
int next_alert_id;
// Internal C++ mapping
map<string, int> alert_name_map;
map<int, shared_alert_def> alert_ref_map;
// Tracked mapping for export
SharedTrackerElement alert_defs;
TrackerElementVector alert_defs_vec;
int num_backlog;
// Backlog of alerts to be sent
vector<kis_alert_info *> alert_backlog;
// Alert configs we read before we know the alerts themselves
map<string, alert_conf_rec *> alert_conf_map;
#ifdef PRELUDE
// Prelude client
Prelude::ClientEasy *prelude_client;
#endif
};
#endif