Net-Nessus

 view release on metacpan or  search on metacpan

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

=item password

User name and password to use for logging into the Nessus server. There
are no defaults, you must set these attributes.

=item ntp_proto

An optional version of the NTP protocol to run. Defaults to the highest
available number, 1.1 as of this writing.

=back

Example: Log into the Nessus server running at machine "gate.company.com",
port 2367 as user "joe" with password "what_password" and NTP version
1.0:

  require Net::Nessus::Client;
  my $client = Net::Nessus::Client->new('host' => 'gate.company.com',
                                        'port' => 2367,
                                        'user' => 'joe',
                                        'password' => 'what_password',
                                        'ntp_proto' => '1.0');

=cut


sub new {
    my $class = shift;
    my %attr = @_;

    my $host = $attr{'host'} or Carp::croak("Missing Nessus host");
    my $port = $attr{'port'} || 1241;
    my $proto = $attr{'ntp_proto'} || '1.1';
    my $user = $attr{'user'} or Carp::croak("Missing user name");
    my $pass = $attr{'password'} or Carp::croak("Missing password");
    my $sock = $attr{'socket'} =
	Net::Telnet->new('Binmode' => 1,
			 'Host' => $host,
			 'Port' => $port,
			 'Dump_Log' => $attr{'Dump_Log'},
			 'Input_Log' => $attr{'Input_Log'},
			 'Output_Log' => $attr{'Output_Log'},
			 'Telnetmode' => 0,
			 'Timeout' => ($attr{'Timeout'} || 300))
	    or Carp::croak("Cannot connect: $!");

    $sock->print("< NTP/$proto >")
	or Carp::croak("Error while writing proto: $!");
    my $line = $sock->getline();
    die "Error while requesting NTP proto $proto: $!" unless defined($line);
    die "Protocol $proto not supported: $line"
	unless ($line =~ /\<\s+NTP\/$proto\s+\>/);
    $sock->waitfor('Match' => '/[Uu]ser\s+:\s*/');
    $sock->print($user);
    $sock->waitfor('Match' => '/[Pp]assword\s+:\s*/');
    $sock->print($pass);
    my $self = \%attr;
    bless($self, (ref($class) or $class));
    $self->{'plugins'} = $self->GetMsg('PLUGIN_LIST');
    if ($proto >= 1.1) {
	$self->{'prefs'} = $self->GetMsg('PREFERENCES');
	$self->{'rules'} = $self->GetMsg('RULES');
    }
    $self;
}


=pod

=head2 Reading the plugin list

  my $plugins = $self->Plugins();
  my $prefs = $self->Prefs();
  my $rules = $self->Rules();

(Instance Methods) Read the plugin list, the current preferences or the
list of rules. The plugin list is an array of hash refs, each hash ref
with attributes I<id>, I<category> and so on. The prefs are a single
hash ref of name/value pairs and the rules are an array ref of strings.

When talking to an NTP/1.0 server, the Prefs() and Rules() methods
will return undef.

Examples:

  my $plugins = $self->Plugins();
  print("The first plugins ID is ", $plugins->[0]->{'id'}, "\n");
  print("The second plugins description is ",
        $plugins->[1]->{'description'}, "\n");
  my $prefs = $self->Prefs();
  print "\nThe current prefs are:\n";
  while (my($var, $val) = each %$prefs) {
    print "  $var = $val\n";
  }
  my $rules = $self->Rules();
  print "\nThe current rules are:\n";
  foreach my $rule (@$rules) {
    print "  $rule\n";
  }

=cut

sub Plugins { shift->{'plugins'}->Plugins() }
sub Prefs { my $prefs = shift->{'prefs'}; $prefs ? $prefs->Prefs() : undef }
sub Rules { my $rules = shift->{'rules'}; $rules ? $rules->Rules() : undef }


=pod

=head2 Sending a message to the server

  $client->Print($msg);

(Instance Method) The print method is used for sending a previously
created message to the server. Depending on the message type you
should continue calling the I<GetMsg> method.

Example:

  my $rules = ['n:*.fr;', 'y:*.my.de;'];
  my $msg = Net::Nessus::Message::Rules($rules);
  $client->print($msg);

=cut

sub Print {
    my $self = shift; my $msg = shift;
    $msg->print('CLIENT', $self->{'socket'})
}

=pod

=head2 Reading a message from the server

  $msg = $client->GetMsg($type, $timeout);

(Instance method) The I<GetMsg> method is reading a message from the server.
If the argument $type is undef, then any message is accepted, otherwise
any message other message type is treated as an error. Valid message
types are PLUGIN_LIST, PREFERENCES and so on.

If the argument $timeout is given, then an error will be triggered, if
the server is not sending any message for that much seconds. If no
timeout is given, then the default timeout will be used.

=cut

sub GetMsg {
    my $self = shift; my $type = shift; my $timeout = shift;
    Net::Nessus::Message->new('sender' => 'SERVER',
			      'type' => $type,
			      'socket' => $self->{'socket'},
			      'timeout' => $timeout);
}


=head2 Launching an attack

  my $messages = $client->Attack(@hosts);
  $client->ShowSTATUS($msg);
  $client->ShowPORT($msg);
  $client->ShowHOLE($msg);
  $client->ShowINFO($msg);
  $client->ShowPLUGINS_ORDER($msg);



( run in 0.559 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )