summaryrefslogtreecommitdiffstats
path: root/scripts/broadcast-ripng-discover.nse
blob: 82f90dd085f9f953519ad979833fbfca9f1723b8 (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
local ipOps = require "ipOps"
local nmap = require "nmap"
local stdnse = require "stdnse"
local string = require "string"
local tab = require "tab"
local table = require "table"

description = [[
Discovers hosts and routing information from devices running RIPng on the
LAN by sending a broadcast RIPng Request command and collecting any responses.
]]

---
-- @usage
-- nmap --script broadcast-ripng-discover
--
-- @output
-- | broadcast-ripng-discover:
-- |   fe80::a00:27ff:fe9a:880c
-- |     route                       metric  next hop
-- |     fe80:470:0:0:0:0:0:0/64     1
-- |     fe80:471:0:0:0:0:0:0/64     1
-- |_    fe80:472:0:0:0:0:0:0/64     1
--
-- @args broadcast-ripng-discover.timeout sets the connection timeout
--       (default: 5s)

author = "Patrik Karlsson"
license = "Same as Nmap--See https://nmap.org/book/man-legal.html"
categories = {"broadcast", "safe"}


prerule = function() return ( nmap.address_family() == "inet6" ) end

RIPng = {

  -- Supported RIPng commands
  Command = {
    Request = 1,
    Response = 2,
  },

  -- Route table entry
  RTE = {

    -- Creates a new Route Table Entry
    -- @param prefix string containing the ipv6 route prefix
    -- @param tag number containing the route tag
    -- @param prefix_len number containing the length in bits of the
    --        significant part of the prefix
    -- @param metric number containing the current metric for the
    --        destination
    new = function(self, prefix, tag, prefix_len, metric)
      local o = {
        prefix = prefix,
        tag = tag,
        prefix_len = prefix_len,
        metric = metric
      }
      setmetatable(o, self)
      self.__index = self
      return o
    end,

    -- Parses a byte string and creates an instance of RTE
    -- @param data string of bytes
    -- @return rte instance of RTE
    parse = function(data)
      local rte = RIPng.RTE:new()
      local pos, ip

      ip, rte.tag, rte.prefix_len, rte.metric, pos = string.unpack(">c16 I2 BB", data)
      rte.prefix = ipOps.str_to_ip(ip, 'inet6')
      return rte
    end,

    -- Converts a RTE instance to string
    -- @return string of bytes to send to the server
    __tostring = function(self)
      local ipstr = ipOps.ip_to_str(self.prefix)
      assert(16 == #ipstr, "Invalid IPv6 address encountered")
      return ipstr .. string.pack(">I2 BB", self.tag, self.prefix_len, self.metric)
    end,


  },

  -- The Request class contains functions to build a RIPv2 Request
  Request = {

    -- Creates a new Request instance
    --
    -- @param command number containing the RIPv2 Command to use
    -- @return o instance of request
    new = function(self, entries)
      local o = {
        command = 1,
        version = 1,
        entries = entries,
      }
      setmetatable(o, self)
      self.__index = self
      return o
    end,

    -- Converts the whole request to a string
    __tostring = function(self)
      local RESERVED = 0
      local str = {string.pack(">BB I2", self.command, self.version, RESERVED)}
      for _, rte in ipairs(self.entries) do
        str[#str+1] = tostring(rte)
      end
      return table.concat(str)
    end,

  },

  -- A RIPng Response
  Response = {

    -- Creates a new Response instance
    -- @return o new instance of Response
    new = function(self)
      local o = {  }
      setmetatable(o, self)
      self.__index = self
      return o
    end,

    -- Creates a new Response instance based on a string of bytes
    -- @return resp new instance of Response
    parse = function(data)
      local resp = RIPng.Response:new()
      local pos, _

      resp.command, resp.version, _, pos = string.unpack(">BB I2", data)
      resp.entries = {}
      while( pos < #data ) do
        local e = RIPng.RTE.parse(data:sub(pos))
        table.insert(resp.entries, e)
        pos = pos + 20
      end

      return resp
    end,
  }
}

local function fail(err) return stdnse.format_output(false, err) end

-- Parses a RIPng response
-- @return ret string containing the routing table
local function parse_response(resp)
  local next_hop
  local result = tab.new(3)
  tab.addrow(result, "route", "metric", "next hop")
  for _, rte in pairs(resp.entries or {}) do
    -- next hop information is specified in a separate RTE according to
    -- RFC 2080 section 2.1.1
    if ( 0xFF == rte.metric ) then
      next_hop = rte.prefix
    else
      tab.addrow(result, ("%s/%d"):format(rte.prefix, rte.prefix_len), rte.metric, next_hop or "")
    end
  end
  return tab.dump(result)
end

action = function()

  local req = RIPng.Request:new( { RIPng.RTE:new("0::", 0, 0, 16) } )
  local host, port = "FF02::9", { number = 521, protocol = "udp" }
  local iface = nmap.get_interface()
  local timeout = stdnse.parse_timespec(stdnse.get_script_args(SCRIPT_NAME..".timeout"))
  timeout = (timeout or 5) * 1000

  local sock = nmap.new_socket("udp")
  sock:bind(nil, 521)
  sock:set_timeout(timeout)

  local status = sock:sendto(host, port, tostring(req))

  -- do we need to add the interface name to the address?
  if ( not(status) ) then
    if ( not(iface) ) then
      return fail("Couldn't determine what interface to use, try supplying it with -e")
    end
    status = sock:sendto(host .. "%" .. iface, port, tostring(req))
  end

  if ( not(status) ) then
    return fail("Failed to send request to server")
  end

  local responses = {}
  while(true) do
    local status, data = sock:receive()
    if ( not(status) ) then
      break
    else
      local status, _, _, rhost = sock:get_info()
      if ( not(status) ) then
        rhost = "unknown"
      end
      responses[rhost] = RIPng.Response.parse(data)
    end
  end

  local result = {}
  for ip, resp in pairs(responses) do
    stdnse.debug1(ip, resp)
    table.insert(result, { name = ip, parse_response(resp) } )
  end
  return stdnse.format_output(true, result)
end