Skip to content

Instantly share code, notes, and snippets.

@rantoniuk
Last active December 13, 2015 22:29
Show Gist options
  • Save rantoniuk/4985271 to your computer and use it in GitHub Desktop.
Save rantoniuk/4985271 to your computer and use it in GitHub Desktop.
This script is meant to run a NESSUS scan on selected target networks, defined in targets.txt, and send an email report when the scan is finished. This script is based on the pieces I have found on the Internet, customised to my needs and for CentOS 6. For CentOS6, the XMLRPC.pm library has to be patched to work properly with the attached diff. …
#!/usr/bin/perl
# Script to run Nessus scan on targets.txt and send HTML report
# tested under CentOS 6
# create a user in Nessus GUI and create one policy with credentilas you want.
# place this script and targets.txt in /opt/nessus-scripts
# Use "perl -MCPAN -e shell" and then "install Net::Nessus::XMLRPC" etc to install
# perl modules needed (in "use" directive below)
# You will also need xsltproc - installed with "yum libxslt"
use Net::SSL;
use Net::Nessus::XMLRPC;
use Net::SMTP;
use MIME::Lite;
use strict;
use warnings;
my $my_file = '/opt/nessus-scripts/report.html';
my $your_file = 'report.html';
my $reportfile ='/opt/nessus-scripts/report.xml';
my $targetsfile ='/opt/nessus-scripts/targets.txt';
### Adjust sender, recipient and your SMTP mailhost
my $from_address = 'nessus@your.host.com';
my $to_address = 'recipient@domain.com';
my $mail_host = 'localhost';
### Adjust subject and body message
my $subject = 'Nessus Report';
my $message_body = "Report attached";
#Nessus Web INterface Login and Pass
my $username = "USERNAME";
my $password = "PASSWORD";
my $hostname = 'https://localhost:8834/';
#--------------------------------------------------------------------------------------------------
my $n = Net::Nessus::XMLRPC->new ($hostname,$username,$password);
die "Cannot login to: ".$n->nurl."\n" unless ($n->logged_in);
print "Logged in\n";
my $polid=$n->policy_get_first;
print "Using policy ID: $polid ";
my $polname=$n->policy_get_name($polid);
my $targets = "";
my $scanid=$n->scan_new_file($polid,"AutoScan",$targets,$targetsfile);
while (not $n->scan_finished($scanid)) {
if (!$n->logged_in){
$n->login($username,$password);
}
print "$scanid: ".$n->scan_status($scanid)."\n";
sleep 30;
}
print "$scanid: ".$n->scan_status($scanid)."\n";
my $reportcont=$n->report_file_download($scanid);
open (FILE,">$reportfile") or die "Cannot open file $reportfile: $!";
print FILE $reportcont;
close (FILE);
#--------------------------------------------------------------------------------------------------
#convert
system ('/usr/bin/xsltproc -o /opt/nessus-scripts/report.html /opt/nessus/var/nessus/www/html.xsl /opt/nessus-scripts/report.xml');
#--------------------------------------------------------------------------------------------------
# Send E-Mail
my $msg = MIME::Lite->new (
From => $from_address,
To => $to_address,
Subject => $subject,
Type =>'multipart/mixed'
) or die "Error creating multipart container: $!\n";
### Add the text message part
$msg->attach (
Type => 'TEXT',
Data => $message_body
) or die "Error adding the text message part: $!\n";
### Add the file
$msg->attach (
Type => 'text/html',
Path => $my_file,
Filename => $your_file,
Disposition => 'attachment'
) or die "Error adding $my_file: $!\n";
### Send the Message
MIME::Lite->send('smtp', $mail_host, Timeout=>60);
$msg->send;
139d138
< $self->{_token} = undef;
353,363c352,355
<
< my $scanList = $xmls->{'contents'}->[0]->{'scans'}->[0]->{'scanList'};
<
< if(ref($scanList->[0])) {
<
< if(exists($scanList->[0]->{'scan'})) {
< foreach my $scan (@{$scanList->[0]->{'scan'}}) {
< push @list, $scan->{'uuid'}->[0];
< } # foreach
< } # if
< }
---
> if ($xmls->{'contents'}->[0]->{'scans'}->[0]->{'scanList'}->[0]->{'scan'}) {
> foreach my $scan (@{$xmls->{'contents'}->[0]->{'scans'}->[0]->{'scanList'}->[0]->{'scan'}}) {
> push @list, $scan->{'uuid'}->[0];
> } # foreach
365c357
<
---
> } # if
380,387c372,377
< my $scanList = $xmls->{'contents'}->[0]->{'scans'}->[0]->{'scanList'};
<
< if(ref($scanList->[0])) {
< foreach my $scan (@{$xmls->{'contents'}->[0]->{'scans'}->[0]->{'scanList'}->[0]->{'scan'}}) {
< if ($scan->{'uuid'}->[0] eq $uuid) {
< return $scan->{'readableName'}->[0];
< }
< } # foreach
---
> if ($xmls->{'contents'}->[0]->{'scans'}->[0]->{'scanList'}->[0]->{'scan'}) {
> foreach my $scan (@{$xmls->{'contents'}->[0]->{'scans'}->[0]->{'scanList'}->[0]->{'scan'}}) {
> if ($scan->{'uuid'}->[0] eq $uuid) {
> return $scan->{'readableName'}->[0];
> }
> } # foreach
404,411c394,400
<
< if (ref($xmls)) {
< foreach my $report (@{$xmls->{'contents'}->[0]->{'reports'}->[0]->{'report'}}) {
< if ($report->{'name'}->[0] eq $uuid) {
< return $report->{'status'}->[0];
< }
< } # foreach
< } # if
---
> if ($xmls->{'contents'}->[0]->{'reports'}->[0]->{'report'}) {
> foreach my $report (@{$xmls->{'contents'}->[0]->{'reports'}->[0]->{'report'}}) {
> if ($report->{'name'}->[0] eq $uuid) {
> return $report->{'status'}->[0];
> }
> } # foreach
> } # if
525d513
< # my $scanList = $xmls->{'contents'}->[0]->{'reports'}->[0]->{'report'};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment