Class: RubyNessus::Version2::XML

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ruby-nessus/version2/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.


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

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

Instance Method Details

#critical_severity_countInteger

Return the Critical severity count.

Examples:

scan.critical_severity_count #=> 10

Returns:

  • (Integer)

    The Critical Severity Count


219
220
221
# File 'lib/ruby-nessus/version2/scan.rb', line 219

def critical_severity_count
  count_stats[:critical].to_i
end

#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.


97
98
99
# File 'lib/ruby-nessus/version2/scan.rb', line 97

def each_host(&block)
  hosts.each(&block)
end

#event_percentage_for(type, round_percentage = nil) ⇒ 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. [critical, high, medium, low, informational, all]


296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/ruby-nessus/version2/scan.rb', line 296

def event_percentage_for(type, round_percentage = nil)
  @sc ||= count_stats
  if %w[critical high medium low tcp udp icmp 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, tdp, udp, icmp, critical, high, medium and low."
  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.


325
326
327
328
329
330
331
332
333
# File 'lib/ruby-nessus/version2/scan.rb', line 325

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

  @xml.xpath('//ReportHost').each do |host|
    next unless host['name'].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


232
233
234
# File 'lib/ruby-nessus/version2/scan.rb', line 232

def high_severity_count
  count_stats[:high].to_i
end

#host_countInteger

Return the nessus scan host count.

Examples:

scan.host_count #=> 23

Returns:

  • (Integer)

    The Nessus Scan Host Count


122
123
124
# File 'lib/ruby-nessus/version2/scan.rb', line 122

def host_count
  each_host.size
end

#hostsArray<String>

Parses the hosts of the scan.

Returns:

  • (Array<String>)

    The Hosts of the scan.


107
108
109
110
111
# File 'lib/ruby-nessus/version2/scan.rb', line 107

def hosts
  @xml.xpath('//ReportHost').map do |host|
    Host.new(host)
  end
end

#icmp_countInteger

Return the ICMP Event Count.

Examples:

scan.icmp_count #=> 3

Returns:

  • (Integer)

    The ICMP Event Count


193
194
195
# File 'lib/ruby-nessus/version2/scan.rb', line 193

def icmp_count
  count_stats[:icmp].to_i
end

#informational_severity_countInteger

Return the informational severity count.

Examples:

scan.informational_severity_count #=> 1203

Returns:

  • (Integer)

    The Informational Severity Count


206
207
208
# File 'lib/ruby-nessus/version2/scan.rb', line 206

def informational_severity_count
  count_stats[:informational].to_i
end

#low_severity_countInteger

Return the Low severity count.

Examples:

scan.low_severity_count #=> 114

Returns:

  • (Integer)

    The Low Severity Count


258
259
260
# File 'lib/ruby-nessus/version2/scan.rb', line 258

def low_severity_count
  count_stats[:low].to_i
end

#medium_severity_countInteger

Return the Medium severity count.

Examples:

scan.medium_severity_count #=> 234

Returns:

  • (Integer)

    The Medium Severity Count


245
246
247
# File 'lib/ruby-nessus/version2/scan.rb', line 245

def medium_severity_count
  count_stats[:medium].to_i
end

#open_ports_countInteger

Return the Open Ports count.

Examples:

scan.open_ports_count #=> 1203

Returns:

  • (Integer)

    The Open Ports Count


154
155
156
# File 'lib/ruby-nessus/version2/scan.rb', line 154

def open_ports_count
  count_stats[:open_ports].to_i
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


67
68
69
# File 'lib/ruby-nessus/version2/scan.rb', line 67

def policy_notes
  @policy_notes ||= @xml.at('//Policy/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


57
58
59
# File 'lib/ruby-nessus/version2/scan.rb', line 57

def policy_title
  @policy_name ||= @xml.at('//Policy/policyName').inner_text
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


78
79
80
81
82
83
# File 'lib/ruby-nessus/version2/scan.rb', line 78

def target_hosts
  @xml.xpath('//Preferences/ServerPreferences/preference').each do |element|
    return element.children[3].inner_text.split(',') if element.children[1].inner_text == 'TARGET'
  end
  nil
end

#tcp_countInteger

Return the TCP Event Count.

Examples:

scan.tcp_count #=> 3

Returns:

  • (Integer)

    The TCP Event Count


167
168
169
# File 'lib/ruby-nessus/version2/scan.rb', line 167

def tcp_count
  count_stats[:tcp].to_i
end

#titleString

Return the nessus report title.

Examples:

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

Returns:

  • (String)

    The Nessus Report Title


47
48
49
# File 'lib/ruby-nessus/version2/scan.rb', line 47

def title
  @report_name ||= @xml.at('Report/@name').inner_text
end

#total_event_count(count_informational = nil) ⇒ Integer

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

Examples:

scan.total_event_count #=> 1561

Parameters:

  • argname (true, false)

    only true or false

Returns:

  • (Integer)

    The Total Severity Count


273
274
275
276
277
278
279
# File 'lib/ruby-nessus/version2/scan.rb', line 273

def total_event_count(count_informational = nil)
  if count_informational
    count_stats[:all].to_i + informational_severity_count
  else
    count_stats[:all].to_i
  end
end

#udp_countInteger

Return the UDP Event Count.

Examples:

scan.udp_count #=> 3

Returns:

  • (Integer)

    The UDP Event Count


180
181
182
# File 'lib/ruby-nessus/version2/scan.rb', line 180

def udp_count
  count_stats[:udp].to_i
end

#unique_portsArray

Retunrs an array of all unique ports.

Examples:

scan.unique_ports #=> 234

Returns:

  • (Array)

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

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

#versionObject


34
35
36
# File 'lib/ruby-nessus/version2/scan.rb', line 34

def version
  2
end