File: PortSpec.pm

package info (click to toggle)
ndiff 0.05beta4-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 300 kB
  • ctags: 254
  • sloc: perl: 1,806; makefile: 56
file content (292 lines) | stat: -rw-r--r-- 5,986 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
#!/usr/bin/perl
#
# $Id: PortSpec.pm,v 1.13 2001/11/24 22:28:15 levine Exp $
#
# Copyright (C) 2000  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.
#
####################################################################

#
# Encapsulates the properties of a "port", in the general
# sense for TCP and UDP
#



use strict;

package PortScan::PortSpec;



# generate a string for the port in the normal 
# nmap human-readable format

sub nmap_default_human_format
{
    my $me = shift;

    return sprintf( "%-8d%-12s%-10s%-10s",
		    $me->number(), $me->state(), $me->proto(), $me->service() );
}

# TODO: document u1, u2, u3 from nmap source 
sub new
{
    my $type = shift;
    my( $number, $state, $proto, $u1, $service, $u2, $u3 ) = @_;

    my $self =
    {   
	number => '',	
	state => '',     # 'open' | 'closed' | 'filtered' | 'unfiltered' | 'unknown'
	proto => '',     # 'tcp' | 'udp'
	u1 => '',
	service => '',   # well-known service name from /etc/services
	u2 => '',
	u3 => '',
    };

    bless $self, $type;

    $self->number( $number );
    $self->state( $state );
    $self->proto( $proto );
    $self->service( $service );

    return $self;
}

# generate a canonical key from a ( port, proto ) pair
sub make_key
{
    my( $port, $proto ) = @_;
    return( "$port/$proto" );
}


# reverse-map a canonical key into ( $port, $proto )
sub decode_key
{				# returns ($port, $proto)
    my $key = shift;

    split /\//, $key;
}

# generate the canonical key for this instance 
# for use in hashes
# public.
sub key_for
{
    my( $self ) = shift; return make_key( $self->number(), $self->proto() );
}



# make an exact copy, it's deep since all members are scalars
sub clone
{
    my $self = shift;
    my $n = new PortScan::PortSpec;

    $n->number ( $self->number() );
    $n->state  ( $self->state() );
    $n->proto  ( $self->proto() );
    $n->service( $self->service() );
    $n->u1     ( $self->u1() );
    $n->u2     ( $self->u2() );
    $n->u3     ( $self->u3() );

    $n;
}


# generic field-accessor method
sub set_or_get
{
    my $field = shift;
    my $self = shift;
    my $val = shift;

    $self->{ $field } = $val if defined $val;

    $self->{ $field };
}

# specificly exposed field accessor methods
sub number   { set_or_get( 'number',   @_ ); }
sub state    { set_or_get( 'state',    @_ ); }
sub proto    { set_or_get( 'proto',    @_ ); }
sub service  { set_or_get( 'service',  @_ ); }
sub u1  { set_or_get( 'u1',  @_ ); }
sub u2  { set_or_get( 'u2',  @_ ); }
sub u3  { set_or_get( 'u3',  @_ ); }


# map an nmap state name to a single character mnemonic
# TODO: U and X appear to be reversed
sub state_sm
{
    my $self = shift;
    my $s = $self->state();

    ($s eq "open") && return "O";
    ($s eq "closed") && return "C";
    ($s eq "filtered") && return "F";
    ($s eq "unfiltered") && return "U";	
    return "X";			# unknown
}



# maps ( service-name, proto ) to a canonical key
sub known_port_by_name
  {
    my ($name, $proto) = @_;

    $PortScan::PortSpec::by_name->{ make_key( $name, $proto ) };
  }

# maps ( port-number, proto ) to a canonical key -- is this necessary?
sub known_port_by_number
  {
    my ($number, $proto) = @_;
    $PortScan::PortSpec::by_number->{ make_key( $number, $proto ) };
  }

my $by_name = {};		# global well-known-ports by name
my $by_number = {};		# global well-known ports by number


# returns the list of all well-known udp services
sub known_udp_ports
{
    my @udp = grep /udp/, keys %$PortScan::PortSpec::by_number;
    map { s/\/udp//} @udp;

    @udp;
}

# returns the list of all well-known tcp services
sub known_tcp_ports
{
    my @tcp = grep /tcp/, keys %$PortScan::PortSpec::by_number;
    map { s/\/tcp//} @tcp;

    @tcp;
}


# returns a pair listref [ @tcp, @udp ] of all known services
sub expand_all_known_ports
{
    my @t;
    map { push @t, new PortScan::PortSpec( $_, "open", "tcp", "", "", "", "") } known_tcp_ports();

    my @u;
    map { push @u, new PortScan::PortSpec( $_, "open", "udp", "", "", "", "") } known_udp_ports();

    return [ @t, @u ];
}

# reads the well-known ports from a file in /etc/services format,
# inserts into by_name and by_number globals

sub ports_from_file
  {
    my ($f) = @_;

    open IN, "<$f" || return 0;

    my @lines = <IN>;
    close IN;

    chomp @lines;
    
  L:
    foreach my $l (@lines)
      {
	next L if !length($l);

	$l =~ /^\s*(.)/;
        next L if ($1 eq '#');

	$l =~ /^(\S*)\s*(\S*)/;
	
	my $label = $1;
	
	my($num, $proto) = split /\//, $2;

	next L if $num < 1;
	
	$PortScan::PortSpec::by_name->{make_key($label, $proto)} = $num;
	$PortScan::PortSpec::by_number->{make_key($num, $proto)} = $label;
      }
  }



# sorts a list of PortSpecs by proto and number
sub sorted_list
{
    return sort compare @_;
}


# sort comparator
sub compare
{
    return $a->number() <=> $b->number() if $a->proto() eq $b->proto();

    return -1 if $a->proto() eq "tcp";
    return 1;

}

# sort comparator
sub compare_pair
{
    return compare( $_[0]->[0], $_[1]->[0] );
}

# set up globals, especially well-known ports
BEGIN 
  {
    ports_from_file( "/etc/services" );
    ports_from_file( "/usr/local/share/nmap/nmap-services" );
    ports_from_file( $ENV{"NDIFF_SERVICES_FILE"} );
  }



1;