Class: LogStash::Codecs::Nmap

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/codecs/nmap.rb

Overview

This codec may be used to decode (via inputs) only. It decodes nmap generated XML and outputs each host as its own event

Instance Method Summary collapse

Instance Method Details

#decode(data) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/logstash/codecs/nmap.rb', line 17

def decode(data)
  xml = Nmap::XML.parse(data)
  xml.each_host do |host|
    event = host_to_event(host)

    event['arguments'.freeze] = xml.scanner.arguments
    event['version'.freeze] = xml.scanner.version

    yield event
  end
rescue StandardError => e
  raise e #TODO: REMOVEME
  @logger.warn("An unexpected error occurred parsing nmap XML",
               :input => data,
               :message => e.message,
               :class => e.class.name,
               :backtrace => e.backtrace)
end

#hashify_hostname(hostname) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/logstash/codecs/nmap.rb', line 69

def hashify_hostname(hostname)
  return unless hostname

  {
    'name'.freeze => hostname.name, # str
    'type'.freeze => hostname.type, # str
  }
end

#hashify_os(os) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/logstash/codecs/nmap.rb', line 78

def hashify_os(os)
  return unless os

  {
    'ports_used'.freeze => os.ports_used,
    'fingerprint'.freeze => os.fingerprint,
    'classes'.freeze => hashify_os_matches(os.classes),
    'matches'.freeze => hashify_structs(os_matches)
  }
end

#hashify_os_classes(classes) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/logstash/codecs/nmap.rb', line 89

def hashify_os_classes(classes)
  return if !classes || classes.empty?

  classes.each do |klass|
    {
      'type'.freeze => klass.type.to_s, # returned as sym originally
      'vendor'.freeze => klass.vendor.to_s,
      'family'.freeze => klass.family.to_s,
      'gen'.freeze => klass.gen.to_s,
      'accuracy'.freeze => klass.accuracy # int
    }
  end
end

#hashify_port(port) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/logstash/codecs/nmap.rb', line 128

def hashify_port(port)
  return unless port

  {
    'number'.freeze => port.number,
    'reason'.freeze => port.reason,
    'protocol'.freeze => port.protocol.to_s,
    'service'.freeze => hashify_service(port.service),
    'state'.freeze => port.state
  }
end

#hashify_service(service) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/logstash/codecs/nmap.rb', line 112

def hashify_service(service)
  return unless service

  {
    'name'.freeze => service.name,
    'ssl'.freeze => service.ssl?,
    'protocol'.freeze => service.protocol,
    'product'.freeze => service.product,
    'hostname'.freeze => service.hostname, # This is just a string
    'device_type'.freeze => service.device_type,
    'fingerprint_method'.freeze => service.fingerprint_method,
    'fingerprint'.freeze => service.fingerprint,
    'confidence'.freeze => service.confidence
  }
end

#hashify_status(status) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/logstash/codecs/nmap.rb', line 60

def hashify_status(status)
  return unless status

  {
    'state'.freeze => status.state, # str
    'reason'.freeze => status.reason # str
  }
end

#hashify_struct(struct) ⇒ Object



161
162
163
# File 'lib/logstash/codecs/nmap.rb', line 161

def hashify_struct(struct)
  Hash[struct.each_pair.to_a]
end

#hashify_structs(structs) ⇒ Object



157
158
159
# File 'lib/logstash/codecs/nmap.rb', line 157

def hashify_structs(structs)
  structs.map {|s| hashify_struct(s)}
end

#hashify_traceroute(traceroute) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/logstash/codecs/nmap.rb', line 140

def hashify_traceroute(traceroute)
  return unless traceroute

  {
    'port'.freeze => traceroute.port, # int
    'protocol'.freeze => traceroute.protocol.to_s,
    'hops' => traceroute.map.with_index do |hop, idx|
      {
        'address'.freeze => hop.addr, # str
        'hostname'.freeze => hop.host, # str
        'ttl'.freeze => hop.ttl.to_i, # int
        'index' => idx # int (for searching by distance)
      }
    end
  }
end

#hashify_uptime(uptime) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/logstash/codecs/nmap.rb', line 103

def hashify_uptime(uptime)
  return unless uptime

  {
    'seconds'.freeze => uptime.seconds,
    'last_boot'.freeze => uptime.last_boot
  }
end

#host_to_event(host) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/logstash/codecs/nmap.rb', line 36

def host_to_event(host)
  event = LogStash::Event.new()
  event['start_time'.freeze] = host.start_time
  event['end_time'.freeze] = host.end_time

  # These two are actually different.
  # Address may contain a MAC, addresses will not AFAICT
  event['addresses'.freeze] = hashify_structs(host.addresses)
  event['address'.freeze] = host.address # str

  event['ip'.freeze] = host.ip # str
  event['ipv4'.freeze] = host.ipv4 # str
  event['ipv6'.freeze] = host.ipv6 # str
  event['ports'.freeze] = host.ports.map {|p| hashify_port(p)}
  event['mac'.freeze] = host.mac # str
  event['status'.freeze] = hashify_status(host.status)
  event['hostname'.freeze] = hashify_hostname(host.hostname)
  event['uptime'.freeze] = hashify_uptime(host.uptime)
  event['os'.freeze] = hashify_os(host.os)
  event['traceroute'.freeze] = hashify_traceroute(host.traceroute)

  event
end

#registerObject



13
14
# File 'lib/logstash/codecs/nmap.rb', line 13

def register
end