Class: Msf::Plugin::Nessus::ConsoleCommandDispatcher

Inherits:
Object
  • Object
show all
Includes:
Ui::Console::CommandDispatcher
Defined in:
plugins/nessus.rb

Instance Attribute Summary

Attributes included from Ui::Console::CommandDispatcher

#driver

Attributes included from Rex::Ui::Text::DispatcherShell::CommandDispatcher

#shell, #tab_complete_items

Instance Method Summary collapse

Methods included from Ui::Console::CommandDispatcher

#active_module, #active_module=, #active_session, #active_session=, #build_range_array, #docs_dir, #framework, #initialize, #load_config, #log_error, #remove_lines

Methods included from Rex::Ui::Text::DispatcherShell::CommandDispatcher

#cmd_help, #cmd_help_help, #cmd_help_tabs, #deprecated_cmd, #deprecated_commands, #deprecated_help, #docs_dir, #help_to_s, included, #initialize, #print, #print_error, #print_good, #print_line, #print_status, #print_warning, #tab_complete_directory, #tab_complete_filenames, #tab_complete_generic, #tab_complete_source_address, #unknown_command, #update_prompt

Instance Method Details

#check_scan(*args) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'plugins/nessus.rb', line 206

def check_scan(*args)
  case args.length
  when 1
    scan_id = args[0]
  else
    print_error('No scan ID supplied')
    return
  end
  scans = @n.scan_list
  scans.each do |scan|
    if scan['scans']['id'] == scan_id && scan['scans']['status'] == 'completed'
      return true
    end
  end
  return false
end

#cmd_nessus_admin(*args) ⇒ Object



481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'plugins/nessus.rb', line 481

def cmd_nessus_admin(*args)
  while (arg = args.shift)
    case arg
    when '-h', '--help'
      print_status('nessus_admin')
      print_status('Example:> nessus_admin')
      print_status('Checks to see if the current user is an admin')
      print_status('Use nessus_user_list to list all users')
      return
    end
  end

  if !nessus_verify_token
    return
  end

  if !@n.is_admin
    print_error('Your Nessus user is not an admin')
  else
    print_good('Your Nessus user is an admin')
  end
end

#cmd_nessus_connect(*args) ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'plugins/nessus.rb', line 308

def cmd_nessus_connect(*args)
  # Check if config file exists and load it
  if !args[0]
    if File.exist?(nessus_yaml)
      lconfig = YAML.load_file(nessus_yaml)
      @user = lconfig['default']['username'].to_s
      @pass = lconfig['default']['password'].to_s
      @host = lconfig['default']['server'].to_s
      @port = lconfig['default']['port'].to_s
      
    else
      ncusage
    end
    return
  end

  if args[0] == '-h'
    print_status('%redYou must do this before any other commands.%clr')
    print_status('Usage: ')
    print_status('nessus_connect username:password@hostname:port <ssl_verify/ssl_ignore>')
    print_status('%bldusername%clr and %bldpassword%clr are the ones you use to login to the nessus web front end')
    print_status('%bldhostname%clr can be an IP address or a DNS name of the Nessus server.')
    print_status('%bldport%clr is the RPC port that the Nessus web front end runs on. By default it is TCP port 8834.')
    print_status('The "ssl_verify" to verify the SSL certificate used by the Nessus front end. By default the server')
    print_status('use a self signed certificate, therefore, users should use ssl_ignore.')
    return
  end

  if !@token == ''
    print_error('You are already authenticated.  Call nessus_logout before authenticating again')
    return
  end
  if (args.empty? || args[0].empty?)
    ncusage
    return
  end

  @user = @pass = @host = @port = @sslv = nil
  case args.length
  when 1, 2
    if args[0].include? '@'
      cred, _split, targ = args[0].rpartition('@')
      @user, @pass = cred.split(':', 2)
      targ ||= '127.0.0.1:8834'
      @host, @port = targ.split(':', 2)
    else
      @host, @port = args[0].split(':', 2)
    end
    @port ||= '8834'
    @sslv = args[1]
  when 3, 4, 5
    ncusage
    return
  else
    ncusage
    return
  end
  if %r{//}.match(@host)
    ncusage
    return
  end
  if !@user
    print_error('Missing Username')
    ncusage
    return
  end
  if !@pass
    print_error('Missing Password')
    ncusage
    return
  end
  if !((@user && !@user.empty?) && (@host && !@host.empty?) && (@port && !@port.empty? && (@port.to_i > 0)) && (@pass && !@pass.empty?))
    ncusage
    return
  end
  
end

#cmd_nessus_db_import(*args) ⇒ Object



1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
# File 'plugins/nessus.rb', line 1168

def cmd_nessus_db_import(*args)
  if args[0] == '-h'
    print_status('nessus_db_import <scan ID>')
    print_status('Example:> nessus_db_import 500')
    print_status('Use nessus_scan_list -c to list all completed scans')
  end
  if !nessus_verify_db
    return
  end
  if !nessus_verify_token
    return
  end

  case args.length
  when 1
    scan_id = args[0]
  else
    print_status('Usage: ')
    print_status('nessus_db_import <scan ID>')
    print_status('Example:> nessus_db_import 500')
    print_status('Use nessus_scan_list -c to list all completed scans')
  end
  if is_scan_complete(scan_id)
    print_status("Exporting scan ID #{scan_id} is Nessus format...")
    export = @n.scan_export(scan_id, 'nessus')
    status = {}
    if export['file']
      file_id = export['file']
      print_good("The export file ID for scan ID #{scan_id} is #{file_id}")
      print_status('Checking export status...')
      loop do
        status = @n.scan_export_status(scan_id, file_id)
        print_status('Export status: ' + status['status'])
        if status['status'] == 'ready'
          break
        end

        sleep(1)
        break unless (status['status'] == 'loading')
      end
      if status['status'] == 'ready'
        print_status("The status of scan ID #{scan_id} export is ready")
        select(nil, nil, nil, 5)
        report = @n.report_download(scan_id, file_id)
        print_status('Importing scan results to the database...')
        framework.db.import({ data: report }) do |type, data|
          case type
          when :address
            print_status("Importing data of #{data}")
          end
        end
        print_good('Done')
      else
        print_error("There was some problem in exporting the scan. The error message is #{status}")
      end
    else
      print_error(export)
    end
  else
    print_error('Only completed scans could be used for import')
  end
end

#cmd_nessus_db_scan(*args) ⇒ Object



1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
# File 'plugins/nessus.rb', line 1053

def cmd_nessus_db_scan(*args)
  if args[0] == '-h'
    print_status('nessus_db_scan <policy ID> <scan name> <scan description>')
    print_status('Creates a scan based on all the hosts listed in db_hosts.')
    print_status('Use nessus_policy_list to list all available policies with their corresponding policy IDs')
    return
  end
  if !nessus_verify_db
    return
  end
  if !nessus_verify_token
    return
  end

  case args.length
  when 3
    policy_id = args[0]
    name = args[1]
    desc = args[3]
  else
    print_status('Usage: ')
    print_status('nessus_db_scan <policy ID> <scan name> <scan description>')
    print_status('Use nessus_policy_list to list all available policies with their corresponding policy IDs')
    return
  end
  if !valid_policy(policy_id)
    print_error('That policy does not exist.')
    return
  end
  targets = ''
  framework.db.hosts.each do |host|
    targets << host.address
    targets << ','
  end
  targets.chop!
  print_status("Creating scan from policy #{policy_id}, called \"#{name}\" and scanning all hosts in all the workspaces")
  et = {
    'enabled' => false,
    'launch' => 'ONETIME',
    'name' => name,
    'text_targets' => targets,
    'description' => desc,
    'launch_now' => true
  }
  scan = @n.scan_create(policy_id, et)
  if !scan['error']
    scan = scan['scan']
    print_status("Scan ID #{scan['id']} successfully created and launched")
  else
    print_error(JSON.pretty_generate(scan))
  end
end

#cmd_nessus_db_scan_workspace(*args) ⇒ Object



1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
# File 'plugins/nessus.rb', line 1106

def cmd_nessus_db_scan_workspace(*args)
  if args[0] == '-h'
    print_status('nessus_db_scan_workspace <policy ID> <scan name> <scan description> <workspace>')
    print_status('Creates a scan based on all the hosts listed in db_hosts for a given workspace.')
    print_status('Use nessus_policy_list to list all available policies with their corresponding policy IDs')
    return
  end
  if !nessus_verify_db
    return
  end
  if !nessus_verify_token
    return
  end

  case args.length
  when 4
    policy_id = args[0]
    name = args[1]
    desc = args[2]
    new_workspace = framework.db.find_workspace(args[3])
  else
    print_status('Usage: ')
    print_status('nessus_db_scan_workspace <policy ID> <scan name> <scan description> <workspace>')
    print_status('Use nessus_policy_list to list all available policies with their corresponding policy IDs')
    return
  end
  if !valid_policy(policy_id)
    print_error('That policy does not exist.')
    return
  end
  if new_workspace.nil?
    print_error('That workspace does not exist.')
    return
  end
  framework.db.workspace = new_workspace
  print_status("Switched workspace: #{framework.db.workspace.name}")
  targets = ''
  framework.db.hosts.each do |host|
    targets << host.address
    targets << ','
    print_status("Targets: #{targets}")
  end
  targets.chop!
  print_status("Creating scan from policy #{policy_id}, called \"#{name}\" and scanning all hosts in #{framework.db.workspace.name}")
  et = {
    'enabled' => false,
    'launch' => 'ONETIME',
    'name' => name,
    'text_targets' => targets,
    'description' => desc,
    'launch_now' => false
  }
  scan = @n.scan_create(policy_id, et)
  if !scan['error']
    scan = scan['scan']
    print_status("Scan ID #{scan['id']} successfully created")
    print_status("Run nessus_scan_launch #{scan['id']} to launch the scan")
  else
    print_error(JSON.pretty_generate(scan))
  end
end

#cmd_nessus_family_list(*args) ⇒ Object



1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
# File 'plugins/nessus.rb', line 1595

def cmd_nessus_family_list(*args)
  search_term = nil
  while (arg = args.shift)
    case arg
    when '-h', '--help'
      print_status('nessus_family_list')
      print_status('Example:> nessus_family_list -S searchterm')
      print_status('Returns a list of all the plugin families along with their corresponding family IDs and plugin count.')
      return
    when '-S', '--search'
      search_term = /#{args.shift}/nmi
    end
  end

  list = @n.list_families
  tbl = Rex::Text::Table.new(
    'SearchTerm' => search_term,
    'Columns' => [
      'Family ID',
      'Family Name',
      'Number of Plugins'
    ]
  )
  list['families'].each do |family|
    tbl << [ family['id'], family['name'], family['count'] ]
  end
  print_line
  print_line tbl.to_s
end

#cmd_nessus_folder_list(*args) ⇒ Object



553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'plugins/nessus.rb', line 553

def cmd_nessus_folder_list(*args)
  search_term = nil
  while (arg = args.shift)
    case arg
    when '-S', '--search'
      search_term = /#{args.shift}/nmi
    end
  end
  if !nessus_verify_token
    return
  end

  list = @n.list_folders
  tbl = Rex::Text::Table.new(
    'SearchTerm' => search_term,
    'Columns' => [
      'ID',
      'Name',
      'Type'
    ]
  )
  list['folders'].each do |folder|
    tbl << [ folder['id'], folder['name'], folder['type'] ]
  end
  print_line
  print_line tbl.to_s
end

#cmd_nessus_help(*_args) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'plugins/nessus.rb', line 234

def cmd_nessus_help(*_args)
  tbl = Rex::Text::Table.new(
    'Columns' => [
      'Command',
      'Help Text'
    ],
    'SortIndex' => -1
  )
  tbl << [ 'Generic Commands', '' ]
  tbl << [ '-----------------', '-----------------']
  tbl << [ 'nessus_connect', 'Connect to a Nessus server' ]
  tbl << [ 'nessus_logout', 'Logout from the Nessus server' ]
  tbl << [ 'nessus_login', 'Login into the connected Nesssus server with a different username and password']
  tbl << [ 'nessus_save', 'Save credentials of the logged in user to nessus.yml']
  tbl << [ 'nessus_help', 'Listing of available nessus commands' ]
  tbl << [ 'nessus_server_properties', 'Nessus server properties such as feed type, version, plugin set and server UUID.' ]
  tbl << [ 'nessus_server_status', 'Check the status of your Nessus Server' ]
  tbl << [ 'nessus_admin', 'Checks if user is an admin' ]
  tbl << [ 'nessus_template_list', 'List scan or policy templates' ]
  tbl << [ 'nessus_folder_list', 'List all configured folders on the Nessus server' ]
  tbl << [ 'nessus_scanner_list', 'List all the scanners configured on the Nessus server' ]
  tbl << [ 'Nessus Database Commands', '' ]
  tbl << [ '-----------------', '-----------------' ]
  tbl << [ 'nessus_db_scan', 'Create a scan of all IP addresses in db_hosts' ]
  tbl << [ 'nessus_db_scan_workspace', 'Create a scan of all IP addresses in db_hosts for a given workspace' ]
  tbl << [ 'nessus_db_import', 'Import Nessus scan to the Metasploit connected database' ]
  tbl << [ '', '']
  tbl << [ 'Reports Commands', '' ]
  tbl << [ '-----------------', '-----------------']
  tbl << [ 'nessus_report_hosts', 'Get list of hosts from a report' ]
  tbl << [ 'nessus_report_vulns', 'Get list of vulns from a report' ]
  tbl << [ 'nessus_report_host_details', 'Get detailed information from a report item on a host' ]
  tbl << [ '', '']
  tbl << [ 'Scan Commands', '' ]
  tbl << [ '-----------------', '-----------------']
  tbl << [ 'nessus_scan_list', 'List of all current Nessus scans' ]
  tbl << [ 'nessus_scan_new', 'Create a new Nessus Scan' ]
  tbl << [ 'nessus_scan_launch', 'Launch a newly created scan. New scans need to be manually launched through this command' ]
  tbl << [ 'nessus_scan_pause', 'Pause a running Nessus scan' ]
  tbl << [ 'nessus_scan_pause_all', 'Pause all running Nessus scans' ]
  tbl << [ 'nessus_scan_stop', 'Stop a running or paused Nessus scan' ]
  tbl << [ 'nessus_scan_stop_all', 'Stop all running or paused Nessus scans' ]
  tbl << [ 'nessus_scan_resume', 'Resume a pasued Nessus scan' ]
  tbl << [ 'nessus_scan_resume_all', 'Resume all paused Nessus scans' ]
  tbl << [ 'nessus_scan_details', 'Return detailed information of a given scan' ]
  tbl << [ 'nessus_scan_export', 'Export a scan result in either Nessus, HTML, PDF, CSV, or DB format' ]
  tbl << [ 'nessus_scan_export_status', 'Check the status of an exported scan' ]
  tbl << [ '', '']
  tbl << [ 'Plugin Commands', '' ]
  tbl << [ '-----------------', '-----------------']
  tbl << [ 'nessus_plugin_list', 'List all plugins in a particular plugin family.' ]
  tbl << [ 'nessus_family_list', 'List all the plugin families along with their corresponding family IDs and plugin count.' ]
  tbl << [ 'nessus_plugin_details', 'List details of a particular plugin' ]
  tbl << [ '', '']
  tbl << [ 'User Commands', '' ]
  tbl << [ '-----------------', '-----------------']
  tbl << [ 'nessus_user_list', 'Show Nessus Users' ]
  tbl << [ 'nessus_user_add', 'Add a new Nessus User' ]
  tbl << [ 'nessus_user_del', 'Delete a Nessus User' ]
  tbl << [ 'nessus_user_passwd', 'Change Nessus Users Password' ]
  tbl << [ '', '']
  tbl << [ 'Policy Commands', '' ]
  tbl << [ '-----------------', '-----------------']
  tbl << [ 'nessus_policy_list', 'List all polciies' ]
  tbl << [ 'nessus_policy_del', 'Delete a policy' ]
  print_line ''
  print_line tbl.to_s
  print_line ''
end

#cmd_nessus_indexObject



304
305
306
# File 'plugins/nessus.rb', line 304

def cmd_nessus_index
  nessus_index
end

#cmd_nessus_logoutObject



386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'plugins/nessus.rb', line 386

def cmd_nessus_logout
  logout = @n.user_logout
  status = logout.to_s
  if status == '200'
    print_good('User account logged out successfully')
    @token = ''
  elsif status == '403'
    print_status('No user session to logout')
  else
    print_error("There was some problem in logging out the user #{@user}")
  end
  return
end

#cmd_nessus_plugin_details(*args) ⇒ Object



1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
# File 'plugins/nessus.rb', line 1625

def cmd_nessus_plugin_details(*args)
  search_term = nil
  plugin_id = nil
  while (arg = args.shift)
    case arg
    when '-h', '--help'
      print_status('nessus_plugin_details <Plugin ID>')
      print_status('Example:> nessus_plugin_details 10264 -S searchterm')
      print_status('Returns details on a particular plugin.')
      print_status('Use nessus_plugin_list to list all plugins and their corresponding plugin IDs belonging to a particular plugin family.')
      return
    when '-S', '--search'
      search_term = /#{args.shift}/nmi
    else
      plugin_id = arg
    end
  end

  if !nessus_verify_token
    return
  end

  if plugin_id.nil?
    print_status('Usage: ')
    print_status('nessus_plugin_details <Plugin ID>')
    print_status('Use nessus_plugin_list to list all plugins and their corresponding plugin IDs belonging to a particular plugin family.')
    return
  end
  tbl = Rex::Text::Table.new(
    'SearchTerm' => search_term,
    'Columns' => [
      'Reference',
      'Value'
    ]
  )
  begin
    list = @n.plugin_details(plugin_id)
  rescue ::Exception => e
    if e.message =~ /unexpected token/
      print_error('No plugin info found')
      return
    else
      raise e
    end
  end
  list['attributes'].each do |attrib|
    tbl << [ attrib['attribute_name'], attrib['attribute_value'] ]
  end
  print_line
  print_good("Plugin Name: #{list['name']}")
  print_good("Plugin Family: #{list['family_name']}")
  print_line
  print_line tbl.to_s
end

#cmd_nessus_plugin_list(*args) ⇒ Object



1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
# File 'plugins/nessus.rb', line 1554

def cmd_nessus_plugin_list(*args)
  search_term = nil
  family_id = nil
  while (arg = args.shift)
    case arg
    when '-h', '--help'
      print_status('nessus_plugin_list <Family ID> -S searchterm')
      print_status('Example:> nessus_plugin_list 10')
      print_status('Returns a list of all plugins in that family.')
      print_status('Use nessus_family_list to display all the plugin families along with their corresponding family IDs')
      return
    when '-S', '--search'
      search_term = /#{args.shift}/nmi
    else
      family_id = arg
    end
  end

  if family_id.nil?
    print_status('Usage: ')
    print_status('nessus_plugin_list <Family ID>')
    print_status('Use nessus_family_list to display all the plugin families along with their corresponding family IDs')
    return
  end
  tbl = Rex::Text::Table.new(
    'SearchTerm' => search_term,
    'Columns' => [
      'Plugin ID',
      'Plugin Name'
    ]
  )
  list = @n.list_plugins(family_id)
  list['plugins'].each do |plugin|
    tbl << [ plugin['id'], plugin['name'] ]
  end
  print_line
  print_good("Plugin Family Name: #{list['name']}")
  print_line
  print_line tbl.to_s
end

#cmd_nessus_policy_del(*args) ⇒ Object



1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
# File 'plugins/nessus.rb', line 1880

def cmd_nessus_policy_del(*args)
  if args[0] == '-h'
    print_status('nessus_policy_del <policy ID>')
    print_status('Example:> nessus_policy_del 1')
    print_status('You must be an admin to delete policies.')
    print_status('Use nessus_policy_list to list all policies with their corresponding policy IDs')
    return
  end
  if !nessus_verify_token
    return
  end

  if !@n.is_admin
    print_error('Your Nessus user is not an admin')
    return
  end
  case args.length
  when 1
    policy_id = args[0]
  else
    print_status('Usage: ')
    print_status('nessus_policy_del <policy ID>')
    print_status('Use nessus_policy_list to list all the policies with their corresponding policy IDs')
    return
  end
  del = @n.policy_delete(policy_id)
  status = del.to_s
  if status == '200'
    print_good("Policy ID #{policy_id} successfully deleted")
  elsif status == '403'
    print_error("You do not have permission to delete policy ID #{policy_id}")
  elsif status == '404'
    print_error("Policy ID #{policy_id} does not exist")
  elsif status == '405'
    print_error("Policy ID #{policy_id} is currently in use and cannot be deleted")
  else
    print_error("Unknown problem occured by deleting the user account having user ID #{user_id}.")
  end
end

#cmd_nessus_policy_list(*args) ⇒ Object



1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
# File 'plugins/nessus.rb', line 1842

def cmd_nessus_policy_list(*args)
  search_term = nil
  while (arg = args.shift)
    case arg
    when '-h', '--help'
      print_status('nessus_policy_list')
      print_status('Example:> nessus_policy_list -S searchterm')
      print_status('Lists all policies on the server')
      return
    when '-S', '--search'
      search_term = /#{args.shift}/nmi
    end
  end

  if !nessus_verify_token
    return
  end

  list = @n.list_policies

  unless list['policies']
    print_error('No policies found')
    return
  end

  tbl = Rex::Text::Table.new(
    'Columns' => [
      'Policy ID',
      'Name',
      'Policy UUID'
    ]
  )
  list['policies'].each do |policy|
    tbl << [ policy['id'], policy['name'], policy['template_uuid'] ]
  end
  print_line tbl.to_s
end

#cmd_nessus_report_del(*args) ⇒ Object



857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
# File 'plugins/nessus.rb', line 857

def cmd_nessus_report_del(*args)
  if args[0] == '-h'
    print_status('nessus_report_del <reportname>')
    print_status('Example:> nessus_report_del f0eabba3-4065-7d54-5763-f191e98eb0f7f9f33db7e75a06ca')
    print_status('Must be an admin to del reports.')
    print_status('Use nessus_report_list to list all reports')
    return
  end
  if !nessus_verify_token
    return
  end

  if !@n.is_admin
    print_error('Your Nessus user is not an admin')
    return
  end
  case args.length
  when 1
    rid = args[0]
  else
    print_status('Usage: ')
    print_status('nessus_report_del <report ID>')
    print_status('nessus_report_list to find the id.')
    return
  end
  del = @n.report_del(rid)
  status = del.root.elements['status'].text
  if status == 'OK'
    print_good("Report #{rid} has been deleted")
  else
    print_error("Report #{rid} was not deleted")
  end
end

#cmd_nessus_report_download(*args) ⇒ Object



776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
# File 'plugins/nessus.rb', line 776

def cmd_nessus_report_download(*args)
  if args[0] == '-h'
    print_status('nessus_scan_report_download <scan_id> <file ID> ')
    print_status('Use nessus_scan_export_status <scan ID> <file ID> to check the export status.')
    print_status('Use nessus_scan_list -c to list all completed scans along with their corresponding scan IDs')
    return
  end
  if !nessus_verify_token
    return
  end

  case args.length
  when 2
    scan_id = args[0]
    file_id = args[1]
    if is_scan_complete(scan_id)
      report = @n.report_download(scan_id, file_id)
      File.open("#{msf_local}/#{scan_id}-#{file_id}", 'w+') do |f|
        f.puts report
        print_status("Report downloaded to #{msf_local} directory")
      end
    else
      print_error('Only completed scans can be downloaded')
    end
  else
    print_status('Usage: ')
    print_status('nessus_scan_report_download <scan_id> <file ID> ')
    print_status('Use nessus_scan_export_status <scan ID> <file ID> to check the export status.')
    print_status('Use nessus_scan_list -c to list all completed scans along with their corresponding scan IDs')
  end
end

#cmd_nessus_report_host_details(*args) ⇒ Object



709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
# File 'plugins/nessus.rb', line 709

def cmd_nessus_report_host_details(*args)
  search_term = nil
  search_vuln = nil
  scan_id = nil
  host_id = nil
  while (arg = args.shift)
    case arg
    when '-h', '--help'
      print_status('nessus_report_host_details <scan ID> <host ID>')
      print_status('Example:> nessus_report_host_details 10 5 -S hostinfo -SV vulninfo')
      print_status('Use nessus_scan_list to get list of all scans. Only completed scans can be used for reporting.')
      print_status('Use nessus_report_hosts to get a list of all the hosts along with their corresponding host IDs.')
      return
    when '-S', '--search'
      search_term = /#{args.shift}/nmi
    when '-SV', '--search-vuln'
      search_vuln = /#{args.shift}/nmi
    else
      scan_id =
        arg,
        host_id = args.shift
    end
  end

  if [scan_id, host_id].any?(&:nil?)
    print_status('Usage: ')
    print_status('nessus_report_host_detail <scan ID> <host ID>')
    print_status('Example:> nessus_report_host_detail 10 5')
    print_status('Use nessus_scan_list to get list of all scans. Only completed scans can be used for reporting.')
    print_status('Use nessus_report_hosts <scan ID> to get a list of all the hosts along with their corresponding host IDs.')
    return
  end
  tbl = Rex::Text::Table.new(
    'SearchTerm' => search_term,
    'Columns' => [
      'Plugin Name',
      'Plugin Famil',
      'Severity'
    ]
  )
  details = @n.host_detail(scan_id, host_id)
  print_line
  print_status('Host information')
  print_line("IP Address: #{details['info']['host-ip']}")
  print_line("Hostname: #{details['info']['host-name']}")
  print_line("Operating System: #{details['info']['operating-system']}")
  print_line
  print_status('Vulnerability information')
  details['vulnerabilities'].each do |vuln|
    tbl << [ vuln['plugin_name'], vuln['plugin_family'], vuln['severity'] ]
  end
  print_line tbl.to_s
  tbl2 = Rex::Text::Table.new(
    'SearchTerm' => search_vuln,
    'Columns' => [
      'Plugin Name',
      'Plugin Famil',
      'Severity'
    ]
  )
  print_status('Compliance information')
  details['compliance'].each do |comp|
    tbl2 << [ comp['plugin_name'], comp['plugin_family'], comp['severity'] ]
  end
  print_line tbl2.to_s
end

#cmd_nessus_report_host_ports(*args) ⇒ Object



808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
# File 'plugins/nessus.rb', line 808

def cmd_nessus_report_host_ports(*args)
  search_term = nil
  rid = nil
  host = nil
  while (arg = args.shift)
    case arg
    when '-h', '--help'
      print_status('nessus_report_host_ports <hostname> <report id>')
      print_status('Example:> nessus_report_host_ports 192.168.1.250 f0eabba3-4065-7d54-5763-f191e98eb0f7f9f33db7e75a06ca -S searchterm')
      print_status('Returns all the ports associated with a host and details about their vulnerabilities')
      print_status('Use nessus_report_hosts to list all available hosts for a report')
      return
    when '-S', '--search'
      search_term = /#{args.shift}/nmi
    else
      scan_id = arg
    end
  end

  if [host, rid].any?(&:nil?)
    print_status('Usage: ')
    print_status('nessus_report_host_ports <hostname> <report id>')
    print_status('Use nessus_report_list to list all available reports')
    return
  end
  tbl = Rex::Text::Table.new(
    'SearchTerm' => search_term,
    'Columns' => [
      'Port',
      'Protocol',
      'Severity',
      'Service Name',
      'Sev 0',
      'Sev 1',
      'Sev 2',
      'Sev 3'
    ]
  )
  ports = @n.report_host_ports(rid, host)
  ports.each do |port|
    tbl << [ port['portnum'], port['protocol'], port['severity'], port['svcname'], port['sev0'], port['sev1'], port['sev2'], port['sev3'] ]
  end
  print_good('Host Info')
  print_good "\n"
  print_line tbl.to_s
  print_status('You can:')
  print_status('Get detailed scan infromation about a specfic port: nessus_report_host_detail <hostname> <port> <protocol> <report id>')
end

#cmd_nessus_report_hosts(*args) ⇒ Object



619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
# File 'plugins/nessus.rb', line 619

def cmd_nessus_report_hosts(*args)
  search_term = nil
  scan_id = nil
  while (arg = args.shift)
    case arg
    when '-h', '--help'
      print_status('nessus_report_hosts <scan ID> -S searchterm')
      print_status('Use nessus_scan_list to get a list of all the scans. Only completed scans can be reported.')
      return
    when '-S', '--search'
      search_term = /#{args.shift}/nmi
    else
      scan_id = arg
    end
  end

  if scan_id.nil?
    print_status('Usage: ')
    print_status('nessus_report_hosts <scan ID> -S searchterm')
    print_status('Use nessus_scan_list to get a list of all the scans. Only completed scans can be reported.')
    return
  end

  tbl = Rex::Text::Table.new(
    'SearchTerm' => search_term,
    'Columns' => [
      'Host ID',
      'Hostname',
      '% of Critical Findings',
      '% of High Findings',
      '% of Medium Findings',
      '% of Low Findings'
    ]
  )
  if is_scan_complete(scan_id)
    details = @n.scan_details(scan_id)
    details['hosts'].each do |host|
      tbl << [ host['host_id'], host['hostname'], host['critical'], host['high'], host['medium'], host['low'] ]
    end
    print_line
    print_line tbl.to_s
  else
    print_error('Only completed scans can be used for host reporting')
    return
  end
end

#cmd_nessus_report_vulns(*args) ⇒ Object



666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
# File 'plugins/nessus.rb', line 666

def cmd_nessus_report_vulns(*args)
  search_term = nil
  scan_id = nil
  while (arg = args.shift)
    case arg
    when '-h', '--help'
      print_status('nessus_report_vulns <scan ID> -S searchterm')
      print_status('Use nessus_scan_list to get a list of all the scans. Only completed scans can be reported.')
      return
    when '-S', '--search'
      search_term = /#{args.shift}/nmi
    else
      scan_id = arg
    end
  end
  if scan_id.nil?
    print_status('Usage: ')
    print_status('nessus_report_vulns <scan ID>')
    print_status('Use nessus_scan_list to get a list of all the scans. Only completed scans can be reported.')
    return
  end
  tbl = Rex::Text::Table.new(
    'SearchTerm' => search_term,
    'Columns' => [
      'Plugin ID',
      'Plugin Name',
      'Plugin Family',
      'Vulnerability Count'
    ]
  )
  if is_scan_complete(scan_id)
    details = @n.scan_details(scan_id)
    details['vulnerabilities'].each do |vuln|
      tbl << [ vuln['plugin_id'], vuln['plugin_name'], vuln['plugin_family'], vuln['count'] ]
    end
    print_line
    print_line tbl.to_s
  else
    print_error('Only completed scans can be used for vulnerability reporting')
  end
  return
end

#cmd_nessus_save(*args) ⇒ Object



400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'plugins/nessus.rb', line 400

def cmd_nessus_save(*args)
  # if we are logged in, save session details to nessus.yaml
  if args[0] == '-h'
    print_status(' nessus_save')
    return
  end
  if args[0]
    print_status('Usage: ')
    print_status('nessus_save')
    return
  end
  group = 'default'
  if ((@user && !@user.empty?) && (@host && !@host.empty?) && (@port && !@port.empty? && (@port.to_i > 0)) && (@pass && !@pass.empty?))
    config = Hash.new
    config = { group.to_s => { 'username' => @user, 'password' => @pass, 'server' => @host, 'port' => @port } }
    File.open(nessus_yaml.to_s, 'w+') do |f|
      f.puts YAML.dump(config)
    end
    print_good("#{nessus_yaml} created.")
  else
    print_error('Missing username/password/server/port - relogin and then try again.')
    return
  end
end

#cmd_nessus_scan_details(*args) ⇒ Object



1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
# File 'plugins/nessus.rb', line 1376

def cmd_nessus_scan_details(*args)
  valid_categories = ['info', 'hosts', 'vulnerabilities', 'history']
  search_term = nil
  scan_id = nil
  category = nil
  while (arg = args.shift)
    case arg
    when '-h', '--help'
      print_status('Usage: ')
      print_status('nessus_scan_details <scan ID> <category> -S searchterm')
      print_status('Availble categories are info, hosts, vulnerabilities, and history')
      print_status('Use nessus_scan_list to list all available scans with their corresponding scan IDs')
      return
    when '-S', '--search'
      search_term = /#{args.shift}/nmi
    else
      scan_id = arg
      if args[0].in?(valid_categories)
        category = args.shift
      else
        print_error('Invalid category. The available categories are info, hosts, vulnerabilities, and history')
        return
      end
    end
  end

  if !nessus_verify_token
    return
  end

  details = @n.scan_details(scan_id)
  if category == 'info'
    tbl = Rex::Text::Table.new(
      'SearchTerm' => search_term,
      'Columns' => [
        'Status',
        'Policy',
        'Scan Name',
        'Scan Targets',
        'Scan Start Time',
        'Scan End Time'
      ]
    )
    tbl << [ details['info']['status'], details['info']['policy'], details['info']['name'], details['info']['targets'], details['info']['scan_start'], details['info']['scan_end'] ]
  elsif category == 'hosts'
    tbl = Rex::Text::Table.new(
      'SearchTerm' => search_term,
      'Columns' => [
        'Host ID',
        'Hostname',
        '% of Critical Findings',
        '% of High Findings',
        '% of Medium Findings',
        '% of Low Findings'
      ]
    )
    details['hosts'].each do |host|
      tbl << [ host['host_id'], host['hostname'], host['critical'], host['high'], host['medium'], host['low'] ]
    end
  elsif category == 'vulnerabilities'
    tbl = Rex::Text::Table.new(
      'SearchTerm' => search_term,
      'Columns' => [
        'Plugin ID',
        'Plugin Name',
        'Plugin Family',
        'Count'
      ]
    )
    details['vulnerabilities'].each do |vuln|
      tbl << [ vuln['plugin_id'], vuln['plugin_name'], vuln['plugin_family'], vuln['count'] ]
    end
  elsif category == 'history'
    tbl = Rex::Text::Table.new(
      'SearchTerm' => search_term,
      'Columns' => [
        'History ID',
        'Status',
        'Creation Date',
        'Last Modification Date'
      ]
    )
    details['history'].each do |hist|
      tbl << [ hist['history_id'], hist['status'], hist['creation_date'], hist['modification_date'] ]
    end
  end
  print_line tbl.to_s
end

#cmd_nessus_scan_export(*args) ⇒ Object



1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
# File 'plugins/nessus.rb', line 1465

def cmd_nessus_scan_export(*args)
  if args[0] == '-h'
    print_status('nessus_scan_export <scan ID> <export format>')
    print_status('The available export formats are Nessus, HTML, PDF, CSV, or DB')
    print_status('Use nessus_scan_list to list all available scans with their corresponding scan IDs')
    return
  end
  if !nessus_verify_token
    return
  end

  case args.length
  when 2
    scan_id = args[0]
    format = args[1].downcase
  else
    print_status('Usage: ')
    print_status('nessus_scan_export <scan ID> <export format>')
    print_status('The available export formats are Nessus, HTML, PDF, CSV, or DB')
    print_status('Use nessus_scan_list to list all available scans with their corresponding scan IDs')
    return
  end
  if format.in?(['nessus', 'html', 'pdf', 'csv', 'db'])
    export = @n.scan_export(scan_id, format)
    status = {}
    if export['file']
      file_id = export['file']
      print_good("The export file ID for scan ID #{scan_id} is #{file_id}")
      print_status('Checking export status...')
      loop do
        status = @n.scan_export_status(scan_id, file_id)
        print_status('Export status: ' + status['status'])
        if status['status'] == 'ready'
          break
        end

        sleep(1)
        break unless (status['status'] == 'loading')
      end
      if status['status'] == 'ready'
        print_good("The status of scan ID #{scan_id} export is ready")
      else
        print_error("There was some problem in exporting the scan. The error message is #{status}")
      end
    else
      print_error(export)
    end
  else
    print_error('Invalid export format. The available export formats are Nessus, HTML, PDF, CSV, or DB')
    return
  end
end

#cmd_nessus_scan_export_status(*args) ⇒ Object



1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
# File 'plugins/nessus.rb', line 1518

def cmd_nessus_scan_export_status(*args)
  if args[0] == '-h'
    print_status('nessus_scan_export_status <scan ID> <file ID>')
    print_status('Use nessus_scan_export <scan ID> <format> to export a scan and get its file ID')
  end
  if !nessus_verify_token
    return
  end

  case args.length
  when 2
    scan_id = args[0]
    file_id = args[1]
    status = {}
    loop do
      status = @n.scan_export_status(scan_id, file_id)
      print_status('Export status: ' + status['status'])
      if status['status'] == 'ready'
        break
      end

      sleep(1)
      break unless (status['status'] == 'loading')
    end
    if status['status'] == 'ready'
      print_status("The status of scan ID #{scan_id} export is ready")
    else
      print_error("There was some problem in exporting the scan. The error message is #{status}")
    end
  else
    print_status('Usage: ')
    print_status('nessus_scan_export_status <scan ID> <file ID>')
    print_status('Use nessus_scan_export <scan ID> <format> to export a scan and get its file ID')
  end
end

#cmd_nessus_scan_launch(*args) ⇒ Object



1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
# File 'plugins/nessus.rb', line 1002

def cmd_nessus_scan_launch(*args)
  if args[0] == '-h'
    print_status('nessus_scan_launch <scan ID>')
    print_status('Use nessus_scan_list to list all the availabla scans with their corresponding scan IDs')
  end
  if !nessus_verify_token
    return
  end

  case args.length
  when 1
    scan_id = args[0]
  else
    print_status('Usage: ')
    print_status('nessus_scan_launch <scan ID>')
    print_status('Use nessus_scan_list to list all the availabla scans with their corresponding scan IDs')
    return
  end
  launch = @n.scan_launch(scan_id)
  print_good("Scan ID #{scan_id} successfully launched. The Scan UUID is #{launch['scan_uuid']}")
end

#cmd_nessus_scan_list(*args) ⇒ Object



891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
# File 'plugins/nessus.rb', line 891

def cmd_nessus_scan_list(*args)
  search_term = nil
  while (arg = args.shift)
    case arg
    when '-h', '--help'
      print_status('nessus_scan_list')
      print_status('Example:> nessus_scan_list -S searchterm')
      print_status('Returns a list of information about currently running scans.')
      return
    when '-S', '--search'
      search_term = /#{args.shift}/nmi
    end
  end

  if !nessus_verify_token
    return
  end

  list = @n.scan_list
  if list.to_s.empty?
    print_status('No scans performed.')
    return
  else
    tbl = Rex::Text::Table.new(
      'SearchTerm' => search_term,
      'Columns' => [
        'Scan ID',
        'Name',
        'Owner',
        'Started',
        'Status',
        'Folder'
      ]
    )

    list['scans'].each do |scan|
      if args[0] == '-r'
        if scan['status'] == 'running'
          tbl << [ scan['id'], scan['name'], scan['owner'], scan['starttime'], scan['status'], scan['folder_id'] ]
        end
      elsif args[0] == '-p'
        if scan['status'] == 'paused'
          tbl << [ scan['id'], scan['name'], scan['owner'], scan['starttime'], scan['status'], scan['folder_id'] ]
        end
      elsif args[0] == '-c'
        if scan['status'] == 'completed'
          tbl << [ scan['id'], scan['name'], scan['owner'], scan['starttime'], scan['status'], scan['folder_id'] ]
        end
      elsif args[0] == '-a'
        if scan['status'] == 'canceled'
          tbl << [ scan['id'], scan['name'], scan['owner'], scan['starttime'], scan['status'], scan['folder_id'] ]
        end
      else
        tbl << [ scan['id'], scan['name'], scan['owner'], scan['starttime'], scan['status'], scan['folder_id'] ]
      end
    end
    print_line tbl.to_s
  end
end

#cmd_nessus_scan_new(*args) ⇒ Object



951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# File 'plugins/nessus.rb', line 951

def cmd_nessus_scan_new(*args)
  if args[0] == '-h'
    print_status('nessus_scan_new <UUID of Policy> <Scan name> <Description> <Targets>')
    print_status('Use nessus_policy_list to list all available policies with their corresponding UUIDs')
    return
  end
  if !nessus_verify_token
    return
  end

  case args.length
  when 4
    uuid = args[0]
    scan_name = args[1]
    description = args[2]
    targets = args[3]
  else
    print_status('Usage: ')
    print_status('nessus_scan_new <UUID of Policy> <Scan name> <Description> <Targets>')
    print_status('Use nessus_policy_list to list all available policies with their corresponding UUIDs')
    return
  end
  if valid_policy(uuid)
    print_status("Creating scan from policy number #{uuid}, called #{scan_name} - #{description} and scanning #{targets}")
    et = {
      'enabled' => false,
      'launch' => 'ONETIME',
      'name' => scan_name,
      'text_targets' => targets,
      'description' => description,
      'launch_now' => false
    }
    scan = @n.scan_create(uuid, et)
    tbl = Rex::Text::Table.new(
      'Columns' => [
        'Scan ID',
        'Scanner ID',
        'Policy ID',
        'Targets',
        'Owner'
      ]
    )
    print_status('New scan added')
    tbl << [ scan['scan']['id'], scan['scan']['scanner_id'], scan['scan']['policy_id'], scan['scan']['custom_targets'], scan['scan']['owner'] ]
    print_status("Use nessus_scan_launch #{scan['scan']['id']} to launch the scan")
    print_line tbl.to_s
  else
    print_error('The policy does not exist')
  end
end

#cmd_nessus_scan_pause(*args) ⇒ Object



1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
# File 'plugins/nessus.rb', line 1024

def cmd_nessus_scan_pause(*args)
  if args[0] == '-h'
    print_status('nessus_scan_pause <scan id>')
    print_status('Example:> nessus_scan_pause f0eabba3-4065-7d54-5763-f191e98eb0f7f9f33db7e75a06ca')
    print_status('Pauses a running scan')
    print_status('Use nessus_scan_list to list all available scans')
    return
  end
  if !nessus_verify_token
    return
  end

  case args.length
  when 1
    sid = args[0]
  else
    print_status('Usage: ')
    print_status('nessus_scan_pause <scan id>')
    print_status('Use nessus_scan_list to list all available scans')
    return
  end
  pause = @n.scan_pause(sid)
  if pause['error']
    print_error 'Invalid scan ID'
  else
    print_status("#{sid} has been paused")
  end
end

#cmd_nessus_scan_pause_all(*args) ⇒ Object



1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
# File 'plugins/nessus.rb', line 1231

def cmd_nessus_scan_pause_all(*args)
  scan_ids = Array.new
  if args[0] == '-h'
    print_status('nessus_scan_pause_all')
    print_status('Example:> nessus_scan_pause_all')
    print_status('Pauses all currently running scans')
    print_status('Use nessus_scan_list to list all running scans')
    return
  end
  if !nessus_verify_token
    return
  end

  list = @n.scan_list
  list['scans'].each do |scan|
    if scan['status'] == 'running'
      scan_ids << scan['id']
    end
  end
  if !scan_ids.empty?
    scan_ids.each do |scan_id|
      @n.scan_pause(scan_id)
    end
    print_status('All scans have been paused')
  else
    print_error('No running scans')
  end
end

#cmd_nessus_scan_resume(*args) ⇒ Object



1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
# File 'plugins/nessus.rb', line 1318

def cmd_nessus_scan_resume(*args)
  if args[0] == '-h'
    print_status('nessus_scan_resume <scan id>')
    print_status('Example:> nessus_scan_resume f0eabba3-4065-7d54-5763-f191e98eb0f7f9f33db7e75a06ca')
    print_status('resumes a running scan')
    print_status('Use nessus_scan_list to list all available scans')
    return
  end
  if !nessus_verify_token
    return
  end

  case args.length
  when 1
    sid = args[0]
  else
    print_status('Usage: ')
    print_status('nessus_scan_resume <scan id>')
    print_status('Use nessus_scan_list to list all available scans')
    return
  end
  resume = @n.scan_resume(sid)
  if resume['error']
    print_error 'Invalid scan ID'
  else
    print_status("#{sid} has been resumed")
  end
end

#cmd_nessus_scan_resume_all(*args) ⇒ Object



1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
# File 'plugins/nessus.rb', line 1347

def cmd_nessus_scan_resume_all(*args)
  scan_ids = Array.new
  if args[0] == '-h'
    print_status('nessus_scan_resume_all')
    print_status('Example:> nessus_scan_resume_all')
    print_status('resumes all currently running scans')
    print_status('Use nessus_scan_list to list all running scans')
    return
  end
  if !nessus_verify_token
    return
  end

  list = @n.scan_list
  list['scans'].each do |scan|
    if scan['status'] == 'paused'
      scan_ids << scan['id']
    end
  end
  if !scan_ids.empty?
    scan_ids.each do |scan_id|
      @n.scan_resume(scan_id)
    end
    print_status('All scans have been resumed')
  else
    print_error('No running scans to be resumed')
  end
end

#cmd_nessus_scan_stop(*args) ⇒ Object



1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
# File 'plugins/nessus.rb', line 1260

def cmd_nessus_scan_stop(*args)
  if args[0] == '-h'
    print_status('nessus_scan_stop <scan id>')
    print_status('Example:> nessus_scan_stop f0eabba3-4065-7d54-5763-f191e98eb0f7f9f33db7e75a06ca')
    print_status('Stops a currently running scans')
    print_status('Use nessus_scan_list to list all running scans')
    return
  end
  if !nessus_verify_token
    return
  end

  case args.length
  when 1
    sid = args[0]
  else
    print_status('Usage: ')
    print_status('nessus_scan_stop <scan id>')
    print_status('Use nessus_scan_list to list all available scans')
    return
  end
  stop = @n.scan_stop(sid)
  if stop['error']
    print_error 'Invalid scan ID'
  else
    print_status("#{sid} has been stopped")
  end
end

#cmd_nessus_scan_stop_all(*args) ⇒ Object



1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
# File 'plugins/nessus.rb', line 1289

def cmd_nessus_scan_stop_all(*args)
  scan_ids = Array.new
  if args[0] == '-h'
    print_status('nessus_scan_stop_all')
    print_status('Example:> nessus_scan_stop_all')
    print_status('stops all currently running scans')
    print_status('Use nessus_scan_list to list all running scans')
    return
  end
  if !nessus_verify_token
    return
  end

  list = @n.scan_list
  list['scans'].each do |scan|
    if scan['status'] == 'running' || scan['status'] == 'paused'
      scan_ids << scan['id']
    end
  end
  if !scan_ids.empty?
    scan_ids.each do |scan_id|
      @n.scan_stop(scan_id)
    end
    print_status('All scans have been stopped')
  else
    print_error('No running or paused scans to be stopped')
  end
end

#cmd_nessus_scanner_list(*args) ⇒ Object



581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# File 'plugins/nessus.rb', line 581

def cmd_nessus_scanner_list(*args)
  search_term = nil
  while (arg = args.shift)
    case arg
    when '-h', '--help'
      print_status('nessus_scanner_list')
      print_status('Example:> nessus_scanner_list -S searchterm')
      print_status('Returns information about the feed type and server version.')
      return
    when '-S', '--search'
      search_term = /#{args.shift}/nmi
    end
  end
  if !nessus_verify_token
    return
  end
  if !@n.is_admin
    return
  end

  list = @n.list_scanners
  tbl = Rex::Text::Table.new(
    'SearchTerm' => search_term,
    'Columns' => [
      'ID',
      'Name',
      'Status',
      'Platform',
      'Plugin Set',
      'UUID'
    ]
  )
  list.each do |scanner|
    tbl << [ scanner['id'], scanner['name'], scanner['status'], scanner['platform'], scanner['loaded_plugin_set'], scanner['uuid'] ]
  end
  print_line tbl.to_s
end

#cmd_nessus_server_properties(*args) ⇒ Object



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'plugins/nessus.rb', line 425

def cmd_nessus_server_properties(*args)
  search_term = nil
  while (arg = args.shift)
    case arg
    when '-h', '--help'
      print_status('nessus_server_properties')
      print_status('Example:> nessus_server_properties -S searchterm')
      print_status('Returns information about the feed type and server version.')
      return
    when '-S', '--search'
      search_term = /#{args.shift}/nmi
    end
  end

  resp = @n.server_properties
  tbl = Rex::Text::Table.new(
    'SearchTerm' => search_term,
    'Columns' => [
      'Feed',
      'Type',
      'Nessus Version',
      'Nessus Web Version',
      'Plugin Set',
      'Server UUID'
    ]
  )
  tbl << [ resp['feed'], resp['nessus_type'], resp['server_version'], resp['nessus_ui_version'], resp['loaded_plugin_set'], resp['server_uuid'] ]
  print_line tbl.to_s
end

#cmd_nessus_server_status(*args) ⇒ Object



455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File 'plugins/nessus.rb', line 455

def cmd_nessus_server_status(*args)
  search_term = nil
  while (arg = args.shift)
    case arg
    when '-h', '--help'
      print_status('nessus_server_status')
      print_status('Example:> nessus_server_status -S searchterm')
      print_status('Returns some status items for the server..')
      return
    when '-S', '--search'
      search_term = /#{args.shift}/nmi
    end
  end

  tbl = Rex::Text::Table.new(
    'SearchTerm' => search_term,
    'Columns' => [
      'Status',
      'Progress'
    ]
  )
  list = @n.server_status
  tbl << [ list['progress'], list['status'] ]
  print_line tbl.to_s
end

#cmd_nessus_template_list(*args) ⇒ Object



504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'plugins/nessus.rb', line 504

def cmd_nessus_template_list(*args)
  search_term = nil
  while (arg = args.shift)
    case arg
    when '-h', '--help'
      print_status('nessus_template_list <scan> | <policy>')
      print_status('Example:> nessus_template_list scan -S searchterm')
      print_status('OR')
      print_status('nessus_template_list policy')
      print_status('Returns a list of information about the scan or policy templates..')
      return
    when '-S', '--search'
      search_term = /#{args.shift}/nmi
    else
      type = arg
    end
  end

  if !nessus_verify_token
    return
  end

  if type.in?(['scan', 'policy'])
    list = @n.list_templates(type)
  else
    print_error('Only scan and policy are valid templates')
    return
  end
  if list.empty?
    print_status('No templates created')
    return
  end
  tbl = Rex::Text::Table.new(
    'SearchTerm' => search_term,
    'Columns' => [
      'Name',
      'Title',
      'Description',
      'Subscription Only',
      'Cloud Only'
    ]
  )
  list['templates'].each do |template|
    tbl << [ template['name'], template['title'], template['desc'], template['subscription_only'], template['cloud_only'] ]
  end
  print_line
  print_line tbl.to_s
end

#cmd_nessus_user_add(*args) ⇒ Object



1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
# File 'plugins/nessus.rb', line 1720

def cmd_nessus_user_add(*args)
  if args[0] == '-h'
    print_status('nessus_user_add <username> <password> <permissions> <type>')
    print_status('Permissions are 32, 64, and 128')
    print_status('Type can be either local or LDAP')
    print_status('Example:> nessus_user_add msf msf 16 local')
    print_status('You need to be an admin in order to add accounts')
    print_status('Use nessus_user_list to list all users')
    return
  end
  if !nessus_verify_token
    return
  end

  if !@n.is_admin
    print_error('Your Nessus user is not an admin')
    return
  end
  case args.length
  when 4
    user = args[0]
    pass = args[1]
    permissions = args[2]
    type = args[3]
  else
    print_status('Usage')
    print_status('nessus_user_add <username> <password> <permissions> <type>')
    return
  end
  add = @n.user_add(user, pass, permissions, type)
  if add['id']
    print_good("#{user} created successfully")
  else
    print_error(add.to_s)
  end
end

#cmd_nessus_user_del(*args) ⇒ Object



1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
# File 'plugins/nessus.rb', line 1757

def cmd_nessus_user_del(*args)
  if args[0] == '-h'
    print_status('nessus_user_del <User ID>')
    print_status('Example:> nessus_user_del 10')
    print_status('This command can only delete non admin users. You must be an admin to delete users.')
    print_status('Use nessus_user_list to list all users with their corresponding user IDs')
    return
  end
  if !nessus_verify_token
    return
  end

  if !@n.is_admin
    print_error('Your Nessus user is not an admin')
    return
  end
  case args.length
  when 1
    user_id = args[0]
  else
    print_status('Usage: ')
    print_status('nessus_user_del <User ID>')
    print_status('This command can only delete non admin users')
    return
  end
  del = @n.user_delete(user_id)
  status = del.to_s
  if status == '200'
    print_good("User account having user ID #{user_id} deleted successfully")
  elsif status == '403'
    print_error("You do not have permission to delete the user account having user ID #{user_id}")
  elsif status == '404'
    print_error("User account having user ID #{user_id} does not exist")
  elsif status == '409'
    print_error('You cannot delete your own account')
  elsif status == '500'
    print_error("The server failed to delete the user account having user ID #{user_id}")
  else
    print_error("Unknown problem occured by deleting the user account having user ID #{user_id}.")
  end
end

#cmd_nessus_user_list(*args) ⇒ Object



1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
# File 'plugins/nessus.rb', line 1680

def cmd_nessus_user_list(*args)
  scan_id = nil
  while (arg = args.shift)
    case arg
    when '-h', '--help'
      print_status('nessus_user_list')
      print_status('Example:> nessus_user_list -S searchterm')
      print_status('Returns a list of the users on the Nessus server and their access level.')
      return
    when '-S', '--search'
      search_term = /#{args.shift}/nmi
    end
  end

  if !nessus_verify_token
    return
  end

  if !@n.is_admin
    print_status('Your Nessus user is not an admin')
  end
  list = @n.list_users
  tbl = Rex::Text::Table.new(
    'SearchTerm' => search_term,
    'Columns' => [
      'ID',
      'Name',
      'Username',
      'Type',
      'Email',
      'Permissions'
    ]
  )
  list['users'].each do |user|
    tbl << [ user['id'], user['name'], user['username'], user['type'], user['email'], user['permissions'] ]
  end
  print_line
  print_line tbl.to_s
end

#cmd_nessus_user_passwd(*args) ⇒ Object



1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
# File 'plugins/nessus.rb', line 1799

def cmd_nessus_user_passwd(*args)
  if args[0] == '-h'
    print_status('nessus_user_passwd <User ID> <New Password>')
    print_status('Example:> nessus_user_passwd 10 mynewpassword')
    print_status('Changes the password of a user. You must be an admin to change passwords.')
    print_status('Use nessus_user_list to list all users with their corresponding user IDs')
    return
  end
  if !nessus_verify_token
    return
  end

  if !@n.is_admin
    print_error('Your Nessus user is not an admin')
    return
  end
  case args.length
  when 2
    user_id = args[0]
    pass = args[1]
  else
    print_status('Usage: ')
    print_status('nessus_user_passwd <User ID> <New Password>')
    print_status('Use nessus_user_list to list all users with their corresponding user IDs')
    return
  end
  pass = @n.user_chpasswd(user_id, pass)
  status = pass.to_s
  if status == '200'
    print_good("Password of account having user ID #{user_id} changed successfully")
  elsif status == '400'
    print_error('Password is too short')
  elsif status == '403'
    print_error("You do not have the permission to change password for the user having user ID #{user_id}")
  elsif status == '404'
    print_error("User having user ID #{user_id} does not exist")
  elsif status == '500'
    print_error('Nessus server failed to changed the user password')
  else
    print_error('Unknown problem occured while changing the user password')
  end
end

#commandsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'plugins/nessus.rb', line 37

def commands
  {
    'nessus_connect' => 'Connect to a nessus server: nconnect username:password@hostname:port <verify_ssl>',
    'nessus_admin' => 'Checks if user is an admin',
    'nessus_help' => 'Get help on all commands',
    'nessus_logout' => 'Terminate the session',
    'nessus_server_status' => 'Check the status of your Nessus server',
    'nessus_server_properties' => 'Nessus server properties such as feed type, version, plugin set and server UUID',
    'nessus_report_download' => 'Download a report from the nessus server in either Nessus, HTML, PDF, CSV, or DB format',
    'nessus_report_vulns' => 'Get list of vulns from a report',
    'nessus_report_hosts' => 'Get list of hosts from a report',
    'nessus_report_host_details' => 'Get detailed information from a report item on a host',
    'nessus_scan_list' => 'List of currently running Nessus scans',
    'nessus_scan_new' => 'Create a new Nessus scan',
    'nessus_scan_launch' => 'Launch a previously added scan',
    'nessus_scan_pause' => 'Pause a running Nessus scan',
    'nessus_scan_pause_all' => 'Pause all running Nessus scans',
    'nessus_scan_stop' => 'Stop a running or paused Nessus scan',
    'nessus_scan_stop_all' => 'Stop all running or paused Nessus scans',
    'nessus_scan_resume' => 'Resume a paused Nessus scan',
    'nessus_scan_resume_all' => 'Resume all paused Nessus scans',
    'nessus_scan_details' => 'Return detailed information of a given scan',
    'nessus_scan_export' => 'Export a scan result in either Nessus, HTML, PDF, CSV, or DB format',
    'nessus_scan_export_status' => 'Check the status of scan export',
    'nessus_user_list' => 'List of Nessus users',
    'nessus_user_add' => 'Add a new Nessus user',
    'nessus_user_del' => 'Delete a Nessus user',
    'nessus_user_passwd' => 'Change Nessus Users Password',
    'nessus_plugin_details' => 'List details of a particular plugin',
    'nessus_plugin_list' => 'Display plugin details in a particular plugin family',
    'nessus_policy_list' => 'List all polciies',
    'nessus_policy_del' => 'Delete a policy',
    'nessus_index' => 'Manually generates a search index for exploits',
    'nessus_template_list' => 'List all the templates on the server',
    'nessus_db_scan' => 'Create a scan of all IP addresses in db_hosts',
    'nessus_db_scan_workspace' => 'Create a scan of all IP addresses in db_hosts for a given workspace',
    'nessus_db_import' => 'Import Nessus scan to the Metasploit connected database',
    'nessus_save' => 'Save credentials of the logged in user to nessus.yml',
    'nessus_folder_list' => 'List folders configured on the Nessus server',
    'nessus_scanner_list' => 'List the configured scanners on the Nessus server',
    'nessus_family_list' => 'List all the plugin families along with their corresponding family IDs and plugin count'
  }
end

#create_xindexObject

creates the index of exploit details to make searching for exploits much faster.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'plugins/nessus.rb', line 100

def create_xindex
  start = Time.now
  print_status("Creating Exploit Search Index - (#{xindex}) - this won't take long.")
  # Use Msf::Config.config_directory as the location.
  File.open(xindex.to_s, 'w+') do |f|
    # need to add version line.
    f.puts(Msf::Framework::Version)
    framework.exploits.sort.each do |refname, mod|
      stuff = ''
      o = nil
      begin
        o = mod.new
      rescue ::Exception
      end
      stuff << "#{refname}|#{o.name}|#{o.platform_to_s}|#{o.arch_to_s}"
      next if !o

      o.references.map do |x|
        if x.ctx_id != 'URL'
          if (x.ctx_id == 'MSB')
            stuff << "|#{x.ctx_val}"
          else
            stuff << "|#{x.ctx_id}-#{x.ctx_val}"
          end
        end
      end
      stuff << "\n"
      f.puts(stuff)
    end
  end
  total = Time.now - start
  print_status("It has taken : #{total} seconds to build the exploits search index")
end

#is_scan_complete(scan_id) ⇒ Object



223
224
225
226
227
228
229
230
231
232
# File 'plugins/nessus.rb', line 223

def is_scan_complete(scan_id)
  complete = false
  status = @n.scan_list
  status['scans'].each do |scan|
    if scan['id'] == scan_id.to_i && (scan['status'] == 'completed' || scan['status'] == 'imported')
      complete = true
    end
  end
  complete
end

#msf_localObject



33
34
35
# File 'plugins/nessus.rb', line 33

def msf_local
  Msf::Config.local_directory.to_s
end

#nameObject



21
22
23
# File 'plugins/nessus.rb', line 21

def name
  PLUGIN_NAME
end

#ncusageObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'plugins/nessus.rb', line 81

def ncusage
  print_status('%redYou must do this before any other commands.%clr')
  print_status('Usage: ')
  print_status('nessus_connect username:password@hostname:port <ssl_verify>')
  print_status('Example:> nessus_connect msf:[email protected]:8834')
  print_status('OR')
  print_status('nessus_connect username@hostname:port ssl_verify')
  print_status('Example:> nessus_connect [email protected]:8834 ssl_verify')
  print_status('OR')
  print_status('nessus_connect hostname:port ssl_verify')
  print_status('Example:> nessus_connect 192.168.1.10:8834 ssl_verify')
  print_status('OR')
  print_status('nessus_connect')
  print_status('Example:> nessus_connect')
  print_status('This only works after you have saved creds with nessus_save')
  return
end

#nessus_indexObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'plugins/nessus.rb', line 134

def nessus_index
  if File.exist?(xindex.to_s)
    # check if it's version line matches current version.
    File.open(xindex.to_s) do |f|
      line = f.readline
      line.chomp!
      if line.to_i == Msf::Framework::RepoRevision
        print_good("Exploit Index - (#{xindex}) - is valid.")
      else
        create_xindex
      end
    end
  else
    create_xindex
  end
end

#nessus_loginObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'plugins/nessus.rb', line 151

def 
  if !((@user && !@user.empty?) && (@host && !@host.empty?) && (@port && !@port.empty? && (@port.to_i > 0)) && (@pass && !@pass.empty?))
    print_status('You need to connect to a server first.')
    ncusage
    return
  end
  @url = "https://#{@host}:#{@port}/"
  print_status("Connecting to #{@url} as #{@user}")
  verify_ssl = false
  if @sslv == 'verify_ssl'
    verify_ssl = true
  end
  @n = NessusREST::Client.new(url: @url, username: @user, password: @pass, ssl_verify: verify_ssl)
  if @n.authenticated
    print_status("User #{@user} authenticated successfully.")
    @token = 1
  else
    print_error('Error connecting/logging to the server!')
    return
  end
end

#nessus_verify_dbObject



198
199
200
201
202
203
204
# File 'plugins/nessus.rb', line 198

def nessus_verify_db
  if !(framework.db && framework.db.active)
    print_error('No database has been configured, please use db_create/db_connect first')
    return false
  end
  true
end

#nessus_verify_tokenObject



173
174
175
176
177
178
179
# File 'plugins/nessus.rb', line 173

def nessus_verify_token
  if @token.nil? || (@token == '')
    ncusage
    return false
  end
  true
end

#nessus_yamlObject



29
30
31
# File 'plugins/nessus.rb', line 29

def nessus_yaml
  "#{Msf::Config.config_directory}/nessus.yaml"
end

#valid_policy(*args) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'plugins/nessus.rb', line 181

def valid_policy(*args)
  case args.length
  when 1
    pid = args[0]
  else
    print_error('No Policy ID supplied.')
    return
  end
  pol = @n.list_policies
  pol['policies'].each do |p|
    if p['template_uuid'] == pid
      return true
    end
  end
  return false
end

#xindexObject



25
26
27
# File 'plugins/nessus.rb', line 25

def xindex
  "#{Msf::Config.config_directory}/nessus_index"
end