Class: Nmap::XML

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/nmap/xml.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Creates a new XML object.

Parameters:

  • path (String)

    The path to the Nmap XML scan file.

Yields:

  • (xml)

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

Yield Parameters:

  • xml (XML)

    The newly created XML object.



28
29
30
31
32
33
# File 'lib/nmap/xml.rb', line 28

def initialize(path)
  @path = File.expand_path(path)
  @doc = Nokogiri::XML(File.new(@path))

  yield self if block_given?
end

Instance Attribute Details

#pathObject (readonly)

Path of the Nmap XML scan file



14
15
16
# File 'lib/nmap/xml.rb', line 14

def path
  @path
end

Instance Method Details

#debuggingInteger

Parses the debugging level.

Returns:

  • (Integer)

    The debugging level.



98
99
100
# File 'lib/nmap/xml.rb', line 98

def debugging
  @debugging ||= @doc.at('debugging/@level').inner_text.to_i
end

#each(&block) ⇒ Object

Parses the hosts that were found to be up during the scan.

See Also:



194
195
196
# File 'lib/nmap/xml.rb', line 194

def each(&block)
  each_up_host(&block)
end

#each_host {|host| ... } ⇒ XML, Enumerator

Parses the hosts in the scan.

Yields:

  • (host)

    Each host will be passed to a given block.

Yield Parameters:

  • host (Host)

    A host in the scan.

Returns:

  • (XML, Enumerator)

    The XML object. If no block was given, an enumerator object will be returned.



136
137
138
139
140
141
142
143
144
# File 'lib/nmap/xml.rb', line 136

def each_host(&block)
  return enum_for(:each_host) unless block

  @doc.xpath('/nmaprun/host').each do |host|
    Host.new(host,&block)
  end

  return self
end

#each_up_host {|host| ... } ⇒ XML, Enumerator

Parses the hosts that were found to be up during the scan.

Yields:

  • (host)

    Each host will be passed to a given block.

Yield Parameters:

  • host (Host)

    A host in the scan.

Returns:

  • (XML, Enumerator)

    The XML parser. If no block was given, an enumerator object will be returned.



169
170
171
172
173
174
175
176
177
# File 'lib/nmap/xml.rb', line 169

def each_up_host(&block)
  return enum_for(:each_up_host) unless block

  @doc.xpath("/nmaprun/host[status[@state='up']]").each do |host|
    Host.new(host,&block)
  end

  return self
end

#hostsArray<Host>

Parses the hosts in the scan.

Returns:

  • (Array<Host>)

    The hosts in the scan.



152
153
154
# File 'lib/nmap/xml.rb', line 152

def hosts
  each_host.to_a
end

#scan_infoArray<Scan>

Parses the scan information.

Returns:

  • (Array<Scan>)

    The scan information.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/nmap/xml.rb', line 66

def scan_info
  @doc.xpath('/nmaprun/scaninfo').map do |scaninfo|
    Scan.new(
      scaninfo['type'].to_sym,
      scaninfo['protocol'].to_sym,
      scaninfo['services'].split(',').map { |ports|
        if ports.include?('-')
          Range.new(*(ports.split('-',2)))
        else
          ports.to_i
        end
      }
    )
  end
end

#scannerScanner

Parses the scanner information.

Returns:

  • (Scanner)

    The scanner that was used and generated the scan file.



41
42
43
44
45
46
47
48
# File 'lib/nmap/xml.rb', line 41

def scanner
  @scanner ||= Scanner.new(
    @doc.root['scanner'],
    @doc.root['version'],
    @doc.root['args'],
    Time.at(@doc.root['start'].to_i)
  )
end

#tasksArray<ScanTask>

Parses the tasks of the scan.

Returns:

  • (Array<ScanTask>)

    The tasks of the scan.

Since:

  • 0.1.2



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/nmap/xml.rb', line 110

def tasks
  @doc.xpath('/nmaprun/taskbegin').map do |task_begin|
    task_end = task_begin.xpath('following-sibling::taskend').first

    ScanTask.new(
      task_begin['task'],
      Time.at(task_begin['time'].to_i),
      Time.at(task_end['time'].to_i),
      task_end['extrainfo']
    )
  end
end

#to_sString

Converts the XML parser to a String.

Returns:

  • (String)

    The path of the XML scan file.



204
205
206
# File 'lib/nmap/xml.rb', line 204

def to_s
  @path.to_s
end

#up_hostsArray<Host>

Parses the hosts found to be up during the scan.

Returns:

  • (Array<Host>)

    The hosts in the scan.



185
186
187
# File 'lib/nmap/xml.rb', line 185

def up_hosts
  each_up_host.to_a
end

#verboseInteger

Parses the verbose level.

Returns:

  • (Integer)

    The verbose level.



88
89
90
# File 'lib/nmap/xml.rb', line 88

def verbose
  @verbose ||= @doc.at('verbose/@level').inner_text.to_i
end

#versionString

Parses the XML scan file version.

Returns:

  • (String)

    The version of the XML scan file.



56
57
58
# File 'lib/nmap/xml.rb', line 56

def version
  @version ||= @doc.root['xmloutputversion']
end