diff --git a/lib/nexpose.rb b/lib/nexpose.rb index e3ba32b8..5f1633ec 100644 --- a/lib/nexpose.rb +++ b/lib/nexpose.rb @@ -65,6 +65,7 @@ require 'nexpose/ajax' require 'nexpose/api_request' require 'nexpose/asset' +require 'nexpose/blackout' require 'nexpose/common' require 'nexpose/console' require 'nexpose/credential' @@ -78,6 +79,7 @@ require 'nexpose/filter' require 'nexpose/discovery' require 'nexpose/discovery/filter' +require 'nexpose/global_blackout' require 'nexpose/global_settings' require 'nexpose/group' require 'nexpose/dag' diff --git a/lib/nexpose/blackout.rb b/lib/nexpose/blackout.rb new file mode 100644 index 00000000..f142d2a9 --- /dev/null +++ b/lib/nexpose/blackout.rb @@ -0,0 +1,44 @@ +module Nexpose +# Constants useful across the Nexpose module. +# Configuration structure for blackouts. + class Blackout < APIObject + # Whether or not this blackout is enabled. + attr_accessor :enabled + # Valid schedule types: daily, hourly, monthly-date, monthly-day, weekly. + attr_accessor :blackout_type + # The repeat interval based upon type. + attr_accessor :blackout_interval + # The earliest date to generate the report on (in ISO 8601 format). + attr_accessor :blackout_start + # The amount of time, in minutes, a blackout period should last. + attr_accessor :blackout_duration + + def initialize(start, enabled=true, duration, type, interval) + @blackout_start = start + @enabled =enabled + @blackout_duration = duration.to_i + @blackout_type = type + @blackout_interval = interval.to_i + end + + def self.from_hash(hash) + repeat_blackout_hash = hash[:repeat_blackout] + blackout = new(hash[:start_date], hash[:blackout_duration], repeat_blackout_hash[:type], repeat_blackout_hash[:interval]) + blackout + end + + def to_h + blackout_hash = { + start_date: @blackout_start, + enabled: @enabled, + blackout_duration: @blackout_duration, + } + repeat_hash= { + type: @blackout_type, + interval: @blackout_interval + } + blackout_hash[:repeat_blackout] = repeat_hash + blackout_hash + end + end +end \ No newline at end of file diff --git a/lib/nexpose/global_blackout.rb b/lib/nexpose/global_blackout.rb new file mode 100644 index 00000000..1bca28ad --- /dev/null +++ b/lib/nexpose/global_blackout.rb @@ -0,0 +1,43 @@ +module Nexpose + + class GlobalBlackout < APIObject + require 'json' + include JsonSerializer + + # [Array] Blackout starting dates, times and duration for blackout periods. + attr_accessor :blackout + + def initialize(blackout) + @blackout = Array(blackout) + end + + def save(nsc) + params = to_json + AJAX.post(nsc, '/api/2.1/silo_blackout/', params, AJAX::CONTENT_TYPE::JSON) + end + + def to_h + { + blackouts: + (@blackout || []).map { |blackout| blackout.to_h } + } + end + + def to_json + JSON.generate(to_h) + end + + def self.json_initializer(data) + new(blackout: data) + end + + def self.load(nsc) + uri = '/api/2.1/silo_blackout/' + resp = AJAX.get(nsc, uri, AJAX::CONTENT_TYPE::JSON) + hash = JSON.parse(resp, symbolize_names: true) + blackout = self.json_initializer(hash).deserialize(hash) + blackout.blackout = (hash[:blackouts] || []).map { |blackout| Nexpose::Blackout.from_hash(blackout) } + blackout + end + end +end diff --git a/lib/nexpose/site.rb b/lib/nexpose/site.rb index a67b4891..edce07bc 100644 --- a/lib/nexpose/site.rb +++ b/lib/nexpose/site.rb @@ -108,6 +108,8 @@ class Site < APIObject # [Array] Schedule starting dates and times for scans, and set their frequency. attr_accessor :schedules + # [Array] Blackout starting dates, times and duration for blackout periods. + attr_accessor :blackouts # The risk factor associated with this site. Default: 1.0 attr_accessor :risk_factor @@ -162,6 +164,7 @@ def initialize(name = nil, scan_template_id = 'full-audit-without-web-spider') @risk_factor = 1.0 @config_version = 3 @schedules = [] + @blackouts = [] @included_scan_targets = { addresses: [], asset_groups: [] } @excluded_scan_targets = { addresses: [], asset_groups: [] } @site_credentials = [] @@ -474,6 +477,7 @@ def to_h scan_template_id: @scan_template_id, risk_factor: @risk_factor, schedules: (@schedules || []).map {|schedule| schedule.to_h}, + blackouts: (@blackouts || []).map {|blackout| blackout.to_h}, shared_credentials: (@shared_credentials || []).map {|cred| cred.to_h}, site_credentials: (@site_credentials || []).map {|cred| cred.to_h}, web_credentials: (@web_credentials || []).map {|webCred| webCred.to_h}, @@ -510,6 +514,7 @@ def self.load(nsc, id) site.organization = Organization.create(site.organization) site.schedules = (hash[:schedules] || []).map {|schedule| Nexpose::Schedule.from_hash(schedule) } + site.blackouts = (hash[:blackouts] || []).map {|blackout| Nexpose::Blackout.from_hash(blackout) } site.site_credentials = hash[:site_credentials].map {|cred| Nexpose::SiteCredentials.new.object_from_hash(nsc,cred)} site.shared_credentials = hash[:shared_credentials].map {|cred| Nexpose::SiteCredentials.new.object_from_hash(nsc,cred)} site.discovery_config = Nexpose::DiscoveryConnection.new.object_from_hash(nsc, hash[:discovery_config]) unless hash[:discovery_config].nil?