diff --git a/nselib/stdnse.lua b/nselib/stdnse.lua index b5d80d660..684e8c1c7 100644 --- a/nselib/stdnse.lua +++ b/nselib/stdnse.lua @@ -303,6 +303,13 @@ function tohex( s, options ) return hex end +---Format a MAC address as colon-separated hex bytes. +--@param mac The MAC address in binary, such as host.mac_addr +--@return The MAC address in XX:XX:XX:XX:XX:XX format +function format_mac(mac) + return tohex(mac, {separator=":"}) +end + ---Either return the string itself, or return "" (or the value of the second parameter) if the string -- was blank or nil. -- diff --git a/scripts/address-info.nse b/scripts/address-info.nse index ecc4a165f..202b032cb 100644 --- a/scripts/address-info.nse +++ b/scripts/address-info.nse @@ -3,6 +3,7 @@ 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. @@ -156,15 +157,8 @@ local function get_manuf(mac) end local function format_mac(mac) - local octets - - octets = {} - for _, v in ipairs(mac) do - octets[#octets + 1] = string.format("%02x", v) - end - local out = stdnse.output_table() - out.address = stdnse.strjoin(":", octets) + out.address = stdnse.format_mac(string.char(table.unpack(mac))) out.manuf = get_manuf(mac) return out end diff --git a/scripts/broadcast-ataoe-discover.nse b/scripts/broadcast-ataoe-discover.nse index 9fc3a7242..436e1469a 100644 --- a/scripts/broadcast-ataoe-discover.nse +++ b/scripts/broadcast-ataoe-discover.nse @@ -125,10 +125,6 @@ local function sendConfigInfoRequest(iface) dnet:ethernet_close() end -local function mactostr(bin_mac) - return stdnse.tohex(bin_mac, { separator=":", group=2 }) -end - local function fail(err) return ("\n ERROR: %s"):format(err or "") end action = function() @@ -151,7 +147,7 @@ action = function() local pcap = nmap.new_socket() pcap:set_timeout(5000) - pcap:pcap_open(iface.device, 1500, true, "ether proto 0x88a2 && !ether src " .. mactostr(iface.mac)) + pcap:pcap_open(iface.device, 1500, true, "ether proto 0x88a2 && !ether src " .. stdnse.format_mac(iface.mac)) sendConfigInfoRequest(iface) @@ -165,7 +161,7 @@ action = function() f:ether_parse() local str = ("Server: %s; Version: %d; Major: %d; Minor: %d"):format( - mactostr(f.mac_src), + stdnse.format_mac(f.mac_src), header.version, header.major, header.minor) diff --git a/scripts/broadcast-ping.nse b/scripts/broadcast-ping.nse index 7b9462139..23a7b426a 100644 --- a/scripts/broadcast-ping.nse +++ b/scripts/broadcast-ping.nse @@ -193,8 +193,7 @@ local broadcast_if = function(if_table,icmp_responders) if icmpreply:ip_parse() and icmp_ids[icmp_id] then if not icmp_responders[icmpreply.ip_src] then -- [key = IP]=MAC - local mac_pretty = string.format("%02x:%02x:%02x:%02x:%02x:%02x",l2:byte(7), - l2:byte(8),l2:byte(9),l2:byte(10),l2:byte(11),l2:byte(12)) + local mac_pretty = stdnse.format_mac(l2:sub(7,12)) icmp_responders[icmpreply.ip_src] = mac_pretty end else diff --git a/scripts/broadcast-pppoe-discover.nse b/scripts/broadcast-pppoe-discover.nse index 048d89490..2e744f9c1 100644 --- a/scripts/broadcast-pppoe-discover.nse +++ b/scripts/broadcast-pppoe-discover.nse @@ -49,12 +49,6 @@ local function fail(err) end end -local function mac_tostr(mac) - local srv_mac = {} - for i=1, #mac do table.insert(srv_mac, select(2,bin.unpack("H", mac, i))) end - return stdnse.strjoin(":", srv_mac) -end - local function discoverPPPoE(helper) local status, err = helper:connect() @@ -117,7 +111,7 @@ action = function() end helper:close() - local output = { name = ("Server: %s"):format(mac_tostr(pado.mac_srv)) } + local output = { name = ("Server: %s"):format(stdnse.format_mac(pado.mac_srv)) } table.insert(output, ("Version: %d"):format(pado.header.version)) table.insert(output, ("Type: %d"):format(pado.header.type)) diff --git a/scripts/duplicates.nse b/scripts/duplicates.nse index 70a72cfe1..ed7bbdf96 100644 --- a/scripts/duplicates.nse +++ b/scripts/duplicates.nse @@ -164,20 +164,12 @@ end local function processMAC(tab) - local function format_mac(mac) - local octets = {} - for _, v in ipairs({ string.byte(mac, 1, #mac) }) do - octets[#octets + 1] = string.format("%02x", v) - end - return stdnse.strjoin(":", octets) - end - local mac local mac_table = {} for host in pairs(tab) do if ( host.mac_addr ) then - mac = format_mac(host.mac_addr) + mac = stdnse.format_mac(host.mac_addr) mac_table[mac] = mac_table[mac] or {} if ( not(contains(mac_table[mac], host.ip)) ) then table.insert(mac_table[mac], host.ip) diff --git a/scripts/ip-forwarding.nse b/scripts/ip-forwarding.nse index d39d252d0..97c0163b5 100644 --- a/scripts/ip-forwarding.nse +++ b/scripts/ip-forwarding.nse @@ -46,23 +46,12 @@ hostrule = function(host) end -local function format_mac(mac) - local octets - - octets = {} - for _, v in ipairs({ string.byte(mac, 1, #mac) }) do - octets[#octets + 1] = string.format("%02x", v) - end - - return stdnse.strjoin(":", octets) -end - icmpEchoRequest = function(ifname, host, addr) local iface = nmap.get_interface_info(ifname) local dnet, pcap = nmap.new_dnet(), nmap.new_socket() pcap:set_timeout(5000) - pcap:pcap_open(iface.device, 128, false, ("ether src %s and icmp and ( icmp[0] = 0 or icmp[0] = 5 ) and dst %s"):format(format_mac(host.mac_addr), iface.address)) + pcap:pcap_open(iface.device, 128, false, ("ether src %s and icmp and ( icmp[0] = 0 or icmp[0] = 5 ) and dst %s"):format(stdnse.format_mac(host.mac_addr), iface.address)) dnet:ethernet_open(iface.device) local probe = packet.Frame:new() diff --git a/scripts/lltd-discovery.nse b/scripts/lltd-discovery.nse index 8a8a898fe..79974df3f 100644 --- a/scripts/lltd-discovery.nse +++ b/scripts/lltd-discovery.nse @@ -75,7 +75,7 @@ local function get_mac_addr( mac ) else local prefix = string.upper(string.format("%02x%02x%02x", mac:byte(1), mac:byte(2), mac:byte(3))) local manuf = mac_prefixes[prefix] or "Unknown" - return string.format("%02x:%02x:%02x:%02x:%02x:%02x (%s)", mac:byte(1), mac:byte(2), mac:byte(3), mac:byte(4), mac:byte(5), mac:byte(6), manuf ) + return string.format("%s (%s)", stdnse.format_mac(mac:sub(1,6)), manuf ) end end diff --git a/scripts/snmp-interfaces.nse b/scripts/snmp-interfaces.nse index 9a5004d56..297aee6b6 100644 --- a/scripts/snmp-interfaces.nse +++ b/scripts/snmp-interfaces.nse @@ -192,7 +192,7 @@ function get_mac_addr( mac ) else local prefix = string.upper(string.format("%02x%02x%02x", mac:byte(1), mac:byte(2), mac:byte(3))) local manuf = mac_prefixes[prefix] or "Unknown" - return string.format("%02x:%02x:%02x:%02x:%02x:%02x (%s)", mac:byte(1), mac:byte(2), mac:byte(3), mac:byte(4), mac:byte(5), mac:byte(6), manuf ) + return string.format("%s (%s)", stdnse.format_mac(mac:sub(1,6)), manuf ) end end diff --git a/scripts/targets-ipv6-multicast-echo.nse b/scripts/targets-ipv6-multicast-echo.nse index e19000641..739ed2a03 100644 --- a/scripts/targets-ipv6-multicast-echo.nse +++ b/scripts/targets-ipv6-multicast-echo.nse @@ -60,17 +60,6 @@ local function get_interfaces() return interfaces end -local function format_mac(mac) - local octets - - octets = {} - for _, v in ipairs({ string.byte(mac, 1, #mac) }) do - octets[#octets + 1] = string.format("%02x", v) - end - - return stdnse.strjoin(":", octets) -end - local function single_interface_broadcast(if_nfo, results) stdnse.print_debug("Starting " .. SCRIPT_NAME .. " on " .. if_nfo.device) @@ -129,7 +118,7 @@ local function single_interface_broadcast(if_nfo, results) if target.ALLOW_NEW_TARGETS then target.add(target_str) end - results[#results + 1] = { address = target_str, mac = format_mac(reply.mac_src), iface = if_nfo.device } + results[#results + 1] = { address = target_str, mac = stdnse.format_mac(reply.mac_src), iface = if_nfo.device } results[target_str] = true end end diff --git a/scripts/targets-ipv6-multicast-invalid-dst.nse b/scripts/targets-ipv6-multicast-invalid-dst.nse index 03af23fb6..964f042ca 100644 --- a/scripts/targets-ipv6-multicast-invalid-dst.nse +++ b/scripts/targets-ipv6-multicast-invalid-dst.nse @@ -78,17 +78,6 @@ local function get_interfaces() return interfaces end -local function format_mac(mac) - local octets - - octets = {} - for _, v in ipairs({ string.byte(mac, 1, #mac) }) do - octets[#octets + 1] = string.format("%02x", v) - end - - return stdnse.strjoin(":", octets) -end - local function single_interface_broadcast(if_nfo, results) stdnse.print_debug("Starting " .. SCRIPT_NAME .. " on " .. if_nfo.device) @@ -161,7 +150,7 @@ local function single_interface_broadcast(if_nfo, results) if target.ALLOW_NEW_TARGETS then target.add(target_str) end - results[#results + 1] = { address = target_str, mac = format_mac(l2reply.mac_src), iface = if_nfo.device } + results[#results + 1] = { address = target_str, mac = stdnse.format_mac(l2reply.mac_src), iface = if_nfo.device } results[target_str] = true end end diff --git a/scripts/targets-ipv6-multicast-mld.nse b/scripts/targets-ipv6-multicast-mld.nse index 354bb9e01..1ec210ac6 100644 --- a/scripts/targets-ipv6-multicast-mld.nse +++ b/scripts/targets-ipv6-multicast-mld.nse @@ -126,7 +126,7 @@ local function single_interface_broadcast(if_nfo, results) if target.ALLOW_NEW_TARGETS then target.add(target_str) end - results[target_str] = { address = target_str, mac = stdnse.tohex(l2reply.mac_src, {separator = ":", group = 2}), iface = if_nfo.device } + results[target_str] = { address = target_str, mac = stdnse.format_mac(l2reply.mac_src), iface = if_nfo.device } end end end diff --git a/scripts/targets-ipv6-multicast-slaac.nse b/scripts/targets-ipv6-multicast-slaac.nse index a1ea20fb7..21b8f97d9 100644 --- a/scripts/targets-ipv6-multicast-slaac.nse +++ b/scripts/targets-ipv6-multicast-slaac.nse @@ -120,17 +120,6 @@ local function get_interfaces() return interfaces end -local function format_mac(mac) - local octets - - octets = {} - for _, v in ipairs({ string.byte(mac, 1, #mac) }) do - octets[#octets + 1] = string.format("%02x", v) - end - - return stdnse.strjoin(":", octets) -end - local function single_interface_broadcast(if_nfo, results) stdnse.print_debug("Starting " .. SCRIPT_NAME .. " on " .. if_nfo.device) @@ -208,7 +197,7 @@ local function single_interface_broadcast(if_nfo, results) if target.ALLOW_NEW_TARGETS then target.add(actual_addr_str) end - results[#results + 1] = { address = actual_addr_str, mac = format_mac(l2reply.mac_src), iface = if_nfo.device } + results[#results + 1] = { address = actual_addr_str, mac = stdnse.format_mac(l2reply.mac_src), iface = if_nfo.device } results[actual_addr_str] = true end end