From 8df11582e6c05d8cb6073f29054877eda5290569 Mon Sep 17 00:00:00 2001 From: dmiller Date: Sun, 19 Jan 2020 16:37:36 +0000 Subject: [PATCH] Move comma-separated list code to outlib --- nselib/outlib.lua | 29 +++++++++++++++++++++++++++++ scripts/afp-serverinfo.nse | 11 +++-------- scripts/broadcast-dhcp-discover.nse | 10 +++------- scripts/dhcp-discover.nse | 9 ++------- scripts/ipv6-node-info.nse | 15 +++++---------- scripts/reverse-index.nse | 9 ++------- 6 files changed, 44 insertions(+), 39 deletions(-) diff --git a/nselib/outlib.lua b/nselib/outlib.lua index c04eff8bc..933206058 100644 --- a/nselib/outlib.lua +++ b/nselib/outlib.lua @@ -17,6 +17,7 @@ local yield = coroutine.yield local table = require "table" local sort = table.sort +local concat = table.concat local setmetatable = setmetatable local ipairs = ipairs @@ -46,4 +47,32 @@ function sorted_by_key(t) return out end +local commasep = { + __tostring = function (t) + return concat(t, ", ") + end +} + +--- Comma-separated list output +-- +-- This adds a __tostring metamethod to a list (integer-indexed +-- table) so that it will be formatted as a comma-separated list when converted +-- to a string. +-- @param t The table to format +-- @param sep (Optional) list separator character, default: ", " +function list_sep(t, sep) + -- Reuse closures and metatables as much as possible + local oldmt = getmetatable(t) + local newmt = sep and { + __tostring = function(tt) + return concat(tt, sep) + end} or commasep + -- Avoid clobbering old metatable or our static commasep table + if oldmt and oldmt ~= commasep then + oldmt.__tostring = newmt.__tostring + else + setmetatable(t, newmt) + end +end + return _ENV diff --git a/scripts/afp-serverinfo.nse b/scripts/afp-serverinfo.nse index a0c4c0602..d192e0f0b 100644 --- a/scripts/afp-serverinfo.nse +++ b/scripts/afp-serverinfo.nse @@ -1,5 +1,6 @@ local afp = require "afp" local nmap = require "nmap" +local outlib = require "outlib" local shortport = require "shortport" local stdnse = require "stdnse" local table = require "table" @@ -88,12 +89,6 @@ categories = {"default", "discovery", "safe"} portrule = shortport.port_or_service(548, "afp") -local commasep = { - __tostring = function (t) - return table.concat(t, ", ") - end -} - action = function(host, port) local socket = nmap.new_socket() @@ -148,11 +143,11 @@ action = function(host, port) -- list the supported AFP versions result["AFP Versions"] = response.afp_versions - setmetatable(result["AFP Versions"], commasep) + outlib.list_sep(result["AFP Versions"]) -- list the supported UAMs (User Authentication Modules) result["UAMs"] = response.uams - setmetatable(result["UAMs"], commasep) + outlib.list_sep(result["UAMs"]) -- server signature, not sure of the format here so just showing a hex string if response.flags.ServerSignature then diff --git a/scripts/broadcast-dhcp-discover.nse b/scripts/broadcast-dhcp-discover.nse index 977f27cfa..5bea5b486 100644 --- a/scripts/broadcast-dhcp-discover.nse +++ b/scripts/broadcast-dhcp-discover.nse @@ -3,7 +3,9 @@ local dhcp = require "dhcp" local ipOps = require "ipOps" local math = require "math" local nmap = require "nmap" +local outlib = require "outlib" local packet = require "packet" +local rand = require "rand" local stdnse = require "stdnse" local string = require "string" local table = require "table" @@ -141,12 +143,6 @@ local function dhcp_listener(sock, timeout, xid, result) condvar "signal" end -local commasep = { - __tostring = function (t) - return table.concat(t, ", ") - end -} - local function fail (err) return stdnse.format_output(false, err) end action = function() @@ -230,7 +226,7 @@ action = function() result_table["IP Offered"] = r.yiaddr_str for _, v in ipairs(r.options) do if(type(v.value) == 'table') then - setmetatable(v.value, commasep) + outlib.list_sep(v.value) end result_table[ v.name ] = v.value end diff --git a/scripts/dhcp-discover.nse b/scripts/dhcp-discover.nse index 65e6cff22..856266368 100644 --- a/scripts/dhcp-discover.nse +++ b/scripts/dhcp-discover.nse @@ -1,6 +1,7 @@ local dhcp = require "dhcp" local rand = require "rand" local nmap = require "nmap" +local outlib = require "outlib" local shortport = require "shortport" local stdnse = require "stdnse" local string = require "string" @@ -103,12 +104,6 @@ function portrule(host, port) return shortport.portnumber(67, "udp")(host, port) end -local commasep = { - __tostring = function (t) - return table.concat(t, ", ") - end -} - action = function(host, port) local dhcptype = (stdnse.get_script_args(SCRIPT_NAME .. ".dhcptype") or "DHCPINFORM"):upper() local dhcptypeid = dhcp.request_types[dhcptype] @@ -185,7 +180,7 @@ action = function(host, port) end for _, v in ipairs(result.options) do if type(v.value) == 'table' then - setmetatable(v.value, commasep) + outlib.list_sep(v.value) end result_table[ v.name ] = v.value end diff --git a/scripts/ipv6-node-info.nse b/scripts/ipv6-node-info.nse index 89311a996..2104a509d 100644 --- a/scripts/ipv6-node-info.nse +++ b/scripts/ipv6-node-info.nse @@ -1,6 +1,7 @@ local dns = require "dns" local ipOps = require "ipOps" local nmap = require "nmap" +local outlib = require "outlib" local packet = require "packet" local stdnse = require "stdnse" local string = require "string" @@ -152,12 +153,6 @@ local function stringify_noop(flags, data) return "replied" end -local commasep = { - __tostring = function (t) - return table.concat(t, ", ") - end -} - -- RFC 4620, section 6.3. local function stringify_nodename(flags, data) local status, names @@ -170,7 +165,7 @@ local function stringify_nodename(flags, data) names[#names+1] = "(parsing error)" end - setmetatable(names, commasep) + outlib.list_sep(names) return names end @@ -195,7 +190,7 @@ local function stringify_nodeaddresses(flags, data) addrs[#addrs+1] = "(more omitted for space reasons)" end - setmetatable(addrs, commasep) + outlib.list_sep(addrs) return addrs end @@ -217,7 +212,7 @@ local function stringify_nodeipv4addresses(flags, data) -- Check for DNS names. status, names = try_decode_nodenames(data .. "\0\0") if status then - setmetatable(names, commasep) + outlib.list_sep(names) return names end @@ -237,7 +232,7 @@ local function stringify_nodeipv4addresses(flags, data) addrs[#addrs+1] = "(more omitted for space reasons)" end - setmetatable(addrs, commasep) + outlib.list_sep(addrs) return addrs end diff --git a/scripts/reverse-index.nse b/scripts/reverse-index.nse index 993fcf09a..04e37d472 100644 --- a/scripts/reverse-index.nse +++ b/scripts/reverse-index.nse @@ -1,5 +1,6 @@ local ipOps = require "ipOps" local nmap = require "nmap" +local outlib = require "outlib" local stdnse = require "stdnse" local table = require "table" local tableaux = require "tableaux" @@ -85,12 +86,6 @@ hostaction = function(host) end end -local commasep = { - __tostring = function (t) - return table.concat(t, ", ") - end -} - postaction = function() local db = nmap.registry[SCRIPT_NAME] if ( db == nil ) then @@ -108,7 +103,7 @@ postaction = function() local result_entries = ports[port] ipOps.ip_sort(result_entries) if mode == 'horizontal' then - setmetatable(result_entries, commasep) + outlib.list_sep(result_entries) end results[("%s/%s"):format(port, proto)] = result_entries end