-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDashButton.pl
More file actions
116 lines (98 loc) · 3.35 KB
/
DashButton.pl
File metadata and controls
116 lines (98 loc) · 3.35 KB
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
use Net::Pcap;
use NetPacket::Ethernet;
use NetPacket::IP;
use NetPacket::TCP;
use Socket;
use LWP::Simple;
use strict;
use File::Basename;
my $err;
my $lastPacketTime = time;
my $currentTime = time;
my %devices;
#load devices from ini
my ( $scriptName, $scriptPath, $scriptSuffix ) = fileparse( $0, qr{\.[^.]*$} );
my $propFile = "$scriptPath$scriptName.ini";
open INI, "<$propFile";
while (my $row = <INI>) {
chomp $row;
my @devSplit = split(/\t/, $row);
$devices{$devSplit[0]} = $devSplit[1];
}
# Use network device passed in program arguments or if no
# argument is passed, determine an appropriate network
# device for packet sniffing using the
# Net::Pcap::lookupdev method
my $dev = $ARGV[0];
unless (defined $dev) {
$dev = Net::Pcap::lookupdev(\$err);
if (defined $err) {
die 'Unable to determine network device for monitoring - ', $err;
}
}
# Look up network address information about network
# device using Net::Pcap::lookupnet - This also acts as a
# check on bogus network device arguments that may be
# passed to the program as an argument
my ($address, $netmask);
if (Net::Pcap::lookupnet($dev, \$address, \$netmask, \$err)) {
die 'Unable to look up device information for ', $dev, ' - ', $err;
}
my $net = inet_ntoa( pack 'N', $address );
my $mask = inet_ntoa( pack 'N', $netmask);
#print "$net\n";
#print "$mask\n";
# Create packet capture object on device
my $object;
$object = Net::Pcap::open_live($dev, 1500, 0, 0, \$err);
unless (defined $object) {
die 'Unable to create packet capture on device ', $dev, ' - ', $err;
}
# Compile and set packet filter for packet capture
# object - For the capture of TCP packets with the SYN
# header flag set directed at the external interface of
# the local host, the packet filter of '(dst IP) && (tcp
# [13] & 2 != 0)' is used where IP is the IP address of
# the external interface of the machine. For
# illustrative purposes, the IP address of 127.0.0.1 is
# used in this example.
my $filter;
Net::Pcap::compile(
$object,
\$filter,
'(ip src 0.0.0.0) && (src port 68)',
0,
$netmask
) && die 'Unable to compile packet capture filter';
Net::Pcap::setfilter($object, $filter) &&
die 'Unable to set packet capture filter';
# Set callback function and initiate packet capture loop
Net::Pcap::loop($object, -1, \&syn_packets, '') ||
die 'Unable to perform packet capture';
Net::Pcap::close($object);
sub syn_packets {
my ($user_data, $header, $packet) = @_;
# Strip ethernet encapsulation of captured packet
my $ether_data = NetPacket::Ethernet::strip($packet);
# Decode contents of TCP/IP packet contained within
# captured ethernet packet
my $ip = NetPacket::IP->decode($ether_data);
my $frame = NetPacket::Ethernet->decode($packet);
my $tcp = NetPacket::TCP->decode($ip->{'data'});
# Print all out where its coming from and where its
# going to!
#print
# $ip->{'src_ip'}, ":", $frame ->{'src_mac'}, ":", $tcp->{'src_port'}, " -> ",
# $ip->{'dest_ip'}, ":", $frame ->{'dest_mac'}, ":", $tcp->{'dest_port'}, "\n";
my $srcMac = $frame->{'src_mac'};
dashPacket($srcMac);
}
sub dashPacket{
my $currMac = shift;
$currentTime = time;
if($currentTime-$lastPacketTime>1 && exists $devices{$currMac}){
my $url = $devices{$currMac};
get $url;
}
$lastPacketTime = time;
}