forked from kismetwireless/kismet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dumpfile_pcap.cc
539 lines (435 loc) · 15.7 KB
/
dumpfile_pcap.cc
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*
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
*/
#include "config.h"
#ifdef HAVE_LIBPCAP
#include <errno.h>
#include "endian_magic.h"
#include "dumpfile_pcap.h"
#include "kis_ppi.h"
#include "phy_80211.h"
int dumpfilepcap_chain_hook(CHAINCALL_PARMS) {
Dumpfile_Pcap *auxptr = (Dumpfile_Pcap *) auxdata;
return auxptr->chain_handler(in_pack);
}
Dumpfile_Pcap::Dumpfile_Pcap() {
fprintf(stderr, "FATAL OOPS: Dumpfile_Pcap called with no globalreg\n");
exit(1);
}
Dumpfile_Pcap::Dumpfile_Pcap(GlobalRegistry *in_globalreg) : Dumpfile(in_globalreg) {
globalreg = in_globalreg;
parent = NULL;
type = "pcapdump";
logclass = "pcap";
// Default to dot11
dlt = DLT_IEEE802_11;
cbfilter = NULL;
cbaux = NULL;
pack_comp_80211 = globalreg->packetchain->RegisterPacketComponent("PHY80211");
pack_comp_mangleframe = globalreg->packetchain->RegisterPacketComponent("MANGLEDATA");
pack_comp_radiodata = globalreg->packetchain->RegisterPacketComponent("RADIODATA");
pack_comp_gps = globalreg->packetchain->RegisterPacketComponent("GPS");
pack_comp_checksum = globalreg->packetchain->RegisterPacketComponent("CHECKSUM");
pack_comp_decap = globalreg->packetchain->RegisterPacketComponent("DECAP");
pack_comp_linkframe = globalreg->packetchain->RegisterPacketComponent("LINKFRAME");
Startup_Dumpfile();
}
Dumpfile_Pcap::Dumpfile_Pcap(GlobalRegistry *in_globalreg, string in_type,
int in_dlt, Dumpfile_Pcap *in_parent,
dumpfile_pcap_filter_cb in_filter, void *in_aux) :
Dumpfile(in_globalreg) {
globalreg = in_globalreg;
type = in_type;
logclass = "pcap";
parent = in_parent;
cbfilter = in_filter;
cbaux = in_aux;
// Use whatever DLT we got
dlt = in_dlt;
Startup_Dumpfile();
}
void Dumpfile_Pcap::Startup_Dumpfile() {
// Default DLT to the basic type
int fdlt = dlt;
dumpfile = NULL;
dumper = NULL;
// Process a resume request
dumpformat = dump_unknown;
if (globalreg->kismet_config->FetchOpt(type + "format") == "ppi") {
#ifndef HAVE_PPI
_MSG("Cannot log in PPI format, the libpcap available when Kismet was "
"compiled did not support PPI, defaulting to basic packet format.",
MSGFLAG_ERROR);
#else
_MSG("Pcap log in PPI format", MSGFLAG_INFO);
dumpformat = dump_ppi;
// Set us to PPI
fdlt = DLT_PPI;
#endif
} else {
_MSG("Pcap logging for type " + type, MSGFLAG_INFO);
}
// Find the file name
if ((fname = ProcessConfigOpt()) == "" ||
globalreg->fatal_condition) {
return;
}
dumpfile = pcap_open_dead(fdlt, MAX_PACKET_LEN);
if (dumpfile == NULL) {
_MSG("Failed to open pcap dump file '" + fname + "': " +
string(strerror(errno)), MSGFLAG_FATAL);
globalreg->fatal_condition = 1;
return;
}
dumper = pcap_dump_open(dumpfile, fname.c_str());
if (dumper == NULL) {
_MSG("Failed to open pcap dump file '" + fname + "': " +
string(strerror(errno)), MSGFLAG_FATAL);
globalreg->fatal_condition = 1;
return;
}
_MSG("Opened pcapdump log file '" + fname + "'", MSGFLAG_INFO);
beaconlog = 1;
phylog = 1;
corruptlog = 1;
globalreg->packetchain->RegisterHandler(&dumpfilepcap_chain_hook, this,
CHAINPOS_LOGGING, -100);
globalreg->RegisterDumpFile(this);
}
Dumpfile_Pcap::~Dumpfile_Pcap() {
globalreg->packetchain->RemoveHandler(&dumpfilepcap_chain_hook,
CHAINPOS_LOGGING);
// Close files
if (dumper != NULL) {
Flush();
pcap_dump_flush(dumper);
pcap_dump_close(dumper);
}
if (dumpfile != NULL) {
pcap_close(dumpfile);
}
dumper = NULL;
dumpfile = NULL;
}
int Dumpfile_Pcap::Flush() {
if (dumper == NULL || dumpfile == NULL)
return 0;
pcap_dump_flush(dumper);
return 1;
}
void Dumpfile_Pcap::RegisterPPICallback(dumpfile_ppi_cb in_cb, void *in_aux) {
for (unsigned int x = 0; x < ppi_cb_vec.size(); x++) {
if (ppi_cb_vec[x].cb == in_cb && ppi_cb_vec[x].aux == in_aux)
return;
}
ppi_cb_rec r;
r.cb = in_cb;
r.aux = in_aux;
ppi_cb_vec.push_back(r);
}
void Dumpfile_Pcap::RemovePPICallback(dumpfile_ppi_cb in_cb, void *in_aux) {
for (unsigned int x = 0; x < ppi_cb_vec.size(); x++) {
if (ppi_cb_vec[x].cb == in_cb && ppi_cb_vec[x].aux == in_aux) {
ppi_cb_vec.erase(ppi_cb_vec.begin() + x);
return;
}
}
}
int Dumpfile_Pcap::chain_handler(kis_packet *in_pack) {
// Grab the mangled frame if we have it, then try to grab up the list of
// data types and die if we can't get anything
dot11_packinfo *packinfo =
(dot11_packinfo *) in_pack->fetch(pack_comp_80211);
// Grab the generic mangled frame
kis_datachunk *chunk =
(kis_datachunk *) in_pack->fetch(pack_comp_mangleframe);
kis_layer1_packinfo *radioinfo =
(kis_layer1_packinfo *) in_pack->fetch(pack_comp_radiodata);
kis_gps_packinfo *gpsdata =
(kis_gps_packinfo *) in_pack->fetch(pack_comp_gps);
kis_packet_checksum *fcsdata =
(kis_packet_checksum *) in_pack->fetch(pack_comp_checksum);
if (cbfilter != NULL) {
// If we have a filter, grab the data using that
chunk = (*cbfilter)(globalreg, in_pack, cbaux);
} else if (chunk == NULL) {
// Look for the 802.11 frame
if ((chunk =
(kis_datachunk *) in_pack->fetch(pack_comp_decap)) == NULL) {
// Look for any link frame, we'll check the DLT soon
chunk =
(kis_datachunk *) in_pack->fetch(pack_comp_linkframe);
}
}
// If after all of that we still didn't find a packet
if (chunk == NULL) {
return 0;
}
if (chunk != NULL && (chunk->length < 0 || chunk->length > MAX_PACKET_LEN)) {
_MSG("Weird frame in pcap logger with the wrong size...", MSGFLAG_ERROR);
return 0;
}
// Make sure we have the right DLT for simple matching conditions
if (cbfilter == NULL && chunk->dlt != dlt) {
return 0;
}
int dump_offset = 0;
if (packinfo != NULL) {
if (phylog == 0 && packinfo->type == packet_phy)
return 0;
if (corruptlog == 0 && packinfo->corrupt)
return 0;
if (beaconlog == 0 && packinfo->type == packet_management &&
packinfo->subtype == packet_sub_beacon) {
macmap<uint32_t>::iterator bcmi =
bssid_csum_map.find(packinfo->bssid_mac);
if (bcmi != bssid_csum_map.end() &&
*(bcmi->second) == packinfo->ssid_csum) {
return 0;
}
bssid_csum_map.insert(packinfo->bssid_mac, packinfo->ssid_csum);
}
}
unsigned int dump_len = 0;
if (chunk != NULL)
dump_len += chunk->length;
u_char *dump_data = NULL;
// Assemble the full packet
if (dumpformat == dump_ppi) {
ppi_packet_header *ppi_ph;
int gps_tagsize = 0; //include struct ppi fieldheader
int dot11common_tagsize = 0; //include ppi_fieldheader
unsigned int ppi_len = 0;
unsigned int ppi_pos = sizeof(ppi_packet_header);
/* The size of the gps tag varies depending if we have altitude or not
* we do all of the length math up front, and throughout the rest of the
* function make our decision based on the length, not the gpsdata.
* this helps keep logic error minimized.
*/
if (gpsdata != NULL) {
gps_tagsize = sizeof(ppi_gps_hdr); //12
if (gpsdata->fix <= 1) //no fix
gps_tagsize = 0; //dont bother storing anything
if (gpsdata->fix >= 2)
gps_tagsize += 12; // lon, lat, appid,
if (gpsdata->fix >= 3)
gps_tagsize +=4; // altitude
//Could eventually include hdop, vdop using simillar scheme here
}
/* although dot11common tags are constant size, we follow the same pattern here*/
if (radioinfo != NULL) {
dot11common_tagsize = sizeof(ppi_80211_common);
if (fcsdata != NULL)
dump_len += 4;
}
//printf("sizeof ppi_gps_hdr:%d\n", sizeof(ppi_gps_hdr));
//printf("Computed gps tagsize of %d\n", gps_tagsize);
//printf("Computed dot11common tagsize of %d\n", dot11common_tagsize);
//printf("Sizeof ppi_packetheader: %d\n", sizeof(ppi_packet_header));
ppi_len += sizeof(ppi_packet_header);
ppi_len += gps_tagsize;
ppi_len += dot11common_tagsize;
//printf("ppi_len=%d\n", ppi_len);
//With the static-ppi fields out of the way, handle any dynamic ones
//(f.ex) plugin-spectool
// Collate the allocation sizes of any callbacks
for (unsigned int p = 0; p < ppi_cb_vec.size(); p++) {
ppi_len += (*(ppi_cb_vec[p].cb))(globalreg, 1, in_pack, NULL, 0,
ppi_cb_vec[p].aux);
}
// If we have a parent, call them
if (parent != NULL) {
for (unsigned int p = 0; p < parent->ppi_cb_vec.size(); p++) {
ppi_len += (*(parent->ppi_cb_vec[p].cb))(globalreg, 1, in_pack, NULL, 0,
parent->ppi_cb_vec[p].aux);
}
}
dump_len += ppi_len; //dumplen now accoutns for all ppi data
//printf("dump_len=%d\n", dump_len);
if (dump_len == 0 && ppi_len == 0)
return 0;
dump_data = new u_char[dump_len];
//memset(dump_data, 0xcc, dump_len); //Good for debugging ppi stuff.
ppi_ph = (ppi_packet_header *) dump_data;
ppi_ph->pph_version = 0;
ppi_ph->pph_flags = 0;
ppi_ph->pph_len = kis_htole16(ppi_len);
// Use the DLT in the PPI internal
ppi_ph->pph_dlt = kis_htole32(dlt);
//First lay out the GPS tag, if applicable
if (gpsdata != NULL) {
unsigned int gps_data_offt = 0; //offsets to fields, from begging of field data.
if (gps_tagsize > 0) {
ppi_gps_hdr *ppigps = NULL;
union block {
uint8_t u8;
uint16_t u16;
uint32_t u32;
} *u;
//printf("debug - logging ppi gps packet. gps_tagsize: %d\n", gps_tagsize);
ppigps = (ppi_gps_hdr *) &(dump_data[ppi_pos]);
ppigps->pfh_datatype = kis_htole16(PPI_FIELD_GPS);
// Header + lat/lon minus PPI overhead.
ppigps->pfh_datalen = gps_tagsize - 4; //subtract ppi fieldheader
ppigps->version = 2;
ppigps->fields_present = PPI_GPS_FLAG_LAT | PPI_GPS_FLAG_LON | PPI_GPS_FLAG_APPID;
//GPSLAT
//printf("lat: %3.7f %f \n", gpsdata->lat, gpsdata->lat);
u = (block *) &(ppigps->field_data[gps_data_offt]);
u->u32 = kis_htole32(double_to_fixed3_7(gpsdata->lat));
gps_data_offt += 4;
//GPSLON
//printf("lon: %3.7f %f\n", gpsdata->lon, gpsdata->lon);
u = (block *) &(ppigps->field_data[gps_data_offt]);
u->u32 = kis_htole32(double_to_fixed3_7(gpsdata->lon));
gps_data_offt += 4;
//GPSALT
if (gps_tagsize >= 28) //include alt
{
u = (block *) &(ppigps->field_data[gps_data_offt]);
u->u32 = kis_htole32(double_to_fixed6_4(gpsdata->alt));
//u->u32 = kis_htole32(0x6b484390);
gps_data_offt += 4;
ppigps->fields_present |= PPI_GPS_FLAG_ALT;
}
//APPID
//printf("gps_data_offt %d gpslen = %d\n", gps_data_offt,ppigps->gps_len);
u = (block *) &(ppigps->field_data[gps_data_offt]);
u->u32 = kis_htole32(0x0053494B); //KIS0
gps_data_offt += 4;
ppigps->magic = PPI_GPS_MAGIC;
ppigps->gps_len = gps_tagsize - 4; //subtract ppi fieldheader
// Convert endian state
ppigps->fields_present = kis_htole32(ppigps->fields_present);
ppigps->pfh_datalen = kis_htole32(ppigps->pfh_datalen);
ppigps->gps_len = kis_htole16(ppigps->gps_len);
//Advance ppi cursor for other PPI tags.
ppi_pos += gps_tagsize;
}//tagsize > 0
}//gpsdata present
dump_offset = ppi_pos;
if (radioinfo != NULL) {
ppi_80211_common *ppi_common;
ppi_common = (ppi_80211_common *) &(dump_data[ppi_pos]);
ppi_pos += sizeof(ppi_80211_common);
ppi_common->pfh_datatype = kis_htole16(PPI_FIELD_11COMMON);
ppi_common->pfh_datalen = kis_htole16(sizeof(ppi_80211_common) -
sizeof(ppi_field_header));
if (packinfo != NULL)
ppi_common->tsf_timer = kis_htole64(packinfo->timestamp);
else
ppi_common->tsf_timer = 0;
// Assemble the flags in host mode then convert them all at once
ppi_common->flags = 0;
if (packinfo != NULL && packinfo->corrupt)
ppi_common->flags |= PPI_80211_FLAG_PHYERROR;
if (fcsdata != NULL) {
ppi_common->flags |= PPI_80211_FLAG_FCS;
if (fcsdata->checksum_valid == 0)
ppi_common->flags |= PPI_80211_FLAG_INVALFCS;
}
ppi_common->flags = kis_htole16(ppi_common->flags);
ppi_common->rate = kis_htole16(radioinfo->datarate / 5);
ppi_common->freq_mhz = kis_htole16((uint16_t) (radioinfo->freq_khz / 1000));
// Assemble the channel flags then endian swap them
ppi_common->chan_flags = 0;
switch (radioinfo->encoding) {
case encoding_cck:
ppi_common->chan_flags |= PPI_80211_CHFLAG_CCK;
break;
case encoding_ofdm:
ppi_common->chan_flags |= PPI_80211_CHFLAG_OFDM;
break;
case encoding_dynamiccck:
ppi_common->chan_flags |= PPI_80211_CHFLAG_DYNAMICCCK;
break;
case encoding_gfsk:
ppi_common->chan_flags |= PPI_80211_CHFLAG_GFSK;
break;
case encoding_pbcc:
case encoding_unknown:
break;
}
switch (radioinfo->carrier) {
case carrier_80211b:
ppi_common->chan_flags |= (PPI_80211_CHFLAG_2GHZ | PPI_80211_CHFLAG_CCK);
break;
case carrier_80211bplus:
ppi_common->chan_flags |= (PPI_80211_CHFLAG_2GHZ | PPI_80211_CHFLAG_CCK | PPI_80211_CHFLAG_TURBO);
break;
case carrier_80211a:
ppi_common->chan_flags |= (PPI_80211_CHFLAG_5GHZ | PPI_80211_CHFLAG_OFDM);
break;
case carrier_80211g:
// Could be PPI_80211_CHFLAG_OFDM or PPI_80211_CHFLAG_DYNAMICCCK
ppi_common->chan_flags |= PPI_80211_CHFLAG_2GHZ;
break;
case carrier_80211fhss:
ppi_common->chan_flags |= (PPI_80211_CHFLAG_2GHZ | PPI_80211_CHFLAG_GFSK);
break;
case carrier_80211dsss:
ppi_common->chan_flags |= PPI_80211_CHFLAG_2GHZ;
break;
case carrier_80211n20:
case carrier_80211n40:
// FIXME Dunno how to restore spectrum
ppi_common->chan_flags |= PPI_80211_CHFLAG_OFDM;
break;
case carrier_unknown:
break;
}
ppi_common->chan_flags = kis_htole16(ppi_common->chan_flags);
ppi_common->fhss_hopset = 0;
ppi_common->fhss_pattern = 0;
ppi_common->signal_dbm = radioinfo->signal_dbm;
ppi_common->noise_dbm = radioinfo->noise_dbm;
}
// Collate the allocation sizes of any callbacks
for (unsigned int p = 0; p < ppi_cb_vec.size(); p++) {
// Ignore errors for now
ppi_pos = (*(ppi_cb_vec[p].cb))(globalreg, 0, in_pack, dump_data, ppi_pos,
ppi_cb_vec[p].aux);
}
dump_offset = ppi_pos;
}
if (dump_len == 0) {
// printf("debug - nothing to dump\n");
return 0;
}
// printf("debug - making new dump, len %d\n", dump_len);
if (dump_data == NULL)
dump_data = new u_char[dump_len];
// copy the packet content in, offset if necessary
if (chunk != NULL) {
memcpy(&(dump_data[dump_offset]), chunk->data, chunk->length);
dump_offset += chunk->length;
}
// Lousy little hack to append the FCS after the data in PPI
if (dumpformat == dump_ppi && fcsdata != NULL &&
chunk != NULL && radioinfo != NULL) {
memcpy(&(dump_data[dump_offset]), fcsdata->data, 4);
dump_offset += 4;
}
// Fake a header
struct pcap_pkthdr wh;
wh.ts.tv_sec = in_pack->ts.tv_sec;
wh.ts.tv_usec = in_pack->ts.tv_usec;
wh.caplen = wh.len = dump_len;
// Dump it
pcap_dump((u_char *) dumper, &wh, dump_data);
delete[] dump_data;
dumped_frames++;
return 1;
}
#endif /* have_libpcap */