1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-11 10:19:03 +00:00

New function stdnse.format_mac

This function will format a MAC address as colon-separated hex bytes.
It's really very simple: stdnse.tohex(mac, {separator=":"})

This commit updates all the instances I could find of the varying
convoluted attempts at performing this conversion.
This commit is contained in:
dmiller
2014-01-16 21:50:30 +00:00
parent a6b79f9976
commit 197f28265f
13 changed files with 21 additions and 83 deletions

View File

@@ -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 <code>host.mac_addr</code>
--@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 "<blank>" (or the value of the second parameter) if the string
-- was blank or nil.
--

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -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))

View File

@@ -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)

View File

@@ -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()

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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