Class: Nessus::Version2::XML

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/gemcache/ruby-nessus/ruby-nessus/Version2/version2.rb

Instance Method Summary collapse

Constructor Details

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

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

Examples:

Nessus::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/gemcache/ruby-nessus/ruby-nessus/Version2/version2.rb', line 30

def initialize(xml)
  @xml = xml
  raise "Error: Not A Version 2.0 .Nessus file." unless @xml.at('NessusClientData_v2')
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.



85
86
87
88
89
90
91
92
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version2/version2.rb', line 85

def each_host(&block)
  hosts = []
  @xml.xpath("//ReportHost").each do |host|
    hosts << host['name'] if host['name']
    block.call(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]



274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version2/version2.rb', line 274

def event_percentage_for(type, round_percentage=false)
  @sc ||= count_stats
  if %W(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}"
    else
      return "#{calc}"
    end
  else
    raise "Error: #{type} is not an acceptable severity. Possible options include: all, tdp, udp, icmp, 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.



303
304
305
306
307
308
309
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version2/version2.rb', line 303

def find_by_hostname(hostname, &block)
  raise "Error: hostname can't be blank." if hostname.blank?
  @xml.xpath('//ReportHost').each do |host|
    next unless host['name'].match(hostname)
    block.call(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



210
211
212
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version2/version2.rb', line 210

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



113
114
115
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version2/version2.rb', line 113

def host_count
  each_host.size
end

#hostsArray<String>

Parses the hosts of the scan.

Returns:

  • (Array<String>)

    The Hosts of the scan.



100
101
102
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version2/version2.rb', line 100

def hosts
  Enumerator.new(self,:each_host).to_a
end

#icmp_countInteger

Return the ICMP Event Count.

Examples:

scan.icmp_count #=> 3

Returns:

  • (Integer)

    The ICMP Event Count



184
185
186
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version2/version2.rb', line 184

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



197
198
199
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version2/version2.rb', line 197

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



236
237
238
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version2/version2.rb', line 236

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



223
224
225
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version2/version2.rb', line 223

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



145
146
147
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version2/version2.rb', line 145

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



69
70
71
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version2/version2.rb', line 69

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



59
60
61
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version2/version2.rb', line 59

def policy_title
  @policy_name ||= @xml.at("//Policy/policyName").inner_text
end

#tcp_countInteger

Return the TCP Event Count.

Examples:

scan.tcp_count #=> 3

Returns:

  • (Integer)

    The TCP Event Count



158
159
160
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version2/version2.rb', line 158

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



49
50
51
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version2/version2.rb', line 49

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

#total_event_count(count_informational = false) ⇒ Integer

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

Examples:

scan.total_event_count #=> 1561

Parameters:

  • argname (true, false)

    only true or false

Returns:

  • (Integer)

    The Total Severity Count



251
252
253
254
255
256
257
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version2/version2.rb', line 251

def total_event_count(count_informational = false)
  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



171
172
173
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version2/version2.rb', line 171

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)


125
126
127
128
129
130
131
132
133
134
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version2/version2.rb', line 125

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



36
37
38
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version2/version2.rb', line 36

def version
  2
end