-
Notifications
You must be signed in to change notification settings - Fork 0
/
net-dissector.pl
executable file
·157 lines (124 loc) · 4.55 KB
/
net-dissector.pl
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
#!/usr/bin/perl
# Copyright (C) 2015 Dimitar Petkov <[email protected]>
# This program 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.
# This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
use FindBin;
BEGIN {push @INC, "$FindBin::Bin/libs"}
require Net::PcapUtils; #./libs/Net/PcapUtils.pm
require JsonUtils;
require PacketProcessor;
use JSON;
use Getopt::Long;
use strict;
use warnings;
# Autoflush STDOUT
$| = 1;
#---------------------------------------------------------------------------
# Signal handlers
use sigtrap qw/handler sig_handler normal-signals/;
sub sig_handler {
die "Exiting on signal '".$!."'\n";
}
#---------------------------------------------------------------------------
# CLI Options
sub print_usage {
print
"\n"
."Usage: ".$0." [OPTIONS] CONFIG_FILE\n"
." Dissects sniffed Network packets and outputs parsed fields in JSON format."
."\n\n"
."CONFIG_FILE: \n"
." A JSON formatted configuration file, that determines how captured data should be parsed \n"
." and outputted."
."\n\n"
."Options:\n"
." -l, --list List all available network devices end exit.\n"
."\n"
." -i, --interface=INTERFACE Network Interface to sniff on. Either -i or -r must be specified.\n"
." Example (Windows): -i \\Device\\NPF_{00000000-0000-0000-0000-000000000000}\n"
." Example (Linux): -i eth0\n"
." -s, --savefile=FILE A pcap file to read. Either -i or -r must be specified.\n"
." -p, --promisc Brings the Interface to promiscuous mode. Default behavior.\n"
." -nop, --nopromisc Disables promiscuous mode for the Interface.\n"
." -f, --filter=FILTER Filter to pass to libpcap.\n"
." Example: --filter='port 53' - capture DNS traffic only.\n"
."\n"
." -h, --help Print this help message and exit.\n";
}
sub get_alldevs {
my %devinfo;
my $err = "";
Net::Pcap::pcap_findalldevs(\%devinfo, \$err) or die "Error finding all devices: $err \n";
return %devinfo;
}
my $help = '';
my $list = 0;
my $interface = '';
my $savefile = '';
my $promisc = 1;
my $filter = '';
GetOptions (
'help' => \$help,
'list' => \$list,
'interface=s' => \$interface,
'savefile=s' => \$savefile,
'promisc!' => \$promisc,
'filter=s' => \$filter );
my ($config_fname) = @ARGV;
#---------------------------------------------------------------------------
# List Interfaces
my %devinfo = get_alldevs();
if ($list) {
for my $dev (keys %devinfo) {
print "$dev $devinfo{$dev}\n";
}
exit 0;
}
#---------------------------------------------------------------------------
# Help
if ($help) {
print_usage();
exit 0;
}
if (!defined $config_fname) {
print "--------------------------\n";
print "ERROR: Missing CONFIG_FILE\n";
print "--------------------------\n";
print_usage();
exit 1;
}
if (($interface eq "" and $savefile eq "")) {
print "----------------------------------------\n";
print "ERROR: Either -i or -s must be specified\n";
print "----------------------------------------\n";
print_usage();
exit 1;
}
if ($interface ne "" and $savefile ne "") {
print "----------------------------------------------------\n";
print "ERROR: Both -i or -s cannot be used at the same time\n";
print "----------------------------------------------------\n";
print_usage();
exit 1;
}
#---------------------------------------------------------------------------
# Main
my $config = JsonUtils::fparse($config_fname);
my $err_msg = Net::PcapUtils::loop(
\&PacketProcessor::process, USERDATA => $config,
DEV => $interface,
SAVEFILE => $savefile,
PROMISC => $promisc,
FILTER => $filter,
SNAPLEN => 65536 );
# Unless an error has occured, Net::PcapUtils::loop should not return
print STDERR $err_msg."\n";
exit 1;