-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
510 lines (393 loc) · 11.7 KB
/
main.cc
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*
* Copyright (c) 2001 - 2003
* Sebastian Majewski. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Sebastian Majewski
* and his contributors.
* 4. Neither the name of Sebastian Majewski nor the names of his contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifdef OS_AIX
#include <string.h>
#endif
#ifdef OS_LINUX
#include <string.h>
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <pcap.h>
#include <fcntl.h>
#include <string.h>
#include <netz.h>
#include "version.h"
#include "support.h"
#include "ph_packet.h"
#include "help.h"
extern int opterr;
extern int optind;
bool filter_from_file = false;
char filter_file[64];
bool debug_mode = false;
bool screen_output = true;
bool file_output = false;
int output_file;
bool hide_ether_loopback = false;
bool hide_cisco_cdp = false;
bool hide_eigrp_hello = false;
bool hide_ospf_hello = false;
string pcap_filter[2048] = "";
void print_version_info()
{
message((char*)"BLACKBIRD version %s, build date %s\n",
BBIRD_VERSION, BBIRD_BUILD_DATE);
char netz_version_string[32];
char netz_build_date_string[32];
message((char*)"NETZ version %s, build date %s\n",
netz_version(netz_version_string),
netz_build_date(netz_build_date_string));
}
int open_output_file(string* filename)
{
output_file = open(filename, O_RDWR|O_CREAT|O_APPEND, 400);
if(output_file < 0)
{
return -1;
}
return 0;
}
void print_help()
{
printf("%s", help_string);
}
int pcap_init(string* interface, bool promisc, u_int cap_len )
{
string errbuf[PCAP_ERRBUF_SIZE];
int retval;
pcap_t* pcap = pcap_open_live(interface, cap_len, promisc, 1024, errbuf);
if(pcap == NULL)
{
message((char*)"interface open failed: %s\n", errbuf);
return -1;
}
int linklayer = pcap_datalink(pcap);
if(pcap < 0)
{
message((char*)"pcap_datalink failed: %s\n", errbuf);
return -1;
}
else
{
message((char*)"link type: ");
switch(linklayer)
{
case DLT_NULL:
message((char*)"no link-layer encapsulation (gif)\n");
break;
case DLT_EN10MB:
message((char*)"Ethernet\n");
break;
case DLT_EN3MB:
message((char*)"Experimental Ethernet (3Mb) [not supported]\n");
return -1;
case DLT_AX25:
message((char*)"Amateur Radio AX.25 [not supported]\n");
return -1;
case DLT_PRONET:
message((char*)"Proteon ProNET Token Ring [not supported]\n");
return -1;
case DLT_CHAOS:
message((char*)"Chaos [not supported]\n");
return -1;
case DLT_IEEE802:
message((char*)"IEEE 802 Networks [not supported]\n");
return -1;
case DLT_ARCNET:
message((char*)"Arcnet [not supported]\n");
return -1;
case DLT_SLIP:
message((char*)"Serial Line IP [not supported]\n");
return -1;
case DLT_PPP:
message((char*)"Point-to-Point Protocol [not supported]\n");
return -1;
case DLT_FDDI:
message((char*)"FDDI [not supported]\n");
return -1;
case DLT_ATM_RFC1483:
message((char*)"LLC/SNAM Encapsulated atm [not supported]\n");
return -1;
#ifdef OS_OPENBSD
case DLT_LOOP:
message((char*)"loopback type (af header)\n");
break;
case DLT_ENC:
message((char*)"IPSEC enc type\n");
break;
#endif
case DLT_RAW:
message((char*)"raw IP [not supported]\n");
return -1;
case DLT_SLIP_BSDOS:
message((char*)"BSD/OS Serial Line IP [not supported]\n");
return -1;
case DLT_PPP_BSDOS:
message((char*)"BSD/OS Point-to-Point Protocol [not supported]\n");
return -1;
default:
message((char*)"unidentified\n");
return -1;
}
}
dword localnet = 0;
dword netmask = 0;
retval = pcap_lookupnet(interface, &localnet, &netmask, errbuf);
if(retval < 0)
{
message((char*)"interface %s has no IPV4 address assigned\n", interface);
}
else
{
string localnet_string[16];
string netmask_string[16];
conv_ip_str(localnet_string, localnet);
conv_ip_str(netmask_string, netmask);
message((char*)"localnet: %s\n", localnet_string);
message((char*)"netmask: %s\n", netmask_string);
}
message((char*)"hiden packets: ");
if(hide_ether_loopback)
{
message((char*)"ether loopback; ");
}
if(hide_cisco_cdp)
{
message((char*)"cisco cdp; ");
}
if(hide_eigrp_hello)
{
message((char*)"eigrp hello; ");
}
if(hide_ospf_hello)
{
message((char*)"ospf hello; ");
}
message((char*)"\n");
message((char*)"pcap filter: '%s'\n", pcap_filter);
bpf_program filtercode;
retval = pcap_compile(pcap, &filtercode, pcap_filter, 1, netmask);
if(retval < 0)
{
message((char*)"filter compiling failed: %s\n", pcap_geterr(pcap));
return -1;
}
else
{
message((char*)"filter compiled...\n");
}
retval = pcap_setfilter(pcap, &filtercode);
if(retval < 0)
{
message((char*)"filter setting failed: %s\n", pcap_geterr(pcap));
return -1;
}
else
{
message((char*)"filter applied...\n");
}
message((char*)"capturing started...\n\n");
pcap_loop(pcap, -1, (pcap_handler)packet_handler, (byte*)&linklayer);
return 0;
}
int main(int argc, char** argv)
{
char bbird_build_date[64];
strncpy(bbird_build_date, BBIRD_BUILD_DATE, 64);
message((char*)"BLACKBIRD %s (c) 2000-%s Sebastian Majewski\n",
BBIRD_VERSION, bbird_build_date + strlen(bbird_build_date) - 4);
string interface[16] = "none";
bool promisc = true;
u_int cap_len = 0;
int retval;
int option;
opterr = 1;
while((option = getopt(argc, argv, "dvi:pF:c:ho:O:H:")) != -1)
{
switch(option)
{
case 'd':
debug_mode = true;
break;
case 'v':
print_version_info();
return 0;
case 'i':
snprintf(interface, 16, "%s", optarg);
break;
case 'o':
retval = open_output_file(optarg);
if(retval)
{
message((char*)"cannot open or create output file: %s\n", optarg);
return -1;
}
file_output = true;
break;
case 'O':
retval = open_output_file(optarg);
if(retval)
{
message((char*)"cannot open or create output file: %s\n", optarg);
return -1;
}
file_output = true;
screen_output = false;
break;
case 'p':
promisc = false;
break;
case 'F':
filter_from_file = true;
strncpy(filter_file, optarg, 64);
break;
case 'c':
cap_len = atoi(optarg);
break;
case 'h':
print_help();
return 0;
case 'H':
if(!strcmp(optarg, "el"))
{
hide_ether_loopback = true;
}
else if(!strcmp(optarg, "cdp"))
{
hide_cisco_cdp = true;
}
else if(!strcmp(optarg, "eigrp_hello"))
{
hide_eigrp_hello = true;
}
else if(!strcmp(optarg, "ospf_hello"))
{
hide_ospf_hello = true;
}
else
{
message((char*)"bad hide argument: %s\n", optarg);
return -1;
}
break;
case '?':
return -1;
}
}
if (filter_from_file)
{
int file = open(filter_file, O_RDONLY);
if(file < 0)
{
message((char*)"cannot open filter file: %s\n", filter_file);
return -1;
}
read(file, pcap_filter, 2048);
close(file);
}
else
{
if(argv[optind])
argvncpy(pcap_filter, &argv[optind], 256);
}
if(debug_mode)
{
message((char*)"debug mode\n");
}
if(screen_output)
{
message((char*)"screen output mode: ON\n");
}
else
{
message((char*)"screen output mode: OFF\n");
}
if(file_output)
{
message((char*)"file output mode: ON\n");
}
else
{
message((char*)"file output mode: OFF\n");
}
if(strcmp(interface, "none"))
{
message((char*)"interface: %s\n", interface);
ifreq ifr;
strncpy(ifr.ifr_name, interface, 8);
int sock = socket(AF_INET, SOCK_DGRAM, 0);
if(ioctl(sock, SIOCGIFMTU, (caddr_t)&ifr) < 0)
{
message((char*)"%s mtu discovery failed...\n", interface);
return -1;
}
else
{
u_int mtu = ifr.ifr_mtu;
message((char*)"mtu: %u\n", mtu);
if(!cap_len)
{
cap_len = mtu + 32;
}
}
}
else
{
message((char*)"interface not set...\n", interface);
return -1;
}
if((cap_len > 0) && (cap_len < 65536))
{
message((char*)"capture length: %u\n", cap_len);
}
else
{
message((char*)"wrong capture length set, can be 1 - 65535\n");
return -1;
}
if(promisc)
{
message((char*)"promisc mode: ON\n");
}
else
{
message((char*)"promisc mode: OFF\n");
}
return pcap_init(interface, promisc, cap_len);
}