Class: Nmap::XML

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/nmap/xml.rb,
lib/nmap/xml/os.rb,
lib/nmap/xml/cpe.rb,
lib/nmap/xml/hop.rb,
lib/nmap/xml/host.rb,
lib/nmap/xml/port.rb,
lib/nmap/xml/scan.rb,
lib/nmap/xml/script.rb,
lib/nmap/xml/status.rb,
lib/nmap/xml/uptime.rb,
lib/nmap/xml/address.rb,
lib/nmap/xml/cpe/url.rb,
lib/nmap/xml/scanner.rb,
lib/nmap/xml/scripts.rb,
lib/nmap/xml/service.rb,
lib/nmap/xml/hostname.rb,
lib/nmap/xml/os_class.rb,
lib/nmap/xml/os_match.rb,
lib/nmap/xml/run_stat.rb,
lib/nmap/xml/sequence.rb,
lib/nmap/xml/prescript.rb,
lib/nmap/xml/scan_task.rb,
lib/nmap/xml/postscript.rb,
lib/nmap/xml/traceroute.rb,
lib/nmap/xml/host_script.rb,
lib/nmap/xml/tcp_sequence.rb,
lib/nmap/xml/ip_id_sequence.rb,
lib/nmap/xml/tcp_ts_sequence.rb

Overview

Represents an Nmap XML file.

Defined Under Namespace

Modules: CPE, Scripts Classes: Address, Hop, Host, HostScript, Hostname, IpIdSequence, OS, OSClass, OSMatch, Port, Postscript, Prescript, RunStat, Scan, ScanTask, Scanner, Script, Sequence, Service, Status, TcpSequence, TcpTsSequence, Traceroute, Uptime

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc, path: nil) {|xml| ... } ⇒ XML

Creates a new XML object.

Parameters:

  • doc (Nokogiri::XML::Document)

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

  • path (String, nil) (defaults to: nil)

    The optional path the XML was loaded from.

Yields:

  • (xml)

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

Yield Parameters:

  • xml (XML)

    The newly created XML object.



48
49
50
51
52
53
# File 'lib/nmap/xml.rb', line 48

def initialize(doc, path: nil)
  @doc  = doc
  @path = File.expand_path(path) if path

  yield self if block_given?
end

Instance Attribute Details

#docNokogiri::XML (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The parsed XML document.

Returns:

  • (Nokogiri::XML)


26
27
28
# File 'lib/nmap/xml.rb', line 26

def doc
  @doc
end

#pathString? (readonly)

Path of the Nmap XML scan file

Returns:

  • (String, nil)


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

def path
  @path
end

Class Method Details

.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



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

def self.open(path,&block)
  path = File.expand_path(path)
  doc  = Nokogiri::XML(File.open(path))

  new(doc, path: 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



69
70
71
# File 'lib/nmap/xml.rb', line 69

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

Instance Method Details

#debuggingInteger

Parses the debugging level.

Returns:

  • (Integer)

    The debugging level.



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

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



376
377
378
# File 'lib/nmap/xml.rb', line 376

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



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

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:



429
430
431
# File 'lib/nmap/xml.rb', line 429

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



347
348
349
350
351
352
353
354
355
# File 'lib/nmap/xml.rb', line 347

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.



301
302
303
304
305
306
307
308
309
# File 'lib/nmap/xml.rb', line 301

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



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

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



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

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.



393
394
395
396
397
398
399
400
401
# File 'lib/nmap/xml.rb', line 393

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



328
329
330
# File 'lib/nmap/xml.rb', line 328

def host
  each_host.first
end

#hostsArray<Host>

Parses the hosts in the scan.

Returns:

  • (Array<Host>)

    The hosts in the scan.



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

def hosts
  each_host.to_a
end

#postscriptPostscript

The NSE scripts ran after the scan.

Returns:

  • (Postscript)

    Contains the script output and data.

Since:

  • 0.9.0



282
283
284
285
286
# File 'lib/nmap/xml.rb', line 282

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

#prescriptPrescript

The NSE scripts ran before the scan.

Returns:

  • (Prescript)

    Contains the script output and data.

Since:

  • 0.9.0



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

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



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

def run_stats
  each_run_stat.to_a
end

#scan_infoArray<Scan>

Parses the scan information.

Returns:

  • (Array<Scan>)

    The scan information.



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

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.



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

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

#task(name) ⇒ ScanTask?

Finds the task with the given name.

Parameters:

  • name (String)

    The task name to search for.

Returns:

  • (ScanTask, nil)

    The scan task with the matching name or nil.

Since:

  • 0.10.0



256
257
258
# File 'lib/nmap/xml.rb', line 256

def task(name)
  each_task.find { |scan_task| scan_task.name == name }
end

#tasksArray<ScanTask>

Parses the tasks of the scan.

Returns:

  • (Array<ScanTask>)

    The tasks of the scan.

Since:

  • 0.1.2



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

def tasks
  each_task.to_a
end

#to_sString

Converts the XML parser to a String.

Returns:

  • (String)

    The path of the XML file or the raw XML.



439
440
441
442
443
# File 'lib/nmap/xml.rb', line 439

def to_s
  if @path then @path.to_s
  else          @doc.to_s
  end
end

#up_hostHost

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

Returns:

Since:

  • 0.8.0



420
421
422
# File 'lib/nmap/xml.rb', line 420

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.



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

def up_hosts
  each_up_host.to_a
end

#verboseInteger

Parses the verbose level.

Returns:

  • (Integer)

    The verbose level.



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

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.



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

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