Class: Nessus::Host

Inherits:
Object
  • Object
show all
Defined in:
lib/nessus/host.rb

Overview

This class represents each of the /NessusClientData_v2/Report/ReportHost elements in the Nessus XML document.

It provides a convenient way to access the information scattered all over the XML in attributes and nested tags.

Instead of providing separate methods for each supported property we rely on Ruby’s #method_missing to do most of the work.

Instance Method Summary collapse

Constructor Details

#initialize(xml_node) ⇒ Host

Accepts an XML node from Nokogiri::XML.



12
13
14
# File 'lib/nessus/host.rb', line 12

def initialize(xml_node)
  @xml = xml_node
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

This method is invoked by Ruby when a method that is not defined in this instance is called.

In our case we inspect the @method@ parameter and try to find the corresponding <tag/> element inside the ./HostProperties child.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/nessus/host.rb', line 47

def method_missing(method, *args)
  # We could remove this check and return nil for any non-recognized tag.
  # The problem would be that it would make tricky to debug problems with
  # typos. For instance: <>.potr would return nil instead of raising an
  # exception
  unless supported_tags.include?(method)
    super
    return
  end

  # first we try the attributes: name
  translations_table = {}
  method_name = translations_table.fetch(method, method.to_s)
  return @xml.attributes[method_name].value if @xml.attributes.key?(method_name)


  # translation of Host properties
  translations_table = {
    ip:               'host-ip',
    fqdn:             'host-fqdn',
    operating_system: 'operating-system',
    mac_address:      'mac-address',
    netbios_name:     'netbios-name',
    scan_start_time:  'HOST_START',
    scan_stop_time:   'HOST_END'
  }
  method_name = translations_table.fetch(method, method.to_s)

  if property = @xml.at_xpath("./HostProperties/tag[@name='#{method_name}']")
    return property.text
  else
    return nil
  end
end

Instance Method Details

#report_itemsObject

Each of the entries associated with this host. Returns an array of Nessus::ReportItem objects



31
32
33
# File 'lib/nessus/host.rb', line 31

def report_items
  @xml.xpath('./ReportItem').collect { |xml_report_item| ReportItem.new(xml_report_item) }
end

#respond_to?(method, include_private = false) ⇒ Boolean

This allows external callers (and specs) to check for implemented properties

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/nessus/host.rb', line 37

def respond_to?(method, include_private=false)
  return true if supported_tags.include?(method.to_sym)
  super
end

#supported_tagsObject

List of supported tags. They are all desdendents of the ./HostProperties node.



18
19
20
21
22
23
24
25
26
27
# File 'lib/nessus/host.rb', line 18

def supported_tags
  [
    # attributes
    :name,

    # simple tags
    :ip, :fqdn, :operating_system, :mac_address, :netbios_name,
    :scan_start_time, :scan_stop_time
  ]
end