OpenVAS: Assign another host to a target using omp

A target that is used in a task can not be edited. In order to assign a further host to a target, however, you have to work with a small trick.

The greenborn guide copies an existing target, changes the copy, and then assigns the copy of the task.

I have chosen a different path, which I consider to be more elegant:

First, a temporary targetĀ is created that will be entered as a new target. Then the original target can be edited, e.g. a host can be added. In the next step, the original target of the task is assigned and the temporary target is deleted.

#!/bin/bash
#title :openVAS-add-host.sh
#description :This script will add a host entry to a existing target.
# A target that is used in a task can not be changed.
# For this reason, a dummy target is used here.
# It is inserted temporarily in the task, whose target is to be changed temporaer, so that the original target can be changed.
# Then the original target is assigned to the task again.
#author :jochen.dehm@neustadt.eu
#date :26.01.2017
#version :0.4 
#usage :./openVAS-add-host.sh <Name of the task> <Host to addt> <Name of the OpenVAS server> <OpenVAS user> <OpenVAS password>
#notes :Install xmlstarlet open-vas-cli
#==============================================================================

#Name to be used for the dummy target.
DUMMY_TARGET_NAME=Dummy
#A dummy target is generated durring runtime and then deleted

#Debug to console 1=on, 0=off
DEBUG_OPENVAS=0

if [ $# = "5" ] 
then
 echo "Taskname: "$1
 echo "New Host: "$2
 echo "Name or IP OpenVAS-Server: "$3
 echo "Name OpenVAS-User: "$4
 echo "Password OpenVAS-User: "$5
 echo "----------------------------------"
else 
 echo "Syntax: "$0" <Name of the task> <Host to add> <Name of the OpenVAS server> <OpenVAS user> <OpenVAS password>"
 echo "for example: "$0" Switch server1.dehm.local sv-openvas.dehm.local user1 password1"
 exit
fi

#Generate Dummy-target
CREATE_DUMMY_STATUS=$(omp -h $3 -u $4 -w $5 --xml "<create_target><name>$DUMMY_TARGET_NAME</name><comment>Placeeholder</comment><hosts>127.0.0.1</hosts></create_target>")
DUMMY_TARGET_UUID=$(omp -h $3 -u $4 -w $5 --xml "<get_targets></get_targets>" | xmlstarlet sel -t -c "/get_targets_response/target[name='$DUMMY_TARGET_NAME']" | xmlstarlet sel -t -v "/target/@id")
echo " DEBUG: Dummy-target generated. UUID = "$DUMMY_TARGET_UUID

#searching for UUID Task
ORIGINAL_TASK_UUID=$(omp -h $3 -u $4 -w $5 --xml "<get_tasks></get_tasks>" | xmlstarlet sel -t -c "/get_tasks_response/task[name='$1']" | xmlstarlet sel -t -v "/task/@id")
if [ $DEBUG_OPENVAS = "1" ]
then
 echo " DEBUG: The task named "$1" has the: UUID = "$ORIGINAL_TASK_UUID
fi

#searching for UUID Target
ORIGINAL_TARGET_UUID=$(omp -h $3 -u $4 -w $5 --xml "<get_tasks></get_tasks>" | xmlstarlet sel -t -c "/get_tasks_response/task[name='$1']" | xmlstarlet sel -t -v "/task/target/@id")
if [ $DEBUG_OPENVAS = "1" ]
then
echo " DEBUG: The target that should be changed has the UUID = "$ORIGINAL_TARGET_UUID
fi

#searching for current Hosts in original target
ORIGINAL_TARGET_HOSTS=$(omp -h $3 -u $4 -w $5 --xml "<get_targets target_id=\"$ORIGINAL_TARGET_UUID\"></get_targets>" | xmlstarlet sel -t -v /get_targets_response/target/hosts)
NEW_TARGET_HOSTS=$ORIGINAL_TARGET_HOSTS", "$2
if [ $DEBUG_OPENVAS = "1" ]
then
 echo " DEBUG: Currently registered hosts in target: "$ORIGINAL_TARGET_HOSTS
 echo " DEBUG: Change hosts to: "$NEW_TARGET_HOSTS
 echo " DEBUG: Duplicate entries are ignored."
 echo " DEBUG: ----------------------------------"
fi

#modify task to use Dummy-target
MODIFY_TASK_RESPONSE_1=$(omp -h $3 -u $4 -w $5 --xml "<modify_task task_id=\"$ORIGINAL_TASK_UUID\"><target id=\"$DUMMY_TARGET_UUID\"/></modify_task>")

#change hosts in free original Target
MODIFY_TARGET_RESPONSE=$(omp -h $3 -u $4 -w $5 --xml "<modify_target target_id=\"$ORIGINAL_TARGET_UUID\"><hosts>$NEW_TARGET_HOSTS</hosts><exclude_hosts/></modify_target>")

##modify task to use original target again.
MODIFY_TASK_RESPONSE_2=$(omp -h $3 -u $4 -w $5 --xml "<modify_task task_id=\"$ORIGINAL_TASK_UUID\"><target id=\"$ORIGINAL_TARGET_UUID\"/></modify_task>")

#Delete Dummy-target
DELETE_DUMMY_STATUS=$(omp -h $3 -u $4 -w $5 --xml "<delete_target target_id='$DUMMY_TARGET_UUID'/>")

if [ $DEBUG_OPENVAS = "1" ]
then
 echo " DEBUG: " $MODIFY_TASK_RESPONSE_1
 echo " DEBUG: " $MODIFY_TARGET_RESPONSE
 echo " DEBUG: " $MODIFY_TASK_RESPONSE_2
 echo " DEBUG: " $DELETE_DUMMY_STATUS
 echo " DEBUG: ----------------------------------"
fi
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s