Class: Nexpose::ScanTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/nexpose/scan_template.rb

Overview

Configuration object for a scan template.

The constructor is designed to take a valid XML representation of a scan template. If you wish to create a new scan template from scratch, use the #load method without a template ID. If you wish to copy and modify an existing template, use the #copy method.

This class is only a partial representation of some of the features available for configuration.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ ScanTemplate

Returns a new instance of ScanTemplate.

Parameters:

  • xml (String)

    XML representation of a scan template.



42
43
44
# File 'lib/nexpose/scan_template.rb', line 42

def initialize(xml)
  @xml = REXML::Document.new(xml)
end

Instance Attribute Details

#xmlObject (readonly)

Parsed XML of a scan template.



39
40
41
# File 'lib/nexpose/scan_template.rb', line 39

def xml
  @xml
end

Class Method Details

.copy(nsc, id) ⇒ ScanTemplate

Copy an existing scan template, changing the id and title.

Parameters:

  • nsc (Connection)

    API connection to a Nexpose console.

  • id (String)

    Unique identifier of an existing scan template.

Returns:

  • (ScanTemplate)

    A copy of the requested scan template configuration.



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

def self.copy(nsc, id)
  dupe = load(nsc, id)
  dupe.id = '#NewScanTemplate#'
  dupe.title = "#{dupe.title} Copy"
  dupe
end

.load(nsc, id = nil) ⇒ ScanTemplate

Load a scan template.

Parameters:

  • nsc (Connection)

    API connection to a Nexpose console.

  • id (String) (defaults to: nil)

    Unique identifier of an existing scan template. If no ID is provided, a blank, base template will be returned.

Returns:

  • (ScanTemplate)

    The requested scan template configuration.



320
321
322
323
324
325
326
327
328
# File 'lib/nexpose/scan_template.rb', line 320

def self.load(nsc, id = nil)
  if id
    response = JSON.parse(AJAX.get(nsc, "/data/scan/templates/#{URI.encode(id)}"))
    xml = response['value']
  else
    xml = AJAX.get(nsc, '/ajax/scantemplate_config.txml')
  end
  new(xml)
end

Instance Method Details

#_disable_check(check, elem) ⇒ Object



246
247
248
249
250
# File 'lib/nexpose/scan_template.rb', line 246

def _disable_check(check, elem)
  checks = REXML::XPath.first(@xml, '//VulnerabilityChecks')
  checks.elements.delete("Enabled/#{elem}[@name='#{check}']")
  checks.elements['Disabled'].add_element(elem, { 'name' => check })
end

#_enable_check(check, elem) ⇒ Object



240
241
242
243
244
# File 'lib/nexpose/scan_template.rb', line 240

def _enable_check(check, elem)
  checks = REXML::XPath.first(@xml, '//VulnerabilityChecks')
  checks.elements.delete("Disabled/#{elem}[@name='#{check}']")
  checks.elements['Enabled'].add_element(elem, { 'name' => check })
end

#_remove_check(check, elem) ⇒ Object



252
253
254
255
256
# File 'lib/nexpose/scan_template.rb', line 252

def _remove_check(check, elem)
  checks = REXML::XPath.first(@xml, '//VulnerabilityChecks')
  checks.elements.delete("Disabled/#{elem}[@name='#{check}']")
  checks.elements.delete("Enabled/#{elem}[@name='#{check}']")
end

#checks_by_categoryArray[String]

Get a list of the check categories enabled for this scan template.

Returns:

  • (Array[String])

    List of enabled categories.



176
177
178
179
# File 'lib/nexpose/scan_template.rb', line 176

def checks_by_category
  checks = REXML::XPath.first(@xml, '//VulnerabilityChecks/Enabled')
  checks.elements.to_a('VulnCategory').map { |c| c.attributes['name'] }
end

#checks_by_typeArray[String]

Get a list of the check types enabled for this scan template.

Returns:

  • (Array[String])

    List of enabled check types.



210
211
212
213
# File 'lib/nexpose/scan_template.rb', line 210

def checks_by_type
  checks = REXML::XPath.first(@xml, '//VulnerabilityChecks/Enabled')
  checks.elements.to_a('CheckType').map { |c| c.attributes['name'] }
end

#correlate=(enable) ⇒ Object

Adjust whether to correlate reliable checks with regular checks.

Parameters:

  • enable (Boolean)

    Whether to turn on vulnerability correlation.



139
140
141
142
# File 'lib/nexpose/scan_template.rb', line 139

def correlate=(enable)
  vuln_checks = REXML::XPath.first(@xml, 'ScanTemplate/VulnerabilityChecks')
  vuln_checks.attributes['correlate'] = enable ? '1' : '0'
end

#correlate?Boolean

Returns Whether to correlate reliable checks with regular checks.

Returns:

  • (Boolean)

    Whether to correlate reliable checks with regular checks.



132
133
134
135
# File 'lib/nexpose/scan_template.rb', line 132

def correlate?
  vuln_checks = REXML::XPath.first(@xml, 'ScanTemplate/VulnerabilityChecks')
  vuln_checks.attributes['correlate'] == '1'
end

#delete(nsc) ⇒ Object

Delete this scan template from the console. Cannot be used to delete a built-in template.

Parameters:

  • nsc (Connection)

    API connection to a Nexpose console.



348
349
350
# File 'lib/nexpose/scan_template.rb', line 348

def delete(nsc)
  nsc.delete_scan_template(@id)
end

#descriptionString

Returns Description of this scan template.

Returns:

  • (String)

    Description of this scan template.



73
74
75
76
# File 'lib/nexpose/scan_template.rb', line 73

def description
  desc = REXML::XPath.first(@xml, 'ScanTemplate/templateDescription')
  desc.nil? ? nil : desc.text.to_s
end

#description=(description) ⇒ Object

Assign a description to this scan template. Require attribute.

Parameters:

  • description (String)

    Description of the scan template.



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/nexpose/scan_template.rb', line 80

def description=(description)
  desc = REXML::XPath.first(@xml, 'ScanTemplate/templateDescription')
  if desc
    desc.text = description
  else
    root = REXML::XPath.first(xml, 'ScanTemplate')
    desc = REXML::Element.new('templateDescription')
    desc.add_text(description)
    root.add_element(desc)
  end
end

#disable_checks_by_category(category) ⇒ Object

Disable checks by category for this template.

Parameters:

  • category (String)

    Category to disable. @see #list_vuln_categories



193
194
195
# File 'lib/nexpose/scan_template.rb', line 193

def disable_checks_by_category(category)
  _disable_check(category, 'VulnCategory')
end

#disable_checks_by_type(type) ⇒ Object

Disable checks by type for this template.

Parameters:

  • type (String)

    Type to disable. @see #list_vuln_types



227
228
229
# File 'lib/nexpose/scan_template.rb', line 227

def disable_checks_by_type(type)
  _disable_check(type, 'CheckType')
end

#disable_vuln_check(check_id) ⇒ Object

Disable individual check for this template.

Parameters:

  • check_id (String)

    Unique identifier of vuln check.



281
282
283
284
285
# File 'lib/nexpose/scan_template.rb', line 281

def disable_vuln_check(check_id)
  checks = REXML::XPath.first(@xml, '//VulnerabilityChecks')
  checks.elements.delete("Enabled/Check[@id='#{check_id}']")
  checks.elements['Disabled'].add_element('Check', { 'id' => check_id })
end

#enable_checks_by_category(category) ⇒ Object

Enable checks by category for this template.

Parameters:

  • category (String)

    Category to enable. @see #list_vuln_categories



185
186
187
# File 'lib/nexpose/scan_template.rb', line 185

def enable_checks_by_category(category)
  _enable_check(category, 'VulnCategory')
end

#enable_checks_by_type(type) ⇒ Object

Enable checks by type for this template.

Parameters:

  • type (String)

    Type to enable. @see #list_vuln_types



219
220
221
# File 'lib/nexpose/scan_template.rb', line 219

def enable_checks_by_type(type)
  _enable_check(type, 'CheckType')
end

#enable_vuln_check(check_id) ⇒ Object

Enable individual check for this template.

Parameters:

  • check_id (String)

    Unique identifier of vuln check.



271
272
273
274
275
# File 'lib/nexpose/scan_template.rb', line 271

def enable_vuln_check(check_id)
  checks = REXML::XPath.first(@xml, '//VulnerabilityChecks')
  checks.elements.delete("Disabled/Check[@id='#{check_id}']")
  checks.elements['Enabled'].add_element('Check', { 'id' => check_id })
end

#idString

Returns Unique identifier of the scan template.

Returns:

  • (String)

    Unique identifier of the scan template.



47
48
49
50
# File 'lib/nexpose/scan_template.rb', line 47

def id
  root = REXML::XPath.first(xml, 'ScanTemplate')
  root.attributes['id']
end

#nameString

Returns Name or title of this scan template.

Returns:

  • (String)

    Name or title of this scan template.



53
54
55
56
# File 'lib/nexpose/scan_template.rb', line 53

def name
  desc = REXML::XPath.first(@xml, 'ScanTemplate/templateDescription')
  desc.nil? ? nil : desc.attributes['title']
end

#name=(name) ⇒ Object

Assign name to this scan template. Required attribute.

Parameters:

  • name (String)

    Title to assign.



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/nexpose/scan_template.rb', line 60

def name=(name)
  desc = REXML::XPath.first(@xml, 'ScanTemplate/templateDescription')
  if desc
    desc.attributes['title'] = name
  else
    root = REXML::XPath.first(xml, 'ScanTemplate')
    desc = REXML::Element.new('templateDescription')
    desc.add_attribute('title', name)
    root.add_element(desc)
  end
end

#policy_scanning=(enable) ⇒ Object

Adjust whether to perform policy scanning with this template.

Parameters:

  • enable (Boolean)

    Whether to turn on policy scanning.



113
114
115
116
# File 'lib/nexpose/scan_template.rb', line 113

def policy_scanning=(enable)
  gen = REXML::XPath.first(@xml, 'ScanTemplate/General')
  gen.attributes['disablePolicyScan'] = enable ? '0' : '1'
end

#policy_scanning?Boolean

Returns Whether policy scanning in enabled.

Returns:

  • (Boolean)

    Whether policy scanning in enabled.



106
107
108
109
# File 'lib/nexpose/scan_template.rb', line 106

def policy_scanning?
  gen = REXML::XPath.first(@xml, 'ScanTemplate/General')
  gen.attributes['disablePolicyScan'] == '0'
end

#potential_checks=(enable) ⇒ Object

Adjust whether to perform potential vulnerability checks with this template.

Parameters:

  • enable (Boolean)

    Whether to turn on potential checks.



167
168
169
170
# File 'lib/nexpose/scan_template.rb', line 167

def potential_checks=(enable)
  checks = REXML::XPath.first(@xml, 'ScanTemplate/VulnerabilityChecks')
  checks.attributes['potential'] = enable ? '1' : '0'
end

#potential_checks?Boolean

Returns Whether potential vulnerability checks are performed with this template.

Returns:

  • (Boolean)

    Whether potential vulnerability checks are performed with this template.



160
161
162
163
# File 'lib/nexpose/scan_template.rb', line 160

def potential_checks?
  checks = REXML::XPath.first(@xml, 'ScanTemplate/VulnerabilityChecks')
  checks.attributes['potential'] == '1'
end

#remove_checks_by_category(category) ⇒ Object

Remove checks by category for this template. Removes both enabled and disabled checks.

Parameters:

  • category (String)

    Category to remove. @see #list_vuln_categories



202
203
204
# File 'lib/nexpose/scan_template.rb', line 202

def remove_checks_by_category(category)
  _remove_check(category, 'VulnCategory')
end

#remove_checks_by_type(type) ⇒ Object

Remove checks by type for this template. Removes both enabled and disabled checks.

Parameters:

  • type (String)

    Type to remove. @see #list_vuln_types



236
237
238
# File 'lib/nexpose/scan_template.rb', line 236

def remove_checks_by_type(type)
  _remove_check(type, 'CheckType')
end

#remove_vuln_check(check_id) ⇒ Object

Remove individual check for this template. Removes both enabled and disabled checks.

Parameters:

  • check_id (String)

    Unique identifier of vuln check.



292
293
294
295
296
# File 'lib/nexpose/scan_template.rb', line 292

def remove_vuln_check(check_id)
  checks = REXML::XPath.first(@xml, '//VulnerabilityChecks')
  checks.elements.delete("Disabled/Check[@id='#{check_id}']")
  checks.elements.delete("Enabled/Check[@id='#{check_id}']")
end

#save(nsc) ⇒ Object

Save this scan template configuration to a Nexpose console.

Parameters:

  • nsc (Connection)

    API connection to a Nexpose console.



302
303
304
305
306
307
308
309
310
311
# File 'lib/nexpose/scan_template.rb', line 302

def save(nsc)
  root = REXML::XPath.first(@xml, 'ScanTemplate')
  if root.attributes['id'] == '#NewScanTemplate#'
    response = JSON.parse(AJAX.post(nsc, '/data/scan/templates', xml))
    root.attributes['id'] = response['value']
  else
    response = JSON.parse(AJAX.put(nsc, "/data/scan/templates/#{URI.encode(id)}", xml))
  end
  response['value']
end

#unsafe_checks=(enable) ⇒ Object

Adjust whether to perform unsafe vulnerability checks with this template.

Parameters:

  • enable (Boolean)

    Whether to turn on unsafe checks.



153
154
155
156
# File 'lib/nexpose/scan_template.rb', line 153

def unsafe_checks=(enable)
  checks = REXML::XPath.first(@xml, 'ScanTemplate/VulnerabilityChecks')
  checks.attributes['unsafe'] = enable ? '1' : '0'
end

#unsafe_checks?Boolean

Returns Whether unsafe vulnerability checks are performed by this template.

Returns:

  • (Boolean)

    Whether unsafe vulnerability checks are performed by this template.



146
147
148
149
# File 'lib/nexpose/scan_template.rb', line 146

def unsafe_checks?
  checks = REXML::XPath.first(@xml, 'ScanTemplate/VulnerabilityChecks')
  checks.attributes['unsafe'] == '1'
end

#vuln_checksArray[String]

Get a list of the individual vuln checks enabled for this scan template.

Returns:

  • (Array[String])

    List of enabled vulnerability checks.



262
263
264
265
# File 'lib/nexpose/scan_template.rb', line 262

def vuln_checks
  checks = REXML::XPath.first(@xml, '//VulnerabilityChecks/Enabled')
  checks.elements.to_a('Check').map { |c| c.attributes['id'] }
end

#vuln_scanning=(enable) ⇒ Object

Adjust whether to perform vuln scanning with this template.

Parameters:

  • enable (Boolean)

    Whether to turn on vuln scanning.



100
101
102
103
# File 'lib/nexpose/scan_template.rb', line 100

def vuln_scanning=(enable)
  gen = REXML::XPath.first(@xml, 'ScanTemplate/General')
  gen.attributes['disableVulnScan'] = enable ? '0' : '1'
end

#vuln_scanning?Boolean

Returns Whether vuln scanning in enabled.

Returns:

  • (Boolean)

    Whether vuln scanning in enabled.



93
94
95
96
# File 'lib/nexpose/scan_template.rb', line 93

def vuln_scanning?
  gen = REXML::XPath.first(@xml, 'ScanTemplate/General')
  gen.attributes['disableVulnScan'] == '0'
end

#web_spidering=(enable) ⇒ Object

Adjust whether to perform web spidering with this template.

Parameters:

  • enable (Boolean)

    Whether to turn on web spider scanning.



126
127
128
129
# File 'lib/nexpose/scan_template.rb', line 126

def web_spidering=(enable)
  gen = REXML::XPath.first(@xml, 'ScanTemplate/General')
  gen.attributes['disableWebSpider'] = enable ? '0' : '1'
end

#web_spidering?Boolean

Returns Whether web spidering in enabled.

Returns:

  • (Boolean)

    Whether web spidering in enabled.



119
120
121
122
# File 'lib/nexpose/scan_template.rb', line 119

def web_spidering?
  gen = REXML::XPath.first(@xml, 'ScanTemplate/General')
  gen.attributes['disableWebSpider'] == '0'
end