Module: ScanDB::Nmap

Includes:
LibXML
Defined in:
lib/scandb/nmap.rb

Class Method Summary collapse

Class Method Details

.from_xml(path) ⇒ Object

Imports scan information from a Nmap XML scan file, specified by the path. Returns an Array of Host objects.

Nmap.from_xml('path/to/scan.xml')
# => [...]


40
41
42
43
44
45
46
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
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/scandb/nmap.rb', line 40

def Nmap.from_xml(path)
  doc = XML::Document.file(path)
  hosts = []

  doc.find("/nmaprun/host[status[@state='up']]").each do |host|
    ip = host.find_first("address[@addr and @addrtype='ipv4']")['addr']
    new_host = Host.first_or_create(:ip => ip)

    host.find('hostname').each do |hostname|
      new_host.names << HostName.first_or_create(
        :name => hostname['name'],
        :host_id => new_host.id
      )
    end

    host.find('os/osclass').each do |osclass|
      new_os = OS.first_or_create(
        :type => osclass['type'],
        :vendor => osclass['vendor'],
        :family => osclass['osfamily'],
        :version => osclass['osgen']
      )

      new_host.os_guesses << OSGuess.first_or_create(
        :os_id => new_os.id,
        :accuracy => osclass['accuracy'].to_i
      )
    end

    host.find('ports/port').each do |port|
      new_port = Port.first_or_create(
        :number => port['portid'].to_i,
        :protocol => port['protocol'].to_sym
      )

      new_service = Service.first_or_create(
        :name => port.find_first('service[@name]')['name']
      )

      new_host.scanned_ports << ScannedPort.first_or_create(
        :status => port.find_first('state[@state]')['state'].to_sym,
        :service_id => new_service.id,
        :port_id => new_port.id,
        :host_id => new_host.id
      )
    end

    new_host.save

    hosts << new_host
  end

  return hosts
end