Class: RubyNessus::Version1::XML

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ruby-nessus/version1/scan.rb

Instance Method Summary collapse

Constructor Details

#initialize(xml) {|prog| ... } ⇒ XML

Creates a new .Nessus (XML) object to be parser

Examples:

RubyNessus::XML.new(nessus_scan_file) do |scan|
  scan.report_name
end

Parameters:

  • file (String)

    The Nessus xml results file to parse.

Yields:

  • (prog)

    If a block is given, it will be passed the newly created XML object.

Yield Parameters:

  • prog (XML)

    The newly created XML object.


30
31
32
33
# File 'lib/ruby-nessus/version1/scan.rb', line 30

def initialize(xml)
  @xml = xml
  raise 'Error: Not A Version 1.0 .Nessus file.' unless @xml.at('NessusClientData')
end

Instance Method Details

#each_host {|prog| ... } ⇒ Object

Creates a new Host object to be parser

Examples:

scan.hosts do |host|
  puts host.hostname
end

Yields:

  • (prog)

    If a block is given, it will be passed the newly created Host object.

Yield Parameters:

  • prog (XML)

    The newly created Host object.


202
203
204
205
206
207
208
209
# File 'lib/ruby-nessus/version1/scan.rb', line 202

def each_host(&block)
  hosts = []
  @xml.xpath('//ReportHost').each do |host|
    hosts << host.at('HostName').inner_text if host.at('HostName').inner_text
    yield(Host.new(host)) if block
  end
  hosts
end

#event_percentage_for(type, round_percentage = false) ⇒ Integer

Return the Total severity count.

Examples:

scan.event_percentage_for("low", true) #=> 11%

Parameters:

  • severity (String)

    the severity in which to calculate percentage for.

  • round (Boolean)

    round the result to the nearest whole number.

Returns:

  • (Integer)

    The Percentage Of Events For A Passed Severity

Raises:

  • (ExceptionClass)

    One of the following severity options must be passed. [high, medium, low, informational, all]


333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/ruby-nessus/version1/scan.rb', line 333

def event_percentage_for(type, round_percentage = false)
  @sc ||= count_severity
  if %w[high medium low all].include?(type)
    calc = ((@sc[:"#{type}"].to_f / @sc[:all].to_f) * 100)
    if round_percentage
      return calc.round.to_s
    else
      return calc.to_s
    end
  else
    raise "Error: #{type} is not an acceptable severity. Possible options include: all, high, medium, low and informational."
  end
end

#find_by_hostname(hostname) {|prog| ... } ⇒ Object

Creates a new Host object to be parser from a passed search param.

Examples:

scan.find_by_hostname('127.0.0.1') do |host|
  puts host.hostname
end

Parameters:

  • hostname (String)

    the hostname to build a Host object for.

Yields:

  • (prog)

    If a block is given, it will be passed the newly created Host object.

Yield Parameters:

  • prog (XML)

    The newly created Host object.


362
363
364
365
366
367
368
369
370
# File 'lib/ruby-nessus/version1/scan.rb', line 362

def find_by_hostname(hostname, &block)
  raise "Error: hostname can't be blank." if hostname.nil? || hostname.empty?

  @xml.xpath('//ReportHost[HostName]').each do |host|
    next unless host.inner_text.match(hostname)

    yield(Host.new(host)) if block
  end
end

#high_severity_countInteger

Return the High severity count.

Examples:

scan.high_severity_count #=> 10

Returns:

  • (Integer)

    The High Severity Count


275
276
277
# File 'lib/ruby-nessus/version1/scan.rb', line 275

def high_severity_count
  count_severity[:high].to_i
end

#host_countInteger

Return the nessus scan host count.

Examples:

scan.host_count #=> 23

Returns:

  • (Integer)

    The Nessus Scan Host Count


230
231
232
# File 'lib/ruby-nessus/version1/scan.rb', line 230

def host_count
  hosts.size
end

#hostsArray<String>

Parses the hosts of the scan.

Returns:

  • (Array<String>)

    The Hosts of the scan.


217
218
219
# File 'lib/ruby-nessus/version1/scan.rb', line 217

def hosts
  to_enum(:each_host).to_a
end

#low_severity_countInteger

Return the Low severity count.

Examples:

scan.low_severity_count #=> 114

Returns:

  • (Integer)

    The Low Severity Count


301
302
303
# File 'lib/ruby-nessus/version1/scan.rb', line 301

def low_severity_count
  count_severity[:low].to_i
end

#medium_severity_countInteger

Return the Medium severity count.

Examples:

scan.medium_severity_count #=> 234

Returns:

  • (Integer)

    The Medium Severity Count


288
289
290
# File 'lib/ruby-nessus/version1/scan.rb', line 288

def medium_severity_count
  count_severity[:medium].to_i
end

#open_ports_countInteger

Return the informational severity count.

Examples:

scan.informational_severity_count #=> 1203

Returns:

  • (Integer)

    The Informational Severity Count


262
263
264
# File 'lib/ruby-nessus/version1/scan.rb', line 262

def open_ports_count
  count_severity[:open_ports].to_i
end

#plugin_idsArray

Returns and array of the plugin ids userd for the passed .nessus scan.

Examples:

scan.plugin_ids #=> [1234,2343,9742,5452,5343,2423,1233]

Returns:

  • (Array)

    The Nessus Scan Plugin Ids


152
153
154
155
156
157
158
159
160
161
162
# File 'lib/ruby-nessus/version1/scan.rb', line 152

def plugin_ids
  unless @plugin_ids
    @plugin_ids = []

    @xml.xpath('//PluginSelection').last.text.split(';').each do |id|
      @plugin_ids << id
    end
  end

  @plugin_ids
end

#pluginsArray

Returns and array of the plugin names userd for the passed .nessus scan.

Examples:

scan.plugins #=> ["PHP < 5.2.1 Multiple Vulnerabilities", "PHP < 4.4.1 / 5.0.6 Multiple Vulnerabilities"]

Returns:

  • (Array)

    The Nessus Scan Plugin Names


173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/ruby-nessus/version1/scan.rb', line 173

def plugins
  unless @plugins
    # get elements with attribute:
    @plugins = []

    @xml.xpath('//pluginName').each do |x|
      @plugins << x.inner_text unless x.inner_text.empty?
    end

    @plugins.uniq!
    @plugins.sort!
  end

  @plugins
end

#policy_notesString

Return the nessus scan policy comments. This is the description field when creating a new policy with the Nessus GUI client.

Returns:

  • (String)

    The Nessus Scan Policy Comments


124
125
126
# File 'lib/ruby-nessus/version1/scan.rb', line 124

def policy_notes
  @policy_comments ||= @xml.xpath('//NessusClientData//Report//policyComments').inner_text
end

#policy_titleString

Return the nessus scan policy name. When creating a nessus policy this is usually the title field.

Returns:

  • (String)

    The Nessus Scan Policy Name


114
115
116
# File 'lib/ruby-nessus/version1/scan.rb', line 114

def policy_title
  @policy_name ||= @xml.xpath('//NessusClientData//Report//policyName').inner_text
end

#runtimeString

Return the scan run time.

Examples:

scan.runtime #=> '2 hours 5 minutes and 16 seconds'

Returns:

  • (String)

    The Nessus Scan Run Time


101
102
103
104
105
106
# File 'lib/ruby-nessus/version1/scan.rb', line 101

def runtime
  h = (Time.parse(stop_time.to_s).strftime('%H').to_i - Time.parse(start_time.to_s).strftime('%H').to_i).to_s.delete('-')
  m = (Time.parse(stop_time.to_s).strftime('%M').to_i - Time.parse(start_time.to_s).strftime('%M').to_i).to_s.delete('-')
  s = (Time.parse(stop_time.to_s).strftime('%S').to_i - Time.parse(start_time.to_s).strftime('%S').to_i).to_s.delete('-')
  "#{h} hours #{m} minutes and #{s} seconds"
end

#start_timeDateTime

Return the scan start time.

Examples:

scan.start_time #=> 'Fri Nov 11 23:36:54 1985'

Returns:

  • (DateTime)

    The Nessus Scan Start Time


75
76
77
# File 'lib/ruby-nessus/version1/scan.rb', line 75

def start_time
  @start_time = DateTime.strptime(@xml.xpath('//NessusClientData//Report//StartTime').inner_text, '%a %b %d %H:%M:%S %Y')
end

#stop_timeDateTime

Return the scan stop time.

Examples:

scan.stop_time #=> 'Mon Nov 11 23:36:54 1985'

Returns:

  • (DateTime)

    The Nessus Scan Stop Time


88
89
90
# File 'lib/ruby-nessus/version1/scan.rb', line 88

def stop_time
  @stop_time = DateTime.strptime(@xml.xpath('//NessusClientData//Report//StopTime').inner_text, '%a %b %d %H:%M:%S %Y')
end

#target_hostsArray<String>

Return the hosts the were targeted for the initial scan. These are the hosts that were inputed when creating the scan.

Returns:

  • (Array<String>)

    Array of hosts


135
136
137
138
139
140
141
# File 'lib/ruby-nessus/version1/scan.rb', line 135

def target_hosts
  hosts = []
  @xml.xpath('//Targets/Target/value').each do |element|
    hosts << element.inner_text
  end
  hosts.sort.uniq!
end

#timeString

Return the nessus report time.

Examples:

scan.report_time #=> "09/11/08 02:21:22 AM"

Returns:

  • (String)

    The Nessus Report Time


61
62
63
64
# File 'lib/ruby-nessus/version1/scan.rb', line 61

def time
  datetime = @xml.xpath('//NessusClientData//Report//ReportName').inner_text.split(' - ').first
  @report_time ||= DateTime.strptime(datetime, '%y/%m/%d %I:%M:%S %p')
end

#titleString

Return the nessus report title.

Examples:

scan.report_name #=> "My Super Cool Nessus Report"

Returns:

  • (String)

    The Nessus Report Title


48
49
50
# File 'lib/ruby-nessus/version1/scan.rb', line 48

def title
  @report_name ||= @xml.xpath('//NessusClientData//Report//ReportName').inner_text.split(' - ').last
end

#total_event_countInteger

Return the Total severity count. [high, medium, low, informational]

Examples:

scan.total_event_count #=> 1561

Returns:

  • (Integer)

    The Total Severity Count


314
315
316
# File 'lib/ruby-nessus/version1/scan.rb', line 314

def total_event_count
  count_severity[:all].to_i
end

#unique_portsArray

Retunrs an array of all unique ports.

Examples:

scan.unique_ports #=> 234

Returns:

  • (Array)

242
243
244
245
246
247
248
249
250
251
# File 'lib/ruby-nessus/version1/scan.rb', line 242

def unique_ports
  return if @unique_ports

  @unique_ports = []
  @xml.xpath('//ReportItem//port').each do |port|
    @unique_ports << port.inner_text
  end
  @unique_ports.uniq!
  @unique_ports.sort!
end

#versionObject


35
36
37
# File 'lib/ruby-nessus/version1/scan.rb', line 35

def version
  1
end