nessus.rb 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933
  1. # $Id$ $Revision$
  2. require 'nessus_rest'
  3. module Msf
  4. PLUGIN_NAME = 'Nessus'.freeze
  5. PLUGIN_DESCRIPTION = 'Nessus Bridge for Metasploit'.freeze
  6. class Plugin::Nessus < Msf::Plugin
  7. def name
  8. PLUGIN_NAME
  9. end
  10. def desc
  11. PLUGIN_DESCRIPTION
  12. end
  13. class ConsoleCommandDispatcher
  14. include Msf::Ui::Console::CommandDispatcher
  15. def name
  16. PLUGIN_NAME
  17. end
  18. def xindex
  19. "#{Msf::Config.config_directory}/nessus_index"
  20. end
  21. def nessus_yaml
  22. "#{Msf::Config.config_directory}/nessus.yaml"
  23. end
  24. def msf_local
  25. Msf::Config.local_directory.to_s
  26. end
  27. def commands
  28. {
  29. 'nessus_connect' => 'Connect to a nessus server: nconnect username:password@hostname:port <verify_ssl>',
  30. 'nessus_admin' => 'Checks if user is an admin',
  31. 'nessus_help' => 'Get help on all commands',
  32. 'nessus_logout' => 'Terminate the session',
  33. 'nessus_server_status' => 'Check the status of your Nessus server',
  34. 'nessus_server_properties' => 'Nessus server properties such as feed type, version, plugin set and server UUID',
  35. 'nessus_report_download' => 'Download a report from the nessus server in either Nessus, HTML, PDF, CSV, or DB format',
  36. 'nessus_report_vulns' => 'Get list of vulns from a report',
  37. 'nessus_report_hosts' => 'Get list of hosts from a report',
  38. 'nessus_report_host_details' => 'Get detailed information from a report item on a host',
  39. 'nessus_scan_list' => 'List of currently running Nessus scans',
  40. 'nessus_scan_new' => 'Create a new Nessus scan',
  41. 'nessus_scan_launch' => 'Launch a previously added scan',
  42. 'nessus_scan_pause' => 'Pause a running Nessus scan',
  43. 'nessus_scan_pause_all' => 'Pause all running Nessus scans',
  44. 'nessus_scan_stop' => 'Stop a running or paused Nessus scan',
  45. 'nessus_scan_stop_all' => 'Stop all running or paused Nessus scans',
  46. 'nessus_scan_resume' => 'Resume a paused Nessus scan',
  47. 'nessus_scan_resume_all' => 'Resume all paused Nessus scans',
  48. 'nessus_scan_details' => 'Return detailed information of a given scan',
  49. 'nessus_scan_export' => 'Export a scan result in either Nessus, HTML, PDF, CSV, or DB format',
  50. 'nessus_scan_export_status' => 'Check the status of scan export',
  51. 'nessus_user_list' => 'List of Nessus users',
  52. 'nessus_user_add' => 'Add a new Nessus user',
  53. 'nessus_user_del' => 'Delete a Nessus user',
  54. 'nessus_user_passwd' => 'Change Nessus Users Password',
  55. 'nessus_plugin_details' => 'List details of a particular plugin',
  56. 'nessus_plugin_list' => 'Display plugin details in a particular plugin family',
  57. 'nessus_policy_list' => 'List all polciies',
  58. 'nessus_policy_del' => 'Delete a policy',
  59. 'nessus_index' => 'Manually generates a search index for exploits',
  60. 'nessus_template_list' => 'List all the templates on the server',
  61. 'nessus_db_scan' => 'Create a scan of all IP addresses in db_hosts',
  62. 'nessus_db_scan_workspace' => 'Create a scan of all IP addresses in db_hosts for a given workspace',
  63. 'nessus_db_import' => 'Import Nessus scan to the Metasploit connected database',
  64. 'nessus_save' => 'Save credentials of the logged in user to nessus.yml',
  65. 'nessus_folder_list' => 'List folders configured on the Nessus server',
  66. 'nessus_scanner_list' => 'List the configured scanners on the Nessus server',
  67. 'nessus_family_list' => 'List all the plugin families along with their corresponding family IDs and plugin count'
  68. }
  69. end
  70. def ncusage
  71. print_status('%redYou must do this before any other commands.%clr')
  72. print_status('Usage: ')
  73. print_status('nessus_connect username:password@hostname:port <ssl_verify>')
  74. print_status('Example:> nessus_connect msf:msf@192.168.1.10:8834')
  75. print_status('OR')
  76. print_status('nessus_connect username@hostname:port ssl_verify')
  77. print_status('Example:> nessus_connect msf@192.168.1.10:8834 ssl_verify')
  78. print_status('OR')
  79. print_status('nessus_connect hostname:port ssl_verify')
  80. print_status('Example:> nessus_connect 192.168.1.10:8834 ssl_verify')
  81. print_status('OR')
  82. print_status('nessus_connect')
  83. print_status('Example:> nessus_connect')
  84. print_status('This only works after you have saved creds with nessus_save')
  85. return
  86. end
  87. # creates the index of exploit details to make searching for exploits much faster.
  88. def create_xindex
  89. start = Time.now
  90. print_status("Creating Exploit Search Index - (#{xindex}) - this won't take long.")
  91. # Use Msf::Config.config_directory as the location.
  92. File.open(xindex.to_s, 'w+') do |f|
  93. # need to add version line.
  94. f.puts(Msf::Framework::Version)
  95. framework.exploits.each_module do |refname, mod|
  96. stuff = ''
  97. o = nil
  98. begin
  99. o = mod.new
  100. rescue ::Exception
  101. end
  102. stuff << "#{refname}|#{o.name}|#{o.platform_to_s}|#{o.arch_to_s}"
  103. next if !o
  104. o.references.map do |x|
  105. if x.ctx_id != 'URL'
  106. if (x.ctx_id == 'MSB')
  107. stuff << "|#{x.ctx_val}"
  108. else
  109. stuff << "|#{x.ctx_id}-#{x.ctx_val}"
  110. end
  111. end
  112. end
  113. stuff << "\n"
  114. f.puts(stuff)
  115. end
  116. end
  117. total = Time.now - start
  118. print_status("It has taken : #{total} seconds to build the exploits search index")
  119. end
  120. def nessus_index
  121. if File.exist?(xindex.to_s)
  122. # check if it's version line matches current version.
  123. File.open(xindex.to_s) do |f|
  124. line = f.readline
  125. line.chomp!
  126. if line.to_i == Msf::Framework::RepoRevision
  127. print_good("Exploit Index - (#{xindex}) - is valid.")
  128. else
  129. create_xindex
  130. end
  131. end
  132. else
  133. create_xindex
  134. end
  135. end
  136. def nessus_login
  137. if !((@user && !@user.empty?) && (@host && !@host.empty?) && (@port && !@port.empty? && (@port.to_i > 0)) && (@pass && !@pass.empty?))
  138. print_status('You need to connect to a server first.')
  139. ncusage
  140. return
  141. end
  142. @url = "https://#{@host}:#{@port}/"
  143. print_status("Connecting to #{@url} as #{@user}")
  144. verify_ssl = false
  145. if @sslv == 'verify_ssl'
  146. verify_ssl = true
  147. end
  148. @n = NessusREST::Client.new(url: @url, username: @user, password: @pass, ssl_verify: verify_ssl)
  149. if @n.authenticated
  150. print_status("User #{@user} authenticated successfully.")
  151. @token = 1
  152. else
  153. print_error('Error connecting/logging to the server!')
  154. return
  155. end
  156. end
  157. def nessus_verify_token
  158. if @token.nil? || (@token == '')
  159. ncusage
  160. return false
  161. end
  162. true
  163. end
  164. def valid_policy(*args)
  165. case args.length
  166. when 1
  167. pid = args[0]
  168. else
  169. print_error('No Policy ID supplied.')
  170. return
  171. end
  172. pol = @n.list_policies
  173. pol['policies'].each do |p|
  174. if p['template_uuid'] == pid
  175. return true
  176. end
  177. end
  178. return false
  179. end
  180. def nessus_verify_db
  181. if !(framework.db && framework.db.active)
  182. print_error('No database has been configured, please use db_connect first')
  183. return false
  184. end
  185. true
  186. end
  187. def check_scan(*args)
  188. case args.length
  189. when 1
  190. scan_id = args[0]
  191. else
  192. print_error('No scan ID supplied')
  193. return
  194. end
  195. scans = @n.scan_list
  196. scans.each do |scan|
  197. if scan['scans']['id'] == scan_id && scan['scans']['status'] == 'completed'
  198. return true
  199. end
  200. end
  201. return false
  202. end
  203. def is_scan_complete(scan_id)
  204. complete = false
  205. status = @n.scan_list
  206. status['scans'].each do |scan|
  207. if scan['id'] == scan_id.to_i && (scan['status'] == 'completed' || scan['status'] == 'imported')
  208. complete = true
  209. end
  210. end
  211. complete
  212. end
  213. def cmd_nessus_help(*_args)
  214. tbl = Rex::Text::Table.new(
  215. 'Columns' => [
  216. 'Command',
  217. 'Help Text'
  218. ],
  219. 'SortIndex' => -1
  220. )
  221. tbl << [ 'Generic Commands', '' ]
  222. tbl << [ '-----------------', '-----------------']
  223. tbl << [ 'nessus_connect', 'Connect to a Nessus server' ]
  224. tbl << [ 'nessus_logout', 'Logout from the Nessus server' ]
  225. tbl << [ 'nessus_login', 'Login into the connected Nesssus server with a different username and password']
  226. tbl << [ 'nessus_save', 'Save credentials of the logged in user to nessus.yml']
  227. tbl << [ 'nessus_help', 'Listing of available nessus commands' ]
  228. tbl << [ 'nessus_server_properties', 'Nessus server properties such as feed type, version, plugin set and server UUID.' ]
  229. tbl << [ 'nessus_server_status', 'Check the status of your Nessus Server' ]
  230. tbl << [ 'nessus_admin', 'Checks if user is an admin' ]
  231. tbl << [ 'nessus_template_list', 'List scan or policy templates' ]
  232. tbl << [ 'nessus_folder_list', 'List all configured folders on the Nessus server' ]
  233. tbl << [ 'nessus_scanner_list', 'List all the scanners configured on the Nessus server' ]
  234. tbl << [ 'Nessus Database Commands', '' ]
  235. tbl << [ '-----------------', '-----------------' ]
  236. tbl << [ 'nessus_db_scan', 'Create a scan of all IP addresses in db_hosts' ]
  237. tbl << [ 'nessus_db_scan_workspace', 'Create a scan of all IP addresses in db_hosts for a given workspace' ]
  238. tbl << [ 'nessus_db_import', 'Import Nessus scan to the Metasploit connected database' ]
  239. tbl << [ '', '']
  240. tbl << [ 'Reports Commands', '' ]
  241. tbl << [ '-----------------', '-----------------']
  242. tbl << [ 'nessus_report_hosts', 'Get list of hosts from a report' ]
  243. tbl << [ 'nessus_report_vulns', 'Get list of vulns from a report' ]
  244. tbl << [ 'nessus_report_host_details', 'Get detailed information from a report item on a host' ]
  245. tbl << [ '', '']
  246. tbl << [ 'Scan Commands', '' ]
  247. tbl << [ '-----------------', '-----------------']
  248. tbl << [ 'nessus_scan_list', 'List of all current Nessus scans' ]
  249. tbl << [ 'nessus_scan_new', 'Create a new Nessus Scan' ]
  250. tbl << [ 'nessus_scan_launch', 'Launch a newly created scan. New scans need to be manually launched through this command' ]
  251. tbl << [ 'nessus_scan_pause', 'Pause a running Nessus scan' ]
  252. tbl << [ 'nessus_scan_pause_all', 'Pause all running Nessus scans' ]
  253. tbl << [ 'nessus_scan_stop', 'Stop a running or paused Nessus scan' ]
  254. tbl << [ 'nessus_scan_stop_all', 'Stop all running or paused Nessus scans' ]
  255. tbl << [ 'nessus_scan_resume', 'Resume a pasued Nessus scan' ]
  256. tbl << [ 'nessus_scan_resume_all', 'Resume all paused Nessus scans' ]
  257. tbl << [ 'nessus_scan_details', 'Return detailed information of a given scan' ]
  258. tbl << [ 'nessus_scan_export', 'Export a scan result in either Nessus, HTML, PDF, CSV, or DB format' ]
  259. tbl << [ 'nessus_scan_export_status', 'Check the status of an exported scan' ]
  260. tbl << [ '', '']
  261. tbl << [ 'Plugin Commands', '' ]
  262. tbl << [ '-----------------', '-----------------']
  263. tbl << [ 'nessus_plugin_list', 'List all plugins in a particular plugin family.' ]
  264. tbl << [ 'nessus_family_list', 'List all the plugin families along with their corresponding family IDs and plugin count.' ]
  265. tbl << [ 'nessus_plugin_details', 'List details of a particular plugin' ]
  266. tbl << [ '', '']
  267. tbl << [ 'User Commands', '' ]
  268. tbl << [ '-----------------', '-----------------']
  269. tbl << [ 'nessus_user_list', 'Show Nessus Users' ]
  270. tbl << [ 'nessus_user_add', 'Add a new Nessus User' ]
  271. tbl << [ 'nessus_user_del', 'Delete a Nessus User' ]
  272. tbl << [ 'nessus_user_passwd', 'Change Nessus Users Password' ]
  273. tbl << [ '', '']
  274. tbl << [ 'Policy Commands', '' ]
  275. tbl << [ '-----------------', '-----------------']
  276. tbl << [ 'nessus_policy_list', 'List all polciies' ]
  277. tbl << [ 'nessus_policy_del', 'Delete a policy' ]
  278. print_line ''
  279. print_line tbl.to_s
  280. print_line ''
  281. end
  282. def cmd_nessus_index
  283. nessus_index
  284. end
  285. def cmd_nessus_connect(*args)
  286. # Check if config file exists and load it
  287. if !args[0]
  288. if File.exist?(nessus_yaml)
  289. lconfig = YAML.load_file(nessus_yaml)
  290. @user = lconfig['default']['username'].to_s
  291. @pass = lconfig['default']['password'].to_s
  292. @host = lconfig['default']['server'].to_s
  293. @port = lconfig['default']['port'].to_s
  294. nessus_login
  295. else
  296. ncusage
  297. end
  298. return
  299. end
  300. if args[0] == '-h'
  301. print_status('%redYou must do this before any other commands.%clr')
  302. print_status('Usage: ')
  303. print_status('nessus_connect username:password@hostname:port <ssl_verify/ssl_ignore>')
  304. print_status('%bldusername%clr and %bldpassword%clr are the ones you use to login to the nessus web front end')
  305. print_status('%bldhostname%clr can be an IP address or a DNS name of the Nessus server.')
  306. print_status('%bldport%clr is the RPC port that the Nessus web front end runs on. By default it is TCP port 8834.')
  307. print_status('The "ssl_verify" to verify the SSL certificate used by the Nessus front end. By default the server')
  308. print_status('use a self signed certificate, therefore, users should use ssl_ignore.')
  309. return
  310. end
  311. if !@token == ''
  312. print_error('You are already authenticated. Call nessus_logout before authenticating again')
  313. return
  314. end
  315. if (args.empty? || args[0].empty?)
  316. ncusage
  317. return
  318. end
  319. @user = @pass = @host = @port = @sslv = nil
  320. case args.length
  321. when 1, 2
  322. if args[0].include? '@'
  323. cred, _split, targ = args[0].rpartition('@')
  324. @user, @pass = cred.split(':', 2)
  325. targ ||= '127.0.0.1:8834'
  326. @host, @port = targ.split(':', 2)
  327. else
  328. @host, @port = args[0].split(':', 2)
  329. end
  330. @port ||= '8834'
  331. @sslv = args[1]
  332. when 3, 4, 5
  333. ncusage
  334. return
  335. else
  336. ncusage
  337. return
  338. end
  339. if %r{//}.match(@host)
  340. ncusage
  341. return
  342. end
  343. if !@user
  344. print_error('Missing Username')
  345. ncusage
  346. return
  347. end
  348. if !@pass
  349. print_error('Missing Password')
  350. ncusage
  351. return
  352. end
  353. if !((@user && !@user.empty?) && (@host && !@host.empty?) && (@port && !@port.empty? && (@port.to_i > 0)) && (@pass && !@pass.empty?))
  354. ncusage
  355. return
  356. end
  357. nessus_login
  358. end
  359. def cmd_nessus_logout
  360. logout = @n.user_logout
  361. status = logout.to_s
  362. if status == '200'
  363. print_good('User account logged out successfully')
  364. @token = ''
  365. elsif status == '403'
  366. print_status('No user session to logout')
  367. else
  368. print_error("There was some problem in logging out the user #{@user}")
  369. end
  370. return
  371. end
  372. def cmd_nessus_save(*args)
  373. # if we are logged in, save session details to nessus.yaml
  374. if args[0] == '-h'
  375. print_status(' nessus_save')
  376. return
  377. end
  378. if args[0]
  379. print_status('Usage: ')
  380. print_status('nessus_save')
  381. return
  382. end
  383. group = 'default'
  384. if ((@user && !@user.empty?) && (@host && !@host.empty?) && (@port && !@port.empty? && (@port.to_i > 0)) && (@pass && !@pass.empty?))
  385. config = Hash.new
  386. config = { group.to_s => { 'username' => @user, 'password' => @pass, 'server' => @host, 'port' => @port } }
  387. File.open(nessus_yaml.to_s, 'w+') do |f|
  388. f.puts YAML.dump(config)
  389. end
  390. print_good("#{nessus_yaml} created.")
  391. else
  392. print_error('Missing username/password/server/port - relogin and then try again.')
  393. return
  394. end
  395. end
  396. def cmd_nessus_server_properties(*args)
  397. search_term = nil
  398. while (arg = args.shift)
  399. case arg
  400. when '-h', '--help'
  401. print_status('nessus_server_properties')
  402. print_status('Example:> nessus_server_properties -S searchterm')
  403. print_status('Returns information about the feed type and server version.')
  404. return
  405. when '-S', '--search'
  406. search_term = /#{args.shift}/nmi
  407. end
  408. end
  409. resp = @n.server_properties
  410. tbl = Rex::Text::Table.new(
  411. 'SearchTerm' => search_term,
  412. 'Columns' => [
  413. 'Feed',
  414. 'Type',
  415. 'Nessus Version',
  416. 'Nessus Web Version',
  417. 'Plugin Set',
  418. 'Server UUID'
  419. ]
  420. )
  421. tbl << [ resp['feed'], resp['nessus_type'], resp['server_version'], resp['nessus_ui_version'], resp['loaded_plugin_set'], resp['server_uuid'] ]
  422. print_line tbl.to_s
  423. end
  424. def cmd_nessus_server_status(*args)
  425. search_term = nil
  426. while (arg = args.shift)
  427. case arg
  428. when '-h', '--help'
  429. print_status('nessus_server_status')
  430. print_status('Example:> nessus_server_status -S searchterm')
  431. print_status('Returns some status items for the server..')
  432. return
  433. when '-S', '--search'
  434. search_term = /#{args.shift}/nmi
  435. end
  436. end
  437. tbl = Rex::Text::Table.new(
  438. 'SearchTerm' => search_term,
  439. 'Columns' => [
  440. 'Status',
  441. 'Progress'
  442. ]
  443. )
  444. list = @n.server_status
  445. tbl << [ list['progress'], list['status'] ]
  446. print_line tbl.to_s
  447. end
  448. def cmd_nessus_admin(*args)
  449. while (arg = args.shift)
  450. case arg
  451. when '-h', '--help'
  452. print_status('nessus_admin')
  453. print_status('Example:> nessus_admin')
  454. print_status('Checks to see if the current user is an admin')
  455. print_status('Use nessus_user_list to list all users')
  456. return
  457. end
  458. end
  459. if !nessus_verify_token
  460. return
  461. end
  462. if !@n.is_admin
  463. print_error('Your Nessus user is not an admin')
  464. else
  465. print_good('Your Nessus user is an admin')
  466. end
  467. end
  468. def cmd_nessus_template_list(*args)
  469. search_term = nil
  470. while (arg = args.shift)
  471. case arg
  472. when '-h', '--help'
  473. print_status('nessus_template_list <scan> | <policy>')
  474. print_status('Example:> nessus_template_list scan -S searchterm')
  475. print_status('OR')
  476. print_status('nessus_template_list policy')
  477. print_status('Returns a list of information about the scan or policy templates..')
  478. return
  479. when '-S', '--search'
  480. search_term = /#{args.shift}/nmi
  481. else
  482. type = arg
  483. end
  484. end
  485. if !nessus_verify_token
  486. return
  487. end
  488. if type.in?(['scan', 'policy'])
  489. list = @n.list_templates(type)
  490. else
  491. print_error('Only scan and policy are valid templates')
  492. return
  493. end
  494. if list.empty?
  495. print_status('No templates created')
  496. return
  497. end
  498. tbl = Rex::Text::Table.new(
  499. 'SearchTerm' => search_term,
  500. 'Columns' => [
  501. 'Name',
  502. 'Title',
  503. 'Description',
  504. 'Subscription Only',
  505. 'Cloud Only'
  506. ]
  507. )
  508. list['templates'].each do |template|
  509. tbl << [ template['name'], template['title'], template['desc'], template['subscription_only'], template['cloud_only'] ]
  510. end
  511. print_line
  512. print_line tbl.to_s
  513. end
  514. def cmd_nessus_folder_list(*args)
  515. search_term = nil
  516. while (arg = args.shift)
  517. case arg
  518. when '-S', '--search'
  519. search_term = /#{args.shift}/nmi
  520. end
  521. end
  522. if !nessus_verify_token
  523. return
  524. end
  525. list = @n.list_folders
  526. tbl = Rex::Text::Table.new(
  527. 'SearchTerm' => search_term,
  528. 'Columns' => [
  529. 'ID',
  530. 'Name',
  531. 'Type'
  532. ]
  533. )
  534. list['folders'].each do |folder|
  535. tbl << [ folder['id'], folder['name'], folder['type'] ]
  536. end
  537. print_line
  538. print_line tbl.to_s
  539. end
  540. def cmd_nessus_scanner_list(*args)
  541. search_term = nil
  542. while (arg = args.shift)
  543. case arg
  544. when '-h', '--help'
  545. print_status('nessus_scanner_list')
  546. print_status('Example:> nessus_scanner_list -S searchterm')
  547. print_status('Returns information about the feed type and server version.')
  548. return
  549. when '-S', '--search'
  550. search_term = /#{args.shift}/nmi
  551. end
  552. end
  553. if !nessus_verify_token
  554. return
  555. end
  556. if !@n.is_admin
  557. return
  558. end
  559. list = @n.list_scanners
  560. tbl = Rex::Text::Table.new(
  561. 'SearchTerm' => search_term,
  562. 'Columns' => [
  563. 'ID',
  564. 'Name',
  565. 'Status',
  566. 'Platform',
  567. 'Plugin Set',
  568. 'UUID'
  569. ]
  570. )
  571. list.each do |scanner|
  572. tbl << [ scanner['id'], scanner['name'], scanner['status'], scanner['platform'], scanner['loaded_plugin_set'], scanner['uuid'] ]
  573. end
  574. print_line tbl.to_s
  575. end
  576. def cmd_nessus_report_hosts(*args)
  577. search_term = nil
  578. scan_id = nil
  579. while (arg = args.shift)
  580. case arg
  581. when '-h', '--help'
  582. print_status('nessus_report_hosts <scan ID> -S searchterm')
  583. print_status('Use nessus_scan_list to get a list of all the scans. Only completed scans can be reported.')
  584. return
  585. when '-S', '--search'
  586. search_term = /#{args.shift}/nmi
  587. else
  588. scan_id = arg
  589. end
  590. end
  591. if scan_id.nil?
  592. print_status('Usage: ')
  593. print_status('nessus_report_hosts <scan ID> -S searchterm')
  594. print_status('Use nessus_scan_list to get a list of all the scans. Only completed scans can be reported.')
  595. return
  596. end
  597. tbl = Rex::Text::Table.new(
  598. 'SearchTerm' => search_term,
  599. 'Columns' => [
  600. 'Host ID',
  601. 'Hostname',
  602. '% of Critical Findings',
  603. '% of High Findings',
  604. '% of Medium Findings',
  605. '% of Low Findings'
  606. ]
  607. )
  608. if is_scan_complete(scan_id)
  609. details = @n.scan_details(scan_id)
  610. details['hosts'].each do |host|
  611. tbl << [ host['host_id'], host['hostname'], host['critical'], host['high'], host['medium'], host['low'] ]
  612. end
  613. print_line
  614. print_line tbl.to_s
  615. else
  616. print_error('Only completed scans can be used for host reporting')
  617. return
  618. end
  619. end
  620. def cmd_nessus_report_vulns(*args)
  621. search_term = nil
  622. scan_id = nil
  623. while (arg = args.shift)
  624. case arg
  625. when '-h', '--help'
  626. print_status('nessus_report_vulns <scan ID> -S searchterm')
  627. print_status('Use nessus_scan_list to get a list of all the scans. Only completed scans can be reported.')
  628. return
  629. when '-S', '--search'
  630. search_term = /#{args.shift}/nmi
  631. else
  632. scan_id = arg
  633. end
  634. end
  635. if scan_id.nil?
  636. print_status('Usage: ')
  637. print_status('nessus_report_vulns <scan ID>')
  638. print_status('Use nessus_scan_list to get a list of all the scans. Only completed scans can be reported.')
  639. return
  640. end
  641. tbl = Rex::Text::Table.new(
  642. 'SearchTerm' => search_term,
  643. 'Columns' => [
  644. 'Plugin ID',
  645. 'Plugin Name',
  646. 'Plugin Family',
  647. 'Vulnerability Count'
  648. ]
  649. )
  650. if is_scan_complete(scan_id)
  651. details = @n.scan_details(scan_id)
  652. details['vulnerabilities'].each do |vuln|
  653. tbl << [ vuln['plugin_id'], vuln['plugin_name'], vuln['plugin_family'], vuln['count'] ]
  654. end
  655. print_line
  656. print_line tbl.to_s
  657. else
  658. print_error('Only completed scans can be used for vulnerability reporting')
  659. end
  660. return
  661. end
  662. def cmd_nessus_report_host_details(*args)
  663. search_term = nil
  664. search_vuln = nil
  665. scan_id = nil
  666. host_id = nil
  667. while (arg = args.shift)
  668. case arg
  669. when '-h', '--help'
  670. print_status('nessus_report_host_details <scan ID> <host ID>')
  671. print_status('Example:> nessus_report_host_details 10 5 -S hostinfo -SV vulninfo')
  672. print_status('Use nessus_scan_list to get list of all scans. Only completed scans can be used for reporting.')
  673. print_status('Use nessus_report_hosts to get a list of all the hosts along with their corresponding host IDs.')
  674. return
  675. when '-S', '--search'
  676. search_term = /#{args.shift}/nmi
  677. when '-SV', '--search-vuln'
  678. search_vuln = /#{args.shift}/nmi
  679. else
  680. scan_id =
  681. arg,
  682. host_id = args.shift
  683. end
  684. end
  685. if [scan_id, host_id].any?(&:nil?)
  686. print_status('Usage: ')
  687. print_status('nessus_report_host_detail <scan ID> <host ID>')
  688. print_status('Example:> nessus_report_host_detail 10 5')
  689. print_status('Use nessus_scan_list to get list of all scans. Only completed scans can be used for reporting.')
  690. print_status('Use nessus_report_hosts <scan ID> to get a list of all the hosts along with their corresponding host IDs.')
  691. return
  692. end
  693. tbl = Rex::Text::Table.new(
  694. 'SearchTerm' => search_term,
  695. 'Columns' => [
  696. 'Plugin Name',
  697. 'Plugin Famil',
  698. 'Severity'
  699. ]
  700. )
  701. details = @n.host_detail(scan_id, host_id)
  702. print_line
  703. print_status('Host information')
  704. print_line("IP Address: #{details['info']['host-ip']}")
  705. print_line("Hostname: #{details['info']['host-name']}")
  706. print_line("Operating System: #{details['info']['operating-system']}")
  707. print_line
  708. print_status('Vulnerability information')
  709. details['vulnerabilities'].each do |vuln|
  710. tbl << [ vuln['plugin_name'], vuln['plugin_family'], vuln['severity'] ]
  711. end
  712. print_line tbl.to_s
  713. tbl2 = Rex::Text::Table.new(
  714. 'SearchTerm' => search_vuln,
  715. 'Columns' => [
  716. 'Plugin Name',
  717. 'Plugin Famil',
  718. 'Severity'
  719. ]
  720. )
  721. print_status('Compliance information')
  722. details['compliance'].each do |comp|
  723. tbl2 << [ comp['plugin_name'], comp['plugin_family'], comp['severity'] ]
  724. end
  725. print_line tbl2.to_s
  726. end
  727. def cmd_nessus_report_download(*args)
  728. if args[0] == '-h'
  729. print_status('nessus_scan_report_download <scan_id> <file ID> ')
  730. print_status('Use nessus_scan_export_status <scan ID> <file ID> to check the export status.')
  731. print_status('Use nessus_scan_list -c to list all completed scans along with their corresponding scan IDs')
  732. return
  733. end
  734. if !nessus_verify_token
  735. return
  736. end
  737. case args.length
  738. when 2
  739. scan_id = args[0]
  740. file_id = args[1]
  741. if is_scan_complete(scan_id)
  742. report = @n.report_download(scan_id, file_id)
  743. File.open("#{msf_local}/#{scan_id}-#{file_id}", 'w+') do |f|
  744. f.puts report
  745. print_status("Report downloaded to #{msf_local} directory")
  746. end
  747. else
  748. print_error('Only completed scans can be downloaded')
  749. end
  750. else
  751. print_status('Usage: ')
  752. print_status('nessus_scan_report_download <scan_id> <file ID> ')
  753. print_status('Use nessus_scan_export_status <scan ID> <file ID> to check the export status.')
  754. print_status('Use nessus_scan_list -c to list all completed scans along with their corresponding scan IDs')
  755. end
  756. end
  757. def cmd_nessus_report_host_ports(*args)
  758. search_term = nil
  759. rid = nil
  760. host = nil
  761. while (arg = args.shift)
  762. case arg
  763. when '-h', '--help'
  764. print_status('nessus_report_host_ports <hostname> <report id>')
  765. print_status('Example:> nessus_report_host_ports 192.168.1.250 f0eabba3-4065-7d54-5763-f191e98eb0f7f9f33db7e75a06ca -S searchterm')
  766. print_status('Returns all the ports associated with a host and details about their vulnerabilities')
  767. print_status('Use nessus_report_hosts to list all available hosts for a report')
  768. return
  769. when '-S', '--search'
  770. search_term = /#{args.shift}/nmi
  771. else
  772. scan_id = arg
  773. end
  774. end
  775. if [host, rid].any?(&:nil?)
  776. print_status('Usage: ')
  777. print_status('nessus_report_host_ports <hostname> <report id>')
  778. print_status('Use nessus_report_list to list all available reports')
  779. return
  780. end
  781. tbl = Rex::Text::Table.new(
  782. 'SearchTerm' => search_term,
  783. 'Columns' => [
  784. 'Port',
  785. 'Protocol',
  786. 'Severity',
  787. 'Service Name',
  788. 'Sev 0',
  789. 'Sev 1',
  790. 'Sev 2',
  791. 'Sev 3'
  792. ]
  793. )
  794. ports = @n.report_host_ports(rid, host)
  795. ports.each do |port|
  796. tbl << [ port['portnum'], port['protocol'], port['severity'], port['svcname'], port['sev0'], port['sev1'], port['sev2'], port['sev3'] ]
  797. end
  798. print_good('Host Info')
  799. print_good "\n"
  800. print_line tbl.to_s
  801. print_status('You can:')
  802. print_status('Get detailed scan information about a specific port: nessus_report_host_detail <hostname> <port> <protocol> <report id>')
  803. end
  804. def cmd_nessus_report_del(*args)
  805. if args[0] == '-h'
  806. print_status('nessus_report_del <reportname>')
  807. print_status('Example:> nessus_report_del f0eabba3-4065-7d54-5763-f191e98eb0f7f9f33db7e75a06ca')
  808. print_status('Must be an admin to del reports.')
  809. print_status('Use nessus_report_list to list all reports')
  810. return
  811. end
  812. if !nessus_verify_token
  813. return
  814. end
  815. if !@n.is_admin
  816. print_error('Your Nessus user is not an admin')
  817. return
  818. end
  819. case args.length
  820. when 1
  821. rid = args[0]
  822. else
  823. print_status('Usage: ')
  824. print_status('nessus_report_del <report ID>')
  825. print_status('nessus_report_list to find the id.')
  826. return
  827. end
  828. del = @n.report_del(rid)
  829. status = del.root.elements['status'].text
  830. if status == 'OK'
  831. print_good("Report #{rid} has been deleted")
  832. else
  833. print_error("Report #{rid} was not deleted")
  834. end
  835. end
  836. def cmd_nessus_scan_list(*args)
  837. search_term = nil
  838. while (arg = args.shift)
  839. case arg
  840. when '-h', '--help'
  841. print_status('nessus_scan_list')
  842. print_status('Example:> nessus_scan_list -S searchterm')
  843. print_status('Returns a list of information about currently running scans.')
  844. return
  845. when '-S', '--search'
  846. search_term = /#{args.shift}/nmi
  847. end
  848. end
  849. if !nessus_verify_token
  850. return
  851. end
  852. list = @n.scan_list
  853. if list.to_s.empty?
  854. print_status('No scans performed.')
  855. return
  856. else
  857. tbl = Rex::Text::Table.new(
  858. 'SearchTerm' => search_term,
  859. 'Columns' => [
  860. 'Scan ID',
  861. 'Name',
  862. 'Owner',
  863. 'Started',
  864. 'Status',
  865. 'Folder'
  866. ]
  867. )
  868. list['scans'].each do |scan|
  869. if args[0] == '-r'
  870. if scan['status'] == 'running'
  871. tbl << [ scan['id'], scan['name'], scan['owner'], scan['starttime'], scan['status'], scan['folder_id'] ]
  872. end
  873. elsif args[0] == '-p'
  874. if scan['status'] == 'paused'
  875. tbl << [ scan['id'], scan['name'], scan['owner'], scan['starttime'], scan['status'], scan['folder_id'] ]
  876. end
  877. elsif args[0] == '-c'
  878. if scan['status'] == 'completed'
  879. tbl << [ scan['id'], scan['name'], scan['owner'], scan['starttime'], scan['status'], scan['folder_id'] ]
  880. end
  881. elsif args[0] == '-a'
  882. if scan['status'] == 'canceled'
  883. tbl << [ scan['id'], scan['name'], scan['owner'], scan['starttime'], scan['status'], scan['folder_id'] ]
  884. end
  885. else
  886. tbl << [ scan['id'], scan['name'], scan['owner'], scan['starttime'], scan['status'], scan['folder_id'] ]
  887. end
  888. end
  889. print_line tbl.to_s
  890. end
  891. end
  892. def cmd_nessus_scan_new(*args)
  893. if args[0] == '-h'
  894. print_status('nessus_scan_new <UUID of Policy> <Scan name> <Description> <Targets>')
  895. print_status('Use nessus_policy_list to list all available policies with their corresponding UUIDs')
  896. return
  897. end
  898. if !nessus_verify_token
  899. return
  900. end
  901. case args.length
  902. when 4
  903. uuid = args[0]
  904. scan_name = args[1]
  905. description = args[2]
  906. targets = args[3]
  907. else
  908. print_status('Usage: ')
  909. print_status('nessus_scan_new <UUID of Policy> <Scan name> <Description> <Targets>')
  910. print_status('Use nessus_policy_list to list all available policies with their corresponding UUIDs')
  911. return
  912. end
  913. if valid_policy(uuid)
  914. print_status("Creating scan from policy number #{uuid}, called #{scan_name} - #{description} and scanning #{targets}")
  915. et = {
  916. 'enabled' => false,
  917. 'launch' => 'ONETIME',
  918. 'name' => scan_name,
  919. 'text_targets' => targets,
  920. 'description' => description,
  921. 'launch_now' => false
  922. }
  923. scan = @n.scan_create(uuid, et)
  924. tbl = Rex::Text::Table.new(
  925. 'Columns' => [
  926. 'Scan ID',
  927. 'Scanner ID',
  928. 'Policy ID',
  929. 'Targets',
  930. 'Owner'
  931. ]
  932. )
  933. print_status('New scan added')
  934. tbl << [ scan['scan']['id'], scan['scan']['scanner_id'], scan['scan']['policy_id'], scan['scan']['custom_targets'], scan['scan']['owner'] ]
  935. print_status("Use nessus_scan_launch #{scan['scan']['id']} to launch the scan")
  936. print_line tbl.to_s
  937. else
  938. print_error('The policy does not exist')
  939. end
  940. end
  941. def cmd_nessus_scan_launch(*args)
  942. if args[0] == '-h'
  943. print_status('nessus_scan_launch <scan ID>')
  944. print_status('Use nessus_scan_list to list all the availabla scans with their corresponding scan IDs')
  945. end
  946. if !nessus_verify_token
  947. return
  948. end
  949. case args.length
  950. when 1
  951. scan_id = args[0]
  952. else
  953. print_status('Usage: ')
  954. print_status('nessus_scan_launch <scan ID>')
  955. print_status('Use nessus_scan_list to list all the availabla scans with their corresponding scan IDs')
  956. return
  957. end
  958. launch = @n.scan_launch(scan_id)
  959. print_good("Scan ID #{scan_id} successfully launched. The Scan UUID is #{launch['scan_uuid']}")
  960. end
  961. def cmd_nessus_scan_pause(*args)
  962. if args[0] == '-h'
  963. print_status('nessus_scan_pause <scan id>')
  964. print_status('Example:> nessus_scan_pause f0eabba3-4065-7d54-5763-f191e98eb0f7f9f33db7e75a06ca')
  965. print_status('Pauses a running scan')
  966. print_status('Use nessus_scan_list to list all available scans')
  967. return
  968. end
  969. if !nessus_verify_token
  970. return
  971. end
  972. case args.length
  973. when 1
  974. sid = args[0]
  975. else
  976. print_status('Usage: ')
  977. print_status('nessus_scan_pause <scan id>')
  978. print_status('Use nessus_scan_list to list all available scans')
  979. return
  980. end
  981. pause = @n.scan_pause(sid)
  982. if pause['error']
  983. print_error 'Invalid scan ID'
  984. else
  985. print_status("#{sid} has been paused")
  986. end
  987. end
  988. def cmd_nessus_db_scan(*args)
  989. if args[0] == '-h'
  990. print_status('nessus_db_scan <policy ID> <scan name> <scan description>')
  991. print_status('Creates a scan based on all the hosts listed in db_hosts.')
  992. print_status('Use nessus_policy_list to list all available policies with their corresponding policy IDs')
  993. return
  994. end
  995. if !nessus_verify_db
  996. return
  997. end
  998. if !nessus_verify_token
  999. return
  1000. end
  1001. case args.length
  1002. when 3
  1003. policy_id = args[0]
  1004. name = args[1]
  1005. desc = args[3]
  1006. else
  1007. print_status('Usage: ')
  1008. print_status('nessus_db_scan <policy ID> <scan name> <scan description>')
  1009. print_status('Use nessus_policy_list to list all available policies with their corresponding policy IDs')
  1010. return
  1011. end
  1012. if !valid_policy(policy_id)
  1013. print_error('That policy does not exist.')
  1014. return
  1015. end
  1016. targets = ''
  1017. framework.db.hosts.each do |host|
  1018. targets << host.address
  1019. targets << ','
  1020. end
  1021. targets.chop!
  1022. print_status("Creating scan from policy #{policy_id}, called \"#{name}\" and scanning all hosts in all the workspaces")
  1023. et = {
  1024. 'enabled' => false,
  1025. 'launch' => 'ONETIME',
  1026. 'name' => name,
  1027. 'text_targets' => targets,
  1028. 'description' => desc,
  1029. 'launch_now' => true
  1030. }
  1031. scan = @n.scan_create(policy_id, et)
  1032. if !scan['error']
  1033. scan = scan['scan']
  1034. print_status("Scan ID #{scan['id']} successfully created and launched")
  1035. else
  1036. print_error(JSON.pretty_generate(scan))
  1037. end
  1038. end
  1039. def cmd_nessus_db_scan_workspace(*args)
  1040. if args[0] == '-h'
  1041. print_status('nessus_db_scan_workspace <policy ID> <scan name> <scan description> <workspace>')
  1042. print_status('Creates a scan based on all the hosts listed in db_hosts for a given workspace.')
  1043. print_status('Use nessus_policy_list to list all available policies with their corresponding policy IDs')
  1044. return
  1045. end
  1046. if !nessus_verify_db
  1047. return
  1048. end
  1049. if !nessus_verify_token
  1050. return
  1051. end
  1052. case args.length
  1053. when 4
  1054. policy_id = args[0]
  1055. name = args[1]
  1056. desc = args[2]
  1057. new_workspace = framework.db.find_workspace(args[3])
  1058. else
  1059. print_status('Usage: ')
  1060. print_status('nessus_db_scan_workspace <policy ID> <scan name> <scan description> <workspace>')
  1061. print_status('Use nessus_policy_list to list all available policies with their corresponding policy IDs')
  1062. return
  1063. end
  1064. if !valid_policy(policy_id)
  1065. print_error('That policy does not exist.')
  1066. return
  1067. end
  1068. if new_workspace.nil?
  1069. print_error('That workspace does not exist.')
  1070. return
  1071. end
  1072. framework.db.workspace = new_workspace
  1073. print_status("Switched workspace: #{framework.db.workspace.name}")
  1074. targets = ''
  1075. framework.db.hosts.each do |host|
  1076. targets << host.address
  1077. targets << ','
  1078. print_status("Targets: #{targets}")
  1079. end
  1080. targets.chop!
  1081. print_status("Creating scan from policy #{policy_id}, called \"#{name}\" and scanning all hosts in #{framework.db.workspace.name}")
  1082. et = {
  1083. 'enabled' => false,
  1084. 'launch' => 'ONETIME',
  1085. 'name' => name,
  1086. 'text_targets' => targets,
  1087. 'description' => desc,
  1088. 'launch_now' => false
  1089. }
  1090. scan = @n.scan_create(policy_id, et)
  1091. if !scan['error']
  1092. scan = scan['scan']
  1093. print_status("Scan ID #{scan['id']} successfully created")
  1094. print_status("Run nessus_scan_launch #{scan['id']} to launch the scan")
  1095. else
  1096. print_error(JSON.pretty_generate(scan))
  1097. end
  1098. end
  1099. def cmd_nessus_db_import(*args)
  1100. if args[0] == '-h'
  1101. print_status('nessus_db_import <scan ID>')
  1102. print_status('Example:> nessus_db_import 500')
  1103. print_status('Use nessus_scan_list -c to list all completed scans')
  1104. end
  1105. if !nessus_verify_db
  1106. return
  1107. end
  1108. if !nessus_verify_token
  1109. return
  1110. end
  1111. case args.length
  1112. when 1
  1113. scan_id = args[0]
  1114. else
  1115. print_status('Usage: ')
  1116. print_status('nessus_db_import <scan ID>')
  1117. print_status('Example:> nessus_db_import 500')
  1118. print_status('Use nessus_scan_list -c to list all completed scans')
  1119. end
  1120. if is_scan_complete(scan_id)
  1121. print_status("Exporting scan ID #{scan_id} is Nessus format...")
  1122. export = @n.scan_export(scan_id, 'nessus')
  1123. status = {}
  1124. if export['file']
  1125. file_id = export['file']
  1126. print_good("The export file ID for scan ID #{scan_id} is #{file_id}")
  1127. print_status('Checking export status...')
  1128. loop do
  1129. status = @n.scan_export_status(scan_id, file_id)
  1130. print_status('Export status: ' + status['status'])
  1131. if status['status'] == 'ready'
  1132. break
  1133. end
  1134. sleep(1)
  1135. break unless (status['status'] == 'loading')
  1136. end
  1137. if status['status'] == 'ready'
  1138. print_status("The status of scan ID #{scan_id} export is ready")
  1139. select(nil, nil, nil, 5)
  1140. report = @n.report_download(scan_id, file_id)
  1141. print_status('Importing scan results to the database...')
  1142. framework.db.import({ data: report }) do |type, data|
  1143. case type
  1144. when :address
  1145. print_status("Importing data of #{data}")
  1146. end
  1147. end
  1148. print_good('Done')
  1149. else
  1150. print_error("There was some problem in exporting the scan. The error message is #{status}")
  1151. end
  1152. else
  1153. print_error(export)
  1154. end
  1155. else
  1156. print_error('Only completed scans could be used for import')
  1157. end
  1158. end
  1159. def cmd_nessus_scan_pause_all(*args)
  1160. scan_ids = Array.new
  1161. if args[0] == '-h'
  1162. print_status('nessus_scan_pause_all')
  1163. print_status('Example:> nessus_scan_pause_all')
  1164. print_status('Pauses all currently running scans')
  1165. print_status('Use nessus_scan_list to list all running scans')
  1166. return
  1167. end
  1168. if !nessus_verify_token
  1169. return
  1170. end
  1171. list = @n.scan_list
  1172. list['scans'].each do |scan|
  1173. if scan['status'] == 'running'
  1174. scan_ids << scan['id']
  1175. end
  1176. end
  1177. if !scan_ids.empty?
  1178. scan_ids.each do |scan_id|
  1179. @n.scan_pause(scan_id)
  1180. end
  1181. print_status('All scans have been paused')
  1182. else
  1183. print_error('No running scans')
  1184. end
  1185. end
  1186. def cmd_nessus_scan_stop(*args)
  1187. if args[0] == '-h'
  1188. print_status('nessus_scan_stop <scan id>')
  1189. print_status('Example:> nessus_scan_stop f0eabba3-4065-7d54-5763-f191e98eb0f7f9f33db7e75a06ca')
  1190. print_status('Stops a currently running scans')
  1191. print_status('Use nessus_scan_list to list all running scans')
  1192. return
  1193. end
  1194. if !nessus_verify_token
  1195. return
  1196. end
  1197. case args.length
  1198. when 1
  1199. sid = args[0]
  1200. else
  1201. print_status('Usage: ')
  1202. print_status('nessus_scan_stop <scan id>')
  1203. print_status('Use nessus_scan_list to list all available scans')
  1204. return
  1205. end
  1206. stop = @n.scan_stop(sid)
  1207. if stop['error']
  1208. print_error 'Invalid scan ID'
  1209. else
  1210. print_status("#{sid} has been stopped")
  1211. end
  1212. end
  1213. def cmd_nessus_scan_stop_all(*args)
  1214. scan_ids = Array.new
  1215. if args[0] == '-h'
  1216. print_status('nessus_scan_stop_all')
  1217. print_status('Example:> nessus_scan_stop_all')
  1218. print_status('stops all currently running scans')
  1219. print_status('Use nessus_scan_list to list all running scans')
  1220. return
  1221. end
  1222. if !nessus_verify_token
  1223. return
  1224. end
  1225. list = @n.scan_list
  1226. list['scans'].each do |scan|
  1227. if scan['status'] == 'running' || scan['status'] == 'paused'
  1228. scan_ids << scan['id']
  1229. end
  1230. end
  1231. if !scan_ids.empty?
  1232. scan_ids.each do |scan_id|
  1233. @n.scan_stop(scan_id)
  1234. end
  1235. print_status('All scans have been stopped')
  1236. else
  1237. print_error('No running or paused scans to be stopped')
  1238. end
  1239. end
  1240. def cmd_nessus_scan_resume(*args)
  1241. if args[0] == '-h'
  1242. print_status('nessus_scan_resume <scan id>')
  1243. print_status('Example:> nessus_scan_resume f0eabba3-4065-7d54-5763-f191e98eb0f7f9f33db7e75a06ca')
  1244. print_status('resumes a running scan')
  1245. print_status('Use nessus_scan_list to list all available scans')
  1246. return
  1247. end
  1248. if !nessus_verify_token
  1249. return
  1250. end
  1251. case args.length
  1252. when 1
  1253. sid = args[0]
  1254. else
  1255. print_status('Usage: ')
  1256. print_status('nessus_scan_resume <scan id>')
  1257. print_status('Use nessus_scan_list to list all available scans')
  1258. return
  1259. end
  1260. resume = @n.scan_resume(sid)
  1261. if resume['error']
  1262. print_error 'Invalid scan ID'
  1263. else
  1264. print_status("#{sid} has been resumed")
  1265. end
  1266. end
  1267. def cmd_nessus_scan_resume_all(*args)
  1268. scan_ids = Array.new
  1269. if args[0] == '-h'
  1270. print_status('nessus_scan_resume_all')
  1271. print_status('Example:> nessus_scan_resume_all')
  1272. print_status('resumes all currently running scans')
  1273. print_status('Use nessus_scan_list to list all running scans')
  1274. return
  1275. end
  1276. if !nessus_verify_token
  1277. return
  1278. end
  1279. list = @n.scan_list
  1280. list['scans'].each do |scan|
  1281. if scan['status'] == 'paused'
  1282. scan_ids << scan['id']
  1283. end
  1284. end
  1285. if !scan_ids.empty?
  1286. scan_ids.each do |scan_id|
  1287. @n.scan_resume(scan_id)
  1288. end
  1289. print_status('All scans have been resumed')
  1290. else
  1291. print_error('No running scans to be resumed')
  1292. end
  1293. end
  1294. def cmd_nessus_scan_details(*args)
  1295. valid_categories = ['info', 'hosts', 'vulnerabilities', 'history']
  1296. search_term = nil
  1297. scan_id = nil
  1298. category = nil
  1299. while (arg = args.shift)
  1300. case arg
  1301. when '-h', '--help'
  1302. print_status('Usage: ')
  1303. print_status('nessus_scan_details <scan ID> <category> -S searchterm')
  1304. print_status('Availble categories are info, hosts, vulnerabilities, and history')
  1305. print_status('Use nessus_scan_list to list all available scans with their corresponding scan IDs')
  1306. return
  1307. when '-S', '--search'
  1308. search_term = /#{args.shift}/nmi
  1309. else
  1310. scan_id = arg
  1311. if args[0].in?(valid_categories)
  1312. category = args.shift
  1313. else
  1314. print_error('Invalid category. The available categories are info, hosts, vulnerabilities, and history')
  1315. return
  1316. end
  1317. end
  1318. end
  1319. if !nessus_verify_token
  1320. return
  1321. end
  1322. details = @n.scan_details(scan_id)
  1323. if category == 'info'
  1324. tbl = Rex::Text::Table.new(
  1325. 'SearchTerm' => search_term,
  1326. 'Columns' => [
  1327. 'Status',
  1328. 'Policy',
  1329. 'Scan Name',
  1330. 'Scan Targets',
  1331. 'Scan Start Time',
  1332. 'Scan End Time'
  1333. ]
  1334. )
  1335. tbl << [ details['info']['status'], details['info']['policy'], details['info']['name'], details['info']['targets'], details['info']['scan_start'], details['info']['scan_end'] ]
  1336. elsif category == 'hosts'
  1337. tbl = Rex::Text::Table.new(
  1338. 'SearchTerm' => search_term,
  1339. 'Columns' => [
  1340. 'Host ID',
  1341. 'Hostname',
  1342. '% of Critical Findings',
  1343. '% of High Findings',
  1344. '% of Medium Findings',
  1345. '% of Low Findings'
  1346. ]
  1347. )
  1348. details['hosts'].each do |host|
  1349. tbl << [ host['host_id'], host['hostname'], host['critical'], host['high'], host['medium'], host['low'] ]
  1350. end
  1351. elsif category == 'vulnerabilities'
  1352. tbl = Rex::Text::Table.new(
  1353. 'SearchTerm' => search_term,
  1354. 'Columns' => [
  1355. 'Plugin ID',
  1356. 'Plugin Name',
  1357. 'Plugin Family',
  1358. 'Count'
  1359. ]
  1360. )
  1361. details['vulnerabilities'].each do |vuln|
  1362. tbl << [ vuln['plugin_id'], vuln['plugin_name'], vuln['plugin_family'], vuln['count'] ]
  1363. end
  1364. elsif category == 'history'
  1365. tbl = Rex::Text::Table.new(
  1366. 'SearchTerm' => search_term,
  1367. 'Columns' => [
  1368. 'History ID',
  1369. 'Status',
  1370. 'Creation Date',
  1371. 'Last Modification Date'
  1372. ]
  1373. )
  1374. details['history'].each do |hist|
  1375. tbl << [ hist['history_id'], hist['status'], hist['creation_date'], hist['modification_date'] ]
  1376. end
  1377. end
  1378. print_line tbl.to_s
  1379. end
  1380. def cmd_nessus_scan_export(*args)
  1381. if args[0] == '-h'
  1382. print_status('nessus_scan_export <scan ID> <export format>')
  1383. print_status('The available export formats are Nessus, HTML, PDF, CSV, or DB')
  1384. print_status('Use nessus_scan_list to list all available scans with their corresponding scan IDs')
  1385. return
  1386. end
  1387. if !nessus_verify_token
  1388. return
  1389. end
  1390. case args.length
  1391. when 2
  1392. scan_id = args[0]
  1393. format = args[1].downcase
  1394. else
  1395. print_status('Usage: ')
  1396. print_status('nessus_scan_export <scan ID> <export format>')
  1397. print_status('The available export formats are Nessus, HTML, PDF, CSV, or DB')
  1398. print_status('Use nessus_scan_list to list all available scans with their corresponding scan IDs')
  1399. return
  1400. end
  1401. if format.in?(['nessus', 'html', 'pdf', 'csv', 'db'])
  1402. export = @n.scan_export(scan_id, format)
  1403. status = {}
  1404. if export['file']
  1405. file_id = export['file']
  1406. print_good("The export file ID for scan ID #{scan_id} is #{file_id}")
  1407. print_status('Checking export status...')
  1408. loop do
  1409. status = @n.scan_export_status(scan_id, file_id)
  1410. print_status('Export status: ' + status['status'])
  1411. if status['status'] == 'ready'
  1412. break
  1413. end
  1414. sleep(1)
  1415. break unless (status['status'] == 'loading')
  1416. end
  1417. if status['status'] == 'ready'
  1418. print_good("The status of scan ID #{scan_id} export is ready")
  1419. else
  1420. print_error("There was some problem in exporting the scan. The error message is #{status}")
  1421. end
  1422. else
  1423. print_error(export)
  1424. end
  1425. else
  1426. print_error('Invalid export format. The available export formats are Nessus, HTML, PDF, CSV, or DB')
  1427. return
  1428. end
  1429. end
  1430. def cmd_nessus_scan_export_status(*args)
  1431. if args[0] == '-h'
  1432. print_status('nessus_scan_export_status <scan ID> <file ID>')
  1433. print_status('Use nessus_scan_export <scan ID> <format> to export a scan and get its file ID')
  1434. end
  1435. if !nessus_verify_token
  1436. return
  1437. end
  1438. case args.length
  1439. when 2
  1440. scan_id = args[0]
  1441. file_id = args[1]
  1442. status = {}
  1443. loop do
  1444. status = @n.scan_export_status(scan_id, file_id)
  1445. print_status('Export status: ' + status['status'])
  1446. if status['status'] == 'ready'
  1447. break
  1448. end
  1449. sleep(1)
  1450. break unless (status['status'] == 'loading')
  1451. end
  1452. if status['status'] == 'ready'
  1453. print_status("The status of scan ID #{scan_id} export is ready")
  1454. else
  1455. print_error("There was some problem in exporting the scan. The error message is #{status}")
  1456. end
  1457. else
  1458. print_status('Usage: ')
  1459. print_status('nessus_scan_export_status <scan ID> <file ID>')
  1460. print_status('Use nessus_scan_export <scan ID> <format> to export a scan and get its file ID')
  1461. end
  1462. end
  1463. def cmd_nessus_plugin_list(*args)
  1464. search_term = nil
  1465. family_id = nil
  1466. while (arg = args.shift)
  1467. case arg
  1468. when '-h', '--help'
  1469. print_status('nessus_plugin_list <Family ID> -S searchterm')
  1470. print_status('Example:> nessus_plugin_list 10')
  1471. print_status('Returns a list of all plugins in that family.')
  1472. print_status('Use nessus_family_list to display all the plugin families along with their corresponding family IDs')
  1473. return
  1474. when '-S', '--search'
  1475. search_term = /#{args.shift}/nmi
  1476. else
  1477. family_id = arg
  1478. end
  1479. end
  1480. if family_id.nil?
  1481. print_status('Usage: ')
  1482. print_status('nessus_plugin_list <Family ID>')
  1483. print_status('Use nessus_family_list to display all the plugin families along with their corresponding family IDs')
  1484. return
  1485. end
  1486. tbl = Rex::Text::Table.new(
  1487. 'SearchTerm' => search_term,
  1488. 'Columns' => [
  1489. 'Plugin ID',
  1490. 'Plugin Name'
  1491. ]
  1492. )
  1493. list = @n.list_plugins(family_id)
  1494. list['plugins'].each do |plugin|
  1495. tbl << [ plugin['id'], plugin['name'] ]
  1496. end
  1497. print_line
  1498. print_good("Plugin Family Name: #{list['name']}")
  1499. print_line
  1500. print_line tbl.to_s
  1501. end
  1502. def cmd_nessus_family_list(*args)
  1503. search_term = nil
  1504. while (arg = args.shift)
  1505. case arg
  1506. when '-h', '--help'
  1507. print_status('nessus_family_list')
  1508. print_status('Example:> nessus_family_list -S searchterm')
  1509. print_status('Returns a list of all the plugin families along with their corresponding family IDs and plugin count.')
  1510. return
  1511. when '-S', '--search'
  1512. search_term = /#{args.shift}/nmi
  1513. end
  1514. end
  1515. list = @n.list_families
  1516. tbl = Rex::Text::Table.new(
  1517. 'SearchTerm' => search_term,
  1518. 'Columns' => [
  1519. 'Family ID',
  1520. 'Family Name',
  1521. 'Number of Plugins'
  1522. ]
  1523. )
  1524. list['families'].each do |family|
  1525. tbl << [ family['id'], family['name'], family['count'] ]
  1526. end
  1527. print_line
  1528. print_line tbl.to_s
  1529. end
  1530. def cmd_nessus_plugin_details(*args)
  1531. search_term = nil
  1532. plugin_id = nil
  1533. while (arg = args.shift)
  1534. case arg
  1535. when '-h', '--help'
  1536. print_status('nessus_plugin_details <Plugin ID>')
  1537. print_status('Example:> nessus_plugin_details 10264 -S searchterm')
  1538. print_status('Returns details on a particular plugin.')
  1539. print_status('Use nessus_plugin_list to list all plugins and their corresponding plugin IDs belonging to a particular plugin family.')
  1540. return
  1541. when '-S', '--search'
  1542. search_term = /#{args.shift}/nmi
  1543. else
  1544. plugin_id = arg
  1545. end
  1546. end
  1547. if !nessus_verify_token
  1548. return
  1549. end
  1550. if plugin_id.nil?
  1551. print_status('Usage: ')
  1552. print_status('nessus_plugin_details <Plugin ID>')
  1553. print_status('Use nessus_plugin_list to list all plugins and their corresponding plugin IDs belonging to a particular plugin family.')
  1554. return
  1555. end
  1556. tbl = Rex::Text::Table.new(
  1557. 'SearchTerm' => search_term,
  1558. 'Columns' => [
  1559. 'Reference',
  1560. 'Value'
  1561. ]
  1562. )
  1563. begin
  1564. list = @n.plugin_details(plugin_id)
  1565. rescue ::Exception => e
  1566. if e.message =~ /unexpected token/
  1567. print_error('No plugin info found')
  1568. return
  1569. else
  1570. raise e
  1571. end
  1572. end
  1573. list['attributes'].each do |attrib|
  1574. tbl << [ attrib['attribute_name'], attrib['attribute_value'] ]
  1575. end
  1576. print_line
  1577. print_good("Plugin Name: #{list['name']}")
  1578. print_good("Plugin Family: #{list['family_name']}")
  1579. print_line
  1580. print_line tbl.to_s
  1581. end
  1582. def cmd_nessus_user_list(*args)
  1583. scan_id = nil
  1584. while (arg = args.shift)
  1585. case arg
  1586. when '-h', '--help'
  1587. print_status('nessus_user_list')
  1588. print_status('Example:> nessus_user_list -S searchterm')
  1589. print_status('Returns a list of the users on the Nessus server and their access level.')
  1590. return
  1591. when '-S', '--search'
  1592. search_term = /#{args.shift}/nmi
  1593. end
  1594. end
  1595. if !nessus_verify_token
  1596. return
  1597. end
  1598. if !@n.is_admin
  1599. print_status('Your Nessus user is not an admin')
  1600. end
  1601. list = @n.list_users
  1602. tbl = Rex::Text::Table.new(
  1603. 'SearchTerm' => search_term,
  1604. 'Columns' => [
  1605. 'ID',
  1606. 'Name',
  1607. 'Username',
  1608. 'Type',
  1609. 'Email',
  1610. 'Permissions'
  1611. ]
  1612. )
  1613. list['users'].each do |user|
  1614. tbl << [ user['id'], user['name'], user['username'], user['type'], user['email'], user['permissions'] ]
  1615. end
  1616. print_line
  1617. print_line tbl.to_s
  1618. end
  1619. def cmd_nessus_user_add(*args)
  1620. if args[0] == '-h'
  1621. print_status('nessus_user_add <username> <password> <permissions> <type>')
  1622. print_status('Permissions are 32, 64, and 128')
  1623. print_status('Type can be either local or LDAP')
  1624. print_status('Example:> nessus_user_add msf msf 16 local')
  1625. print_status('You need to be an admin in order to add accounts')
  1626. print_status('Use nessus_user_list to list all users')
  1627. return
  1628. end
  1629. if !nessus_verify_token
  1630. return
  1631. end
  1632. if !@n.is_admin
  1633. print_error('Your Nessus user is not an admin')
  1634. return
  1635. end
  1636. case args.length
  1637. when 4
  1638. user = args[0]
  1639. pass = args[1]
  1640. permissions = args[2]
  1641. type = args[3]
  1642. else
  1643. print_status('Usage')
  1644. print_status('nessus_user_add <username> <password> <permissions> <type>')
  1645. return
  1646. end
  1647. add = @n.user_add(user, pass, permissions, type)
  1648. if add['id']
  1649. print_good("#{user} created successfully")
  1650. else
  1651. print_error(add.to_s)
  1652. end
  1653. end
  1654. def cmd_nessus_user_del(*args)
  1655. if args[0] == '-h'
  1656. print_status('nessus_user_del <User ID>')
  1657. print_status('Example:> nessus_user_del 10')
  1658. print_status('This command can only delete non admin users. You must be an admin to delete users.')
  1659. print_status('Use nessus_user_list to list all users with their corresponding user IDs')
  1660. return
  1661. end
  1662. if !nessus_verify_token
  1663. return
  1664. end
  1665. if !@n.is_admin
  1666. print_error('Your Nessus user is not an admin')
  1667. return
  1668. end
  1669. case args.length
  1670. when 1
  1671. user_id = args[0]
  1672. else
  1673. print_status('Usage: ')
  1674. print_status('nessus_user_del <User ID>')
  1675. print_status('This command can only delete non admin users')
  1676. return
  1677. end
  1678. del = @n.user_delete(user_id)
  1679. status = del.to_s
  1680. if status == '200'
  1681. print_good("User account having user ID #{user_id} deleted successfully")
  1682. elsif status == '403'
  1683. print_error("You do not have permission to delete the user account having user ID #{user_id}")
  1684. elsif status == '404'
  1685. print_error("User account having user ID #{user_id} does not exist")
  1686. elsif status == '409'
  1687. print_error('You cannot delete your own account')
  1688. elsif status == '500'
  1689. print_error("The server failed to delete the user account having user ID #{user_id}")
  1690. else
  1691. print_error("Unknown problem occurred by deleting the user account having user ID #{user_id}.")
  1692. end
  1693. end
  1694. def cmd_nessus_user_passwd(*args)
  1695. if args[0] == '-h'
  1696. print_status('nessus_user_passwd <User ID> <New Password>')
  1697. print_status('Example:> nessus_user_passwd 10 mynewpassword')
  1698. print_status('Changes the password of a user. You must be an admin to change passwords.')
  1699. print_status('Use nessus_user_list to list all users with their corresponding user IDs')
  1700. return
  1701. end
  1702. if !nessus_verify_token
  1703. return
  1704. end
  1705. if !@n.is_admin
  1706. print_error('Your Nessus user is not an admin')
  1707. return
  1708. end
  1709. case args.length
  1710. when 2
  1711. user_id = args[0]
  1712. pass = args[1]
  1713. else
  1714. print_status('Usage: ')
  1715. print_status('nessus_user_passwd <User ID> <New Password>')
  1716. print_status('Use nessus_user_list to list all users with their corresponding user IDs')
  1717. return
  1718. end
  1719. pass = @n.user_chpasswd(user_id, pass)
  1720. status = pass.to_s
  1721. if status == '200'
  1722. print_good("Password of account having user ID #{user_id} changed successfully")
  1723. elsif status == '400'
  1724. print_error('Password is too short')
  1725. elsif status == '403'
  1726. print_error("You do not have the permission to change password for the user having user ID #{user_id}")
  1727. elsif status == '404'
  1728. print_error("User having user ID #{user_id} does not exist")
  1729. elsif status == '500'
  1730. print_error('Nessus server failed to changed the user password')
  1731. else
  1732. print_error('Unknown problem occurred while changing the user password')
  1733. end
  1734. end
  1735. def cmd_nessus_policy_list(*args)
  1736. search_term = nil
  1737. while (arg = args.shift)
  1738. case arg
  1739. when '-h', '--help'
  1740. print_status('nessus_policy_list')
  1741. print_status('Example:> nessus_policy_list -S searchterm')
  1742. print_status('Lists all policies on the server')
  1743. return
  1744. when '-S', '--search'
  1745. search_term = /#{args.shift}/nmi
  1746. end
  1747. end
  1748. if !nessus_verify_token
  1749. return
  1750. end
  1751. list = @n.list_policies
  1752. unless list['policies']
  1753. print_error('No policies found')
  1754. return
  1755. end
  1756. tbl = Rex::Text::Table.new(
  1757. 'Columns' => [
  1758. 'Policy ID',
  1759. 'Name',
  1760. 'Policy UUID'
  1761. ]
  1762. )
  1763. list['policies'].each do |policy|
  1764. tbl << [ policy['id'], policy['name'], policy['template_uuid'] ]
  1765. end
  1766. print_line tbl.to_s
  1767. end
  1768. def cmd_nessus_policy_del(*args)
  1769. if args[0] == '-h'
  1770. print_status('nessus_policy_del <policy ID>')
  1771. print_status('Example:> nessus_policy_del 1')
  1772. print_status('You must be an admin to delete policies.')
  1773. print_status('Use nessus_policy_list to list all policies with their corresponding policy IDs')
  1774. return
  1775. end
  1776. if !nessus_verify_token
  1777. return
  1778. end
  1779. if !@n.is_admin
  1780. print_error('Your Nessus user is not an admin')
  1781. return
  1782. end
  1783. case args.length
  1784. when 1
  1785. policy_id = args[0]
  1786. else
  1787. print_status('Usage: ')
  1788. print_status('nessus_policy_del <policy ID>')
  1789. print_status('Use nessus_policy_list to list all the policies with their corresponding policy IDs')
  1790. return
  1791. end
  1792. del = @n.policy_delete(policy_id)
  1793. status = del.to_s
  1794. if status == '200'
  1795. print_good("Policy ID #{policy_id} successfully deleted")
  1796. elsif status == '403'
  1797. print_error("You do not have permission to delete policy ID #{policy_id}")
  1798. elsif status == '404'
  1799. print_error("Policy ID #{policy_id} does not exist")
  1800. elsif status == '405'
  1801. print_error("Policy ID #{policy_id} is currently in use and cannot be deleted")
  1802. else
  1803. print_error("Unknown problem occurred by deleting the user account having user ID #{user_id}.")
  1804. end
  1805. end
  1806. end
  1807. def initialize(framework, opts)
  1808. super
  1809. add_console_dispatcher(ConsoleCommandDispatcher)
  1810. print_status(PLUGIN_DESCRIPTION)
  1811. print_status('Type %bldnessus_help%clr for a command listing')
  1812. end
  1813. def cleanup
  1814. remove_console_dispatcher('Nessus')
  1815. end
  1816. end
  1817. end