-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
123 lines (108 loc) · 3.52 KB
/
main.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
#include "tins/tins.h"
#include <fstream>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <tins/packet.h>
#include <tins/sniffer.h>
using namespace std;
using namespace Tins;
// needed to get MAC addrs
template <class T> HWAddress<6> get_src_addr(const T &data) {
if (!data.from_ds() && !data.to_ds())
return data.addr2();
if (!data.from_ds() && data.to_ds())
return data.addr2();
return data.addr3();
}
string parse_probe(const Packet &packet) {
stringstream ss;
try {
// first check that it's a probe request frame
if (const auto *probe = packet.pdu()->find_pdu<Dot11ProbeRequest>()) {
string ssid = probe->ssid();
if (ssid.empty()) {
ssid = "NULL";
}
ss << "timestamp:" << packet.timestamp().seconds()
<< " src_addr:" << probe->addr2()
<< " ssid:" << ssid;
if (const auto *rt = packet.pdu()->find_pdu<RadioTap>()) {
ss << " dbm_signal:" << (int)rt->dbm_signal()
<< " rate:" << (int)rt->rate()
<< " channel_freq:" << rt->channel_freq();
}
ss << endl;
}
} catch (...) {
}
return ss.str();
}
int main(int argc, char *argv[]) {
int limit = 1000;
string interface = "";
if (argc > 2) {
interface = argv[1];
limit = atoi(argv[2]);
} else {
cerr << "Format error: "
<< "\n";
cerr << "Usage: sudo ./sniff-report <interface> <packet limit>";
return 1;
}
int count = 0;
SnifferConfiguration config;
config.set_promisc_mode(true);
config.set_rfmon(true);
config.set_filter("type data or type mgt subtype probe-req or type mgt subtype beacon");
Sniffer sniffer(interface, config);
// Table setup for output
cout << left << setw(10) << "Count"
<< left << setw(20) << "Timestamp"
<< left << setw(30) << "Src Address"
<< left << setw(30) << "SSID"
<< left << setw(20) << "Signal (dBm)"
<< left << setw(20) << "Rate (Mbps)"
<< left << setw(20) << "Channel Freq (MHz)" << endl;
cout << setfill('-') << setw(150) << "-" << setfill(' ') << endl;
bool running = true;
while (running) {
if (count >= limit) {
running = false;
break;
}
try {
Packet packet = sniffer.next_packet();
// checking if packet has data we care about from parse_probe function
if (parse_probe(packet) != "") {
string parse_result = parse_probe(packet);
stringstream ss(parse_result);
string timestamp, src_addr, ssid, dbm_signal, rate, channel_freq;
getline(ss, timestamp, ' ');
getline(ss, src_addr, ' ');
getline(ss, ssid, ' ');
getline(ss, dbm_signal, ' ');
getline(ss, rate, ' ');
getline(ss, channel_freq, ' ');
// Extract the value after the colon
timestamp = timestamp.substr(timestamp.find(":") + 1);
src_addr = src_addr.substr(src_addr.find(":") + 1);
ssid = ssid.substr(ssid.find(":") + 1);
dbm_signal = dbm_signal.substr(dbm_signal.find(":") + 1);
rate = rate.substr(rate.find(":") + 1);
channel_freq = channel_freq.substr(channel_freq.find(":") + 1);
// Print values in a tabular format
cout << left << setw(10) << count
<< left << setw(20) << timestamp
<< left << setw(30) << src_addr
<< left << setw(30) << ssid
<< left << setw(20) << dbm_signal
<< left << setw(20) << rate
<< left << setw(20) << channel_freq << endl;
count++;
}
} catch (...) {
}
}
return 0;
}