-
Notifications
You must be signed in to change notification settings - Fork 7
/
parse_cap.c
201 lines (157 loc) · 6.5 KB
/
parse_cap.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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <pcap/pcap.h>
#include <pcap/bpf.h>
#include "parse_cap.h"
#include "arguments.h"
#include "sniff.h"
#include "radiotap/radiotap_iter.h"
#include "radiotap/radiotap.h"
#include "radiotap/platform.h"
void parse_cap(const struct arguments *config, struct sniff_stat *stat)
{
pcap_t *handle;
struct pcap_pkthdr *pkt_header;
const u_char *pkt_data;
char errbuf[PCAP_ERRBUF_SIZE];
int status;
double t1;
FILE *fp;
struct bssid_inf wifi_inf;
printf("info: start parse cap-file to txt-file...\n");
//Open cap-file
handle = pcap_open_offline(config->file_cap, errbuf);
if (!handle) {
fprintf(stderr, "system error: pcap_open_offline: %s\n", errbuf);
exit(EXIT_FAILURE);
}
status = pcap_set_datalink(handle, DLT_IEEE802_11_RADIO); //Set type of link-header
if (status) {
fprintf(stderr, "system error: pcap_set_datalink: %s\n", pcap_geterr(handle));
exit(EXIT_FAILURE);
}
//Open txt-file
fp = fopen(config->file_txt, "w");
if (!fp) {
fprintf(stderr, "system error: fopen: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
/* M A I N L O O P */
//Parse cap-file and write link info in txt-file
status = pcap_next_ex(handle, &pkt_header, &pkt_data);
if (status == -1) {
fprintf(stderr, "system error: pcap_next_ex: %s\n", pcap_geterr(handle));
exit(EXIT_FAILURE);
}
t1 = timeval2double(&pkt_header->ts);
wifi_inf.timestamp = timeval2double(&pkt_header->ts) - t1;
wifi_inf.rssi = parse_radiotap(pkt_data);
parse_beacon(pkt_data, wifi_inf.bssid);
cnt_frames(wifi_inf.bssid, config->bssid, config->bssid_cnt, stat->result);
stat->cnt_frames++;
fprintf(fp, "timestamp (sec): RSSI (dBm): BSSID:\n\n");
fprintf_bssid_inf(fp, &wifi_inf);
while ( (status = pcap_next_ex(handle, &pkt_header, &pkt_data)) != -2 ) {
if (status == -1) {
fprintf(stderr, "system error: pcap_next_ex: %s\n", pcap_geterr(handle));
exit(EXIT_FAILURE);
}
wifi_inf.timestamp = timeval2double(&pkt_header->ts) - t1;
wifi_inf.rssi = parse_radiotap(pkt_data);
parse_beacon(pkt_data, wifi_inf.bssid);
cnt_frames(wifi_inf.bssid, config->bssid, config->bssid_cnt, stat->result);
stat->cnt_frames++;
fprintf_bssid_inf(fp, &wifi_inf);
}
fprintf_sniff_stat(fp, config, stat);
//Deallocate resources
pcap_close(handle);
fclose(fp);
printf("info: finished parse cap-file to txt-file\n");
}
/*
*Extract wifi rssi value from radiotap-header of frames
*This func executes parse regardless of radiotap header length (length depend on driver / WNIC)
*/
signed char parse_radiotap(const unsigned char *pkt_data)
{
struct ieee80211_radiotap_iterator iterator;
struct ieee80211_radiotap_header *radiotap_header = (struct ieee80211_radiotap_header *)pkt_data;
int max_length = get_unaligned_le16(&radiotap_header->it_len);
int status;
signed char rssi = 0;
status = ieee80211_radiotap_iterator_init(&iterator, radiotap_header, max_length, NULL);
if (status) {
fprintf(stderr, "system error: ieee80211_radiotap_iterator_init: couldn't get iterator\n");
exit(EXIT_FAILURE);
}
while (!status) {
status = ieee80211_radiotap_iterator_next(&iterator);
if (status)
continue;
if (iterator.this_arg_index == IEEE80211_RADIOTAP_DBM_ANTSIGNAL)
rssi = *iterator.this_arg;
}
return rssi;
}
/*
*Extract source address (MAC of AP)
*This function skips radiotap header
*/
void parse_beacon(const unsigned char *pkt_data, unsigned char *bssid)
{
struct ieee80211_radiotap_header *radiotap_header = (struct ieee80211_radiotap_header *)pkt_data;
int radiotap_length = get_unaligned_le16(&radiotap_header->it_len);
pkt_data += radiotap_length; //skip radiotap header of frame
pkt_data += NUMBER_BYTE_BEFORE_SA; //skip unnecessary beacon inforation
for (int i = 0; i < 6; i++)
bssid[i] = *(pkt_data + i);
}
//Wtire to file @struct bssid_inf information
void fprintf_bssid_inf(FILE *fp, const struct bssid_inf *wifi_inf)
{
fprintf(fp, "%-18.6f%-13hhd", wifi_inf->timestamp, wifi_inf->rssi);
for (int i = 0; i < 5; i++)
fprintf(fp, "%02X:", wifi_inf->bssid[i]);
fprintf(fp, "%02X\n", wifi_inf->bssid[5]);
}
//
void cnt_frames(const unsigned char *frame_bssid_num, const char (*arg_bssid)[18], const int arg_bssid_cnt, unsigned int *result)
{
int status;
char frame_bssid_str[18];
bssid_num2str(frame_bssid_num, frame_bssid_str);
for (int i = 0; i < arg_bssid_cnt; i++) {
status = strcmp(frame_bssid_str, arg_bssid[i]);
if (!status)
result[i] += 1;;
}
}
void bssid_num2str(const unsigned char *frame_bssid_num, char *frame_bssid_str)
{
int status = 0;
char *ptr = frame_bssid_str;
for (int i = 0; i < 5; i++) {
ptr += status;
status = sprintf(ptr, "%02X:", frame_bssid_num[i]);
if (status != 3)
fprintf(stderr, "system error: bssid_num2str: sprintf error\n");
}
sprintf(ptr + status, "%02X", frame_bssid_num[5]);
}
void sniff_stat_init(const struct arguments *config, struct sniff_stat *stat)
{
stat->cnt_bssid = config->bssid_cnt;
stat->result = (unsigned int *) calloc (stat->cnt_bssid, sizeof(unsigned int));
stat->cnt_frames = 0;
}
void fprintf_sniff_stat(FILE *fp, const struct arguments *config, const struct sniff_stat *stat)
{
fprintf(fp, "________________________________________________\n\n");
fprintf(fp, "Number of all sniff beacon frames: %u (it coincides with number of RSSI values)\n", stat->cnt_frames);
for (int i = 0; i < stat->cnt_bssid; i++) {
fprintf(fp, "BSSID %s : %u of %u\n", config->bssid[i], stat->result[i], stat->cnt_frames);
}
}