-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathssidentity.c
286 lines (259 loc) · 9.2 KB
/
ssidentity.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
/*
WIRESHARK FILTERS
Frame type Filter
Management frames wlan.fc.type eq 0
Control frames wlan.fc.type eq 1
Data frames wlan.fc.type eq 2
Frame subtype Filter
Association request wlan.fc.type_subtype eq 0
Association response wlan.fc.type_subtype eq 1
Probe request wlan.fc.type_subtype eq 4
Probe response wlan.fc.type_subtype eq 5
Beacon wlan.fc.type_subtype eq 8
Authentication wlan.fc.type_subtype eq 11
Deauthentication wlan.fc.type_subtype eq 12
SQLite3 REFERENCE
https://www.sqlite.org/c3ref/intro.html
** Use airmon-ng to enable promiscuous mode **
** For the moment, it's easier to debug. **
CLIENT (This) TODOs
# Move timestamp to epoch
# Use MAC as primary Key and keep timestamps as CSV text field
# Handle UTF-8 and other crap leaking through isprint()
# Add remote admin capability (edit settings / modes)
# Add DB upload to server based on settings
# Add distance filtering based on settings
SERVER (Coming) TODOs
# Add Triangulation (Trilateration Estimation)
# Add Network Tree (AP - Known clients)
*/
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
// isalnum
#include <ctype.h>
// memset
#include <string.h>
// FSPL equations
#include <math.h>
// SQL
#include <sqlite3.h>
// Socket
#include <netinet/if_ether.h>
// IP Header structs
#include <netinet/ip.h>
// Local
#include "ssidentity.h"
// Verbose Assertions
#include "cAssert.h"
static int sqlCallback(void *NotUsed, int argc, char **argv, char **azColName){
return 0;
}
int main( int argc, char **argv ) {
//SQL vars
char *sqlCmd;
char *sqlErr;
sqlite3 *requestDB;
int rc = sqlite3_open("observations.db", &requestDB);
cAssertMsg((rc <= 0), "Failed to open observations.db\n");
// Buffer for received 80211 frames
uint8_t pktBuff[PKT_BUFF_SZ];
// Setup a scoket for *any* protocol
struct sockaddr socketAddr;
socklen_t addrLen = sizeof(socketAddr);
// Family, type, protocol
int sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
cAssertMsg( (sock >= 0), "Are you running as root?\n");
while(TRUE) {
// Receive without needing a socket connection
int recvDataSize = recvfrom( sock, pktBuff, PKT_BUFF_SZ,
NOFLAG, &socketAddr, &addrLen);
// Parse it if it's a probe request
Request request = parseRaw(pktBuff, recvDataSize);
// Print it if it's not null
if( request != NULL ){
printf("%s %s \"%s\"\t%ddBm %umHz Dist: %.02fm\n",
request->timeStamp,
request->clientMAC,
request->SSID,
request->RSSI,
request->frequency,
request->distance);
// Add a new entry to the db
sqlCmd = sqlite3_mprintf("INSERT INTO obs "
"(TIMESTAMP, MAC, SSID, RSSI, FREQ, DIST)"
" VALUES ('%q', '%q', '%q', %d, %u, %.02f);",
request->timeStamp,
request->clientMAC,
request->SSID,
request->RSSI,
request->frequency,
request->distance);
/* Execute SQL statement */
rc = sqlite3_exec(requestDB, sqlCmd, sqlCallback, 0, &sqlErr);
if( rc != SQLITE_OK ){
// printf(stderr, "SQL error: %s\n", sqlErr);
sqlite3_free(sqlErr);
}
}
free(request);
}
return EXIT_SUCCESS;
}
/*
\x08\x01\x18\x10\x07\x08\x0A\x0C� �
Parse 802.11 probe requests from raw socket data.
Since this is an open, promiscuous socket..The data
could really be anything.
If the data yields;
- The correct frame protocol at the correct offset
- A broadcast MAC at the correct offset
- A valid SSID length
- Isn't a known protocol (IP layer)
It is then considered a valid probe request.
*/
Request parseRaw( uint8_t *buff, uint16_t buffSize ) {
// Get the frame protocol out
int16_t frameProtocol = (buff[FRAME_CTL_OFFSET] & 0xF0) >> 4;
// Get frame header as iphdr struct
struct iphdr *ip_header;
ip_header = (struct iphdr *)(buff + sizeof(struct ethhdr));
// Get frame protocol
int protocol = ip_header->protocol;
// Skip known ethernet protocols and non broadcast frames
// If statements are eval'd in order, ordered for fast rejection.
if( frameProtocol == IEEE80211_STYPE_PROBE_REQ &&
isBroascast( &buff[DEST_ADDR_OFFSET]) &&
!isKnownProtocol( protocol ) ){
// Begin decoding the frame
int i, validSSID;
validSSID = TRUE;
// Throw a new request onto the heap
Request request = malloc( sizeof( RequestStruct ) );
// Preformat the SSID with NULL chars
memset(request->SSID, '\0', SSID_BUFF_SZ);
// Check the SSID isn't blank or ovrflw
uint8_t SSIDlen = buff[SSID_LEN_OFFSET];
if( SSIDlen > 0 && SSIDlen <= SSID_BUFF_SZ ) {
// Copy SSID into request
for(i = 0; i < SSIDlen; i++){
// If it's a printable char
if( isprint(buff[SSID_CHR_OFFSET + i]) && validSSID ) {
request->SSID[i] = buff[SSID_CHR_OFFSET + i];
// Otherwise it's something interesting...log it.
} else {
// Convert it to a '\xNN' string rep
char hexByte[5];
snprintf(&hexByte[0], 5, "\\x%02X",
(unsigned char)buff[SSID_CHR_OFFSET + i]);
// Set the SSID to printable HEX
request->SSID[i] = hexByte[0];
request->SSID[i+1] = hexByte[1];
request->SSID[i+2] = hexByte[2];
request->SSID[i+3] = hexByte[3];
// Kick i fwd a few spots
i+=3;
validSSID = FALSE;
}
}
// Now get client MAC as uint64
uint64_t longMac = macU8ToU64( &buff[MAC_ADDR_OFFSET] );
// Make a HEX string from it
snprintf(request->clientMAC, 32, "%012lX", longMac);
// Give the request a time-stamp
setTimeStamp( request );
// Set the RSSI
request->RSSI = buff[RSSI_OFFSET] - 0xFF;
// Set the frequency
request->frequency = (buff[FREQ_OFFSET] & 0xFF) << 8;
request->frequency |= (buff[FREQ_OFFSET+1] & 0xFF);
// set the distance estimate
request->distance = signalToDistance(request->RSSI,
request->frequency);
// Tell me if you see that bus go past
if(!validSSID) infoRed("Found that weird STA bus...\n");
// Return the request
return request;
} else {
// SSID length was outside spec
return NULL;
}
} else {
// Frame protocol other than probe request
return NULL;
}
}
/*
Time stamp a request.
It's a shame dealing with time in C is such a pain.
*/
void setTimeStamp( Request request ) {
// Epoch
time_t rawtime;
// Time tm struct
struct tm *info;
// Buffer for string rep
char buffer[80];
// Fill the struct
time( &rawtime );
info = localtime( &rawtime );
// Format the timestamp string
strftime(request->timeStamp, TIME_BUFF_SZ ,"%x - %I:%M%p", info);
}
/*
Check a decoded IP header protocol against known
protocols. Return true if it's;
-UDP
-TCP
-IGMP
-ICMP
*/
int isKnownProtocol( int protocol ) {
if( protocol == PROTO_UDP || protocol == PROTO_TCP ||
protocol == PROTO_IGMP || protocol == PROTO_ICMP ) {
return TRUE;
} else {
return FALSE;
}
}
/*
Check 6 bytes of a buffer for a broadcast MAC.
uint8_t *buff should point to the first byte
of the MAC.
Assumes 6 bytes in buff.
*/
int isBroascast( uint8_t *buff ) {
if( buff[5] == 0xFF && buff[4] == 0xFF &&
buff[3] == 0xFF && buff[2] == 0xFF &&
buff[1] == 0xFF && buff[0] == 0xFF ) {
return TRUE;
} else {
return FALSE;
}
}
/*
Shift a MAC address into a 64 bit unsigned int.
Useful for converting to a string.
*/
uint64_t macU8ToU64( uint8_t *mac ) {
uint64_t macint;
// MAC bytes into a long
macint = (uint64_t)(mac[0] & 0xFF) << (8*5);
macint |= (uint64_t)(mac[1] & 0xFF) << (8*4);
macint |= (uint64_t)(mac[2] & 0xFF) << (8*3);
macint |= (uint64_t)(mac[3] & 0xFF) << (8*2);
macint |= (uint64_t)(mac[4] & 0xFF) << 8;
macint |= (uint64_t) mac[5] & 0xFF;
return macint;
}
/*
Get an estimate of device distance from freq and RSSI.
Uses "free space path loss" equation.
https://en.wikipedia.org/wiki/Free-space_path_loss
for MHz / meters,
RSSI = FSPL(dB) = 20*log10( dist ) + 20*log10( freq ) - 27.55
*/
float signalToDistance( int8_t RSSI, uint16_t frequency ) {
float distance = (27.55-RSSI-(20*log10(frequency)))/20.00;
return (float)pow(10, distance);
}