Module: Nexpose::NexposeAPI

Included in:
Connection
Defined in:
lib/nexpose.rb

Instance Method Summary collapse

Instance Method Details

#asset_group_config(group_id) ⇒ Object


Returns an asset group configuration information for a specific group ID




376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/nexpose.rb', line 376

def asset_group_config(group_id)
  r = execute(make_xml('AssetGroupConfigRequest', {'group-id' => group_id}))

  if r.success
    res = []
    r.res.elements.each('//Devices/device') do |device_info|
      res << {
          :device_id => device_info.attributes['id'].to_i,
          :site_id => device_info.attributes['site-id'].to_i,
          :address => device_info.attributes['address'].to_s,
          :riskfactor => device_info.attributes['riskfactor'].to_f,
      }
    end
    res
  else
    false
  end
end

#asset_group_delete(connection, id, debug = false) ⇒ Object



346
347
348
349
# File 'lib/nexpose.rb', line 346

def asset_group_delete(connection, id, debug = false)
  r = execute(make_xml('AssetGroupDeleteRequest', { 'group-id' => param }))
  r.success
end

#asset_groups_listingObject


Returns all asset group information




354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/nexpose.rb', line 354

def asset_groups_listing()
  r = execute(make_xml('AssetGroupListingRequest'))

  if r.success
    res = []
    r.res.elements.each('//AssetGroupSummary') do |group|
      res << {
          :asset_group_id => group.attributes['id'].to_i,
          :name => group.attributes['name'].to_s,
          :description => group.attributes['description'].to_s,
          :risk_score => group.attributes['riskscore'].to_f,
      }
    end
    res
  else
    false
  end
end

#console_command(cmd_string) ⇒ Object



879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
# File 'lib/nexpose.rb', line 879

def console_command(cmd_string)
  xml = make_xml('ConsoleCommandRequest', {  })
  cmd = REXML::Element.new('Command')
  cmd.text = cmd_string
  xml << cmd
  
  r = execute(xml)

  if(r.success)
    res = ""
    r.res.elements.each("//Output") do |out|
      res << out.text.to_s
    end
    
    return res
  else
    return false
  end
end

#create_multi_tenant_user(user_config, silo_configs) ⇒ Object


Creates a multi-tenant user

user_config - A map of the user data.

REQUIRED PARAMS user-id, authsrcid, user-name, full-name, enabled, superuser

OPTIONAL PARAMS email, password

silo_configs - An array of maps of silo specific data

REQUIRED PARAMS silo-id, role-name, all-groups, all-sites, default-silo

allowed_groups/allowed_sites - An array of ids




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
# File 'lib/nexpose.rb', line 520

def create_multi_tenant_user(user_config, silo_configs)
  xml = make_xml('MultiTenantUserCreateRequest')
  mtu_config_xml = make_xml('MultiTenantUserConfig', user_config, '', false)

  # Add the silo access

  silo_xml = make_xml('SiloAccesses', {}, '', false)
  silo_config_xml = make_xml('SiloAccess', {}, '', false)
  silo_configs.keys.each do |k|
    if k.eql? 'allowed_sites'
      allowed_sites_xml = make_xml('AllowedSites', {}, '', false)
      silo_configs['allowed_sites'].each do |allowed_site|
        allowed_sites_xml.add_element make_xml('AllowedSite', {'id' => allowed_site}, '', false)
      end
      silo_config_xml.add_element allowed_sites_xml
    elsif k.eql? 'allowed_groups'
      allowed_groups_xml = make_xml('AllowedGroups', {}, '', false)
      silo_configs['allowed_groups'].each do |allowed_group|
        allowed_groups_xml.add_element make_xml('AllowedGroup', {'id' => allowed_group}, '', false)
      end
      silo_config_xml.add_element allowed_groups_xml
    else
      silo_config_xml.attributes[k] = silo_configs[k]
    end
  end
  silo_xml.add_element silo_config_xml
  mtu_config_xml.add_element silo_xml
  xml.add_element mtu_config_xml
  r = execute xml, '1.2'
  r.success
end

#create_silo(silo_config) ⇒ Object


Creates a silo

silo_config - A map of the silo creation data.

REQUIRED PARAMS id, name, silo-profile-id, max-assets, max-hosted-assets, max-users

OPTIONAL PARAMS description




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
775
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
# File 'lib/nexpose.rb', line 726

def create_silo silo_config
  xml = make_xml 'SiloCreateRequest'
  silo_config_xml = make_xml 'SiloConfig', {}, '', false

  # Add the attributes

  silo_config.keys.each do |key|
    if not 'merchant'.eql? key and not 'organization'.eql? key
      silo_config_xml.attributes[key] = silo_config[key]
    end
  end

  # Add Organization info

  if silo_config['organization']
    org_xml = make_xml 'Organization', {}, '', false
    silo_config['organization'].keys.each do |key|
      if not 'address'.eql? key
        org_xml.attributes[key] = silo_config['organization'][key]
      end
    end

    address_xml = make_xml 'Address', silo_config['organization']['address'], '', false
    org_xml.add_element address_xml
    silo_config_xml.add_element org_xml
  end

  # Add Merchant info

  if silo_config['merchant']
     merchant_xml = make_xml 'Merchant', {}, '', false

    silo_config['merchant'].keys.each do |key|
      if not 'dba'.eql? key and not 'other_industries'.eql? key and not 'qsa'.eql? key and not 'address'.eql? key
        merchant_xml.attributes[key] = silo_config['merchant'][key]
      end
    end

     # Add the merchant address

     merchant_address_xml = make_xml 'Address', silo_config['merchant']['address'], '', false
     merchant_xml.add_element merchant_address_xml

     #Now add the complex data types

     if silo_config['merchant']['dba']
       dba_xml = make_xml 'DBAs', {}, '', false
       silo_config['merchant']['dba'].each do |name|
        dba_xml.add_element make_xml('DBA', {'name' => name}, '', false)
      end
      merchant_xml.add_element dba_xml
    end

    if silo_config['merchant']['other_industries']
      ois_xml = make_xml 'OtherIndustries', {}, '', false
      silo_config['merchant']['other_industries'].each do |name|
        ois_xml.add_element make_xml('Industry', {'name' => name}, '', false)
      end
      merchant_xml.add_element ois_xml
    end

    if silo_config['merchant']['qsa']
      qsa_xml = make_xml 'QSA', {}, '', false
      silo_config['merchant']['qsa'].keys.each do |key|
        if not 'address'.eql? key
          qsa_xml.attributes[key] = silo_config['merchant']['qsa'][key]
        end
      end

      # Add the address for this QSA

      address_xml = make_xml 'Address', silo_config['merchant']['qsa']['address'], '', false

      qsa_xml.add_element address_xml
      merchant_xml.add_element qsa_xml
    end
    silo_config_xml.add_element merchant_xml
  end

  xml.add_element silo_config_xml
  r = execute xml, '1.2'
  r.success
end

#create_silo_profile(silo_profile_config, permissions) ⇒ Object


Creates a silo profile

silo_config - A map of the silo data.

REQUIRED PARAMS id, name, all‐licensed-modules, all‐global-engines, all-global-report-templates, all‐global-scan‐templates

OPTIONAL PARAMS description

permissions - A map of an array of maps of silo specific data

REQUIRED PARAMS silo-id, role-name, all-groups, all-sites, default-silo

allowed_groups/allowed_sites - An array of ids




612
613
614
615
616
617
618
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
665
666
667
668
# File 'lib/nexpose.rb', line 612

def create_silo_profile silo_profile_config, permissions
  xml = make_xml 'SiloProfileCreateRequest'
  spc_xml = make_xml('SiloProfileConfig', silo_profile_config, '', false)

  # Add the permissions

  if permissions['global_report_templates']
    grt_xml = make_xml('GlobalReportTemplates', {}, '', false)
    permissions['global_report_templates'].each do |name|
      grt_xml.add_element make_xml('GlobalReportTemplate', {'name' => name}, '', false)
    end
    spc_xml.add_element grt_xml
  end

  if permissions['global_scan_engines']
    gse_xml = make_xml('GlobalScanEngines', {}, '', false)
    permissions['global_scan_engines'].each do |name|
      gse_xml.add_element make_xml('GlobalScanEngine', {'name' => name}, '', false)
    end
    spc_xml.add_element gse_xml
  end

  if permissions['global_scan_templates']
    gst_xml = make_xml('GlobalScanTemplates', {}, '', false)
    permissions['global_scan_templates'].each do |name|
      gst_xml.add_element make_xml('GlobalScanTemplate', {'name' => name}, '', false)
    end
    spc_xml.add_element gst_xml
  end

  if permissions['licensed_modules']
    lm_xml = make_xml('LicensedModules', {}, '', false)
    permissions['licensed_modules'].each do |name|
      lm_xml.add_element make_xml('LicensedModule', {'name' => name}, '', false)
    end
    spc_xml.add_element lm_xml
  end

  if permissions['restricted_report_formats']
    rrf_xml = make_xml('RestrictedReportFormats', {}, '', false)
    permissions['restricted_report_formats'].each do |name|
      rrf_xml.add_element make_xml('RestrictedReportFormat', {'name' => name}, '', false)
    end
    spc_xml.add_element rrf_xml
  end

  if permissions['restricted_report_sections']
    rrs_xml = make_xml('RestrictedReportSections', {}, '', false)
    permissions['restricted_report_sections'].each do |name|
      rrs_xml.add_element make_xml('RestrictedReportSection', {'name' => name}, '', false)
    end
    spc_xml.add_element rrs_xml
  end

  xml.add_element spc_xml
  r = execute xml, '1.2'
  r.success
end

#delete_mtu(user_name, user_id) ⇒ Object


Delete a multi-tenant user




583
584
585
586
587
588
# File 'lib/nexpose.rb', line 583

def delete_mtu user_name, user_id
  using_user_name = (user_name and not user_name.empty?)
  xml = make_xml('MultiTenantUserDeleteRequest', (using_user_name ? {'user-name' => user_name} : {'user-id' => user_id}))
  r = execute xml, '1.2'
  r.success
end

#delete_silo(name, id) ⇒ Object


Delete a silo




829
830
831
832
833
834
# File 'lib/nexpose.rb', line 829

def delete_silo name, id
  using_name = (name and not name.empty?)
  xml = make_xml('SiloDeleteRequest', (using_name ? {'silo-name' => name} : {'silo-id' => id}))
  r = execute xml, '1.2'
  r.success
end

#delete_silo_profile(name, id) ⇒ Object


Delete a silo profile




704
705
706
707
708
709
# File 'lib/nexpose.rb', line 704

def delete_silo_profile name, id
  using_name = (name and not name.empty?)
  xml = make_xml('SiloProfileDeleteRequest', (using_name ? {'name' => name} : {'silo-profile-id' => id}))
  r = execute xml, '1.2'
  r.success
end

#device_delete(param) ⇒ Object



341
342
343
344
# File 'lib/nexpose.rb', line 341

def device_delete(param)
  r = execute(make_xml('DeviceDeleteRequest', { 'site-id' => param }))
  r.success
end

#list_mtuObject


Lists all the multi-tenant users and their attributes.




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

def list_mtu
  xml = make_xml('MultiTenantUserListingRequest')
  r = execute xml, '1.2'

  if r.success
    res = []
    r.res.elements.each("//MultiTenantUserSummary") do |mtu|
      res << {
        :id => mtu.attributes['id'],
        :full_name => mtu.attributes['full-name'],
        :user_name => mtu.attributes['user-name'],
        :email => mtu.attributes['email'],
        :super_user => mtu.attributes['superuser'],
        :enabled => mtu.attributes['enabled'],
        :auth_module => mtu.attributes['auth-module'],
        :silo_count => mtu.attributes['silo-count'],
        :locked => mtu.attributes['locked']
      }
    end
    res
  else
    false
  end
end

#list_silo_profilesObject


Lists all the silo profiles and their attributes.




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
# File 'lib/nexpose.rb', line 673

def list_silo_profiles
  xml = make_xml('SiloProfileListingRequest')
  r = execute xml, '1.2'

  if r.success
    res = []
    r.res.elements.each("//SiloProfileSummary") do |silo_profile|
      res << {
        :id => silo_profile.attributes['id'],
        :name => silo_profile.attributes['name'],
        :description => silo_profile.attributes['description'],
        :global_report_template_count => silo_profile.attributes['global-report-template-count'],
        :global_scan_engine_count => silo_profile.attributes['global-scan-engine-count'],
        :global_scan_template_count => silo_profile.attributes['global-scan-template-count'],
        :licensed_module_count => silo_profile.attributes['licensed-module-count'],
        :restricted_report_section_count => silo_profile.attributes['restricted-report-section-count'],
        :all_licensed_modules => silo_profile.attributes['all-licensed-modules'],
        :all_global_engines => silo_profile.attributes['all-global-engines'],
        :all_global_report_templates => silo_profile.attributes['all-global-report-templates'],
        :all_global_scan_templates => silo_profile.attributes['all-global-scan-templates']
      }
    end
    res
  else
    false
  end
end

#list_silosObject


Lists all the silos and their attributes.




807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
# File 'lib/nexpose.rb', line 807

def list_silos
  xml = make_xml('SiloListingRequest')
  r = execute xml, '1.2'

  if r.success
    res = []
    r.res.elements.each("//SiloSummary") do |silo_profile|
      res << {
        :id => silo_profile.attributes['id'],
        :name => silo_profile.attributes['name'],
        :description => silo_profile.attributes['description']
      }
    end
    res
  else
    false
  end
end

#make_xml(name, opts = {}, data = '', append_session_id = true) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/nexpose.rb', line 223

def make_xml(name, opts={}, data='', append_session_id=true)
  xml = REXML::Element.new(name)
  if(@session_id and append_session_id)
    xml.attributes['session-id'] = @session_id
  end

  opts.keys.each do |k|
    xml.attributes[k] = "#{opts[k]}"
  end
  
  xml.text = data

  xml
end

#report_config_delete(param) ⇒ Object



331
332
333
334
# File 'lib/nexpose.rb', line 331

def report_config_delete(param)
  r = execute(make_xml('ReportDeleteRequest', { 'reportcfg-id' => param }))
  r.success
end

#report_delete(param) ⇒ Object



336
337
338
339
# File 'lib/nexpose.rb', line 336

def report_delete(param)
  r = execute(make_xml('ReportDeleteRequest', { 'report-id' => param }))
  r.success
end

#report_generate(param) ⇒ Object



306
307
308
309
# File 'lib/nexpose.rb', line 306

def report_generate(param)
  r = execute(make_xml('ReportGenerateRequest', { 'report-id' => param }))
  r.success
end

#report_history(param) ⇒ Object



327
328
329
# File 'lib/nexpose.rb', line 327

def report_history(param)
  execute(make_xml('ReportHistoryRequest', { 'reportcfg-id' => param }))
end

#report_last(param) ⇒ Object



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/nexpose.rb', line 311

def report_last(param)
  r = execute(make_xml('ReportHistoryRequest', { 'reportcfg-id' => param }))
  res = nil
  if(r.success)
    stk = []
    r.res.elements.each("//ReportSummary") do |rep|
      stk << [ rep.attributes['id'].to_i, rep.attributes['report-URI'] ]
    end
    if (stk.length > 0)
      stk.sort!{|a,b| b[0] <=> a[0]}
      res = stk[0][1]
    end
  end
  res
end

#report_template_listingObject



855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
# File 'lib/nexpose.rb', line 855

def report_template_listing
  r = execute(make_xml('ReportTemplateListingRequest', { }))

  if(r.success)
    res = []
    r.res.elements.each("//ReportTemplateSummary") do |template|
      desc = ''
      template.elements.each("//description") do |ent|
        desc = ent.text
      end
      
      res << {
        :template_id   => template.attributes['id'].to_s,
        :name          => template.attributes['name'].to_s,
        :description   => desc.to_s
      }
    end
    return res
  else
    return false
  end
end

#scan_activityObject



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/nexpose.rb', line 248

def scan_activity
  r = execute(make_xml('ScanActivityRequest', { }))
  if(r.success)
    res = []
    r.res.elements.each("//ScanSummary") do |scan|
      res << {
        :scan_id    => scan.attributes['scan-id'].to_i,
        :site_id    => scan.attributes['site-id'].to_i,
        :engine_id  => scan.attributes['engine-id'].to_i,
        :status     => scan.attributes['status'].to_s,
        :start_time => Date.parse(scan.attributes['startTime'].to_s).to_time
      }
    end
    return res
  else
    return false
  end
end

#scan_statistics(param) ⇒ Object



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
303
304
# File 'lib/nexpose.rb', line 267

def scan_statistics(param)
  r = execute(make_xml('ScanStatisticsRequest', {'scan-id' => param }))
  if(r.success)
    res = {}
    r.res.elements.each("//ScanSummary/nodes") do |node|
      res[:nodes] = {}
      node.attributes.keys.each do |k|
        res[:nodes][k] = node.attributes[k].to_i
      end
    end
    r.res.elements.each("//ScanSummary/tasks") do |task|
      res[:task] = {}
      task.attributes.keys.each do |k|
        res[:task][k] = task.attributes[k].to_i
      end
    end
    r.res.elements.each("//ScanSummary/vulnerabilities") do |vuln|
      res[:vulns] ||= {}
      k = vuln.attributes['status'] + (vuln.attributes['severity'] ? ("-" + vuln.attributes['severity']) : '')
      res[:vulns][k] = vuln.attributes['count'].to_i
    end
    r.res.elements.each("//ScanSummary") do |summ|
      res[:summary] = {}
      summ.attributes.keys.each do |k|
        res[:summary][k] = summ.attributes[k]
        if (res[:summary][k] =~ /^\d+$/)
          res[:summary][k] = res[:summary][k].to_i
        end
      end
     end
     r.res.elements.each("//ScanSummary/message") do |message|
       res[:message] = message.text
     end
    return res
  else
    return false
  end
end

#scan_status(param) ⇒ Object



243
244
245
246
# File 'lib/nexpose.rb', line 243

def scan_status(param)
  r = execute(make_xml('ScanStatusRequest', { 'scan-id' => param }))
  r.success ? r.attributes['status'] : nil
end

#scan_stop(param) ⇒ Object



238
239
240
241
# File 'lib/nexpose.rb', line 238

def scan_stop(param)
  r = execute(make_xml('ScanStopRequest', { 'scan-id' => param }))
  r.success
end

#site_delete(param) ⇒ Object



447
448
449
450
# File 'lib/nexpose.rb', line 447

def site_delete(param)
  r = execute(make_xml('SiteDeleteRequest', { 'site-id' => param }))
  r.success
end

#site_device_listing(site_id) ⇒ Object



836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
# File 'lib/nexpose.rb', line 836

def site_device_listing(site_id)
  r = execute(make_xml('SiteDeviceListingRequest', { 'site-id' => site_id.to_s }))

  if(r.success)
    res = []
    r.res.elements.each("//device") do |device|
      res << {
        :device_id     => device.attributes['id'].to_i,
        :address       => device.attributes['address'].to_s,
        :risk_factor   => device.attributes['risk_factor'].to_f,
        :risk_score    => device.attributes['risk_score'].to_f,
      }
    end
    return res
  else
    return false
  end
end

#site_device_scan_start(site_id, devices, hosts) ⇒ Object


Starts device specific site scanning.

devices - An Array of device IDs hosts - An Array of Hashes [o]=>:range=>“to,from” [1]=>:host=>host




401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/nexpose.rb', line 401

def site_device_scan_start(site_id, devices, hosts)

  if hosts == nil and devices == nil
    raise ArgumentError.new("Both the device and host list is nil")
  end

  xml = make_xml('SiteDevicesScanRequest', {'site-id' => site_id})

  if devices != nil
    inner_xml = REXML::Element.new 'Devices'
    for device_id in devices
      inner_xml.add_element 'device', {'id' => "#{device_id}"}
    end
    xml.add_element inner_xml
  end

  if hosts != nil
    inner_xml = REXML::Element.new 'Hosts'
    hosts.each_index do |x|
      if hosts[x].key? :range
        to = hosts[x][:range].split(',')[0]
        from = hosts[x][:range].split(',')[1]
        inner_xml.add_element 'range', {'to' => "#{to}", 'from' => "#{from}"}
      end
      if hosts[x].key? :host
        host_element = REXML::Element.new 'host'
        host_element.text = "#{hosts[x][:host]}"
        inner_xml.add_element host_element
      end
    end
    xml.add_element inner_xml
  end

  r = execute xml
  if r.success
    r.res.elements.each('//Scan') do |scan_info|
      return {
          :scan_id => scan_info.attributes['scan-id'].to_i,
          :engine_id => scan_info.attributes['engine-id'].to_i
      }
    end
  else
    false
  end
end

#site_listingObject



452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/nexpose.rb', line 452

def site_listing
  r = execute(make_xml('SiteListingRequest', { }))
  
  if(r.success)
    res = []
    r.res.elements.each("//SiteSummary") do |site|
      res << {
        :site_id       => site.attributes['id'].to_i,
        :name          => site.attributes['name'].to_s,
        :risk_factor   => site.attributes['risk_factor'].to_f,
        :risk_score    => site.attributes['risk_score'].to_f,
      }
    end
    return res
  else
    return false
  end
end

#site_scan_history(site_id) ⇒ Object


TODO: Needs to be expanded to included details




474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/nexpose.rb', line 474

def site_scan_history(site_id)
  r = execute(make_xml('SiteScanHistoryRequest', {'site-id' => site_id.to_s}))

  if (r.success)
    res = []
    r.res.elements.each("//ScanSummary") do |site_scan_history|
      res << {
          :site_id => site_scan_history.attributes['site-id'].to_i,
          :scan_id => site_scan_history.attributes['scan-id'].to_i,
          :engine_id => site_scan_history.attributes['engine-id'].to_i,
          :start_time => site_scan_history.attributes['startTime'].to_s,
          :end_time => site_scan_history.attributes['endTime'].to_s
      }
    end
    res
  else
    false
  end
end

#system_informationObject



899
900
901
902
903
904
905
906
907
908
909
910
911
912
# File 'lib/nexpose.rb', line 899

def system_information
  r = execute(make_xml('SystemInformationRequest', { }))

  if(r.success)
    res = {}
    r.res.elements.each("//Statistic") do |stat|
      res[ stat.attributes['name'].to_s ] = stat.text.to_s
    end
    
    return res
  else
    return false
  end
end