Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/plugins/openvas.rb
Views: 11705
# This plugin provides integration with OpenVAS.1# Written by kost and averagesecurityguy.2#3# Distributed under MIT license:4# http://www.opensource.org/licenses/mit-license.php56require 'openvas-omp'78module Msf9class Plugin::OpenVAS < Msf::Plugin10class OpenVASCommandDispatcher11include Msf::Ui::Console::CommandDispatcher1213def name14'OpenVAS'15end1617def commands18{19'openvas_help' => 'Displays help',20'openvas_version' => 'Display the version of the OpenVAS server',21'openvas_debug' => 'Enable/Disable debugging',22'openvas_connect' => 'Connect to an OpenVAS manager using OMP',23'openvas_disconnect' => 'Disconnect from OpenVAS manager',2425'openvas_task_create' => 'Create a task (name, comment, target, config)',26'openvas_task_delete' => 'Delete task by ID',27'openvas_task_list' => 'Display list of tasks',28'openvas_task_start' => 'Start task by ID',29'openvas_task_stop' => 'Stop task by ID',30'openvas_task_pause' => 'Pause task by ID',31'openvas_task_resume' => 'Resume task by ID',32'openvas_task_resume_or_start' => 'Resume task or start task by ID',3334'openvas_target_create' => 'Create target (name, hosts, comment)',35'openvas_target_delete' => 'Delete target by ID',36'openvas_target_list' => 'Display list of targets',3738'openvas_config_list' => 'Quickly display list of configs',3940'openvas_format_list' => 'Display list of available report formats',4142'openvas_report_list' => 'Display a list of available report formats',43'openvas_report_delete' => 'Delete a report specified by ID',44'openvas_report_download' => 'Save a report to disk',45'openvas_report_import' => 'Import report specified by ID into framework'46}47end4849def cmd_openvas_help50print_status('openvas_help Display this help')51print_status('openvas_debug Enable/Disable debugging')52print_status('openvas_version Display the version of the OpenVAS server')53print_status54print_status('CONNECTION')55print_status('==========')56print_status('openvas_connect Connects to OpenVAS')57print_status('openvas_disconnect Disconnects from OpenVAS')58print_status59print_status('TARGETS')60print_status('=======')61print_status('openvas_target_create Create target')62print_status('openvas_target_delete Deletes target specified by ID')63print_status('openvas_target_list Lists targets')64print_status65print_status('TASKS')66print_status('=====')67print_status('openvas_task_create Create task')68print_status('openvas_task_delete Delete a task and all associated reports')69print_status('openvas_task_list Lists tasks')70print_status('openvas_task_start Starts task specified by ID')71print_status('openvas_task_stop Stops task specified by ID')72print_status('openvas_task_pause Pauses task specified by ID')73print_status('openvas_task_resume Resumes task specified by ID')74print_status('openvas_task_resume_or_start Resumes or starts task specified by ID')75print_status76print_status('CONFIGS')77print_status('=======')78print_status('openvas_config_list Lists scan configurations')79print_status80print_status('FORMATS')81print_status('=======')82print_status('openvas_format_list Lists available report formats')83print_status84print_status('REPORTS')85print_status('=======')86print_status('openvas_report_list Lists available reports')87print_status('openvas_report_delete Delete a report specified by ID')88print_status('openvas_report_import Imports an OpenVAS report specified by ID')89print_status('openvas_report_download Downloads an OpenVAS report specified by ID')90end9192# Verify the database is connected and usable93def database?94if !(framework.db && framework.db.usable)95return false96else97return true98end99end100101# Verify there is an active OpenVAS connection102def openvas?103if @ov104return true105else106print_error('No OpenVAS connection available. Please use openvas_connect.')107return false108end109end110111# Verify correct number of arguments and verify -h was not given. Return112# true if correct number of arguments and help was not requested.113def args?(args, min = 1, max = nil)114if !max then max = min end115if ((args.length < min) || (args.length > max) || (args[0] == '-h'))116return false117end118119return true120end121122#--------------------------123# Basic Functions124#--------------------------125def cmd_openvas_debug(*args)126return unless openvas?127128if args?(args)129begin130resp = @ov.debug(args[0].to_i)131print_good(resp)132rescue OpenVASOMP::OMPError => e133print_error(e.to_s)134end135else136print_status('Usage:')137print_status('openvas_debug integer')138end139end140141def cmd_openvas_version142return unless openvas?143144begin145ver = @ov.version_get146print_good("Using OMP version #{ver}")147rescue OpenVASOMP::OMPError => e148print_error(e.to_s)149end150end151152#--------------------------153# Connection Functions154#--------------------------155def cmd_openvas_connect(*args)156# Is the database configured?157if !database?158print_error('No database has been configured.')159return160end161162# Don't allow duplicate sessions163if @ov164print_error('Session already open, please use openvas_disconnect first.')165return166end167168# Make sure the correct number of arguments are present.169if args?(args, 4, 5)170171user, pass, host, port, sslv = args172173# SSL warning. User is required to confirm.174if ((host != 'localhost') && (host != '127.0.0.1') && (sslv != 'ok'))175print_error('Warning: SSL connections are not verified in this release, it is possible for an attacker')176print_error(' with the ability to man-in-the-middle the OpenVAS traffic to capture the OpenVAS')177print_error(" credentials. If you are running this on a trusted network, please pass in 'ok'")178print_error(' as an additional parameter to this command.')179return180end181182begin183print_status("Connecting to OpenVAS instance at #{host}:#{port} with username #{user}...")184ov = OpenVASOMP::OpenVASOMP.new('user' => user, 'password' => pass, 'host' => host, 'port' => port)185rescue OpenVASOMP::OMPAuthError => e186print_error("Authentication failed: #{e.reason}")187return188rescue OpenVASOMP::OMPConnectionError => e189print_error("Connection failed: #{e.reason}")190return191end192print_good('OpenVAS connection successful')193@ov = ov194195else196print_status('Usage:')197print_status('openvas_connect username password host port <ssl-confirm>')198end199end200201# Disconnect from an OpenVAS manager202def cmd_openvas_disconnect203return unless openvas?204205@ov.logout206@ov = nil207end208209#--------------------------210# Target Functions211#--------------------------212def cmd_openvas_target_create(*args)213return unless openvas?214215if args?(args, 3)216begin217resp = @ov.target_create('name' => args[0], 'hosts' => args[1], 'comment' => args[2])218print_status(resp)219cmd_openvas_target_list220rescue OpenVASOMP::OMPError => e221print_error(e.to_s)222end223224else225print_status('Usage: openvas_target_create <name> <hosts> <comment>')226end227end228229def cmd_openvas_target_delete(*args)230return unless openvas?231232if args?(args)233begin234resp = @ov.target_delete(args[0])235print_status(resp)236cmd_openvas_target_list237rescue OpenVASOMP::OMPError => e238print_error(e.to_s)239end240else241print_status('Usage: openvas_target_delete <target_id>')242end243end244245def cmd_openvas_target_list(*_args)246return unless openvas?247248begin249tbl = Rex::Text::Table.new(250'Columns' => ['ID', 'Name', 'Hosts', 'Max Hosts', 'In Use', 'Comment']251)252@ov.target_get_all.each do |target|253tbl << [254target['id'], target['name'], target['hosts'], target['max_hosts'],255target['in_use'], target['comment']256]257end258print_good('OpenVAS list of targets')259print_line260print_line tbl.to_s261print_line262rescue OpenVASOMP::OMPError => e263print_error(e.to_s)264end265end266267#--------------------------268# Task Functions269#--------------------------270def cmd_openvas_task_create(*args)271return unless openvas?272273if args?(args, 4)274begin275resp = @ov.task_create('name' => args[0], 'comment' => args[1], 'config' => args[2], 'target' => args[3])276print_status(resp)277cmd_openvas_task_list278rescue OpenVASOMP::OMPError => e279print_error(e.to_s)280end281282else283print_status('Usage: openvas_task_create <name> <comment> <config_id> <target_id>')284end285end286287def cmd_openvas_task_delete(*args)288return unless openvas?289290if args?(args, 2)291292# User is required to confirm before deleting task.293if (args[1] != 'ok')294print_error('Warning: Deleting a task will also delete all reports associated with the ')295print_error("task, please pass in 'ok' as an additional parameter to this command.")296return297end298299begin300resp = @ov.task_delete(args[0])301print_status(resp)302cmd_openvas_task_list303rescue OpenVASOMP::OMPError => e304print_error(e.to_s)305end306else307print_status('Usage: openvas_task_delete <id> ok')308print_error('This will delete the task and all associated reports.')309end310end311312def cmd_openvas_task_list(*_args)313return unless openvas?314315begin316tbl = Rex::Text::Table.new(317'Columns' => ['ID', 'Name', 'Comment', 'Status', 'Progress']318)319@ov.task_get_all.each do |task|320tbl << [ task['id'], task['name'], task['comment'], task['status'], task['progress'] ]321end322print_good('OpenVAS list of tasks')323print_line324print_line tbl.to_s325print_line326rescue OpenVASOMP::OMPError => e327print_error(e.to_s)328end329end330331def cmd_openvas_task_start(*args)332return unless openvas?333334if args?(args)335begin336resp = @ov.task_start(args[0])337print_status(resp)338rescue OpenVASOMP::OMPError => e339print_error(e.to_s)340end341else342print_status('Usage: openvas_task_start <id>')343end344end345346def cmd_openvas_task_stop(*args)347return unless openvas?348349if args?(args)350begin351resp = @ov.task_stop(args[0])352print_status(resp)353rescue OpenVASOMP::OMPError => e354print_error(e.to_s)355end356else357print_status('Usage: openvas_task_stop <id>')358end359end360361def cmd_openvas_task_pause(*args)362return unless openvas?363364if args?(args)365begin366resp = @ov.task_pause(args[0])367print_status(resp)368rescue OpenVASOMP::OMPError => e369print_error(e.to_s)370end371else372print_status('Usage: openvas_task_pause <id>')373end374end375376def cmd_openvas_task_resume(*args)377return unless openvas?378379if args?(args)380begin381resp = @ov.task_resume_paused(args[0])382print_status(resp)383rescue OpenVASOMP::OMPError => e384print_error(e.to_s)385end386else387print_status('Usage: openvas_task_resume <id>')388end389end390391def cmd_openvas_task_resume_or_start(*args)392return unless openvas?393394if args?(args)395begin396resp = @ov.task_resume_or_start(args[0])397print_status(resp)398rescue OpenVASOMP::OMPError => e399print_error(e.to_s)400end401else402print_status('Usage: openvas_task_resume_or_start <id>')403end404end405406#--------------------------407# Config Functions408#--------------------------409def cmd_openvas_config_list(*_args)410return unless openvas?411412begin413tbl = Rex::Text::Table.new(414'Columns' => [ 'ID', 'Name' ]415)416417@ov.config_get_all.each do |config|418tbl << [ config['id'], config['name'] ]419end420print_good('OpenVAS list of configs')421print_line422print_line tbl.to_s423print_line424rescue OpenVASOMP::OMPError => e425print_error(e.to_s)426end427end428429#--------------------------430# Format Functions431#--------------------------432def cmd_openvas_format_list(*_args)433return unless openvas?434435begin436tbl = Rex::Text::Table.new(437'Columns' => ['ID', 'Name', 'Extension', 'Summary']438)439format_get_all.each do |format|440tbl << [ format['id'], format['name'], format['extension'], format['summary'] ]441end442print_good('OpenVAS list of report formats')443print_line444print_line tbl.to_s445print_line446rescue OpenVASOMP::OMPError => e447print_error(e.to_s)448end449end450451#--------------------------452# Report Functions453#--------------------------454def cmd_openvas_report_list(*_args)455return unless openvas?456457begin458tbl = Rex::Text::Table.new(459'Columns' => ['ID', 'Task Name', 'Start Time', 'Stop Time']460)461462resp = @ov.report_get_raw463464resp.elements.each('//get_reports_response/report') do |report|465report_id = report.elements['report'].attributes['id']466report_task = report.elements['task/name'].get_text467report_start_time = report.elements['creation_time'].get_text468report_stop_time = report.elements['modification_time'].get_text469470tbl << [ report_id, report_task, report_start_time, report_stop_time ]471end472print_good('OpenVAS list of reports')473print_line474print_line tbl.to_s475print_line476rescue OpenVASOMP::OMPError => e477print_error(e.to_s)478end479end480481def cmd_openvas_report_delete(*args)482return unless openvas?483484if args?(args)485begin486resp = @ov.report_delete(args[0])487print_status(resp)488cmd_openvas_report_list489rescue OpenVASOMP::OMPError => e490print_error(e.to_s)491end492else493print_status('Usage: openvas_report_delete <id>')494end495end496497def cmd_openvas_report_download(*args)498return unless openvas?499500if args?(args, 4)501begin502report = @ov.report_get_raw('report_id' => args[0], 'format' => args[1])503::FileUtils.mkdir_p(args[2])504name = ::File.join(args[2], args[3])505print_status("Saving report to #{name}")506output = ::File.new(name, 'w')507data = nil508report.elements.each('//get_reports_response') { |r| data = r.to_s }509output.puts(data)510output.close511rescue OpenVASOMP::OMPError => e512print_error(e.to_s)513end514else515print_status('Usage: openvas_report_download <report_id> <format_id> <path> <report_name>')516end517end518519def cmd_openvas_report_import(*args)520return unless openvas?521522if args?(args, 2)523begin524report = @ov.report_get_raw('report_id' => args[0], 'format' => args[1])525data = nil526report.elements.each('//get_reports_response') { |r| data = r.to_s }527print_status('Importing report to database.')528framework.db.import({ data: data })529rescue OpenVASOMP::OMPError => e530print_error(e.to_s)531end532else533print_status('Usage: openvas_report_import <report_id> <format_id>')534print_status('Only the NBE and XML formats are supported for importing.')535end536end537538#--------------------------539# Format Functions540#--------------------------541# Get a list of report formats542def format_get_all543resp = @ov.omp_request_xml('<get_report_formats/>')544if @debug then print resp end545546list = Array.new547resp.elements.each('//get_report_formats_response/report_format') do |report|548td = Hash.new549td['id'] = report.attributes['id']550td['name'] = report.elements['name'].text551td['extension'] = report.elements['extension'].text552td['summary'] = report.elements['summary'].text553list.push td554end555@formats = list556return list557rescue StandardError558raise OMPResponseError559end560561end562563#------------------------------564# Plugin initialization565#------------------------------566567def initialize(framework, opts)568super569add_console_dispatcher(OpenVASCommandDispatcher)570print_status('Welcome to OpenVAS integration by kost and averagesecurityguy.')571print_status572print_status('OpenVAS integration requires a database connection. Once the ')573print_status('database is ready, connect to the OpenVAS server using openvas_connect.')574print_status('For additional commands use openvas_help.')575print_status576@ov = nil577@formats = nil578@debug = nil579end580581def cleanup582remove_console_dispatcher('OpenVAS')583end584585def name586'OpenVAS'587end588589def desc590'Integrates with the OpenVAS - open source vulnerability management'591end592end593end594595596