Class: Nessus::Version1::XML

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



31
32
33
34
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version1/version1.rb', line 31

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.



188
189
190
191
192
193
194
195
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version1/version1.rb', line 188

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



319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version1/version1.rb', line 319

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}"
    else
      return "#{calc}"
    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.



348
349
350
351
352
353
354
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version1/version1.rb', line 348

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



261
262
263
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version1/version1.rb', line 261

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



216
217
218
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version1/version1.rb', line 216

def host_count
  hosts.size
end

#hostsArray<String>

Parses the hosts of the scan.

Returns:

  • (Array<String>)

    The Hosts of the scan.



203
204
205
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version1/version1.rb', line 203

def hosts
  Enumerator.new(self,: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



287
288
289
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version1/version1.rb', line 287

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



274
275
276
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version1/version1.rb', line 274

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



248
249
250
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version1/version1.rb', line 248

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



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version1/version1.rb', line 138

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



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version1/version1.rb', line 159

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

  return @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



125
126
127
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version1/version1.rb', line 125

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



115
116
117
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version1/version1.rb', line 115

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



102
103
104
105
106
107
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version1/version1.rb', line 102

def runtime
  h = ("#{Time.parse(stop_time.to_s).strftime('%H').to_i - Time.parse(start_time.to_s).strftime('%H').to_i}").gsub('-', '')
  m = ("#{Time.parse(stop_time.to_s).strftime('%M').to_i - Time.parse(start_time.to_s).strftime('%M').to_i}").gsub('-', '')
  s = ("#{Time.parse(stop_time.to_s).strftime('%S').to_i - Time.parse(start_time.to_s).strftime('%S').to_i}").gsub('-', '')
  return "#{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



76
77
78
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version1/version1.rb', line 76

def start_time
  @start_time = DateTime.strptime(@xml.xpath("//NessusClientData//Report//StartTime").inner_text, fmt='%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



89
90
91
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version1/version1.rb', line 89

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

#timeString

Return the nessus report time.

Examples:

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

Returns:

  • (String)

    The Nessus Report Time



62
63
64
65
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version1/version1.rb', line 62

def time
  datetime = @xml.xpath("//NessusClientData//Report//ReportName").inner_text.split(' - ').first
  @report_time ||= DateTime.strptime(datetime, fmt='%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



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

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



300
301
302
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version1/version1.rb', line 300

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)


228
229
230
231
232
233
234
235
236
237
# File 'lib/gemcache/ruby-nessus/ruby-nessus/Version1/version1.rb', line 228

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

#versionObject



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

def version
  1
end