summaryrefslogtreecommitdiffstats
path: root/scripts/address-info.nse
blob: 0455ac3a9a30585aed6516aa4b3cb3700e0c89b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
local datafiles = require "datafiles"
local nmap = require "nmap"
local stdnse = require "stdnse"
local string = require "string"
local table = require "table"

description = [[
Shows extra information about IPv6 addresses, such as embedded MAC or IPv4 addresses when available.

Some IP address formats encode extra information; for example some IPv6
addresses encode an IPv4 address or MAC address. This script can decode
these address formats:
* IPv4-compatible IPv6 addresses,
* IPv4-mapped IPv6 addresses,
* Teredo IPv6 addresses,
* 6to4 IPv6 addresses,
* IPv6 addresses using an EUI-64 interface ID,
* IPv4-embedded IPv6 addresses,
* IPv4-translated IPv6 addresses and
* ISATAP Modified EUI-64 IPv6 addresses.

See RFC 4291 for general IPv6 addressing architecture and the
definitions of some terms.
]]

---
-- @output
-- Nmap scan report for ::1.2.3.4
-- Host script results:
-- | address-info:
-- |   IPv4-compatible:
-- |_    IPv4 address: 1.2.3.4
--
-- Nmap scan report for ::ffff:1.2.3.4
-- Host script results:
-- | address-info:
-- |   IPv4-mapped:
-- |_    IPv4 address: 1.2.3.4
--
-- Nmap scan report for 2001:0:506:708:282a:3d75:fefd:fcfb
-- Host script results:
-- | address-info:
-- |   Teredo:
-- |     Server IPv4 address: 5.6.7.8
-- |     Client IPv4 address: 1.2.3.4
-- |_    UDP port: 49802
--
-- Nmap scan report for 2002:102:304::1
-- Host script results:
-- | address-info:
-- |   6to4:
-- |_    IPv4 address: 1.2.3.4
--
-- Nmap scan report for fe80::a8bb:ccff:fedd:eeff
-- Host script results:
-- | address-info:
-- |   IPv6 EUI-64:
-- |     MAC address:
-- |       address: aa:bb:cc:dd:ee:ff
-- |_      manuf: Unknown
--
-- Nmap scan report for 64:ff9b::c000:221
-- Host script results:
-- | address-info:
-- |   IPv4-embedded IPv6 address:
-- |_    IPv4 address: 192.0.2.33
--
-- Nmap scan report for ::ffff:0:c0a8:101
-- Host script results:
-- | address-info:
-- |   IPv4-translated IPv6 address:
-- |_    IPv4 address: 192.168.1.1

-- * ISATAP. RFC 5214.
--   XXXX:XXXX:XXXX:XX00:0000:5EFE:a.b.c.d

---
--@xmloutput
-- <table key="IPv4-mapped">
--   <elem key="IPv4 address">1.2.3.4</elem>
-- </table>
--
-- <table key="IPv4-compatible">
--   <elem key="IPv4 address">1.2.3.4</elem>
-- </table>
--
-- <table key="Teredo">
--   <elem key="Server IPv4 address">5.6.7.8</elem>
--   <elem key="Client IPv4 address">1.2.3.4</elem>
--   <elem key="UDP port">49802</elem>
-- </table>
--
-- <table key="6to4">
--   <elem key="IPv4 address">1.2.3.4</elem>
-- </table>
--
-- <table key="IPv6 EUI-64">
--   <table key="MAC address">
--     <elem key="address">aa:bb:cc:dd:ee:ff</elem>
--     <elem key="manuf">Unknown</elem>
--   </table>
-- </table>
--
-- <table key="IPv4-embedded IPv6 address">
--   <elem key="IPv4 address">192.0.2.33</elem>
-- </table>
--
-- <table key="IPv4-translated IPv6 address">
--   <elem key="IPv4 address">192.168.1.1</elem>
-- </table>

author = "David Fifield"

license = "Same as Nmap--See https://nmap.org/book/man-legal.html"

categories = {"default", "safe"}


hostrule = function(host)
  return true
end

-- Match an address (array of bytes) against a hex-encoded pattern. "XX" in the
-- pattern is a wildcard.
local function matches(addr, pattern)
  local octet_patterns

  octet_patterns = {}
  for op in pattern:gmatch("([%xX][%xX])") do
    octet_patterns[#octet_patterns + 1] = op
  end

  if #addr ~= #octet_patterns then
    return false
  end

  for i = 1, #addr do
    local a, op

    a = addr[i]
    op = octet_patterns[i]
    if not (op == "XX" or a == tonumber(op, 16)) then
      return false
    end
  end

  return true
end

local function get_manuf(mac)
  local catch = function() return "Unknown" end
  local try = nmap.new_try(catch)
  local mac_prefixes = try(datafiles.parse_mac_prefixes())
  local prefix = string.upper(string.format("%02x%02x%02x", mac[1], mac[2], mac[3]))
  return mac_prefixes[prefix] or "Unknown"
end

local function format_mac(mac)
  local out = stdnse.output_table()
  out.address = stdnse.format_mac(string.char(table.unpack(mac)))
  out.manuf = get_manuf(mac)
  return out
end

local function format_ipv4(ipv4)
  local octets

  octets = {}
  for _, v in ipairs(ipv4) do
    octets[#octets + 1] = string.format("%d", v)
  end

  return table.concat(octets, ".")
end

local function do_ipv4(addr)
  -- intentionally empty
end

-- EUI-64 from MAC, RFC 4291.
local function decode_eui_64(eui_64)
  if eui_64[4] == 0xff and eui_64[5] == 0xfe then
    return { (eui_64[1] ~ 0x02),
      eui_64[2], eui_64[3], eui_64[6], eui_64[7], eui_64[8] }
  end
end

local function do_ipv6(addr)
  local label
  local output

  output = stdnse.output_table()

  if matches(addr, "0000:0000:0000:0000:0000:0000:0000:0001") then
    -- ::1 is localhost. Not much to report.
    return nil
  elseif matches(addr, "0000:0000:0000:0000:0000:0000:XXXX:XXXX") then
    -- RFC 4291 2.5.5.1.
    local ipv4 = { addr[13], addr[14], addr[15], addr[16] }
    return {["IPv4-compatible"]= { ["IPv4 address"] = format_ipv4(ipv4) } }
  elseif matches(addr, "0000:0000:0000:0000:0000:ffff:XXXX:XXXX") then
    -- RFC 4291 2.5.5.2.
    local ipv4 = { addr[13], addr[14], addr[15], addr[16] }
    return {["IPv4-mapped"]= { ["IPv4 address"] = format_ipv4(ipv4) } }
  elseif matches(addr, "2001:0000:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX") then
    -- Teredo, RFC 4380.
    local server_ipv4 = { addr[5], addr[6], addr[7], addr[8] }
    -- RFC 5991 makes the flags mostly meaningless.
    local flags = addr[9] * 256 + addr[10]
    local obs_port = addr[11] * 256 + addr[12]
    local obs_client_ipv4 = { addr[13], addr[14], addr[15], addr[16] }
    local port, client_ipv4

    -- Invert obs_port.
    port = obs_port ~ 0xffff

    -- Invert obs_client_ipv4.
    client_ipv4 = {}
    for _, octet in ipairs(obs_client_ipv4) do
      client_ipv4[#client_ipv4 + 1] = octet ~ 0xff
    end

    output["Server IPv4 address"] = format_ipv4(server_ipv4)
    output["Client IPv4 address"] = format_ipv4(client_ipv4)
    output["UDP port"] = tostring(port)

    return {["Teredo"] = output}
  elseif matches(addr, "0064:ff9b:XXXX:XXXX:00XX:XXXX:XXXX:XXXX") then
    --IPv4-embedded IPv6 addresses. RFC 6052, Section 2

    --skip addr[9]
    if matches(addr,"0064:ff9b:0000:0000:0000:0000:XXXX:XXXX") then
      local ipv4 = {addr[13], addr[14], addr[15], addr[16]}
      return {["IPv4-embedded IPv6 address"]= {["IPv4 address"] = format_ipv4(ipv4)}}
    elseif addr[5] ~= 0x01 then
      local ipv4 = {addr[5], addr[6], addr[7], addr[8]}
      return {["IPv4-embedded IPv6 address"]= {["IPv4 address"] = format_ipv4(ipv4)}}
    elseif addr[6] ~= 0x22 then
      local ipv4 = {addr[6], addr[7], addr[8], addr[10]}
      return {["IPv4-embedded IPv6 address"]= {["IPv4 address"] = format_ipv4(ipv4)}}
    elseif addr[7] ~= 0x03 then
      local ipv4 = {addr[7], addr[8], addr[10], addr[11]}
      return {["IPv4-embedded IPv6 address"]= {["IPv4 address"] = format_ipv4(ipv4)}}
    elseif addr[8] ~= 0x44 then
      local ipv4 = {addr[8], addr[10], addr[11], addr[12]}
      return {["IPv4-embedded IPv6 address"]= {["IPv4 address"] = format_ipv4(ipv4)}}
    elseif addr[10] == 0x00 and addr[11] == 0x00 and addr[12] == 0x00 then
      local ipv4 = {addr[13], addr[14], addr[15], addr[16]}
      return {["IPv4-embedded IPv6 address"]= {["IPv4 address"] = format_ipv4(ipv4)}}
    end
  elseif matches(addr, "0000:0000:0000:0000:ffff:0000:XXXX:XXXX") then
    -- IPv4-translated IPv6 addresses. RFC 2765, Section 2.1
    return {["IPv4-translated IPv6 address"]=
      {["IPv4 address"] = format_ipv4( {addr[13], addr[14], addr[15], addr[16]})}}
  elseif matches(addr, "XXXX:XXXX:XXXX:XX00:0000:5efe:XXXX:XXXX") then
    -- ISATAP. RFC 5214, Appendix A
    -- XXXX:XXXX:XXXX:XX00:0000:5EFE:a.b.c.d
    return {["ISATAP Modified EUI-64 IPv6 Address"]=
      {["IPv4 address"] = format_ipv4( {addr[13], addr[14], addr[15], addr[16]})}}
  end

  -- These following use common handling for the Interface ID part
  -- (last 64 bits).

  if matches(addr, "2002:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX") then
    -- 6to4, RFC 3056.
    local ipv4 = { addr[3], addr[4], addr[5], addr[6] }

    label = "6to4"
    output["IPv4 address"] = format_ipv4(ipv4)
  end

  local mac = decode_eui_64({ addr[9], addr[10], addr[11], addr[12],
    addr[13], addr[14], addr[15], addr[16] })
  if mac then
    output["MAC address"] = format_mac(mac)
    if not label then
      label = "IPv6 EUI-64"
    end
  end

  if label then
    return {[label]= output}
  end
  -- else no match
end

action = function(host)
  local addr_s, addr_t

  addr_s = host.bin_ip
  addr_t = { string.byte(addr_s, 1, #addr_s) }

  if #addr_t == 4 then
    return do_ipv4(addr_t)
  elseif #addr_t == 16 then
    return do_ipv6(addr_t)
  end
end