Net-Nessus-ScanLite

 view release on metacpan or  search on metacpan

lib/Net/Nessus/ScanLite.pm  view on Meta::CPAN

package Net::Nessus::ScanLite;

use 5.008;
use strict;
use warnings;

use IO::Socket::SSL;
use Config::IniFiles;
use Net::Nessus::Client;
use Net::Nessus::Message;


require Exporter;

our @ISA = qw(Exporter IO::Socket::SSL Net::Nessus::Client Net::Nessus::Message );

our %EXPORT_TAGS = ( 'all' => [ qw( ) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw( );

our $VERSION = '0.01';

sub new
        {
        my $class = shift;
	$class = ref($class) || $class;
        my %args = @_;
        my $self = bless {
                _code           => 0,
                _error          => "",
                ntp_version     => '1.2',
                host            => undef,
                port            => 1241,
                user            => undef,
                password        => undef,
                ssl_version     => 'TLSv1',
                timeout         => 1,
		ssl		=> 1,
		debug		=> 1,
                _cfg            => undef,
                _section        => 'nessus',
		_duration	=> 0,
                _prefsect	=> 'preferences',
		_defsection	=> 'defaults',
                _holes          => [],
                _info           => [],
                preferences     => {},

        },$class;
	# Handle ini config handle or path
	if( $args{Cfg} )
		{
		$self->cfg($args{Cfg});
		if( ref($self->cfg) )
                        { $self->init_cfg; }  # assume handle
                else
                        { $self->init_cfg_path; }
                }
	@{$self}{keys %args} = values %args;
        return($self);
        }

#----------------------------------------------------#
# IniFiles methods
#----------------------------------------------------#
sub init_cfg_path
        {
        my $this = shift;
        my $file = $this->cfg;
        my $cfg = new Config::IniFiles( -file => $file, -default => $this->ini_default );
        return($this->set_error(100,"Config file error for $file ($!)")) unless($cfg);
        $this->cfg($cfg);
        $this->ok("Config file $file is ok.");
        $this->init_cfg;
        }
sub init_cfg
        {
        my $this = shift;
        my $cfg = $this->cfg;
	# Get host/login defaults from nessus section override keys in main class.
        init_section($cfg,$this,$this->section);
	# Get preferences from preferences section put them under preferences. 
        init_section($cfg,$this->{preferences},$this->pref_section);
        }
sub init_section
        {
        my ($cfg,$hash,$section) = @_;
        if( $cfg->SectionExists( $section ) )
                {
                foreach( $cfg->Parameters($section)  )
                        {
                        $hash->{$_} = $cfg->val($section,$_);
                        }
                }
        }
sub pref_section
        {
        my $this = shift;
        my $key = '_prefsect';
        $this->{$key} = shift if( @_ );
        return($this->{$key});
        }
sub ini_default
        {
        my $this = shift;
        my $key = '_defsection';
        $this->{$key} = shift if( @_ );
        return($this->{$key});
        }


#----------------------------------------------------#
# Nessus methods
#----------------------------------------------------#
sub plugin_set
        {
        my $this = shift;
        my $class = 'preferences';
        my $key = 'plugin_set';
        $this->preference($key,shift) if( @_ );
        return($this->preference($key));
        }
sub preferences
        {
        my $this = shift;
        my $class = 'preferences';
        $this->{$class} = shift if( @_ );
        return($this->{$class});
        }

sub preference
        {
        my ($this,$key,$value) = @_;
        my $class = 'preferences';
        return(undef) unless($key);
        $this->{$class}->{$key} = $value if($value);
        return(undef) unless($this->{$class}->{$key});
        return($this->{$class}->{$key});
        }
sub login
        {
        my $this = shift;
        my $i = 0;
        $this->user(shift) if( @_ );
        $this->password(shift) if( @_ );
        $this->__connect;
        return(0) if( $this->code );
        my $ssl = $this->socket;
        $ssl->autoflush();
        $ssl->print($this->ntp_fast);
         my $r = join(' ',$ssl->getline);
        chomp($r);
        if( $r ne $this->ntp_proto )
                {
                $this->set_error(1,"Protocol error $r");
                return(0);
                }
        $ssl->print( $this->user . "\n");
        $ssl->print( $this->password . "\n");
        $ssl->print( "CLIENT <|> NESSUS_VERSION  <|> CLIENT\n");
        $r = join(' ',$ssl->getline);
        chomp($r);
        if( $r =~ /Bad login/gis )
                {
                $this->set_error(1,"Bad login ".  $this->user);
                return(0);
                }
        return(1);
        }
sub setprefs
        {
        my $this = shift;
        my $p = "CLIENT <|> PREFERENCES <|>\n";
        $this->socket->flush;
        my $h = $this->preferences;
        foreach( sort keys %$h )
                { $p .= sprintf("%s <|> %s\n",$_,$h->{$_}); }
        $p .= " <|> CLIENT\n";
        $this->socket->print($p);
        my $msg = Net::Nessus::Message->new('socket' => $this->socket,
                                           'sender' => 'SERVER',
                                           'type' => 'PREFERENCES_ERRORS');
        }

sub __connect
        {
        my $this = shift;
	my $sock = undef;
	if( $this->ssl )
		{
        	# $IO::Socket::SSL::DEBUG = $this->{debug};
        	$sock = IO::Socket::SSL->new(
                        PeerAddr        => $this->host,
                        PeerPort        => $this->port,
                        SSL_version     => $this->ssl_version,
                        Timeout         => $this->timeout
                        )
		or $this->set_error(1,sprintf("Connect to %s failed. (%s)",$this->hostport,&IO::Socket::SSL::errstr()));	
		}
	else
		{
		$sock = IO::Socket::INET->new(
                        PeerAddr        => $this->host,
                        PeerPort        => $this->port,
                        Timeout         => $this->timeout
                        )
                or $this->set_error(2,sprintf("Connect to %s failed. ($!)",$this->hostport));      
                }
        $this->socket($sock) if($sock);
        return($this->code);
        }

sub ShowHOLE
        {
        my ($this,$msg) = @_;
        my $key = '_holes';
        push(@{$this->{$key}},$msg);
        }
sub ShowINFO
        {
        my ($this,$msg) = @_;
        my $key = '_info';
        push(@{$this->{$key}},$msg);
        }

sub total_holes
        {
        my $this = shift;
        my $key = '_holes';
        my $a = $this->{$key};

lib/Net/Nessus/ScanLite.pm  view on Meta::CPAN

        {
        my $this = shift;
        return(@{$this->holes});
        }
sub holes2tmpl
        {
        my $this = shift;
        return(nessus2tmpl($this->holes));
        }
sub infos2tmpl
        {
        my $this = shift;
        return(nessus2tmpl($this->info));
        }

sub nessus2tmpl
        {
        my $list = shift;
        my $array = [];
        foreach( @$list )
                {
                my $msg = $_;
                push(@$array,
                        {
                        port            => $msg->Port,
                        host            => $msg->Host,
                        description     => $msg->Description,
                        service         => $msg->Service(),
                        proto           => $msg->Proto,
                        scanid          => $msg->ScanID,
                        });
                }
        return($array);
        }

sub ntp_fast
        {
        my $this = shift;
        return(sprintf("%s< fast_login >\n",$this->ntp_proto));
        }
sub ntp_version
        {
        my $this = shift;
        my $key = 'ntp_version';
        $this->{$key} = shift if( @_ );
        return($this->{$key});
        }

sub ntp_proto
        {
        my $this = shift;
        return(sprintf("< NTP/%s >",$this->ntp_version));
        }



sub attack
        {
        my ($this,$host) = @_;
	my $start = time;
        $this->setprefs;
        my @hosts = ( $host );
        my $status = $this->Attack(@hosts);
	$this->duration(time - $start);
        }
sub user
        {
        my $this = shift;
        my $key = 'user';
        $this->{$key} = shift if( @_ );
        return($this->{$key});
        }
sub password
        {
        my $this = shift;
        my $key = 'password';
        $this->{$key} = shift if( @_ );
        return($this->{$key});
        }
sub hostport
	{
	my $this = shift;
	return($this->host . ':' . $this->port);
	}
sub host
        {
        my $this = shift;
        my $key = 'host';
        $this->{$key} = shift if( @_ );
        return($this->{$key});
        }
sub port
        {
        my $this = shift;
        my $key = 'port';
        $this->{$key} = shift if( @_ );
        return($this->{$key});
        }

sub ssl
        {
        my $this = shift;
        my $key = 'ssl';
        $this->{$key} = shift if( @_ );
        return($this->{$key});
        }

# Note: Net::Nessus::Message is expecting $this->{socket} to be there
sub socket
        {
        my $this = shift;
        my $key = 'socket';
        $this->{$key} = shift if( @_ );
        return($this->{$key});
        }

sub ssl_version
        {
        my $this = shift;
        my $key = 'ssl_version';
        $this->{$key} = shift if( @_ );



( run in 3.482 seconds using v1.01-cache-2.11-cpan-8f98c5d2c55 )