Class: Nexpose::Site

Inherits:
Object
  • Object
show all
Includes:
Sanitize
Defined in:
lib/nexpose/site.rb

Overview

Configuration object representing a Nexpose site.

For a basic walk-through, see https://github.com/rapid7/nexpose-client/wiki/Using-Sites

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sanitize

#replace_entities

Constructor Details

#initialize(name = nil, scan_template = 'full-audit') ⇒ Site

Site constructor. Both arguments are optional.

Parameters:

  • name (String) (defaults to: nil)

    Unique name of the site.

  • scan_template (String) (defaults to: 'full-audit')

    ID of the scan template to use.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/nexpose/site.rb', line 127

def initialize(name = nil, scan_template = 'full-audit')
  @name = name
  @scan_template = scan_template

  @id = -1
  @risk_factor = 1.0
  @config_version = 3
  @is_dynamic = false
  @assets = []
  @schedules = []
  @credentials = []
  @alerts = []
  @exclude = []
end

Instance Attribute Details

#alertsObject

Array

Collection of real-time alerts.



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

def alerts
  @alerts
end

#assetsObject

Array

Collection of assets. May be IPv4, IPv6, or DNS names.

See Also:



83
84
85
# File 'lib/nexpose/site.rb', line 83

def assets
  @assets
end

#config_versionObject

Configuration version. Default: 3



116
117
118
# File 'lib/nexpose/site.rb', line 116

def config_version
  @config_version
end

#credentialsObject

Array

Collection of credentials associated with this site. Does not

include shared credentials.



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

def credentials
  @credentials
end

#descriptionObject

Description of the site.



78
79
80
# File 'lib/nexpose/site.rb', line 78

def description
  @description
end

#engineObject

Scan Engine to use. Will use the default engine if nil or -1.



96
97
98
# File 'lib/nexpose/site.rb', line 96

def engine
  @engine
end

#excludeObject

Array

Collection of excluded assets. May be IPv4, IPv6, or DNS names.



86
87
88
# File 'lib/nexpose/site.rb', line 86

def exclude
  @exclude
end

#idObject

The site ID. An ID of -1 is used to designate a site that has not been saved to a Nexpose console.



72
73
74
# File 'lib/nexpose/site.rb', line 72

def id
  @id
end

#is_dynamicObject

Whether or not this site is dynamic. Dynamic sites are created through Asset Discovery Connections. Modifying their behavior through the API is not recommended.



121
122
123
# File 'lib/nexpose/site.rb', line 121

def is_dynamic
  @is_dynamic
end

#nameObject

Unique name of the site. Required.



75
76
77
# File 'lib/nexpose/site.rb', line 75

def name
  @name
end

#risk_factorObject

The risk factor associated with this site. Default: 1.0



102
103
104
# File 'lib/nexpose/site.rb', line 102

def risk_factor
  @risk_factor
end

#scan_templateObject

Scan template to use when starting a scan job. Default: full-audit



89
90
91
# File 'lib/nexpose/site.rb', line 89

def scan_template
  @scan_template
end

#scan_template_nameObject

Friendly name of scan template to use when starting a scan job. Value is populated when a site is saved or loaded from a console.



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

def scan_template_name
  @scan_template_name
end

#schedulesObject

Array

Schedule starting dates and times for scans, and set their frequency.



99
100
101
# File 'lib/nexpose/site.rb', line 99

def schedules
  @schedules
end

Class Method Details

.copy(connection, id) ⇒ Site

Copy an existing configuration from a Nexpose instance. Returned object will reset the site ID and append “Copy” to the existing name.

Parameters:

  • connection (Connection)

    Connection to the security console.

  • id (Fixnum)

    Site ID of an existing site.

Returns:

  • (Site)

    Site configuration loaded from a Nexpose console.



208
209
210
211
212
213
# File 'lib/nexpose/site.rb', line 208

def self.copy(connection, id)
  site = self.load(connection, id)
  site.id = -1
  site.name = "#{site.name} Copy"
  site
end

.load(connection, id) ⇒ Site

Load an existing configuration from a Nexpose instance.

Parameters:

  • connection (Connection)

    Connection to console where site exists.

  • id (Fixnum)

    Site ID of an existing site.

Returns:

  • (Site)

    Site configuration loaded from a Nexpose console.



194
195
196
197
198
# File 'lib/nexpose/site.rb', line 194

def self.load(connection, id)
  r = APIRequest.execute(connection.url,
                         %(<SiteConfigRequest session-id="#{connection.session_id}" site-id="#{id}"/>))
  parse(r.res)
end

.parse(rexml) ⇒ Site

Parse a response from a Nexpose console into a valid Site object.

Parameters:

  • rexml (REXML::Document)

    XML document to parse.

Returns:

  • (Site)

    Site object represented by the XML. ## TODO What is returned on failure?



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

def self.parse(rexml)
  rexml.elements.each('SiteConfigResponse/Site') do |s|
    site = Site.new(s.attributes['name'])
    site.id = s.attributes['id'].to_i
    site.description = s.attributes['description']
    site.risk_factor = s.attributes['riskfactor'] || 1.0
    site.is_dynamic = true if s.attributes['isDynamic'] == '1'

    s.elements.each('Hosts/range') do |r|
      site.assets << IPRange.new(r.attributes['from'], r.attributes['to'])
    end
    s.elements.each('Hosts/host') do |host|
      site.assets << HostName.new(host.text)
    end

    s.elements.each('ExcludedHosts/range') do |r|
      site.exclude << IPRange.new(r.attributes['from'], r.attributes['to'])
    end
    s.elements.each('ExcludedHosts/host') do |host|
      site.exclude << HostName.new(host.text)
    end

    s.elements.each('Credentials/adminCredentials') do |cred|
      site.credentials << Credential.parse(cred)
    end

    s.elements.each('ScanConfig') do |scan_config|
      site.scan_template_name = scan_config.attributes['name']
      site.scan_template = scan_config.attributes['templateID']
      site.config_version = scan_config.attributes['configVersion'].to_i
      site.engine = scan_config.attributes['engineID'].to_i
      scan_config.elements.each('Schedules/Schedule') do |schedule|
        site.schedules << Schedule.parse(schedule)
      end
    end

    s.elements.each('Alerting/Alert') do |alert|
      site.alerts << Alert.parse(alert)
    end

    return site
  end
  nil
end

Instance Method Details

#add_asset(asset) ⇒ Object

Adds an asset to this site, resolving whether an IP or hostname is provided.

Parameters:

  • asset (String)

    Identifier of an asset, either IP or host name.



174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/nexpose/site.rb', line 174

def add_asset(asset)
  begin
    # If the asset registers as a valid IP, store as IP.
    ip = IPAddr.new(asset)
    add_ip(asset)
  rescue ArgumentError => e
    if e.message == 'invalid address'
      add_host(asset)
    else
      raise "Unable to parse asset: '#{asset}'. #{e.message}"
    end
  end
end

#add_host(hostname) ⇒ Object

Adds an asset to this site by host name.

Parameters:

  • hostname (String)

    FQDN or DNS-resolvable host name of an asset.



150
151
152
# File 'lib/nexpose/site.rb', line 150

def add_host(hostname)
  @assets << HostName.new(hostname)
end

#add_ip(ip) ⇒ Object

Adds an asset to this site by IP address.

Parameters:

  • ip (String)

    IP address of an asset.



157
158
159
# File 'lib/nexpose/site.rb', line 157

def add_ip(ip)
  @assets << IPRange.new(ip)
end

#add_ip_range(from, to) ⇒ Object

Adds assets to this site by IP address range.

Parameters:

  • from (String)

    Beginning IP address of a range.

  • to (String)

    Ending IP address of a range.



165
166
167
# File 'lib/nexpose/site.rb', line 165

def add_ip_range(from, to)
  @assets << IPRange.new(from, to)
end

#delete(connection) ⇒ Boolean

Delete this site from a Nexpose console.

Parameters:

  • connection (Connection)

    Connection to console where this site will be saved.

Returns:

  • (Boolean)

    Whether or not the site was successfully deleted.



230
231
232
233
# File 'lib/nexpose/site.rb', line 230

def delete(connection)
  r = connection.execute(%(<SiteDeleteRequest session-id="#{connection.session_id}" site-id="#{@id}"/>))
  r.success
end

#dynamic?Boolean

Returns true when the site is dynamic.

Returns:

  • (Boolean)


143
144
145
# File 'lib/nexpose/site.rb', line 143

def dynamic?
  is_dynamic
end

#save(connection) ⇒ Fixnum

Saves this site to a Nexpose console.

Parameters:

  • connection (Connection)

    Connection to console where this site will be saved.

Returns:

  • (Fixnum)

    Site ID assigned to this configuration, if successful.



220
221
222
223
# File 'lib/nexpose/site.rb', line 220

def save(connection)
  r = connection.execute('<SiteSaveRequest session-id="' + connection.session_id + '">' + to_xml + ' </SiteSaveRequest>')
  @id = r.attributes['site-id'].to_i if r.success
end

#scan(connection, sync_id = nil) ⇒ Scan

Scan this site.

Parameters:

  • connection (Connection)

    Connection to console where scan will be launched.

  • sync_id (String) (defaults to: nil)

    Optional synchronization token.

Returns:

  • (Scan)

    Scan launch information.



241
242
243
244
245
246
247
248
249
# File 'lib/nexpose/site.rb', line 241

def scan(connection, sync_id = nil)
  xml = REXML::Element.new('SiteScanRequest')
  xml.add_attributes({ 'session-id' => connection.session_id,
                       'site-id' => @id,
                       'sync-id' => sync_id })

  response = connection.execute(xml)
  Scan.parse(response.res) if response.success
end

#to_xmlString

Generate an XML representation of this site configuration

Returns:

  • (String)

    XML valid for submission as part of other requests.



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

def to_xml
  xml = %(<Site id='#{id}' name='#{replace_entities(name)}' description='#{description}' riskfactor='#{risk_factor}'>)

  xml << '<Hosts>'
  xml << assets.reduce('') { |a, e| a << e.to_xml }
  xml << '</Hosts>'

  unless exclude.empty?
    xml << '<ExcludedHosts>'
    xml << exclude.reduce('') { |a, e| a << e.to_xml }
    xml << '</ExcludedHosts>'
  end

  unless credentials.empty?
    xml << '<Credentials>'
    credentials.each do |c|
      xml << c.to_xml if c.respond_to? :to_xml
    end
    xml << '</Credentials>'
  end

  unless alerts.empty?
    xml << '<Alerting>'
    alerts.each do |a|
      xml << a.to_xml if a.respond_to? :to_xml
    end
    xml << '</Alerting>'
  end

  xml << %(<ScanConfig configID="#{@id}" name="#{@scan_template_name || @scan_template}" templateID="#{@scan_template}" configVersion="#{@config_version || 3}" engineID="#{@engine}">)

  xml << '<Schedules>'
  @schedules.each do |schedule|
    xml << schedule.to_xml
  end
  xml << '</Schedules>'
  xml << '</ScanConfig>'
  xml << '</Site>'
end