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/nessus.rb
Views: 11704
# $Id$ $Revision$1require 'nessus_rest'23module Msf4PLUGIN_NAME = 'Nessus'.freeze5PLUGIN_DESCRIPTION = 'Nessus Bridge for Metasploit'.freeze67class Plugin::Nessus < Msf::Plugin89def name10PLUGIN_NAME11end1213def desc14PLUGIN_DESCRIPTION15end1617class ConsoleCommandDispatcher18include Msf::Ui::Console::CommandDispatcher1920def name21PLUGIN_NAME22end2324def xindex25"#{Msf::Config.config_directory}/nessus_index"26end2728def nessus_yaml29"#{Msf::Config.config_directory}/nessus.yaml"30end3132def msf_local33Msf::Config.local_directory.to_s34end3536def commands37{38'nessus_connect' => 'Connect to a nessus server: nconnect username:password@hostname:port <verify_ssl>',39'nessus_admin' => 'Checks if user is an admin',40'nessus_help' => 'Get help on all commands',41'nessus_logout' => 'Terminate the session',42'nessus_server_status' => 'Check the status of your Nessus server',43'nessus_server_properties' => 'Nessus server properties such as feed type, version, plugin set and server UUID',44'nessus_report_download' => 'Download a report from the nessus server in either Nessus, HTML, PDF, CSV, or DB format',45'nessus_report_vulns' => 'Get list of vulns from a report',46'nessus_report_hosts' => 'Get list of hosts from a report',47'nessus_report_host_details' => 'Get detailed information from a report item on a host',48'nessus_scan_list' => 'List of currently running Nessus scans',49'nessus_scan_new' => 'Create a new Nessus scan',50'nessus_scan_launch' => 'Launch a previously added scan',51'nessus_scan_pause' => 'Pause a running Nessus scan',52'nessus_scan_pause_all' => 'Pause all running Nessus scans',53'nessus_scan_stop' => 'Stop a running or paused Nessus scan',54'nessus_scan_stop_all' => 'Stop all running or paused Nessus scans',55'nessus_scan_resume' => 'Resume a paused Nessus scan',56'nessus_scan_resume_all' => 'Resume all paused Nessus scans',57'nessus_scan_details' => 'Return detailed information of a given scan',58'nessus_scan_export' => 'Export a scan result in either Nessus, HTML, PDF, CSV, or DB format',59'nessus_scan_export_status' => 'Check the status of scan export',60'nessus_user_list' => 'List of Nessus users',61'nessus_user_add' => 'Add a new Nessus user',62'nessus_user_del' => 'Delete a Nessus user',63'nessus_user_passwd' => 'Change Nessus Users Password',64'nessus_plugin_details' => 'List details of a particular plugin',65'nessus_plugin_list' => 'Display plugin details in a particular plugin family',66'nessus_policy_list' => 'List all polciies',67'nessus_policy_del' => 'Delete a policy',68'nessus_index' => 'Manually generates a search index for exploits',69'nessus_template_list' => 'List all the templates on the server',70'nessus_db_scan' => 'Create a scan of all IP addresses in db_hosts',71'nessus_db_scan_workspace' => 'Create a scan of all IP addresses in db_hosts for a given workspace',72'nessus_db_import' => 'Import Nessus scan to the Metasploit connected database',73'nessus_save' => 'Save credentials of the logged in user to nessus.yml',74'nessus_folder_list' => 'List folders configured on the Nessus server',75'nessus_scanner_list' => 'List the configured scanners on the Nessus server',76'nessus_family_list' => 'List all the plugin families along with their corresponding family IDs and plugin count'77}78end7980def ncusage81print_status('%redYou must do this before any other commands.%clr')82print_status('Usage: ')83print_status('nessus_connect username:password@hostname:port <ssl_verify>')84print_status('Example:> nessus_connect msf:[email protected]:8834')85print_status('OR')86print_status('nessus_connect username@hostname:port ssl_verify')87print_status('Example:> nessus_connect [email protected]:8834 ssl_verify')88print_status('OR')89print_status('nessus_connect hostname:port ssl_verify')90print_status('Example:> nessus_connect 192.168.1.10:8834 ssl_verify')91print_status('OR')92print_status('nessus_connect')93print_status('Example:> nessus_connect')94print_status('This only works after you have saved creds with nessus_save')95return96end9798# creates the index of exploit details to make searching for exploits much faster.99def create_xindex100start = Time.now101print_status("Creating Exploit Search Index - (#{xindex}) - this won't take long.")102# Use Msf::Config.config_directory as the location.103File.open(xindex.to_s, 'w+') do |f|104# need to add version line.105f.puts(Msf::Framework::Version)106framework.exploits.each_module do |refname, mod|107stuff = ''108o = nil109begin110o = mod.new111rescue ::Exception112end113stuff << "#{refname}|#{o.name}|#{o.platform_to_s}|#{o.arch_to_s}"114next if !o115116o.references.map do |x|117if x.ctx_id != 'URL'118if (x.ctx_id == 'MSB')119stuff << "|#{x.ctx_val}"120else121stuff << "|#{x.ctx_id}-#{x.ctx_val}"122end123end124end125stuff << "\n"126f.puts(stuff)127end128end129total = Time.now - start130print_status("It has taken : #{total} seconds to build the exploits search index")131end132133def nessus_index134if File.exist?(xindex.to_s)135# check if it's version line matches current version.136File.open(xindex.to_s) do |f|137line = f.readline138line.chomp!139if line.to_i == Msf::Framework::RepoRevision140print_good("Exploit Index - (#{xindex}) - is valid.")141else142create_xindex143end144end145else146create_xindex147end148end149150def nessus_login151if !((@user && !@user.empty?) && (@host && !@host.empty?) && (@port && !@port.empty? && (@port.to_i > 0)) && (@pass && !@pass.empty?))152print_status('You need to connect to a server first.')153ncusage154return155end156@url = "https://#{@host}:#{@port}/"157print_status("Connecting to #{@url} as #{@user}")158verify_ssl = false159if @sslv == 'verify_ssl'160verify_ssl = true161end162@n = NessusREST::Client.new(url: @url, username: @user, password: @pass, ssl_verify: verify_ssl)163if @n.authenticated164print_status("User #{@user} authenticated successfully.")165@token = 1166else167print_error('Error connecting/logging to the server!')168return169end170end171172def nessus_verify_token173if @token.nil? || (@token == '')174ncusage175return false176end177true178end179180def valid_policy(*args)181case args.length182when 1183pid = args[0]184else185print_error('No Policy ID supplied.')186return187end188pol = @n.list_policies189pol['policies'].each do |p|190if p['template_uuid'] == pid191return true192end193end194return false195end196197def nessus_verify_db198if !(framework.db && framework.db.active)199print_error('No database has been configured, please use db_connect first')200return false201end202true203end204205def check_scan(*args)206case args.length207when 1208scan_id = args[0]209else210print_error('No scan ID supplied')211return212end213scans = @n.scan_list214scans.each do |scan|215if scan['scans']['id'] == scan_id && scan['scans']['status'] == 'completed'216return true217end218end219return false220end221222def is_scan_complete(scan_id)223complete = false224status = @n.scan_list225status['scans'].each do |scan|226if scan['id'] == scan_id.to_i && (scan['status'] == 'completed' || scan['status'] == 'imported')227complete = true228end229end230complete231end232233def cmd_nessus_help(*_args)234tbl = Rex::Text::Table.new(235'Columns' => [236'Command',237'Help Text'238],239'SortIndex' => -1240)241tbl << [ 'Generic Commands', '' ]242tbl << [ '-----------------', '-----------------']243tbl << [ 'nessus_connect', 'Connect to a Nessus server' ]244tbl << [ 'nessus_logout', 'Logout from the Nessus server' ]245tbl << [ 'nessus_login', 'Login into the connected Nesssus server with a different username and password']246tbl << [ 'nessus_save', 'Save credentials of the logged in user to nessus.yml']247tbl << [ 'nessus_help', 'Listing of available nessus commands' ]248tbl << [ 'nessus_server_properties', 'Nessus server properties such as feed type, version, plugin set and server UUID.' ]249tbl << [ 'nessus_server_status', 'Check the status of your Nessus Server' ]250tbl << [ 'nessus_admin', 'Checks if user is an admin' ]251tbl << [ 'nessus_template_list', 'List scan or policy templates' ]252tbl << [ 'nessus_folder_list', 'List all configured folders on the Nessus server' ]253tbl << [ 'nessus_scanner_list', 'List all the scanners configured on the Nessus server' ]254tbl << [ 'Nessus Database Commands', '' ]255tbl << [ '-----------------', '-----------------' ]256tbl << [ 'nessus_db_scan', 'Create a scan of all IP addresses in db_hosts' ]257tbl << [ 'nessus_db_scan_workspace', 'Create a scan of all IP addresses in db_hosts for a given workspace' ]258tbl << [ 'nessus_db_import', 'Import Nessus scan to the Metasploit connected database' ]259tbl << [ '', '']260tbl << [ 'Reports Commands', '' ]261tbl << [ '-----------------', '-----------------']262tbl << [ 'nessus_report_hosts', 'Get list of hosts from a report' ]263tbl << [ 'nessus_report_vulns', 'Get list of vulns from a report' ]264tbl << [ 'nessus_report_host_details', 'Get detailed information from a report item on a host' ]265tbl << [ '', '']266tbl << [ 'Scan Commands', '' ]267tbl << [ '-----------------', '-----------------']268tbl << [ 'nessus_scan_list', 'List of all current Nessus scans' ]269tbl << [ 'nessus_scan_new', 'Create a new Nessus Scan' ]270tbl << [ 'nessus_scan_launch', 'Launch a newly created scan. New scans need to be manually launched through this command' ]271tbl << [ 'nessus_scan_pause', 'Pause a running Nessus scan' ]272tbl << [ 'nessus_scan_pause_all', 'Pause all running Nessus scans' ]273tbl << [ 'nessus_scan_stop', 'Stop a running or paused Nessus scan' ]274tbl << [ 'nessus_scan_stop_all', 'Stop all running or paused Nessus scans' ]275tbl << [ 'nessus_scan_resume', 'Resume a pasued Nessus scan' ]276tbl << [ 'nessus_scan_resume_all', 'Resume all paused Nessus scans' ]277tbl << [ 'nessus_scan_details', 'Return detailed information of a given scan' ]278tbl << [ 'nessus_scan_export', 'Export a scan result in either Nessus, HTML, PDF, CSV, or DB format' ]279tbl << [ 'nessus_scan_export_status', 'Check the status of an exported scan' ]280tbl << [ '', '']281tbl << [ 'Plugin Commands', '' ]282tbl << [ '-----------------', '-----------------']283tbl << [ 'nessus_plugin_list', 'List all plugins in a particular plugin family.' ]284tbl << [ 'nessus_family_list', 'List all the plugin families along with their corresponding family IDs and plugin count.' ]285tbl << [ 'nessus_plugin_details', 'List details of a particular plugin' ]286tbl << [ '', '']287tbl << [ 'User Commands', '' ]288tbl << [ '-----------------', '-----------------']289tbl << [ 'nessus_user_list', 'Show Nessus Users' ]290tbl << [ 'nessus_user_add', 'Add a new Nessus User' ]291tbl << [ 'nessus_user_del', 'Delete a Nessus User' ]292tbl << [ 'nessus_user_passwd', 'Change Nessus Users Password' ]293tbl << [ '', '']294tbl << [ 'Policy Commands', '' ]295tbl << [ '-----------------', '-----------------']296tbl << [ 'nessus_policy_list', 'List all polciies' ]297tbl << [ 'nessus_policy_del', 'Delete a policy' ]298print_line ''299print_line tbl.to_s300print_line ''301end302303def cmd_nessus_index304nessus_index305end306307def cmd_nessus_connect(*args)308# Check if config file exists and load it309if !args[0]310if File.exist?(nessus_yaml)311lconfig = YAML.load_file(nessus_yaml)312@user = lconfig['default']['username'].to_s313@pass = lconfig['default']['password'].to_s314@host = lconfig['default']['server'].to_s315@port = lconfig['default']['port'].to_s316nessus_login317else318ncusage319end320return321end322323if args[0] == '-h'324print_status('%redYou must do this before any other commands.%clr')325print_status('Usage: ')326print_status('nessus_connect username:password@hostname:port <ssl_verify/ssl_ignore>')327print_status('%bldusername%clr and %bldpassword%clr are the ones you use to login to the nessus web front end')328print_status('%bldhostname%clr can be an IP address or a DNS name of the Nessus server.')329print_status('%bldport%clr is the RPC port that the Nessus web front end runs on. By default it is TCP port 8834.')330print_status('The "ssl_verify" to verify the SSL certificate used by the Nessus front end. By default the server')331print_status('use a self signed certificate, therefore, users should use ssl_ignore.')332return333end334335if !@token == ''336print_error('You are already authenticated. Call nessus_logout before authenticating again')337return338end339if (args.empty? || args[0].empty?)340ncusage341return342end343344@user = @pass = @host = @port = @sslv = nil345case args.length346when 1, 2347if args[0].include? '@'348cred, _split, targ = args[0].rpartition('@')349@user, @pass = cred.split(':', 2)350targ ||= '127.0.0.1:8834'351@host, @port = targ.split(':', 2)352else353@host, @port = args[0].split(':', 2)354end355@port ||= '8834'356@sslv = args[1]357when 3, 4, 5358ncusage359return360else361ncusage362return363end364if %r{//}.match(@host)365ncusage366return367end368if !@user369print_error('Missing Username')370ncusage371return372end373if !@pass374print_error('Missing Password')375ncusage376return377end378if !((@user && !@user.empty?) && (@host && !@host.empty?) && (@port && !@port.empty? && (@port.to_i > 0)) && (@pass && !@pass.empty?))379ncusage380return381end382nessus_login383end384385def cmd_nessus_logout386logout = @n.user_logout387status = logout.to_s388if status == '200'389print_good('User account logged out successfully')390@token = ''391elsif status == '403'392print_status('No user session to logout')393else394print_error("There was some problem in logging out the user #{@user}")395end396return397end398399def cmd_nessus_save(*args)400# if we are logged in, save session details to nessus.yaml401if args[0] == '-h'402print_status(' nessus_save')403return404end405if args[0]406print_status('Usage: ')407print_status('nessus_save')408return409end410group = 'default'411if ((@user && !@user.empty?) && (@host && !@host.empty?) && (@port && !@port.empty? && (@port.to_i > 0)) && (@pass && !@pass.empty?))412config = Hash.new413config = { group.to_s => { 'username' => @user, 'password' => @pass, 'server' => @host, 'port' => @port } }414File.open(nessus_yaml.to_s, 'w+') do |f|415f.puts YAML.dump(config)416end417print_good("#{nessus_yaml} created.")418else419print_error('Missing username/password/server/port - relogin and then try again.')420return421end422end423424def cmd_nessus_server_properties(*args)425search_term = nil426while (arg = args.shift)427case arg428when '-h', '--help'429print_status('nessus_server_properties')430print_status('Example:> nessus_server_properties -S searchterm')431print_status('Returns information about the feed type and server version.')432return433when '-S', '--search'434search_term = /#{args.shift}/nmi435end436end437438resp = @n.server_properties439tbl = Rex::Text::Table.new(440'SearchTerm' => search_term,441'Columns' => [442'Feed',443'Type',444'Nessus Version',445'Nessus Web Version',446'Plugin Set',447'Server UUID'448]449)450tbl << [ resp['feed'], resp['nessus_type'], resp['server_version'], resp['nessus_ui_version'], resp['loaded_plugin_set'], resp['server_uuid'] ]451print_line tbl.to_s452end453454def cmd_nessus_server_status(*args)455search_term = nil456while (arg = args.shift)457case arg458when '-h', '--help'459print_status('nessus_server_status')460print_status('Example:> nessus_server_status -S searchterm')461print_status('Returns some status items for the server..')462return463when '-S', '--search'464search_term = /#{args.shift}/nmi465end466end467468tbl = Rex::Text::Table.new(469'SearchTerm' => search_term,470'Columns' => [471'Status',472'Progress'473]474)475list = @n.server_status476tbl << [ list['progress'], list['status'] ]477print_line tbl.to_s478end479480def cmd_nessus_admin(*args)481while (arg = args.shift)482case arg483when '-h', '--help'484print_status('nessus_admin')485print_status('Example:> nessus_admin')486print_status('Checks to see if the current user is an admin')487print_status('Use nessus_user_list to list all users')488return489end490end491492if !nessus_verify_token493return494end495496if !@n.is_admin497print_error('Your Nessus user is not an admin')498else499print_good('Your Nessus user is an admin')500end501end502503def cmd_nessus_template_list(*args)504search_term = nil505while (arg = args.shift)506case arg507when '-h', '--help'508print_status('nessus_template_list <scan> | <policy>')509print_status('Example:> nessus_template_list scan -S searchterm')510print_status('OR')511print_status('nessus_template_list policy')512print_status('Returns a list of information about the scan or policy templates..')513return514when '-S', '--search'515search_term = /#{args.shift}/nmi516else517type = arg518end519end520521if !nessus_verify_token522return523end524525if type.in?(['scan', 'policy'])526list = @n.list_templates(type)527else528print_error('Only scan and policy are valid templates')529return530end531if list.empty?532print_status('No templates created')533return534end535tbl = Rex::Text::Table.new(536'SearchTerm' => search_term,537'Columns' => [538'Name',539'Title',540'Description',541'Subscription Only',542'Cloud Only'543]544)545list['templates'].each do |template|546tbl << [ template['name'], template['title'], template['desc'], template['subscription_only'], template['cloud_only'] ]547end548print_line549print_line tbl.to_s550end551552def cmd_nessus_folder_list(*args)553search_term = nil554while (arg = args.shift)555case arg556when '-S', '--search'557search_term = /#{args.shift}/nmi558end559end560if !nessus_verify_token561return562end563564list = @n.list_folders565tbl = Rex::Text::Table.new(566'SearchTerm' => search_term,567'Columns' => [568'ID',569'Name',570'Type'571]572)573list['folders'].each do |folder|574tbl << [ folder['id'], folder['name'], folder['type'] ]575end576print_line577print_line tbl.to_s578end579580def cmd_nessus_scanner_list(*args)581search_term = nil582while (arg = args.shift)583case arg584when '-h', '--help'585print_status('nessus_scanner_list')586print_status('Example:> nessus_scanner_list -S searchterm')587print_status('Returns information about the feed type and server version.')588return589when '-S', '--search'590search_term = /#{args.shift}/nmi591end592end593if !nessus_verify_token594return595end596if !@n.is_admin597return598end599600list = @n.list_scanners601tbl = Rex::Text::Table.new(602'SearchTerm' => search_term,603'Columns' => [604'ID',605'Name',606'Status',607'Platform',608'Plugin Set',609'UUID'610]611)612list.each do |scanner|613tbl << [ scanner['id'], scanner['name'], scanner['status'], scanner['platform'], scanner['loaded_plugin_set'], scanner['uuid'] ]614end615print_line tbl.to_s616end617618def cmd_nessus_report_hosts(*args)619search_term = nil620scan_id = nil621while (arg = args.shift)622case arg623when '-h', '--help'624print_status('nessus_report_hosts <scan ID> -S searchterm')625print_status('Use nessus_scan_list to get a list of all the scans. Only completed scans can be reported.')626return627when '-S', '--search'628search_term = /#{args.shift}/nmi629else630scan_id = arg631end632end633634if scan_id.nil?635print_status('Usage: ')636print_status('nessus_report_hosts <scan ID> -S searchterm')637print_status('Use nessus_scan_list to get a list of all the scans. Only completed scans can be reported.')638return639end640641tbl = Rex::Text::Table.new(642'SearchTerm' => search_term,643'Columns' => [644'Host ID',645'Hostname',646'% of Critical Findings',647'% of High Findings',648'% of Medium Findings',649'% of Low Findings'650]651)652if is_scan_complete(scan_id)653details = @n.scan_details(scan_id)654details['hosts'].each do |host|655tbl << [ host['host_id'], host['hostname'], host['critical'], host['high'], host['medium'], host['low'] ]656end657print_line658print_line tbl.to_s659else660print_error('Only completed scans can be used for host reporting')661return662end663end664665def cmd_nessus_report_vulns(*args)666search_term = nil667scan_id = nil668while (arg = args.shift)669case arg670when '-h', '--help'671print_status('nessus_report_vulns <scan ID> -S searchterm')672print_status('Use nessus_scan_list to get a list of all the scans. Only completed scans can be reported.')673return674when '-S', '--search'675search_term = /#{args.shift}/nmi676else677scan_id = arg678end679end680if scan_id.nil?681print_status('Usage: ')682print_status('nessus_report_vulns <scan ID>')683print_status('Use nessus_scan_list to get a list of all the scans. Only completed scans can be reported.')684return685end686tbl = Rex::Text::Table.new(687'SearchTerm' => search_term,688'Columns' => [689'Plugin ID',690'Plugin Name',691'Plugin Family',692'Vulnerability Count'693]694)695if is_scan_complete(scan_id)696details = @n.scan_details(scan_id)697details['vulnerabilities'].each do |vuln|698tbl << [ vuln['plugin_id'], vuln['plugin_name'], vuln['plugin_family'], vuln['count'] ]699end700print_line701print_line tbl.to_s702else703print_error('Only completed scans can be used for vulnerability reporting')704end705return706end707708def cmd_nessus_report_host_details(*args)709search_term = nil710search_vuln = nil711scan_id = nil712host_id = nil713while (arg = args.shift)714case arg715when '-h', '--help'716print_status('nessus_report_host_details <scan ID> <host ID>')717print_status('Example:> nessus_report_host_details 10 5 -S hostinfo -SV vulninfo')718print_status('Use nessus_scan_list to get list of all scans. Only completed scans can be used for reporting.')719print_status('Use nessus_report_hosts to get a list of all the hosts along with their corresponding host IDs.')720return721when '-S', '--search'722search_term = /#{args.shift}/nmi723when '-SV', '--search-vuln'724search_vuln = /#{args.shift}/nmi725else726scan_id =727arg,728host_id = args.shift729end730end731732if [scan_id, host_id].any?(&:nil?)733print_status('Usage: ')734print_status('nessus_report_host_detail <scan ID> <host ID>')735print_status('Example:> nessus_report_host_detail 10 5')736print_status('Use nessus_scan_list to get list of all scans. Only completed scans can be used for reporting.')737print_status('Use nessus_report_hosts <scan ID> to get a list of all the hosts along with their corresponding host IDs.')738return739end740tbl = Rex::Text::Table.new(741'SearchTerm' => search_term,742'Columns' => [743'Plugin Name',744'Plugin Famil',745'Severity'746]747)748details = @n.host_detail(scan_id, host_id)749print_line750print_status('Host information')751print_line("IP Address: #{details['info']['host-ip']}")752print_line("Hostname: #{details['info']['host-name']}")753print_line("Operating System: #{details['info']['operating-system']}")754print_line755print_status('Vulnerability information')756details['vulnerabilities'].each do |vuln|757tbl << [ vuln['plugin_name'], vuln['plugin_family'], vuln['severity'] ]758end759print_line tbl.to_s760tbl2 = Rex::Text::Table.new(761'SearchTerm' => search_vuln,762'Columns' => [763'Plugin Name',764'Plugin Famil',765'Severity'766]767)768print_status('Compliance information')769details['compliance'].each do |comp|770tbl2 << [ comp['plugin_name'], comp['plugin_family'], comp['severity'] ]771end772print_line tbl2.to_s773end774775def cmd_nessus_report_download(*args)776if args[0] == '-h'777print_status('nessus_scan_report_download <scan_id> <file ID> ')778print_status('Use nessus_scan_export_status <scan ID> <file ID> to check the export status.')779print_status('Use nessus_scan_list -c to list all completed scans along with their corresponding scan IDs')780return781end782if !nessus_verify_token783return784end785786case args.length787when 2788scan_id = args[0]789file_id = args[1]790if is_scan_complete(scan_id)791report = @n.report_download(scan_id, file_id)792File.open("#{msf_local}/#{scan_id}-#{file_id}", 'w+') do |f|793f.puts report794print_status("Report downloaded to #{msf_local} directory")795end796else797print_error('Only completed scans can be downloaded')798end799else800print_status('Usage: ')801print_status('nessus_scan_report_download <scan_id> <file ID> ')802print_status('Use nessus_scan_export_status <scan ID> <file ID> to check the export status.')803print_status('Use nessus_scan_list -c to list all completed scans along with their corresponding scan IDs')804end805end806807def cmd_nessus_report_host_ports(*args)808search_term = nil809rid = nil810host = nil811while (arg = args.shift)812case arg813when '-h', '--help'814print_status('nessus_report_host_ports <hostname> <report id>')815print_status('Example:> nessus_report_host_ports 192.168.1.250 f0eabba3-4065-7d54-5763-f191e98eb0f7f9f33db7e75a06ca -S searchterm')816print_status('Returns all the ports associated with a host and details about their vulnerabilities')817print_status('Use nessus_report_hosts to list all available hosts for a report')818return819when '-S', '--search'820search_term = /#{args.shift}/nmi821else822scan_id = arg823end824end825826if [host, rid].any?(&:nil?)827print_status('Usage: ')828print_status('nessus_report_host_ports <hostname> <report id>')829print_status('Use nessus_report_list to list all available reports')830return831end832tbl = Rex::Text::Table.new(833'SearchTerm' => search_term,834'Columns' => [835'Port',836'Protocol',837'Severity',838'Service Name',839'Sev 0',840'Sev 1',841'Sev 2',842'Sev 3'843]844)845ports = @n.report_host_ports(rid, host)846ports.each do |port|847tbl << [ port['portnum'], port['protocol'], port['severity'], port['svcname'], port['sev0'], port['sev1'], port['sev2'], port['sev3'] ]848end849print_good('Host Info')850print_good "\n"851print_line tbl.to_s852print_status('You can:')853print_status('Get detailed scan information about a specific port: nessus_report_host_detail <hostname> <port> <protocol> <report id>')854end855856def cmd_nessus_report_del(*args)857if args[0] == '-h'858print_status('nessus_report_del <reportname>')859print_status('Example:> nessus_report_del f0eabba3-4065-7d54-5763-f191e98eb0f7f9f33db7e75a06ca')860print_status('Must be an admin to del reports.')861print_status('Use nessus_report_list to list all reports')862return863end864if !nessus_verify_token865return866end867868if !@n.is_admin869print_error('Your Nessus user is not an admin')870return871end872case args.length873when 1874rid = args[0]875else876print_status('Usage: ')877print_status('nessus_report_del <report ID>')878print_status('nessus_report_list to find the id.')879return880end881del = @n.report_del(rid)882status = del.root.elements['status'].text883if status == 'OK'884print_good("Report #{rid} has been deleted")885else886print_error("Report #{rid} was not deleted")887end888end889890def cmd_nessus_scan_list(*args)891search_term = nil892while (arg = args.shift)893case arg894when '-h', '--help'895print_status('nessus_scan_list')896print_status('Example:> nessus_scan_list -S searchterm')897print_status('Returns a list of information about currently running scans.')898return899when '-S', '--search'900search_term = /#{args.shift}/nmi901end902end903904if !nessus_verify_token905return906end907908list = @n.scan_list909if list.to_s.empty?910print_status('No scans performed.')911return912else913tbl = Rex::Text::Table.new(914'SearchTerm' => search_term,915'Columns' => [916'Scan ID',917'Name',918'Owner',919'Started',920'Status',921'Folder'922]923)924925list['scans'].each do |scan|926if args[0] == '-r'927if scan['status'] == 'running'928tbl << [ scan['id'], scan['name'], scan['owner'], scan['starttime'], scan['status'], scan['folder_id'] ]929end930elsif args[0] == '-p'931if scan['status'] == 'paused'932tbl << [ scan['id'], scan['name'], scan['owner'], scan['starttime'], scan['status'], scan['folder_id'] ]933end934elsif args[0] == '-c'935if scan['status'] == 'completed'936tbl << [ scan['id'], scan['name'], scan['owner'], scan['starttime'], scan['status'], scan['folder_id'] ]937end938elsif args[0] == '-a'939if scan['status'] == 'canceled'940tbl << [ scan['id'], scan['name'], scan['owner'], scan['starttime'], scan['status'], scan['folder_id'] ]941end942else943tbl << [ scan['id'], scan['name'], scan['owner'], scan['starttime'], scan['status'], scan['folder_id'] ]944end945end946print_line tbl.to_s947end948end949950def cmd_nessus_scan_new(*args)951if args[0] == '-h'952print_status('nessus_scan_new <UUID of Policy> <Scan name> <Description> <Targets>')953print_status('Use nessus_policy_list to list all available policies with their corresponding UUIDs')954return955end956if !nessus_verify_token957return958end959960case args.length961when 4962uuid = args[0]963scan_name = args[1]964description = args[2]965targets = args[3]966else967print_status('Usage: ')968print_status('nessus_scan_new <UUID of Policy> <Scan name> <Description> <Targets>')969print_status('Use nessus_policy_list to list all available policies with their corresponding UUIDs')970return971end972if valid_policy(uuid)973print_status("Creating scan from policy number #{uuid}, called #{scan_name} - #{description} and scanning #{targets}")974et = {975'enabled' => false,976'launch' => 'ONETIME',977'name' => scan_name,978'text_targets' => targets,979'description' => description,980'launch_now' => false981}982scan = @n.scan_create(uuid, et)983tbl = Rex::Text::Table.new(984'Columns' => [985'Scan ID',986'Scanner ID',987'Policy ID',988'Targets',989'Owner'990]991)992print_status('New scan added')993tbl << [ scan['scan']['id'], scan['scan']['scanner_id'], scan['scan']['policy_id'], scan['scan']['custom_targets'], scan['scan']['owner'] ]994print_status("Use nessus_scan_launch #{scan['scan']['id']} to launch the scan")995print_line tbl.to_s996else997print_error('The policy does not exist')998end999end10001001def cmd_nessus_scan_launch(*args)1002if args[0] == '-h'1003print_status('nessus_scan_launch <scan ID>')1004print_status('Use nessus_scan_list to list all the availabla scans with their corresponding scan IDs')1005end1006if !nessus_verify_token1007return1008end10091010case args.length1011when 11012scan_id = args[0]1013else1014print_status('Usage: ')1015print_status('nessus_scan_launch <scan ID>')1016print_status('Use nessus_scan_list to list all the availabla scans with their corresponding scan IDs')1017return1018end1019launch = @n.scan_launch(scan_id)1020print_good("Scan ID #{scan_id} successfully launched. The Scan UUID is #{launch['scan_uuid']}")1021end10221023def cmd_nessus_scan_pause(*args)1024if args[0] == '-h'1025print_status('nessus_scan_pause <scan id>')1026print_status('Example:> nessus_scan_pause f0eabba3-4065-7d54-5763-f191e98eb0f7f9f33db7e75a06ca')1027print_status('Pauses a running scan')1028print_status('Use nessus_scan_list to list all available scans')1029return1030end1031if !nessus_verify_token1032return1033end10341035case args.length1036when 11037sid = args[0]1038else1039print_status('Usage: ')1040print_status('nessus_scan_pause <scan id>')1041print_status('Use nessus_scan_list to list all available scans')1042return1043end1044pause = @n.scan_pause(sid)1045if pause['error']1046print_error 'Invalid scan ID'1047else1048print_status("#{sid} has been paused")1049end1050end10511052def cmd_nessus_db_scan(*args)1053if args[0] == '-h'1054print_status('nessus_db_scan <policy ID> <scan name> <scan description>')1055print_status('Creates a scan based on all the hosts listed in db_hosts.')1056print_status('Use nessus_policy_list to list all available policies with their corresponding policy IDs')1057return1058end1059if !nessus_verify_db1060return1061end1062if !nessus_verify_token1063return1064end10651066case args.length1067when 31068policy_id = args[0]1069name = args[1]1070desc = args[3]1071else1072print_status('Usage: ')1073print_status('nessus_db_scan <policy ID> <scan name> <scan description>')1074print_status('Use nessus_policy_list to list all available policies with their corresponding policy IDs')1075return1076end1077if !valid_policy(policy_id)1078print_error('That policy does not exist.')1079return1080end1081targets = ''1082framework.db.hosts.each do |host|1083targets << host.address1084targets << ','1085end1086targets.chop!1087print_status("Creating scan from policy #{policy_id}, called \"#{name}\" and scanning all hosts in all the workspaces")1088et = {1089'enabled' => false,1090'launch' => 'ONETIME',1091'name' => name,1092'text_targets' => targets,1093'description' => desc,1094'launch_now' => true1095}1096scan = @n.scan_create(policy_id, et)1097if !scan['error']1098scan = scan['scan']1099print_status("Scan ID #{scan['id']} successfully created and launched")1100else1101print_error(JSON.pretty_generate(scan))1102end1103end11041105def cmd_nessus_db_scan_workspace(*args)1106if args[0] == '-h'1107print_status('nessus_db_scan_workspace <policy ID> <scan name> <scan description> <workspace>')1108print_status('Creates a scan based on all the hosts listed in db_hosts for a given workspace.')1109print_status('Use nessus_policy_list to list all available policies with their corresponding policy IDs')1110return1111end1112if !nessus_verify_db1113return1114end1115if !nessus_verify_token1116return1117end11181119case args.length1120when 41121policy_id = args[0]1122name = args[1]1123desc = args[2]1124new_workspace = framework.db.find_workspace(args[3])1125else1126print_status('Usage: ')1127print_status('nessus_db_scan_workspace <policy ID> <scan name> <scan description> <workspace>')1128print_status('Use nessus_policy_list to list all available policies with their corresponding policy IDs')1129return1130end1131if !valid_policy(policy_id)1132print_error('That policy does not exist.')1133return1134end1135if new_workspace.nil?1136print_error('That workspace does not exist.')1137return1138end1139framework.db.workspace = new_workspace1140print_status("Switched workspace: #{framework.db.workspace.name}")1141targets = ''1142framework.db.hosts.each do |host|1143targets << host.address1144targets << ','1145print_status("Targets: #{targets}")1146end1147targets.chop!1148print_status("Creating scan from policy #{policy_id}, called \"#{name}\" and scanning all hosts in #{framework.db.workspace.name}")1149et = {1150'enabled' => false,1151'launch' => 'ONETIME',1152'name' => name,1153'text_targets' => targets,1154'description' => desc,1155'launch_now' => false1156}1157scan = @n.scan_create(policy_id, et)1158if !scan['error']1159scan = scan['scan']1160print_status("Scan ID #{scan['id']} successfully created")1161print_status("Run nessus_scan_launch #{scan['id']} to launch the scan")1162else1163print_error(JSON.pretty_generate(scan))1164end1165end11661167def cmd_nessus_db_import(*args)1168if args[0] == '-h'1169print_status('nessus_db_import <scan ID>')1170print_status('Example:> nessus_db_import 500')1171print_status('Use nessus_scan_list -c to list all completed scans')1172end1173if !nessus_verify_db1174return1175end1176if !nessus_verify_token1177return1178end11791180case args.length1181when 11182scan_id = args[0]1183else1184print_status('Usage: ')1185print_status('nessus_db_import <scan ID>')1186print_status('Example:> nessus_db_import 500')1187print_status('Use nessus_scan_list -c to list all completed scans')1188end1189if is_scan_complete(scan_id)1190print_status("Exporting scan ID #{scan_id} is Nessus format...")1191export = @n.scan_export(scan_id, 'nessus')1192status = {}1193if export['file']1194file_id = export['file']1195print_good("The export file ID for scan ID #{scan_id} is #{file_id}")1196print_status('Checking export status...')1197loop do1198status = @n.scan_export_status(scan_id, file_id)1199print_status('Export status: ' + status['status'])1200if status['status'] == 'ready'1201break1202end12031204sleep(1)1205break unless (status['status'] == 'loading')1206end1207if status['status'] == 'ready'1208print_status("The status of scan ID #{scan_id} export is ready")1209select(nil, nil, nil, 5)1210report = @n.report_download(scan_id, file_id)1211print_status('Importing scan results to the database...')1212framework.db.import({ data: report }) do |type, data|1213case type1214when :address1215print_status("Importing data of #{data}")1216end1217end1218print_good('Done')1219else1220print_error("There was some problem in exporting the scan. The error message is #{status}")1221end1222else1223print_error(export)1224end1225else1226print_error('Only completed scans could be used for import')1227end1228end12291230def cmd_nessus_scan_pause_all(*args)1231scan_ids = Array.new1232if args[0] == '-h'1233print_status('nessus_scan_pause_all')1234print_status('Example:> nessus_scan_pause_all')1235print_status('Pauses all currently running scans')1236print_status('Use nessus_scan_list to list all running scans')1237return1238end1239if !nessus_verify_token1240return1241end12421243list = @n.scan_list1244list['scans'].each do |scan|1245if scan['status'] == 'running'1246scan_ids << scan['id']1247end1248end1249if !scan_ids.empty?1250scan_ids.each do |scan_id|1251@n.scan_pause(scan_id)1252end1253print_status('All scans have been paused')1254else1255print_error('No running scans')1256end1257end12581259def cmd_nessus_scan_stop(*args)1260if args[0] == '-h'1261print_status('nessus_scan_stop <scan id>')1262print_status('Example:> nessus_scan_stop f0eabba3-4065-7d54-5763-f191e98eb0f7f9f33db7e75a06ca')1263print_status('Stops a currently running scans')1264print_status('Use nessus_scan_list to list all running scans')1265return1266end1267if !nessus_verify_token1268return1269end12701271case args.length1272when 11273sid = args[0]1274else1275print_status('Usage: ')1276print_status('nessus_scan_stop <scan id>')1277print_status('Use nessus_scan_list to list all available scans')1278return1279end1280stop = @n.scan_stop(sid)1281if stop['error']1282print_error 'Invalid scan ID'1283else1284print_status("#{sid} has been stopped")1285end1286end12871288def cmd_nessus_scan_stop_all(*args)1289scan_ids = Array.new1290if args[0] == '-h'1291print_status('nessus_scan_stop_all')1292print_status('Example:> nessus_scan_stop_all')1293print_status('stops all currently running scans')1294print_status('Use nessus_scan_list to list all running scans')1295return1296end1297if !nessus_verify_token1298return1299end13001301list = @n.scan_list1302list['scans'].each do |scan|1303if scan['status'] == 'running' || scan['status'] == 'paused'1304scan_ids << scan['id']1305end1306end1307if !scan_ids.empty?1308scan_ids.each do |scan_id|1309@n.scan_stop(scan_id)1310end1311print_status('All scans have been stopped')1312else1313print_error('No running or paused scans to be stopped')1314end1315end13161317def cmd_nessus_scan_resume(*args)1318if args[0] == '-h'1319print_status('nessus_scan_resume <scan id>')1320print_status('Example:> nessus_scan_resume f0eabba3-4065-7d54-5763-f191e98eb0f7f9f33db7e75a06ca')1321print_status('resumes a running scan')1322print_status('Use nessus_scan_list to list all available scans')1323return1324end1325if !nessus_verify_token1326return1327end13281329case args.length1330when 11331sid = args[0]1332else1333print_status('Usage: ')1334print_status('nessus_scan_resume <scan id>')1335print_status('Use nessus_scan_list to list all available scans')1336return1337end1338resume = @n.scan_resume(sid)1339if resume['error']1340print_error 'Invalid scan ID'1341else1342print_status("#{sid} has been resumed")1343end1344end13451346def cmd_nessus_scan_resume_all(*args)1347scan_ids = Array.new1348if args[0] == '-h'1349print_status('nessus_scan_resume_all')1350print_status('Example:> nessus_scan_resume_all')1351print_status('resumes all currently running scans')1352print_status('Use nessus_scan_list to list all running scans')1353return1354end1355if !nessus_verify_token1356return1357end13581359list = @n.scan_list1360list['scans'].each do |scan|1361if scan['status'] == 'paused'1362scan_ids << scan['id']1363end1364end1365if !scan_ids.empty?1366scan_ids.each do |scan_id|1367@n.scan_resume(scan_id)1368end1369print_status('All scans have been resumed')1370else1371print_error('No running scans to be resumed')1372end1373end13741375def cmd_nessus_scan_details(*args)1376valid_categories = ['info', 'hosts', 'vulnerabilities', 'history']1377search_term = nil1378scan_id = nil1379category = nil1380while (arg = args.shift)1381case arg1382when '-h', '--help'1383print_status('Usage: ')1384print_status('nessus_scan_details <scan ID> <category> -S searchterm')1385print_status('Availble categories are info, hosts, vulnerabilities, and history')1386print_status('Use nessus_scan_list to list all available scans with their corresponding scan IDs')1387return1388when '-S', '--search'1389search_term = /#{args.shift}/nmi1390else1391scan_id = arg1392if args[0].in?(valid_categories)1393category = args.shift1394else1395print_error('Invalid category. The available categories are info, hosts, vulnerabilities, and history')1396return1397end1398end1399end14001401if !nessus_verify_token1402return1403end14041405details = @n.scan_details(scan_id)1406if category == 'info'1407tbl = Rex::Text::Table.new(1408'SearchTerm' => search_term,1409'Columns' => [1410'Status',1411'Policy',1412'Scan Name',1413'Scan Targets',1414'Scan Start Time',1415'Scan End Time'1416]1417)1418tbl << [ details['info']['status'], details['info']['policy'], details['info']['name'], details['info']['targets'], details['info']['scan_start'], details['info']['scan_end'] ]1419elsif category == 'hosts'1420tbl = Rex::Text::Table.new(1421'SearchTerm' => search_term,1422'Columns' => [1423'Host ID',1424'Hostname',1425'% of Critical Findings',1426'% of High Findings',1427'% of Medium Findings',1428'% of Low Findings'1429]1430)1431details['hosts'].each do |host|1432tbl << [ host['host_id'], host['hostname'], host['critical'], host['high'], host['medium'], host['low'] ]1433end1434elsif category == 'vulnerabilities'1435tbl = Rex::Text::Table.new(1436'SearchTerm' => search_term,1437'Columns' => [1438'Plugin ID',1439'Plugin Name',1440'Plugin Family',1441'Count'1442]1443)1444details['vulnerabilities'].each do |vuln|1445tbl << [ vuln['plugin_id'], vuln['plugin_name'], vuln['plugin_family'], vuln['count'] ]1446end1447elsif category == 'history'1448tbl = Rex::Text::Table.new(1449'SearchTerm' => search_term,1450'Columns' => [1451'History ID',1452'Status',1453'Creation Date',1454'Last Modification Date'1455]1456)1457details['history'].each do |hist|1458tbl << [ hist['history_id'], hist['status'], hist['creation_date'], hist['modification_date'] ]1459end1460end1461print_line tbl.to_s1462end14631464def cmd_nessus_scan_export(*args)1465if args[0] == '-h'1466print_status('nessus_scan_export <scan ID> <export format>')1467print_status('The available export formats are Nessus, HTML, PDF, CSV, or DB')1468print_status('Use nessus_scan_list to list all available scans with their corresponding scan IDs')1469return1470end1471if !nessus_verify_token1472return1473end14741475case args.length1476when 21477scan_id = args[0]1478format = args[1].downcase1479else1480print_status('Usage: ')1481print_status('nessus_scan_export <scan ID> <export format>')1482print_status('The available export formats are Nessus, HTML, PDF, CSV, or DB')1483print_status('Use nessus_scan_list to list all available scans with their corresponding scan IDs')1484return1485end1486if format.in?(['nessus', 'html', 'pdf', 'csv', 'db'])1487export = @n.scan_export(scan_id, format)1488status = {}1489if export['file']1490file_id = export['file']1491print_good("The export file ID for scan ID #{scan_id} is #{file_id}")1492print_status('Checking export status...')1493loop do1494status = @n.scan_export_status(scan_id, file_id)1495print_status('Export status: ' + status['status'])1496if status['status'] == 'ready'1497break1498end14991500sleep(1)1501break unless (status['status'] == 'loading')1502end1503if status['status'] == 'ready'1504print_good("The status of scan ID #{scan_id} export is ready")1505else1506print_error("There was some problem in exporting the scan. The error message is #{status}")1507end1508else1509print_error(export)1510end1511else1512print_error('Invalid export format. The available export formats are Nessus, HTML, PDF, CSV, or DB')1513return1514end1515end15161517def cmd_nessus_scan_export_status(*args)1518if args[0] == '-h'1519print_status('nessus_scan_export_status <scan ID> <file ID>')1520print_status('Use nessus_scan_export <scan ID> <format> to export a scan and get its file ID')1521end1522if !nessus_verify_token1523return1524end15251526case args.length1527when 21528scan_id = args[0]1529file_id = args[1]1530status = {}1531loop do1532status = @n.scan_export_status(scan_id, file_id)1533print_status('Export status: ' + status['status'])1534if status['status'] == 'ready'1535break1536end15371538sleep(1)1539break unless (status['status'] == 'loading')1540end1541if status['status'] == 'ready'1542print_status("The status of scan ID #{scan_id} export is ready")1543else1544print_error("There was some problem in exporting the scan. The error message is #{status}")1545end1546else1547print_status('Usage: ')1548print_status('nessus_scan_export_status <scan ID> <file ID>')1549print_status('Use nessus_scan_export <scan ID> <format> to export a scan and get its file ID')1550end1551end15521553def cmd_nessus_plugin_list(*args)1554search_term = nil1555family_id = nil1556while (arg = args.shift)1557case arg1558when '-h', '--help'1559print_status('nessus_plugin_list <Family ID> -S searchterm')1560print_status('Example:> nessus_plugin_list 10')1561print_status('Returns a list of all plugins in that family.')1562print_status('Use nessus_family_list to display all the plugin families along with their corresponding family IDs')1563return1564when '-S', '--search'1565search_term = /#{args.shift}/nmi1566else1567family_id = arg1568end1569end15701571if family_id.nil?1572print_status('Usage: ')1573print_status('nessus_plugin_list <Family ID>')1574print_status('Use nessus_family_list to display all the plugin families along with their corresponding family IDs')1575return1576end1577tbl = Rex::Text::Table.new(1578'SearchTerm' => search_term,1579'Columns' => [1580'Plugin ID',1581'Plugin Name'1582]1583)1584list = @n.list_plugins(family_id)1585list['plugins'].each do |plugin|1586tbl << [ plugin['id'], plugin['name'] ]1587end1588print_line1589print_good("Plugin Family Name: #{list['name']}")1590print_line1591print_line tbl.to_s1592end15931594def cmd_nessus_family_list(*args)1595search_term = nil1596while (arg = args.shift)1597case arg1598when '-h', '--help'1599print_status('nessus_family_list')1600print_status('Example:> nessus_family_list -S searchterm')1601print_status('Returns a list of all the plugin families along with their corresponding family IDs and plugin count.')1602return1603when '-S', '--search'1604search_term = /#{args.shift}/nmi1605end1606end16071608list = @n.list_families1609tbl = Rex::Text::Table.new(1610'SearchTerm' => search_term,1611'Columns' => [1612'Family ID',1613'Family Name',1614'Number of Plugins'1615]1616)1617list['families'].each do |family|1618tbl << [ family['id'], family['name'], family['count'] ]1619end1620print_line1621print_line tbl.to_s1622end16231624def cmd_nessus_plugin_details(*args)1625search_term = nil1626plugin_id = nil1627while (arg = args.shift)1628case arg1629when '-h', '--help'1630print_status('nessus_plugin_details <Plugin ID>')1631print_status('Example:> nessus_plugin_details 10264 -S searchterm')1632print_status('Returns details on a particular plugin.')1633print_status('Use nessus_plugin_list to list all plugins and their corresponding plugin IDs belonging to a particular plugin family.')1634return1635when '-S', '--search'1636search_term = /#{args.shift}/nmi1637else1638plugin_id = arg1639end1640end16411642if !nessus_verify_token1643return1644end16451646if plugin_id.nil?1647print_status('Usage: ')1648print_status('nessus_plugin_details <Plugin ID>')1649print_status('Use nessus_plugin_list to list all plugins and their corresponding plugin IDs belonging to a particular plugin family.')1650return1651end1652tbl = Rex::Text::Table.new(1653'SearchTerm' => search_term,1654'Columns' => [1655'Reference',1656'Value'1657]1658)1659begin1660list = @n.plugin_details(plugin_id)1661rescue ::Exception => e1662if e.message =~ /unexpected token/1663print_error('No plugin info found')1664return1665else1666raise e1667end1668end1669list['attributes'].each do |attrib|1670tbl << [ attrib['attribute_name'], attrib['attribute_value'] ]1671end1672print_line1673print_good("Plugin Name: #{list['name']}")1674print_good("Plugin Family: #{list['family_name']}")1675print_line1676print_line tbl.to_s1677end16781679def cmd_nessus_user_list(*args)1680scan_id = nil1681while (arg = args.shift)1682case arg1683when '-h', '--help'1684print_status('nessus_user_list')1685print_status('Example:> nessus_user_list -S searchterm')1686print_status('Returns a list of the users on the Nessus server and their access level.')1687return1688when '-S', '--search'1689search_term = /#{args.shift}/nmi1690end1691end16921693if !nessus_verify_token1694return1695end16961697if !@n.is_admin1698print_status('Your Nessus user is not an admin')1699end1700list = @n.list_users1701tbl = Rex::Text::Table.new(1702'SearchTerm' => search_term,1703'Columns' => [1704'ID',1705'Name',1706'Username',1707'Type',1708'Email',1709'Permissions'1710]1711)1712list['users'].each do |user|1713tbl << [ user['id'], user['name'], user['username'], user['type'], user['email'], user['permissions'] ]1714end1715print_line1716print_line tbl.to_s1717end17181719def cmd_nessus_user_add(*args)1720if args[0] == '-h'1721print_status('nessus_user_add <username> <password> <permissions> <type>')1722print_status('Permissions are 32, 64, and 128')1723print_status('Type can be either local or LDAP')1724print_status('Example:> nessus_user_add msf msf 16 local')1725print_status('You need to be an admin in order to add accounts')1726print_status('Use nessus_user_list to list all users')1727return1728end1729if !nessus_verify_token1730return1731end17321733if !@n.is_admin1734print_error('Your Nessus user is not an admin')1735return1736end1737case args.length1738when 41739user = args[0]1740pass = args[1]1741permissions = args[2]1742type = args[3]1743else1744print_status('Usage')1745print_status('nessus_user_add <username> <password> <permissions> <type>')1746return1747end1748add = @n.user_add(user, pass, permissions, type)1749if add['id']1750print_good("#{user} created successfully")1751else1752print_error(add.to_s)1753end1754end17551756def cmd_nessus_user_del(*args)1757if args[0] == '-h'1758print_status('nessus_user_del <User ID>')1759print_status('Example:> nessus_user_del 10')1760print_status('This command can only delete non admin users. You must be an admin to delete users.')1761print_status('Use nessus_user_list to list all users with their corresponding user IDs')1762return1763end1764if !nessus_verify_token1765return1766end17671768if !@n.is_admin1769print_error('Your Nessus user is not an admin')1770return1771end1772case args.length1773when 11774user_id = args[0]1775else1776print_status('Usage: ')1777print_status('nessus_user_del <User ID>')1778print_status('This command can only delete non admin users')1779return1780end1781del = @n.user_delete(user_id)1782status = del.to_s1783if status == '200'1784print_good("User account having user ID #{user_id} deleted successfully")1785elsif status == '403'1786print_error("You do not have permission to delete the user account having user ID #{user_id}")1787elsif status == '404'1788print_error("User account having user ID #{user_id} does not exist")1789elsif status == '409'1790print_error('You cannot delete your own account')1791elsif status == '500'1792print_error("The server failed to delete the user account having user ID #{user_id}")1793else1794print_error("Unknown problem occurred by deleting the user account having user ID #{user_id}.")1795end1796end17971798def cmd_nessus_user_passwd(*args)1799if args[0] == '-h'1800print_status('nessus_user_passwd <User ID> <New Password>')1801print_status('Example:> nessus_user_passwd 10 mynewpassword')1802print_status('Changes the password of a user. You must be an admin to change passwords.')1803print_status('Use nessus_user_list to list all users with their corresponding user IDs')1804return1805end1806if !nessus_verify_token1807return1808end18091810if !@n.is_admin1811print_error('Your Nessus user is not an admin')1812return1813end1814case args.length1815when 21816user_id = args[0]1817pass = args[1]1818else1819print_status('Usage: ')1820print_status('nessus_user_passwd <User ID> <New Password>')1821print_status('Use nessus_user_list to list all users with their corresponding user IDs')1822return1823end1824pass = @n.user_chpasswd(user_id, pass)1825status = pass.to_s1826if status == '200'1827print_good("Password of account having user ID #{user_id} changed successfully")1828elsif status == '400'1829print_error('Password is too short')1830elsif status == '403'1831print_error("You do not have the permission to change password for the user having user ID #{user_id}")1832elsif status == '404'1833print_error("User having user ID #{user_id} does not exist")1834elsif status == '500'1835print_error('Nessus server failed to changed the user password')1836else1837print_error('Unknown problem occurred while changing the user password')1838end1839end18401841def cmd_nessus_policy_list(*args)1842search_term = nil1843while (arg = args.shift)1844case arg1845when '-h', '--help'1846print_status('nessus_policy_list')1847print_status('Example:> nessus_policy_list -S searchterm')1848print_status('Lists all policies on the server')1849return1850when '-S', '--search'1851search_term = /#{args.shift}/nmi1852end1853end18541855if !nessus_verify_token1856return1857end18581859list = @n.list_policies18601861unless list['policies']1862print_error('No policies found')1863return1864end18651866tbl = Rex::Text::Table.new(1867'Columns' => [1868'Policy ID',1869'Name',1870'Policy UUID'1871]1872)1873list['policies'].each do |policy|1874tbl << [ policy['id'], policy['name'], policy['template_uuid'] ]1875end1876print_line tbl.to_s1877end18781879def cmd_nessus_policy_del(*args)1880if args[0] == '-h'1881print_status('nessus_policy_del <policy ID>')1882print_status('Example:> nessus_policy_del 1')1883print_status('You must be an admin to delete policies.')1884print_status('Use nessus_policy_list to list all policies with their corresponding policy IDs')1885return1886end1887if !nessus_verify_token1888return1889end18901891if !@n.is_admin1892print_error('Your Nessus user is not an admin')1893return1894end1895case args.length1896when 11897policy_id = args[0]1898else1899print_status('Usage: ')1900print_status('nessus_policy_del <policy ID>')1901print_status('Use nessus_policy_list to list all the policies with their corresponding policy IDs')1902return1903end1904del = @n.policy_delete(policy_id)1905status = del.to_s1906if status == '200'1907print_good("Policy ID #{policy_id} successfully deleted")1908elsif status == '403'1909print_error("You do not have permission to delete policy ID #{policy_id}")1910elsif status == '404'1911print_error("Policy ID #{policy_id} does not exist")1912elsif status == '405'1913print_error("Policy ID #{policy_id} is currently in use and cannot be deleted")1914else1915print_error("Unknown problem occurred by deleting the user account having user ID #{user_id}.")1916end1917end1918end19191920def initialize(framework, opts)1921super1922add_console_dispatcher(ConsoleCommandDispatcher)1923print_status(PLUGIN_DESCRIPTION)1924print_status('Type %bldnessus_help%clr for a command listing')1925end19261927def cleanup1928remove_console_dispatcher('Nessus')1929end1930end1931end193219331934