From 7aab4b8ef995b36357e3b62cf44c6903cb348018 Mon Sep 17 00:00:00 2001 From: Brendan Coles Date: Sat, 6 Nov 2021 15:24:11 +0000 Subject: [PATCH] Add broadcast-nmdp-discover.nse Discovers MikroTik devices on a LAN by sending a MikroTik Neighbor Discovery Protocol (MNDP) network broadcast probe. --- scripts/broadcast-mndp-discover.nse | 244 ++++++++++++++++++++++++++++ 1 file changed, 244 insertions(+) create mode 100644 scripts/broadcast-mndp-discover.nse diff --git a/scripts/broadcast-mndp-discover.nse b/scripts/broadcast-mndp-discover.nse new file mode 100644 index 000000000..c1f979dfd --- /dev/null +++ b/scripts/broadcast-mndp-discover.nse @@ -0,0 +1,244 @@ +local nmap = require "nmap" +local stdnse = require "stdnse" +local string = require "string" +local packet = require "packet" +local target = require "target" +local os = require "os" +local table = require "table" + +description = [[ +Discovers MikroTik devices on a LAN by sending a MikroTik Neighbor Discovery Protocol (MNDP) network broadcast probe. + +For more information about MNDP, see: +* https://mikrotik.com/testdocs/ros/2.9/ip/mndp.php +* https://wiki.mikrotik.com/wiki/Manual:IP/Neighbor_discovery +* https://www.wireshark.org/docs/dfref/m/mndp.html +* https://hadler.me/cc/mikrotik-neighbor-discovery-mndp/ +]] + +--- +-- @usage nmap --script broadcast-mndp-discover +-- @usage nmap --script broadcast-mndp-discover --script-args timeout=5s -e eth0 +-- +-- @output +-- Pre-scan script results: +-- | broadcast-mndp-discover: +-- | MAC Address: 00:0c:29:6d:a7:63, IP Address: 0.0.0.0; Identity: MikroTik; Version: 6.42.12 (long-term); Platform: MikroTik; Software ID: GXCE-KYGV; Uptime: 1h14m; Board: x86; Unpacking: None; Interface: ether1 +-- | MAC Address: 00:0c:29:6d:a7:63, IP Address: fe80::20c:29ff:fe6d:a763; Identity: MikroTik; Version: 6.42.12 (long-term); Platform: MikroTik; Software ID: GXCE-KYGV; Uptime: 1h14m; Board: x86; Unpacking: None; Interface: ether1 +-- | MAC Address: 00:0c:29:8b:de:c6, IP Address: 10.1.1.123; Identity: MikroTik; Version: 6.10; Platform: MikroTik; Software ID: 33UY-8JI2; Uptime: 0h42m; Board: x86; Unpacking: None; Interface: ether1 +-- |_ MAC Address: 00:0c:29:8b:de:c6, IP Address: fe80::20c:29ff:fe8b:dec6; Identity: MikroTik; Version: 6.10; Platform: MikroTik; Software ID: 33UY-8JI2; Uptime: 0h42m; Board: x86; Unpacking: None; Interface: ether1 +-- +-- @args broadcast-mndp-discover.address +-- address to which the probe packet is sent. (default: 255.255.255.255) +-- @args broadcast-mndp-discover.timeout +-- socket timeout (default: 5s) +--- + +author = "Brendan Coles" +license = "Same as Nmap--See https://nmap.org/book/man-legal.html" +categories = {"discovery", "broadcast", "safe"} + +prerule = function() return ( nmap.address_family() == "inet") end + +local arg_address = stdnse.get_script_args(SCRIPT_NAME .. ".address") +local arg_timeout = stdnse.parse_timespec(stdnse.get_script_args(SCRIPT_NAME .. ".timeout")) + +-- Listens for MNDP response messages. +--@param interface Network interface to listen on. +--@param timeout Time to listen for a response. +--@param responses table to insert response data into. +local mndpListen = function(interface, timeout, responses) + local condvar = nmap.condvar(responses) + local start = nmap.clock_ms() + local listener = nmap.new_socket() + local filter = 'udp src port 5678 and udp dst port 5678 and src host not ' .. interface.address + listener:set_timeout(500) + listener:pcap_open(interface.device, 1024, true, filter) + + while (nmap.clock_ms() - start) < timeout do + local status, _, _, l3data = listener:pcap_receive() + + if not status then + goto continue + end + + local p = packet.Packet:new(l3data, #l3data) + local data = l3data:sub(p.udp_offset + 9) + + if #data < 4 then + goto continue + end + + stdnse.print_debug(1, "Received MNDP response from %s (%s bytes)", p.ip_src, string.len(data)) + + local tlv_type, tlv_len, tlv_value, pos + pos = 1 + --local header = data:sub(pos, pos + 1) + pos = pos + 2 + --local seqno = data:sub(pos, pos + 1) + pos = pos + 2 + + local mac_address, identity, version, platform, uptime, software_id, board, unpacking, ipv6_address, interface + while (pos < #data) do + tlv_type, tlv_value, pos = string.unpack(">I2s2", data, pos) + + --stdnse.print_debug(2, "TLV Type: %s", tlv_type) + --stdnse.print_debug(2, "TLV Length: %s", string.len(tlv_value)) + --stdnse.print_debug(2, "TLV Value: %s", stdnse.tohex(tlv_value)) + + -- MAC address + if tlv_type == 0x01 then + mac_address = stdnse.format_mac(tlv_value) + + -- Identity + elseif tlv_type == 0x05 then + identity = tlv_value + + -- Version + elseif tlv_type == 0x07 then + version = tlv_value + + -- Platform + elseif tlv_type == 0x08 then + platform = tlv_value + + -- Uptime - unsigned integer + elseif tlv_type == 0x0a then + uptime_num = string.unpack(" 0 then + -- remove duplicates + local hash = {} + for _,v in ipairs(results) do + if (not hash[v]) then + table.insert( output, v ) + hash[v] = true + end + end + return output + end +end