Class: Nmap::XML

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

Overview

Represents an Nmap XML file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Creates a new XML object.

Parameters:

  • document (Nokogiri::XML::Document, IO, String)

    The path to the Nmap XML scan file or Nokogiri::XML::Document.

Yields:

  • (xml)

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

Yield Parameters:

  • xml (XML)

    The newly created XML object.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/nmap/xml.rb', line 34

def initialize(document)
  case document
  when Nokogiri::XML::Document
    @doc = document
  when IO, StringIO
    @doc = Nokogiri::XML(document)
  else
    @path = File.expand_path(document)
    @doc  = File.open(@path) { |file| Nokogiri::XML(file) }
  end

  yield self if block_given?
end

Instance Attribute Details

#pathObject (readonly)

Path of the Nmap XML scan file



20
21
22
# File 'lib/nmap/xml.rb', line 20

def path
  @path
end

Class Method Details

.load(text, &block) ⇒ Object

Deprecated.

Use parse instead.

Since:

  • 0.7.0



71
72
73
# File 'lib/nmap/xml.rb', line 71

def self.load(text,&block)
  parse(text,&block)
end

.open(path) {|xml| ... } ⇒ Object

Creates a new XML object from the file.

Parameters:

  • path (String)

    The path to the XML 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.

Since:

  • 0.7.0



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

def self.open(path,&block)
  new(path,&block)
end

.parse(text) {|xml| ... } ⇒ Object

Creates a new XML object from XML text.

Parameters:

  • text (String)

    XML text of the 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.

Since:

  • 0.8.0



62
63
64
# File 'lib/nmap/xml.rb', line 62

def self.parse(text,&block)
  new(Nokogiri::XML(text),&block)
end

Instance Method Details

#debuggingInteger

Parses the debugging level.

Returns:

  • (Integer)

    The debugging level.



197
198
199
# File 'lib/nmap/xml.rb', line 197

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

#down_hostHost

Returns the first host found to be down during the scan.

Returns:

Since:

  • 0.8.0



364
365
366
# File 'lib/nmap/xml.rb', line 364

def down_host
  each_down_host.first
end

#down_hostsArray<Host>

Parses the hosts found to be down during the scan.

Returns:

  • (Array<Host>)

    The down hosts in the scan.

Since:

  • 0.8.0



353
354
355
# File 'lib/nmap/xml.rb', line 353

def down_hosts
  each_down_host.to_a
end

#each(&block) ⇒ Object

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

See Also:



417
418
419
# File 'lib/nmap/xml.rb', line 417

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

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

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

Yields:

  • (host)

    Each host will be passed to a given block.

Yield Parameters:

  • host (Host)

    A down host in the scan.

Returns:

  • (XML, Enumerator)

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

Since:

  • 0.8.0



335
336
337
338
339
340
341
342
343
# File 'lib/nmap/xml.rb', line 335

def each_down_host
  return enum_for(__method__) unless block_given?

  @doc.xpath("/nmaprun/host[status[@state='down']]").each do |host|
    yield Host.new(host)
  end

  return self
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.



289
290
291
292
293
294
295
296
297
# File 'lib/nmap/xml.rb', line 289

def each_host
  return enum_for(__method__) unless block_given?

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

  return self
end

#each_run_stat {|run_stat| ... } ⇒ Enumerator

Parses the essential runstats information.

Yields:

  • (run_stat)

    The given block will be passed each runstat.

Yield Parameters:

  • run_stat (RunStat)

    A runstat.

Returns:

  • (Enumerator)

    If no block is given, an enumerator will be returned.

Since:

  • 0.7.0



154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/nmap/xml.rb', line 154

def each_run_stat
  return enum_for(__method__) unless block_given?

  @doc.xpath('/nmaprun/runstats/finished').each do |run_stat|
    yield RunStat.new(
      Time.at(run_stat['time'].to_i),
      run_stat['elapsed'],
      run_stat['summary'],
      run_stat['exit']
    )
  end

  return self
end

#each_task {|task| ... } ⇒ Enumerator

Parses the tasks of the scan.

Yields:

  • (task)

    The given block will be passed each scan task.

Yield Parameters:

  • task (ScanTask)

    A task from the scan.

Returns:

  • (Enumerator)

    If no block is given, an enumerator will be returned.

Since:

  • 0.7.0



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/nmap/xml.rb', line 215

def each_task
  return enum_for(__method__) unless block_given?

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

    yield ScanTask.new(
      task_begin['task'],
      Time.at(task_begin['time'].to_i),
      Time.at(task_end['time'].to_i),
      task_end['extrainfo']
    )
  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.



381
382
383
384
385
386
387
388
389
# File 'lib/nmap/xml.rb', line 381

def each_up_host
  return enum_for(__method__) unless block_given?

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

  return self
end

#hostHost

Returns the first host.

Returns:

Since:

  • 0.8.0



316
317
318
# File 'lib/nmap/xml.rb', line 316

def host
  each_host.first
end

#hostsArray<Host>

Parses the hosts in the scan.

Returns:

  • (Array<Host>)

    The hosts in the scan.



305
306
307
# File 'lib/nmap/xml.rb', line 305

def hosts
  each_host.to_a
end

#inspectString

Inspects the XML file.

Returns:

  • (String)

    The inspected XML file.



437
438
439
# File 'lib/nmap/xml.rb', line 437

def inspect
  "#<#{self.class}: #{self}>"
end

#postscriptPostscript Also known as: postscripts

The NSE scripts ran after the scan.

Returns:

  • (Postscript)

    Contains the script output and data.

Since:

  • 0.9.0



268
269
270
271
272
# File 'lib/nmap/xml.rb', line 268

def postscript
  @postscript ||= if (postscript = @doc.at('postscript'))
                    Postscript.new(postscript)
                  end
end

#prescriptPrescript Also known as: prescripts

The NSE scripts ran before the scan.

Returns:

  • (Prescript)

    Contains the script output and data.

Since:

  • 0.9.0



252
253
254
255
256
# File 'lib/nmap/xml.rb', line 252

def prescript
  @prescript ||= if (prescript = @doc.at('prescript'))
                   Prescript.new(prescript)
                 end
end

#run_statsArray<RunStat>

Parses the essential runstats information.

Returns:

  • (Array<RunStat>)

    The runstats.

Since:

  • 0.7.0



177
178
179
# File 'lib/nmap/xml.rb', line 177

def run_stats
  each_run_stat.to_a
end

#scan_infoArray<Scan>

Parses the scan information.

Returns:

  • (Array<Scan>)

    The scan information.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/nmap/xml.rb', line 124

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.



99
100
101
102
103
104
105
106
# File 'lib/nmap/xml.rb', line 99

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



240
241
242
# File 'lib/nmap/xml.rb', line 240

def tasks
  each_task.to_a
end

#to_sString

Converts the XML parser to a String.

Returns:

  • (String)

    The path of the XML scan file.



427
428
429
# File 'lib/nmap/xml.rb', line 427

def to_s
  @path.to_s
end

#up_hostHost

Returns the first host found to be up during the scan.

Returns:

Since:

  • 0.8.0



408
409
410
# File 'lib/nmap/xml.rb', line 408

def up_host
  each_up_host.first
end

#up_hostsArray<Host>

Parses the hosts found to be up during the scan.

Returns:

  • (Array<Host>)

    The hosts in the scan.



397
398
399
# File 'lib/nmap/xml.rb', line 397

def up_hosts
  each_up_host.to_a
end

#verboseInteger

Parses the verbose level.

Returns:

  • (Integer)

    The verbose level.



187
188
189
# File 'lib/nmap/xml.rb', line 187

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.



114
115
116
# File 'lib/nmap/xml.rb', line 114

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