#!/bin/bash
# ------------------------------------------------------------------------------
# $Id: do-scan,v 1.14 2010/09/01 16:38:10 frank_breedijk Exp $
# ------------------------------------------------------------------------------
# This is the shell script that actually kicks off a scan usin nessus
# after kicking of the scan it converts the output to xml and html format and
# starts the process-scan.pl perl script to update the flat file database.
# After this is runs a chmod to fix the permissions for the webserver.
# ------------------------------------------------------------------------------
# Copyright (C) 2008  Schuberg Philis, Frank Breedijk - Under GPLv3
# ------------------------------------------------------------------------------

source /home/seccubus/etc/config
export SENDMAIL
export CONFIG

MODE="safe"

if [ -d "$VAR/$1" ] 
then
	if [ -e $VAR/$1/config ]
	then
		source $VAR/$1/config
	else
		echo "Config file $1/config not found"
		exit
	fi
	if [ -e $VAR/$1/hosts ]
	then
		echo
	else
		echo "Config file $1/hosts not found"
		exit
	fi
else
	echo "Directory for scan $1 ($VAR/$1) does not exist"
	exit
fi

DATE=`date +%Y%m%d%H%M`
cd $VAR/$1

if [ ! -d "$VAR/$1/output" ]; then
	mkdir output
fi
if [ ! -d "$VAR/$1/findings" ]; then
	mkdir findings
fi

eval "$PRESCAN"

# Ticket [ 2979354 ] - Nikto compatiblity
if [ "$MODE" == "nikto" ]
then
	# It's a nikto scan
	CMD="$NIKTOBIN $NIKTOOPTS -h hosts -F nbe -o output/$DATE.nbe"
	echo "CMD = $CMD"
else
	# It's a Nessus or OpenVAS scan
	# BUG [ 2740102 ] Nessus v 2.2.5 issue
	VERSION=`$NESSUSBIN -v|head -1`
	if [ `echo $VERSION|grep -i openvas|wc -l` -ge 1 ]
	then
		# It's OpenVAS
		# BUG [ 2316867 ] OpenVas does not allow the -x flag before 
		# the host spec, but needs it after that.
		CMD="$NESSUSBIN -c $CONFIG/$MODE-nessusrc -q $HOST $PORT $USER '$PASS' hosts output/$DATE.nbe -x"
	else
		# Its's Nessus
        	CMD="$NESSUSBIN -c $CONFIG/$MODE-nessusrc -q -x $HOST $PORT $USER '$PASS' hosts output/$DATE.nbe"
	fi
fi
eval $CMD
eval "$POSTSCAN"
# BUG [ 2433253 ] - When scan fails nbe file should not be processed
# Updated do-scan to not run process-scan if the NBE file does not exist.
if [ -e $VAR/$1/output/$DATE.nbe ] 
then
	# Ticket [ 2979354 ] - Nikto integration
	if [ "$MODE" == "nikto" ]
	then
		# Nikto scan
		echo "This output format is not available for Nikto scans" > output/$DATE.xml
		echo "This output format is not available for Nikto scans" > output/$DATE.html
	else
		# Nessus / OpenVAS scan
		$NESSUSBIN -c $CONFIG/$MODE-nessusrc -i $VAR/$1/output/$DATE.nbe -o $VAR/$1/output/$DATE.xml
		$NESSUSBIN -c $CONFIG/$MODE-nessusrc -i $VAR/$1/output/$DATE.nbe -o $VAR/$1/output/$DATE.html
	fi
	find . -type f |xargs chmod 660
	find . -type d |xargs chmod 775
	# Ticket [ 2978649 ] - Seccubus cannot handle compliance plugin output
	find findings -name "$DATE" -exec rm {} \;
	$BIN/process-scan.pl $1 $EMAIL
	find . -type f |xargs chmod 660
	find . -type d |xargs chmod 775
else
	echo "Scan failed, .NBE file $VAR/$1/output/$DATE.nbe does not exist"
fi
