Class: NexposeTicketing::TicketRepository
- Inherits:
-
Object
- Object
- NexposeTicketing::TicketRepository
- Defined in:
- lib/nexpose_ticketing/ticket_repository.rb
Overview
Repository class that creates and returns generated reports.
Instance Method Summary collapse
-
#all_site_details ⇒ Object
Returns an array of all sites in the users environment.
-
#createVulnerabilityFilter(options = {}) ⇒ Object
Parses user-configured vulnerability filter categories and returns aforementioned categories in a format used by the Nexpose::AdhocReportConfig class.
- #define_query_methods ⇒ Object
- #generate_config(query_name, options, nexpose_items) ⇒ Object
- #generate_tag_asset_list(options = {}, report_config = Nexpose::AdhocReportConfig.new(nil, 'sql')) ⇒ Object
-
#initialize(options = nil) ⇒ TicketRepository
constructor
A new instance of TicketRepository.
-
#last_scans(options = {}) ⇒ Object
Gets the last scan information from nexpose sans the CSV headers.
-
#load_last_scans(options = {}, report_config = Nexpose::AdhocReportConfig.new(nil, 'sql')) ⇒ Object
Loads the last scan info to memory.
- #method_missing(name, *args, &block) ⇒ Object
- #nexpose_login(nexpose_data) ⇒ Object
-
#read_last_scans(csv_file_name) ⇒ Object
Reads a nexpose identifier (tag ID, site ID etc) scan history from disk.
- #read_tag_asset_list(csv_file_name) ⇒ Object
- #request_query(query_name, options = {}, nexpose_items = nil) ⇒ Object
-
#save_last_scans(csv_file_name, options = {}, saved_file = nil) ⇒ Object
Saves the last scan info to disk.
-
#save_to_file(csv_file_name, trimmed_csv, saved_file = nil) ⇒ Object
Saves CSV scan information to disk.
Constructor Details
#initialize(options = nil) ⇒ TicketRepository
Returns a new instance of TicketRepository.
13 14 15 16 17 18 19 20 |
# File 'lib/nexpose_ticketing/ticket_repository.rb', line 13 def initialize( = nil) @timeout = [:timeout] # Gets the suffix of the query method signatures based on the mode @method_suffix = [:query_suffix] define_query_methods end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
268 269 270 271 272 273 274 275 276 277 |
# File 'lib/nexpose_ticketing/ticket_repository.rb', line 268 def method_missing(name, *args, &block) full_method_name = "#{name}#{@method_suffix}" unless Queries.respond_to? full_method_name fail %Q{Query request "#{full_method_name}" not understood} end @log. %Q{Creating query request "#{full_method_name}".} request_query(full_method_name, args[0], args[1]) end |
Instance Method Details
#all_site_details ⇒ Object
Returns an array of all sites in the users environment.
-
Returns :
-
An array of Nexpose::SiteSummary objects.
-
47 48 49 |
# File 'lib/nexpose_ticketing/ticket_repository.rb', line 47 def all_site_details @nsc.sites end |
#createVulnerabilityFilter(options = {}) ⇒ Object
Parses user-configured vulnerability filter categories and returns aforementioned categories in a format used by the Nexpose::AdhocReportConfig class.
-
Args :
-
options
- A Hash with site(s), reported_scan_id and severity level.
-
-
Returns :
-
Returns String @vulnerability_categories
-
148 149 150 151 152 153 154 155 |
# File 'lib/nexpose_ticketing/ticket_repository.rb', line 148 def createVulnerabilityFilter( = {}) if [:vulnerabilityCategories].nil? || [:vulnerabilityCategories].empty? return nil end filter = [:vulnerabilityCategories].strip.split(',') filter.map { |category| "include:#{category}" }.join(',') end |
#define_query_methods ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/nexpose_ticketing/ticket_repository.rb', line 22 def define_query_methods methods = Queries.methods.grep Regexp.new (@method_suffix+'$') methods.each do |m| define_singleton_method m do |, override=nil| request_query(m, , override) end end end |
#generate_config(query_name, options, nexpose_items) ⇒ Object
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 |
# File 'lib/nexpose_ticketing/ticket_repository.rb', line 235 def generate_config(query_name, , nexpose_items) report_config = @report_helper.generate_sql_report_config() nexpose_item = [:nexpose_item] reported_scan_id = [:scan_id] # If it's a non-initial run, we need the last scan ID unless [:initial_run] fail 'Nexpose item cannot be null or empty' if nexpose_item.nil? || reported_scan_id.nil? end report_config.add_filter('version', '1.2.0') report_config.add_filter('query', Queries.send(query_name, )) id_type = [:tag_run] ? 'tag' : 'site' if nexpose_items != nil && !nexpose_items.empty? nexpose_items.each { |id| report_config.add_filter(id_type, id) } else item = [:tag_run] ? [:tag] : nexpose_item report_config.add_filter(id_type, item) end report_config.add_filter('vuln-severity', [:severity] || 0) vuln_filter_cats = createVulnerabilityFilter() unless vuln_filter_cats.nil? || vuln_filter_cats.empty? report_config.add_filter('vuln-categories', vuln_filter_cats) end @report_helper.save_generate_cleanup_report_config(report_config) end |
#generate_tag_asset_list(options = {}, report_config = Nexpose::AdhocReportConfig.new(nil, 'sql')) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/nexpose_ticketing/ticket_repository.rb', line 165 def generate_tag_asset_list( = {}, report_config = Nexpose::AdhocReportConfig.new(nil, 'sql')) report_config.add_filter('version', '1.2.0') = Array([:tags]) report_config.add_filter('query', Queries.last_tag_scans) .each { |tag| report_config.add_filter('tag', tag) } report_output = report_config.generate(@nsc, @timeout) csv_output = CSV.parse(report_output.chomp, headers: :first_row) trimmed_csv = [] trimmed_csv << 'asset_id, last_scan_id' current_tag_id = nil csv_output.each do |row| if (.include? row[0].to_s) && (row[0].to_i != current_tag_id) if(current_tag_id.nil?) #Initial run current_tag_id = row[0].to_i else #New tag ID, finish off the previous tag asset list and start on the new one save_to_file([:csv_file], trimmed_csv) current_tag_id = row[0].to_i trimmed_csv = [] # TODO: test this change trimmed_csv << 'asset_id, last_scan_id' end end if(current_tag_id == row[0].to_i) trimmed_csv << "#{row[1].to_s},#{row[2].to_s}" end end save_to_file([:csv_file], trimmed_csv) if trimmed_csv.any? end |
#last_scans(options = {}) ⇒ Object
Gets the last scan information from nexpose sans the CSV headers.
-
Returns :
-
A hash with nexpose_ids (site ID or tag ID) => last_scan_id
-
220 221 222 223 224 225 226 227 |
# File 'lib/nexpose_ticketing/ticket_repository.rb', line 220 def last_scans( = {}) nexpose_ids= Hash.new(-1) trimmed_csv = load_last_scans() trimmed_csv.drop(1).each do |row| nexpose_ids[row[0]] = row[1] end nexpose_ids end |
#load_last_scans(options = {}, report_config = Nexpose::AdhocReportConfig.new(nil, 'sql')) ⇒ Object
Loads the last scan info to memory.
-
Args :
-
csv_file_name
- CSV File name.
-
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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 133 134 135 136 137 |
# File 'lib/nexpose_ticketing/ticket_repository.rb', line 82 def load_last_scans( = {}, report_config = Nexpose::AdhocReportConfig.new(nil, 'sql')) report_config.add_filter('version', '1.2.0') sites = Array([:sites]).map!(&:to_s) = Array([:tags]).map!(&:to_s) if([:tag_run]) report_config.add_filter('query', Queries.last_tag_scans) .each do |tag| report_config.add_filter('tag', tag) end else report_config.add_filter('query', Queries.last_scans) end report_output = report_config.generate(@nsc, @timeout) csv_output = CSV.parse(report_output.chomp, headers: :first_row) #We only care about sites we are monitoring. trimmed_csv = [] if([:tag_run]) trimmed_csv << 'tag_id,last_scan_fingerprint' current_tag_id = nil tag_finger_print = '' csv_output.each do |row| if (.include? row[0].to_s) && (row[0].to_i != current_tag_id) if(current_tag_id.nil?) #Initial run current_tag_id = row[0].to_i else #New tag ID, finish off the old fingerprint and start on the new one trimmed_csv << CSV::Row.new('tag_id,last_scan_fingerprint'.split(','), "#{current_tag_id},#{Digest::MD5::hexdigest(tag_finger_print)}".split(',')) tag_finger_print.clear current_tag_id = row[0].to_i end end if(current_tag_id == row[0].to_i) #yield current_tag_id, row[1].to_s, row[2].to_s if block_given? tag_finger_print << row[1].to_s tag_finger_print << row[2].to_s end end unless tag_finger_print.empty? trimmed_csv << CSV::Row.new('tag_id,last_scan_fingerprint'.split(','), "#{current_tag_id},#{Digest::MD5::hexdigest(tag_finger_print)}".split(',')) end else trimmed_csv << report_output.lines.first csv_output.each do |row| if sites.include? row[0].to_s trimmed_csv << row end end end trimmed_csv end |
#nexpose_login(nexpose_data) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/nexpose_ticketing/ticket_repository.rb', line 32 def nexpose_login(nexpose_data) @nsc = Nexpose::Connection.new(nexpose_data[:nxconsole], nexpose_data[:nxuser], nexpose_data[:nxpasswd]) @nsc.login @log = NexposeTicketing::NxLogger.instance @log.on_connect(nexpose_data[:nxconsole], 3780, @nsc.session_id, "{}") #After login, create the report helper @report_helper = NexposeReportHelper::ReportOps.new(@nsc, @timeout) end |
#read_last_scans(csv_file_name) ⇒ Object
Reads a nexpose identifier (tag ID, site ID etc) scan history from disk.
-
Args :
-
csv_file_name
- CSV File name.
-
-
Returns :
-
A hash with site_ids => last_scan_id
-
59 60 61 62 63 64 65 |
# File 'lib/nexpose_ticketing/ticket_repository.rb', line 59 def read_last_scans(csv_file_name) file_identifier_histories = Hash.new(-1) CSV.foreach(csv_file_name, headers: true) do |row| file_identifier_histories[row[0]] = row[1] end file_identifier_histories end |
#read_tag_asset_list(csv_file_name) ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/nexpose_ticketing/ticket_repository.rb', line 157 def read_tag_asset_list(csv_file_name) file_identifier_histories = Hash.new(-1) CSV.foreach(csv_file_name, headers: true) do |row| file_identifier_histories[row[0]] = row[1] end file_identifier_histories end |
#request_query(query_name, options = {}, nexpose_items = nil) ⇒ Object
229 230 231 232 233 |
# File 'lib/nexpose_ticketing/ticket_repository.rb', line 229 def request_query(query_name, = {}, nexpose_items = nil) items = Array(nexpose_items || ["#{[:scan_mode]}s".intern]) report_config = generate_config(query_name, , items) end |
#save_last_scans(csv_file_name, options = {}, saved_file = nil) ⇒ Object
Saves the last scan info to disk.
-
Args :
-
csv_file_name
- CSV File name.
-
72 73 74 75 |
# File 'lib/nexpose_ticketing/ticket_repository.rb', line 72 def save_last_scans(csv_file_name, = {}, saved_file = nil) current_scan_state = load_last_scans() save_to_file(csv_file_name, current_scan_state, saved_file) end |
#save_to_file(csv_file_name, trimmed_csv, saved_file = nil) ⇒ Object
Saves CSV scan information to disk
-
Args :
-
csv_file_name
- CSV File name.
-
204 205 206 207 208 209 210 211 212 213 |
# File 'lib/nexpose_ticketing/ticket_repository.rb', line 204 def save_to_file(csv_file_name, trimmed_csv, saved_file = nil) unless saved_file.nil? saved_file.open(csv_file_name, 'w') { |file| file.puts(trimmed_csv) } return end dir = File.dirname(csv_file_name) FileUtils.mkdir_p(dir) unless File.directory?(dir) File.open(csv_file_name, 'w') { |file| file.puts(trimmed_csv) } end |