-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMagni.pm
executable file
·90 lines (78 loc) · 2.32 KB
/
Magni.pm
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
# MAGNI - Simple network sniffer and scanner
# Copyright (C) 2010 Ben Rosenberg
#
# 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 2 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
package Magni;
use strict;
use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw( warn_msg );
our @EXPORT_OK = qw( $internals );
use Net::Pcap;
my $last_ctrl_c = 0;
my $ctrl_c_ct = 0;
# Non-user definable options
our $internals = {
"capture_iface_name" => undef, # Name of pcap capture interface
"capture_iface" => undef, # pcap capture interface pointer
"pcap_output_name" => undef, # Name of output pcap dump file
"pcap_output" => undef, # pcap dump file pointer
"ifaces" => {},
"interrupt" => 0,
};
sub clear_ctrl_c {
$internals->{"interrupt"} = 0;
}
sub set_ctrl_c {
if ( $last_ctrl_c == time ) {
if ( $ctrl_c_ct++ > 2 ) {
exit;
}
} else {
$last_ctrl_c = time;
$ctrl_c_ct = 0;
$internals->{"interrupt"} = 1;
}
}
# Change a non-delimited MAC into a delimited MAC
# ex: 01234567890a -> 01:23:45:67:89:0a
# usage: &format_mac( $mac );
sub format_mac {
my ($mac) = @_;
$mac =~ s/(?<![0-9a-f]{2}:)([0-9a-f]{2})/$1:/g;
$mac =~ s/:$//;
return $mac;
}
sub sig_exit {
my ($sig) = @_;
&warn_msg("Caught SIG$SIG! Exiting...");
exit;
}
# Return the unique values of an array
# usage: @unique_values = &uniq( @non_unique_values );
sub uniq {
my (@ary) = @_;
my %u = map {$_=>1} @ary;
return keys %u;
}
sub warn_msg {
my (@msg) = @_;
print STDERR "error: @msg\n";
}
END {
if ( defined $internals->{"capture_iface_name"} ) {
Net::Pcap::pcap_close( $internals->{"capture_iface"} );
}
}