-
Notifications
You must be signed in to change notification settings - Fork 17
/
sniffer.c
340 lines (277 loc) · 7.82 KB
/
sniffer.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
/* cSploit - a simple penetration testing suite
* Copyright (C) 2014 Massimo Dragano aka tux_mind <[email protected]>
*
* cSploit 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.
*
* cSploit 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 cSploit. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <pthread.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include <stddef.h>
#include <unistd.h>
#include <csploit/logger.h>
#include "netdefs.h"
#include "host.h"
#include "event.h"
#include "nbns.h"
#include "sniffer.h"
#include "resolver.h"
#include "prober.h"
#include "ifinfo.h"
#include "analyzer.h"
struct sniff_data sniffer_info;
/**
* @brief the sniffer thread
*/
void *sniffer(void *arg) {
#ifdef PROFILE
int enq_res = 0;
unsigned long int total_pkts = 0;
unsigned long int rejected_pkts = 0;
unsigned long int oom_pkts = 0;
# ifdef BUG81370
unsigned long int bad_pkts = 0;
# endif
#endif /* PROFILE */
#ifdef HAVE_LIBPCAP
struct pcap_pkthdr pkthdr;
char *bytes, *buffcopy;
while((bytes = (char *) pcap_next(sniffer_info.handle, &pkthdr))) {
# ifdef PROFILE
total_pkts++;
buffcopy = malloc(pkthdr.caplen);
if(buffcopy) {
memcpy(buffcopy, bytes, pkthdr.caplen);
enq_res = enqueue_packet(buffcopy, pkthdr.len, pkthdr.caplen);
if(enq_res) {
free(buffcopy);
if(enq_res == -2)
oom_pkts++;
else
rejected_pkts++;
}
} else {
oom_pkts++;
}
# else
buffcopy = malloc(pkthdr.caplen);
if(buffcopy) {
memcpy(buffcopy, bytes, pkthdr.caplen);
if(enqueue_packet(buffcopy, pkthdr.len, pkthdr.caplen)) {
free(buffcopy);
}
}
# endif /* PROFILE */
}
pcap_close(sniffer_info.handle);
#else /* HAVE_LIBPCAP */
int pktlen, caplen;
struct sockaddr_ll from;
char *buffcopy;
socklen_t fromlen;
# ifdef BUG81370
struct ether_header ethhdr;
# endif
#ifndef MIN
#define MIN(a, b) (a < b ? a : b)
#endif
fromlen = sizeof(from);
memset(&from, 0, fromlen);
/* i decided to not lock the sniffer_info.control.mutex
* for check sniffer_info.control.active to save time.
* sniffer_info.control.active is initially set to 1,
* and changes only one time to 0.
*/
while(sniffer_info.control.active) {
pktlen = recvfrom( sniffer_info.sockfd, sniffer_info.buffer,
sniffer_info.bufflen, MSG_TRUNC,
(struct sockaddr *) &from, &fromlen);
if(pktlen == -1) {
if(errno == EINTR) {
continue;
} else if(errno != EBADF || (sniffer_info.control.active)) {
print( ERROR, "recvfrom: %s", strerror(errno));
}
break;
} else if(pktlen == 0) { // socket has been shutted down
break;
} else if(pktlen < ETH_HLEN) {
continue;
}
caplen = MIN(pktlen, sniffer_info.bufflen);
# ifdef PROFILE
total_pkts++;
# endif
# ifdef BUG81370
memcpy(ðhdr, sniffer_info.buffer, ETH_HLEN);
if(memcmp(ethhdr.ether_shost, from.sll_addr, ETH_ALEN)) {
# ifdef PROFILE
bad_pkts++;
# endif
continue;
}
# endif
buffcopy = malloc(caplen);
if(!buffcopy) {
# ifdef PROFILE
oom_pkts++;
# endif
continue;
}
memcpy(buffcopy, sniffer_info.buffer, caplen);
# ifdef BUG81370
memcpy(buffcopy, ðhdr, ETH_HLEN);
# endif
# ifdef PROFILE
enq_res = enqueue_packet(buffcopy, pktlen, caplen);
if(enq_res) {
free(buffcopy);
if(enq_res == -2) {
oom_pkts++;
} else {
rejected_pkts++;
}
}
# else
if(enqueue_packet(buffcopy, pktlen, caplen)) {
free(buffcopy);
}
# endif
}
#endif /* HAVE_LIBPCAP */
#ifdef PROFILE
# ifdef BUG81370
print( DEBUG, "sniffed %lu packets, %lu corrupted, %lu lost for OOM, %lu rejected",
total_pkts, bad_pkts, oom_pkts, rejected_pkts);
# else
print( DEBUG, "packets: %lu sniffed, %lu lost for OOM, %lu rejected",
total_pkts, oom_pkts, rejected_pkts);
# endif /* BUG81370 */
#endif /* PROFILE */
print( DEBUG, "quitting");
free(sniffer_info.buffer);
return NULL;
}
/**
* @brief start sniffing
* @returns 0 on success, -1 on error.
*/
int start_sniff() {
#ifdef HAVE_LIBPCAP
char errbuff[PCAP_ERRBUF_SIZE];
char filter_str[81];
struct bpf_program filter;
*errbuff = '\0';
sniffer_info.handle = pcap_open_live(ifinfo.name, ifinfo.mtu + ETH_HLEN, 0, 0, errbuff);
if(!sniffer_info.handle) {
print( ERROR, "pcap_open_live: %s", errbuff);
return -1;
}
if(*errbuff) {
print( WARNING, "pcap_open_live: %s", errbuff);
}
snprintf(filter_str, 81,
"((( arp or rarp ) and ether src not %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx ) or ( udp and port 137 ))",
ifinfo.eth_addr[0], ifinfo.eth_addr[1], ifinfo.eth_addr[2],
ifinfo.eth_addr[3], ifinfo.eth_addr[4], ifinfo.eth_addr[5]);
if(pcap_compile(sniffer_info.handle, &filter, filter_str, 1, (bpf_u_int32) ifinfo.ip_mask)) {
print( ERROR, "pcap_compile: %s", pcap_geterr(sniffer_info.handle));
print( DEBUG, "filter: '%s'", filter_str);
goto error;
}
if(pcap_setfilter(sniffer_info.handle, &filter)) {
print( ERROR, "pcap_setfilter: %s", pcap_geterr(sniffer_info.handle));
goto error;
}
#else /* HAVE_LIBPCAP */
struct sockaddr_ll sll;
int err;
socklen_t errlen = sizeof(err);
/* pcap_open_live for Linux */
memset(&sll, 0, sizeof(sll));
sll.sll_family = AF_PACKET;
sll.sll_ifindex = ifinfo.index;
sll.sll_protocol = htons(ETH_P_ALL);
sniffer_info.sockfd = socket(PF_PACKET, SOCK_RAW, sll.sll_protocol);
if(sniffer_info.sockfd == -1) {
print( ERROR, "socket: %s", strerror(errno));
return -1;
}
if (bind(sniffer_info.sockfd, (struct sockaddr *) &sll, sizeof(sll)) == -1) {
print( ERROR, "bind: %s\n", strerror(errno));
goto error;
}
/* Any pending errors, e.g., network is down? */
if (getsockopt(sniffer_info.sockfd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) {
print( ERROR, "getsockopt: %s\n", strerror(errno));
goto error;
}
if (err > 0) {
print( ERROR, "bind: %s\n", strerror(err));
goto error;
}
/* pcap_open_live ends */
#endif /* HAVE_LIBPCAP */
sniffer_info.bufflen = ifinfo.mtu + ETH_HLEN;
sniffer_info.buffer = malloc(sniffer_info.bufflen);
if(!(sniffer_info.buffer)) {
print( ERROR, "malloc: %s", strerror(errno));
goto error;
}
if(init_hosts()) {
print( ERROR, "init_hosts: %s", strerror(errno));
goto hosts_error;
}
if(pthread_create(&(sniffer_info.tid), NULL, sniffer, NULL)) {
print( ERROR, "pthread_create: %s", strerror(errno));
goto pthread_error;
}
return 0;
pthread_error:
sniffer_info.tid = 0;
free(hosts.array);
hosts_error:
free(sniffer_info.buffer);
error:
#ifdef HAVE_LIBPCAP
pcap_close(sniffer_info.handle);
#else
close(sniffer_info.sockfd);
#endif
return -1;
}
/**
* @brief stop the sniffer thread
*/
void stop_sniff() {
pthread_t tid;
pthread_mutex_lock(&(sniffer_info.control.mutex));
tid = sniffer_info.tid;
sniffer_info.tid = 0;
sniffer_info.control.active = 0;
pthread_mutex_unlock(&(sniffer_info.control.mutex));
if(!tid)
return;
#ifdef HAVE_LIBPCAP
pcap_breakloop(sniffer_info.handle);
#else
close(sniffer_info.sockfd);
#endif
pthread_join(tid, NULL);
}