File: IPAddress.pm

package info (click to toggle)
ndiff 0.05beta4-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, sarge
  • size: 300 kB
  • ctags: 254
  • sloc: perl: 1,806; makefile: 56
file content (348 lines) | stat: -rw-r--r-- 7,607 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/perl
#
# $Id: IPAddress.pm,v 1.3 2001/11/24 02:55:56 levine Exp $
#
#
# Copyright (C) 2000, 2001  James D. Levine (jdl@vinecorp.com)
#
#
#   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., 59 Temple Place - Suite 330, Boston, MA 
#   02111-1307, USA.
#
####################################################################


#
# Some useful static utility methods for IP addresses
#


use strict;
use Getopt::Long;
use PortScan::ScannedHost;
use PortScan::ScanContext;
use PortScan::DataStore;
use PortScan::ScanSet;

package PortScan::IPAddress;

use vars qw(@ISA);

@ISA = qw( Exporter );



# private.
# Evaluates a user-specified octet spec from and IP address to a listref.
# A numeric evaluates to itself; a-b evaluates to the list of all values from
# a to b, inclusive.  * evaluates to 0-255 inclusive.
# Returns a listref.

sub expand_ip
{
    my $s = shift;
    
    $s =~ s/\s//g;

    return [0..255] if $s eq '*';

    ($s =~ /(\d*)-(\d*)/) && return [$1 .. $2];

    ($s =~ /(\d*)/) && return [$1];

    return [];
}

#
# Expands a dotted-quad to all specified hosts.  
# For a given octed in the quad, a numeric evaluates to itself; a-b evaluates
# to the list of all values from a to b, inclusive.  * evaluates to 0-255
# inclusive.  CIDR notation is also supported.  (TODO: further test CIDR expansion)
# Returns a listref.


sub expand_host_range
{
    my $cidr = shift;
    $cidr =~ s/\s//g;

    my ($ip_string, $netbits) = split /\//, $cidr;
    my ($a, $b, $c, $d) = split /\./, $ip_string;


    return [] if ( !length($a) || !length($b) || !length($c) || !length($d) );

    $netbits = 32 if !length($netbits);

    my $net_whole_octets = int($netbits / 8);
    my $remainder = $netbits % 8;
    my $host_whole_octets = int ((32 - $netbits) / 8);

    my @final_octets = ();

    foreach my $octet ($a, $b, $c, $d)
    {
	if ($net_whole_octets > 0) 
	{
	    push @final_octets, $octet;
	    -- $net_whole_octets;
	}
	elsif ($remainder)
	{			
	    my $f =  ((255 << $remainder)   & $octet);
	    my $l = $f +((1 << (8 - $remainder))-1) ;
	    push @final_octets,  "${f}-${l}" ;

	    $remainder = 0;
	}
	elsif ($host_whole_octets > 0)
	{
	    push @final_octets, "1-255";
	    -- $host_whole_octets;
	}
    }

    my ($af, $bf, $cf, $df) = @final_octets;

    my $range = [];
    
    my $al = expand_ip($af);
    my $bl = expand_ip($bf);
    my $cl = expand_ip($cf);
    my $dl = expand_ip($df);

    foreach my $ah (@$al) {
	foreach my $bh (@$bl) {
	    foreach my $ch (@$cl) {
		foreach my $dh (@$dl) {
		    push @$range, "$ah.$bh.$ch.$dh";
		}
	    }
	}
    }

    $range;
} 



#
# Evaluates a text port specification, returning a listref of
# PortSpec instances.  A port may be specified as a number N
# or a range N-M, which evaluates to all between N and M
# inclusive.  The protocol and state are specified with modifiers:
#  o = open;  c = closed;  x = unfiltered; f = filtered;  u = udp; t = tcp
#
#  default: open, tcp
#
# Sample:   
#   23/t         tcp port 23, open
#   1-100/uf     udp ports 1-100, filtered

sub expand_port
{
    my ($string) = shift;

    $string =~ s/\s//g;

    # ports . [o | f | u | t | x] open, filtered, unfiltered, tcp, udp
#    my ($ports, $flags) = split /\./,$string; 

#    $string =~ /(\d*)(\w*)/;
    $string =~ /([\W-]*)(\w*)/;
    my ($ports, $flags) = ($1, $2);

    my @range = ();
    my $named_port = "";
#print "expanding $string \n"
#    ;
    ($string =~ /(\d+)-(\d+)(\w*)/) && (@range = ($1 .. $2), $flags = $3,  goto NEXT);

    ($string =~ /(\d+)(\w*)/) && (@range = ($1), $flags = $2,  goto NEXT);

#    ($string =~ /([\w\-]+)\.(\w+)/ ) && ($named_port = $1, $flags = $2, goto NEXT);
    ($string =~ /\./ ) && ( ($named_port, $flags) = split (/\./, $string) , goto NEXT);

    ($string =~ /([\w\-]+)$/ )  && ($named_port = $1, $flags = "", goto NEXT);


NEXT:
#print "flags $flags named_port $named_port\n"
#    ;
    my ($state, $proto) = ("open","tcp");
    
    ($flags =~ /o/) && ($state = "open");
    ($flags =~ /c/) && ($state = "closed");
    ($flags =~ /x/) && ($state = "unfiltered");
    ($flags =~ /f/) && ($state = "filtered");
    ($flags =~ /u/) && ($proto = "udp");
    ($flags =~ /t/) && ($proto = "tcp");

    @range = PortScan::PortSpec::known_port_by_name($named_port, $proto) if $named_port ne "";
#print "range @range\n"
#    ;
    my $result = [];

    return [] if $#range < 0;

    foreach my $port (@range)
    {
	push @$result, new PortScan::PortSpec($port, $state, $proto, "", "", "", "");
    }
    $result;
}


#
# Generates a listref of PortSpecs from a comma-separated string
# of text port specifications, as detailed for expand_port() above.
#

sub expand_port_range
{
    my $port_string = shift;	# comma-separated, port numbers

    my @list = split /,/,$port_string; 

    my $ports = [];

    foreach my $portspec (@list)
    {
	my $portlist = &expand_port($portspec);
	push @$ports, @$portlist;
    }

    $ports;
}



#
# Given a listref of text hostspecs and portspecs, create a full-blown
# ScanSet.  Each ScannedHost in the set gets the same set of PortSpecs,
# and has the default_ignored_state set as specified.

sub make_scanset
{
    my ($hspecs, $pspecs, $default_ignored_state) = @_;

    my $ports_listref;

    if (length $pspecs)
    { 
	$ports_listref =  expand_port_range($pspecs);
    }
    else
    {
	$ports_listref = PortScan::PortSpec::expand_all_known_ports();
    }
	
    my $scanned_ports = {};

    foreach my $p (@$ports_listref)
    {
	$scanned_ports->{$p->key_for()} = $p;
    }

    my $results_set = new PortScan::ScanSet("", undef, undef, $scanned_ports);
    foreach my $spec (@$hspecs)
    {
	my $negate = ($spec =~ /\!/) ? 1 : 0;

	$spec =~ s/\!//g;

	my ($host_spec, $port_spec, $ignored_state) = split /:/,$spec;

	my $host_addrs = &expand_host_range($host_spec);
	my $ports = &expand_port_range($port_spec);

	my @hosts = ();

	foreach my $host_addr (@$host_addrs)
	{
	    my $host_obj = new PortScan::ScannedHost;
	    $host_obj->scan_set($results_set);
	    $host_obj->addr($host_addr);
	    $host_obj->default_state( $default_ignored_state );

	    foreach my $pspec (@$ports)
	    {
		$host_obj->add_port($pspec);
	    }

	    $host_obj->default_state( $ignored_state ) if length $ignored_state;

	    push @hosts, $host_obj;
	}

	if ($negate)
	{
	    foreach my $host (@hosts)
	    {
		my $result_host = $results_set->get_host($host->addr());

		if (defined $result_host)
		{
		    my $port_specs = $host->port_specs();


		    if (keys %$port_specs)
		    { # if host has ports, try to delete each port from the host in results
			foreach my $pkey (keys %$port_specs)
			{
			    $result_host->remove_port($pkey);
			}
		    }
		    else
		    { # if host has no ports, try to delete the host from results
			$results_set->remove_host($host->addr());
		    }
		}
	    }

	}
	else
	{
	    foreach my $host (@hosts)
	    {
		$results_set->add_host($host);
	    }
	}

    }

    $results_set;
}


1;