-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssid-logger.c
449 lines (403 loc) · 12.5 KB
/
ssid-logger.c
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
/*
ssid-logger is a simple software to log SSID you encounter in your vicinity
Copyright © 2020-2022 solsTiCe d'Hiver
This program 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 3 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <pcap/pcap.h>
#include <signal.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include <stdbool.h>
#include <pthread.h>
#include <time.h>
#include <sqlite3.h>
#include <time.h>
#ifdef HAS_SYS_STAT_H
#include <sys/stat.h>
#endif
#include <semaphore.h>
#include <libwifi.h>
#include "queue.h"
#include "hopper_thread.h"
#include "parsers.h"
#include "logger_thread.h"
#include "gps_thread.h"
#ifdef BLINK_LED
#include "blink_thread.h"
#endif
#include "db.h"
#include "config.h"
pcap_t *handle; // global, to use it in sigint_handler
queue_t *queue; // queue to hold parsed bss
pthread_t hopper;
pthread_t logger;
pthread_t gps;
#ifdef BLINK_LED
pthread_t blink;
#endif
int gps_thread_init_result = 0;
pthread_mutex_t mutex_queue = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex_gloc = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex_gtr = PTHREAD_MUTEX_INITIALIZER;
// to manage the return value of retrieve_gps_data thread
pthread_cond_t cv_gtr = PTHREAD_COND_INITIALIZER;
// semaphores to synchronise consumer (process_queue) and producer (process_packet)
sem_t queue_empty;
sem_t queue_full;
sqlite3 *db = NULL;
bool format_csv = false;
option_gps_t option_gps = GPS_LOG_ONZ;
FILE *file_ptr = NULL;
int ret = 0;
void sigint_handler(int s)
{
// stop pcap capture loop
pcap_breakloop(handle);
}
// produces bss and store them in the queue
void process_packet(uint8_t * args, const struct pcap_pkthdr *header, const uint8_t *packet)
{
struct libwifi_radiotap_info rtap_info;
libwifi_parse_radiotap_info(&rtap_info, packet, header->caplen);
struct libwifi_bss *bss = parse_beacon_frame(packet, header->caplen);
bss->signal = rtap_info.signal;
sem_wait(&queue_full);
pthread_mutex_lock(&mutex_queue);
enqueue(queue, bss);
pthread_mutex_unlock(&mutex_queue);
sem_post(&queue_empty);
}
void usage(void)
{
printf("Usage: ssid-logger -i IFACE [-f csv|sqlite3] [-o FILENAME] [-V] [-z] [-z]\n");
printf(" -i IFACE interface to use\n"
" -f csv|sqlite3 output format to use (default sqlite3)\n"
" -o FILENAME explicitly set the output filename\n"
" -V print version and exit\n"
" -z log ssid even if no gps coordinates are available\n"
" -zz or -z -z don't use gpsd and log all ssids\n"
);
}
void parse_args(int argc, char *argv[], bool *format_csv, char **file_name, char **iface, option_gps_t *option_gps)
{
int opt;
char *option_file_format = NULL;
char *option_file_name = NULL;
while ((opt = getopt(argc, argv, "f:hi:o:Vz")) != -1) {
switch (opt) {
case 'f':
option_file_format = optarg;
break;
case 'h':
usage();
exit(EXIT_SUCCESS);
break;
case 'i':
*iface = optarg;
break;
case 'o':
option_file_name = optarg;
break;
case 'V':
printf("%s %s / Copyright © 2020-2022 solsTiCe d'Hiver\n"
"This program comes with ABSOLUTELY NO WARRANTY;"
" this is free software, and you are welcome to redistribute it"
" under certain conditions; for details, see LICENSE.txt\n", NAME, VERSION);
exit(EXIT_SUCCESS);
break;
case 'z':
// one -z: log all SSIDs even if no GPS data so with gps coord. as 0.0
// two -z (-z -z): log all SSIDs with no gps coord. (0.0) and disable the use of gpsd
(*option_gps)++;
break;
case '?':
usage();
exit(EXIT_FAILURE);
default:
usage();
exit(EXIT_FAILURE);
}
}
if (*iface == NULL) {
fprintf(stderr, "Error: no interface selected\n");
exit(EXIT_FAILURE);
}
//printf("The device you entered: %s\n", iface);
if (option_file_format) {
if (strcmp(option_file_format, "csv") == 0) {
*format_csv = true;
} else if (strcmp(option_file_format, "sqlite3") == 0) {
*format_csv = false;
} else {
fprintf(stderr, "Error: unrecognised format (not csv nor sqlite3)\n");
exit(EXIT_FAILURE);
}
}
if (option_file_name == NULL) {
if (*format_csv) {
time_t now = time(NULL);
char timestamp[16];
strftime(timestamp, 16, "%Y%m%dT%H%M%S", gmtime(&now));
*file_name = malloc(32 * sizeof(char));
snprintf(*file_name, 32, "%s-ssid-logger.csv", timestamp);
} else {
*file_name = strdup(DB_NAME);
}
} else {
*file_name = strdup(option_file_name);
}
}
void initialize_pcap(pcap_t **handle, const char *iface)
{
char errbuf[PCAP_ERRBUF_SIZE];
// check if iface is in the list of known devices
pcap_if_t *devs = NULL;
if (pcap_findalldevs(&devs, errbuf) == 0) {
pcap_if_t *d = devs;
bool found = false;
while (!found && d != NULL) {
if ((strlen(d->name) == strlen(iface))
&& (memcmp(d->name, iface, strlen(iface)) == 0)) {
found = true;
break;
}
d = d->next;
}
pcap_freealldevs(devs);
if (!found) {
fprintf(stderr, "Error: %s is not a known interface.\n", iface);
exit(EXIT_FAILURE);
}
}
*handle = pcap_create(iface, errbuf);
if (*handle == NULL) {
fprintf(stderr, "Error: unable to create pcap handle: %s\n", errbuf);
exit(EXIT_FAILURE);
}
pcap_set_snaplen(*handle, SNAP_LEN);
pcap_set_timeout(*handle, 1000);
pcap_set_promisc(*handle, 1);
if (pcap_activate(*handle)) {
pcap_perror(*handle, "Error: ");
exit(EXIT_FAILURE);
}
// only capture packets received by interface
if (pcap_setdirection(*handle, PCAP_D_IN)) {
pcap_perror(*handle, "Error: ");
exit(EXIT_FAILURE);
}
// check if interface/driver can deliver radiotap header
int *dlt_buf, dlt_buf_len;
dlt_buf_len = pcap_list_datalinks(*handle, &dlt_buf);
if (dlt_buf_len < 0) {
pcap_perror(*handle, "Error: ");
exit(EXIT_FAILURE);
}
bool found = false;
for (int i=0; i< dlt_buf_len; i++) {
if (dlt_buf[i] == DLT_IEEE802_11_RADIO) {
found = true;
}
}
pcap_free_datalinks(dlt_buf);
// explicitly set the datalink to radiotap header
if (found) {
if (pcap_set_datalink(*handle, DLT_IEEE802_11_RADIO)) {
pcap_perror(*handle, "Error: ");
exit(EXIT_FAILURE);
}
} else {
fprintf(stderr, "Error: the interface %s does not support radiotap header or is not in monitor mode\n", iface);
exit(EXIT_FAILURE);
}
// only capture beacon frames
struct bpf_program bfp;
#if LIBPCAP_VERSION_1_10_0
char filter_exp[] = "subtype beacon";
#else
char filter_exp[] = "type mgt subtype beacon";
#endif
if (pcap_compile(*handle, &bfp, filter_exp, 1, PCAP_NETMASK_UNKNOWN) == -1) {
fprintf(stderr, "Error: couldn't parse filter %s: %s\n",
filter_exp, pcap_geterr(*handle));
exit(EXIT_FAILURE);
}
if (pcap_setfilter(*handle, &bfp) == -1) {
fprintf(stderr, "Error: couldn't install filter '%s': %s\n",
filter_exp, pcap_geterr(*handle));
exit(EXIT_FAILURE);
}
pcap_freecode(&bfp);
return;
}
int main(int argc, char *argv[])
{
char *iface = NULL;
char *file_name = NULL;
parse_args(argc, argv, &format_csv, &file_name, &iface, &option_gps);
if (option_gps == GPS_LOG_ZERO) {
printf(":: Warning: you have disabled the use of gpsd. All the GPS data will be 0.0.\n"
"** Please, don't upload such data file to wigle.net **\n");
}
initialize_pcap(&handle, iface);
// start the channel hopper thread
if (pthread_create(&hopper, NULL, hop_channel, iface)) {
fprintf(stderr, "Error creating channel hopper thread\n");
ret = EXIT_FAILURE;
goto hopper_failure;
}
pthread_detach(hopper);
// the queue holds the bsses produced by process_packet()
// the semaphores manage the synchronisation with process_queue() (in logger_thread)
queue = new_queue(MAX_QUEUE_SIZE);
sem_init(&queue_empty, 0, 0);
sem_init(&queue_full, 0, MAX_QUEUE_SIZE);
// start the helper logger thread
if (pthread_create(&logger, NULL, process_queue, NULL)) {
fprintf(stderr, "Error creating logger thread\n");
ret = EXIT_FAILURE;
goto logger_failure;
}
pthread_detach(logger);
// start the helper gps thread
if (pthread_create(&gps, NULL, retrieve_gps_data, &option_gps)) {
fprintf(stderr, "Error creating gps thread\n");
ret = EXIT_FAILURE;
goto gps_failure;
}
pthread_detach(gps);
// this is a little over-kill but is there a better way ?
pthread_mutex_lock(&mutex_gtr);
pthread_cond_wait(&cv_gtr, &mutex_gtr);
// wait for the gps thread init status
if (gps_thread_init_result == 2) {
// gps thread can't find gpsd
ret = EXIT_FAILURE;
goto gps_init_failure;
}
pthread_mutex_unlock(&mutex_gtr);
#ifdef BLINK_LED
// start the helper blink thread
if (pthread_create(&blink, NULL, blink_forever, NULL)) {
fprintf(stderr, "Error creating blink thread\n");
ret = EXIT_FAILURE;
goto blink_failure;
}
pthread_detach(blink);
#endif
struct sigaction act;
act.sa_handler = sigint_handler;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
// catch CTRL+C to break loop cleanly
sigaction(SIGINT, &act, NULL);
// catch quit signal to flush data to file on disk
sigaction(SIGQUIT, &act, NULL);
sigaction(SIGTERM, &act, NULL);
if (format_csv) {
// write pre-header and header
char *os_name = NULL, *os_version = NULL;
parse_os_release(&os_name, &os_version);
file_ptr = fopen(file_name, "a");
if (file_ptr == NULL) {
fprintf(stderr, "Error: Can't write to %s\n", file_name);
goto file_init_failure;
}
fprintf(file_ptr, "WigleWifi-1.4,appRelease=%s,model=%s,release=%s,"
"device=ssid-logger,display=ssid-logger,board=ssid-logger,brand=ssid-logger\n",
VERSION, os_name ? os_name : "linux", os_version ? os_version : "unknown");
free(os_name);
free(os_version);
fprintf(file_ptr, "MAC,SSID,AuthMode,FirstSeen,Channel,RSSI,"
"CurrentLatitude,CurrentLongitude,AltitudeMeters,"
"AccuracyMeters,Type\n");
} else {
#ifdef HAS_SYS_STAT_H
if (access(file_name, F_OK) == 0) {
// file exits, so double check it has writable permission
struct stat perm;
stat(file_name, &perm);
if (!(perm.st_mode & S_IWUSR)) {
// abort because the file does exist but is not writable. sqlite3 will not write to it
fprintf(stderr, "Error: %s is not writable\n", file_name);
db = NULL;
goto file_init_failure;
}
}
#endif
if (init_beacon_db(file_name, &db) != SQLITE_OK) {
db = NULL;
goto file_init_failure;
}
begin_txn(db);
}
// we have to cheat a little and print the message before pcap_loop
printf(":: Started sniffing beacon on %s, writing to %s\n", iface, file_name);
printf("Hit CTRL+C to quit\n");
int err;
if ((err = pcap_loop(handle, -1, (pcap_handler) process_packet, NULL))) {
if (err == PCAP_ERROR) {
pcap_perror(handle, "Error: ");
ret = err;
}
if (err == PCAP_ERROR_BREAK) {
printf("exiting...\n");
}
}
if (format_csv) {
fclose(file_ptr);
} else {
if (db != NULL) {
commit_txn(db);
sqlite3_close(db);
}
}
file_init_failure:
#ifdef BLINK_LED
pthread_cancel(blink);
blink_failure:
#endif
pthread_cancel(gps);
gps_init_failure:
pthread_mutex_destroy(&mutex_gtr);
pthread_cond_destroy(&cv_gtr);
gps_failure:
pthread_cancel(logger);
logger_failure:
sem_destroy(&queue_full);
sem_destroy(&queue_empty);
// free up elements of the queue
int qs = queue->size;
struct libwifi_bss *bss;
for (int i = 0; i < qs; i++) {
bss = (struct libwifi_bss *) dequeue(queue);
libwifi_free_bss(bss);
}
free(queue);
pthread_cancel(hopper);
hopper_failure:
pthread_mutex_destroy(&mutex_queue);
pthread_mutex_destroy(&mutex_gloc);
pcap_close(handle);
free(file_name);
#ifdef BLINK_LED
if (ret == EXIT_FAILURE) {
// signal that something has gone wrong
turn_led_on();
}
#endif
return ret;
}