-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcpscam.pl
More file actions
289 lines (218 loc) · 6.82 KB
/
cpscam.pl
File metadata and controls
289 lines (218 loc) · 6.82 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
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
#!/usr/bin/perl
use strict 'refs';
use strict 'subs';
use strict 'vars';
my (
$pcap_err, $pcap_descr,
$pcap_timeout, $pcap_promisc,
$pcap_snaplen, $pcap_int,
$starttime, $nowtime,
$arg_ip, $arg_mask,
$l2offset, $maxinactive,
$lmask, $llocalip,
$lobsrvdip, $progname,
$logoffurl, $listentime,
$elapsed, $host,
$ip_obj, $eth_obj,
%packetcounth, %inactivityh,
);
sub checkmodules; checkmodules;
use Socket;
use Net::Pcap;
use NetPacket::IP;
use NetPacket::Ethernet qw(:strip);
#$SIG{INT} = \&sigint_handler;
$arg_ip = $ARGV[1];
$arg_mask = $ARGV[2];
if ($ARGV[4]) {
$logoffurl = $ARGV[4];
} else {
$logoffurl = "logoff.hotspot.t-mobile.com";
}
# Constants
$pcap_int = $ARGV[0];
$pcap_timeout = 1000; # in ms
$pcap_promisc = 1;
$pcap_snaplen = 1500;
$l2offset = 14;
$maxinactive = 120;
$progname = $0;
$progname =~ s,.*/,,; # only basename left in progname
# Set the duration between reporting host status
if ($ARGV[3]) {
$listentime = $ARGV[3];
} else {
$listentime = 15;
}
# main
if (@ARGV < 3) {
usage('');
}
if ($arg_mask =~ '\.') {
# Mask was passed as dotted-quad
$lmask = dottedquad2long($arg_mask);
} else {
# assume mask is passwd as bit length integer
$lmask = makemask($arg_mask);
}
$llocalip = dottedquad2long($arg_ip);
# Open capture interface and start looping on each packet captured
print "Capturing traffic .. \n";
if ($pcap_descr = Net::Pcap::open_live
($pcap_int,$pcap_snaplen,$pcap_promisc,$pcap_timeout,\$pcap_err)) {
$starttime = time();
# Loop indefinitely, BPF filter for only IP packets
Net::Pcap::loop($pcap_descr, -1, \&process_pkt, 'ip');
} else {
print "Could not open device \'$pcap_int\' for capture: $pcap_err\n";
exit(1);
}
# --- begin subroutines ---
sub process_pkt {
my ($user, $hdr, $pkt) = @_;
$ip_obj = NetPacket::IP->decode(eth_strip($pkt));
$eth_obj = NetPacket::Ethernet->decode($pkt);
$lobsrvdip = dottedquad2long($ip_obj->{src_ip});
# We only want local address, return if a remote source
return unless (netcomp($llocalip, $lmask, $lobsrvdip));
# Increment packet count hash for this address, reset inactivity value
$packetcounth{$ip_obj->{src_ip}}++;
$inactivityh{$ip_obj->{src_ip}} = 0;
if ($ip_obj->{data} =~ /$logoffurl/) {
# This source has logged off, so remove it from the hashes
delete($packetcounth{$ip_obj->{src_ip}});
delete($inactivityh{$ip_obj->{src_ip}});
print "Host at $ip_obj->{src_ip} logged out.\n";
return 0;
}
$elapsed = time() - $starttime;
if ($elapsed > $listentime) {
foreach $host (keys (%packetcounth)) {
if ($packetcounth{$host} == 0) {
$inactivityh{$host} += $elapsed;
if ($inactivityh{$host} > $maxinactive) {
print "The host at " . $host . "\/" .
printmac($eth_obj->{src_mac}) .
" has been inactive for $maxinactive seconds...\n";
Net::Pcap::close($pcap_descr);
exit 0;
}
}
# Reset this parameter to 0 to give hosts another change at
# inactivity
$packetcounth{$host} = 0;
}
$starttime += $elapsed;
&printinactivity;
}
}
sub printmac {
# hack, hack, hack
my ($uglymac) = @_;
my $prettymac = substr($uglymac, 0, 2) . ":" . substr($uglymac, 2, 2) .
":" . substr($uglymac, 4, 2) . ":" . substr($uglymac, 6, 2) .
":" . substr($uglymac, 8, 2) . ":" . substr($uglymac, 10, 2);
return $prettymac;
}
sub printinactivity {
print localtime() . "\n";
foreach my $host (keys (%inactivityh)) {
if ($inactivityh{$host} > 0) {
print "Host $host has been inactive for " . $inactivityh{$host} .
" seconds.\n";
}
}
}
sub sigint_handler {
# Close pcap gracefully after CTRL/C
if ($pcap_descr) {
print "\n";
}
print "Caught CTRL/C, exiting.\n";
exit(0);
}
sub checkmodules {
eval {
require Socket;
};
if ($@) {
print <<EOT;
This tool requires the Socket Perl module. This module is typically bundled
with Perl distributions, something may be amiss with your Perl installation.
Quitting.
EOT
exit(-1);
}
eval {
require Net::Pcap;
};
if ($@) {
print <<EOT;
The Net::Pcap module is required for this tool. Download this tool from the
CPAN website (search.cpan.org), or install via
"perl -MCPAN -e 'install Net::Pcap'". You will need libpcap for this module.
Windows users using ActiveState Perl can install this module with the ppm tool.
As of 2/26/2004 this module isn't in the standard PPM repository, but you can
install it from JL Morel's website with
"ppm install http://www.bribes.org/perl/ppm/Net-Pcap.ppd". You must also
install the WinPcap software from http://winpcap.polito.it/.
Quitting.
EOT
exit(-1);
}
eval {
require NetPacket::IP;
};
if ($@) {
print <<EOT;
The NetPacket::IP module is required for this tool. Download this tool from
the CPAN website (search.cpan.org), or install via
"perl -MCPAN -e 'install NetPacket::IP'". You will need libpcap for this
module.
Windows users using ActiveState Perl can install this module with the ppm tool:
"ppm install NetPacket-IP". You must also
install the WinPcap software from http://winpcap.polito.it/.
Quitting.
EOT
exit(-1);
}
}
sub usage {
my($msg) = @_;
select(STDERR);
print STDERR "$progname: $msg\n" unless ($msg eq "");
print "Usage: $progname ethX localip mask [timerduration] [logoff url]\n";
print "\n";
print "You will need to edit this tool to specify the logoff URL (at least partial path\nor hostname) to identify logged-out clients (\$logoffurl).\n";
exit(1);
}
# Accepts local address, netmask and second address as long integers
# Returns true when two addresses are on the same local network
sub netcomp {
my ($firstip, $mask, $secondip) = @_;
my $netnum = $firstip & $mask;
if (($secondip & $mask) == $netnum) {
return 1;
} else {
return 0;
}
}
# converts decimal dotted quad string to network ordered long
sub dottedquad2long {
return unpack('N', inet_aton(shift));
}
# converts network ordered long to dotted quad string
sub long2dottedquad {
return inet_ntoa(pack('N', shift));
}
# returns netmask as network ordered long
sub makemask {
my $mask_length = shift;
my $binary = '1' x $mask_length . '0' x (32 - $mask_length);
return bin2long($binary);
}
# converts 32bit binary to network ordered long
sub bin2long {
return unpack('N', pack('B32', shift));
}
# --- end subroutines ---