#!/usr/bin/perl

eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell
#
# $Id: ngen,v 1.7 2000/12/16 09:03:18 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.
#
####################################################################

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


sub usage
{
    print <<DONE;

 ngen [-o|-output <filename-or-:tag>] [-p|-ports <port list>] 
      [-h|-host <specification> ... -h|-host <specification N>]

      <specification> = 
      [!]<host spec>[:<port spec>][:ignored state] 

DONE
    ;
    exit 1;
}

my $pspecs;
my @hspecs = ();
my $output_tag = "%F-baseline";
my $help = 0;

GetOptions(
	   "p|ports=s"    => \$pspecs,
	   "h|host=s@"    => \@hspecs,
	   "o|output=s"   => \$output_tag,
	   "help"         => \$help,
	   );

usage() if $help || ! scalar ( @hspecs );

$output_tag = PortScan::DataStore::prepare_tag($output_tag);

my ($processed_output_tag, $output) = PortScan::DataStore::data_store_for($output_tag);

my $results_set = &make_scanset(\@hspecs, $pspecs);

$results_set->tag($processed_output_tag);

exit $output->put_scanset($results_set);


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

    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, $default_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);

	    foreach my $pspec (@$ports)
	    {
		$host_obj->add_port($pspec);
	    }
	    $host_obj->default_state($default_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;
}


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 [];
}

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;
} 


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;
}


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;
}

sub tests
{
    my $plist = sub { my $f = shift; foreach my $i (@$f) { print $i->nmap_format, "\n"; } };

    my $ports = &expand_port("22222fx");

    &$plist($ports);
    
    my $iplist = sub { my $f = shift; foreach my $i (@$f) { print $i, "\n"; } };
#    my $ips = &expand_ip("1-20");
#    &$iplist($ips);


    my $ips = &expand_host_range("248.2.3.4/20");
    &$iplist($ips);

}




=head1 NAME

ngen - create a baseline set of nmap results from command-line
specifications and/or an nmap results file.

=head1 SYNOPSIS

 ngen [-o|-output <filename-or-:tag>] [-p|-ports <port list>] 
      [-h|-host <specification> ... -h|-host <specification N>]

      <specification> = 
      [!]<host spec>[:<port spec>][:ignored state] 


=head1 DESCRIPTION

The ngen utility creates an nmap-style machine-readable results
file for use as a baseline for ndiff comparision.  

=head1 OPTIONS

=over 4

=item -o <filename-or-:tag>

=item -output <filename-or-:tag>

Specifies the output filename, or optionally a data store
tag, if begins with a colon (:).  See L<"DATA STORES"> below
for more information.


=item -p <port list>

=item -ports <port list>


Specifies the set of all ports scanned, comma-separated.  Ranges may be
specified.  For example:

    -p 80,443,6000-6010,22
    -ports 53

By default, tcp is assumed; udp may be specified by appending the "u" flag.
("t" may be specified but is redundant)

    -p 323u,80,6000-6010t

If the B<-p> option is omitted, ngen will grab the set of ports from
/usr/local/lib/nmap/nmap-services and /etc/services.

The default, "Ignored" specified for a given host is applied to all
ports in this list, except for "interesting" ports, which are
specified below with the B<-h> option.

See the description of port specifications with the B<-h> option
below for more information.


=item -h [!]<host ranges>[:<port ranges>][:<ignored state>]

=item -host  [!]<host ranges>[:<port ranges>][:<ignored state>]


Adds a host or range of hosts.  For example

    -h 192.168.2.2                   # one host
    -h 10.0.2.0-64                   # 65 hosts
    -host 192.168.1.0/26             # 64 hosts
    -host 192.168.*.*                # 65536 hosts

  
The above examples add hosts with all ports closed.  To add
ports to the hosts, append a colon and a port spec.  For example
to add localhost with tcp ports 80 and 53 open, ignored state "closed",
the specification would be

    -host 127.0.0.1:80,53

Flags may be appended to change the protocol or state for a given port, for example:

    -host 127.0.0.1:7uf

which adds echo service, udp port 7 filtered state.  


The full list of port flags are as follows:


    t - tcp port  (default)
    u - udp port

    o - port is in open state (default)
    c - port is in closed state 
    f - port is in filtered state
    x - port is in unfiltered state


The ignored state for the host may be specified by appending a colon and 
the state to the host spec.   Acceptable state labels are "open", "closed",
"filtered", and "unfiltered".

  -h 10.0.2.0:80f:closed

which says all scanned ports default to closed state, except for 80/tcp which
is filtered.

A host spec is treated as a negation if it starts with "!".  If ports are specified
as part of the host spec, those ports are deleted from any hosts previously added
which fall in the host range.  

Host specs are applied in order as they appear on the command line, and their
effects are cumulative.

=back

=head1 DATA STORES

Nrun and its related tools can manipulate results in regular nmap-format
files, in any user-specified location, or they can handle storing and organizing
the data on behalf of the user, through a user-configurable "data store".  

Whenever you precede a results tag with a colon (:), the tag will be treated
as a unique key into a data store, identifying the results set.  

Currently the only supported data store is  nmap format files placed
in a preconfigured directory.  Other types may be added at a later date.  

A legal tag may contain any alphanumeric string, plus dash, underscore, and dot.
%-style substitutions in the ilk of the "date" command are also supported,
allowing a tag to contain date, time, or the local hostname.  See L<"SUBSTITUTIONS">
below for more information.

=head1 SUBSTITUTIONS


%-style substitutions supported in tags as follows:

=over 4

=item %H = hour

=item %M = minute

=item %S = second 

=item %D = day of month

=item %m = month of year (01-12)

=item %Y = year, four digits

=item %j = day of year, three digits

=item %w = day of week (0-6) one digit

=back

Except where noted, the above items are two digits, and local time.  All are zero-padded
as appropriate.

In addtion-

=over 4

=item %F = output of "hostname" on the local machine

=back


=head1 BUGS

It is possible to create invalid nmap result files with ngen if you really
try to.
No support for human-readable hostnames and portnames.

=head1 AUTHOR

James Levine <jdl@vinecorp.com>

=cut













