1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-07 21:21:31 +00:00

New outlib library for output-related functions

This commit is contained in:
dmiller
2020-01-16 19:12:58 +00:00
parent 8f3364a441
commit 17416feb5e
5 changed files with 57 additions and 42 deletions

View File

@@ -1,5 +1,8 @@
#Nmap Changelog ($Id$); -*-text-*- #Nmap Changelog ($Id$); -*-text-*-
o [NSE] New outlib library will consolidate functions related to NSE output,
both string formatting conventions and structured output. [Daniel Miller]
o New XML output "hosthint" tag emitted during host discovery when a target is o New XML output "hosthint" tag emitted during host discovery when a target is
found to be up. This gives earlier notification than waiting for the found to be up. This gives earlier notification than waiting for the
hostgroup to finish all scan phases. [Paul Miseiko] hostgroup to finish all scan phases. [Paul Miseiko]

49
nselib/outlib.lua Normal file
View File

@@ -0,0 +1,49 @@
--- Helper functions for NSE script output
--
-- These functions are useful for ensuring output is consistently ordered
-- between scans and following conventions for output formatting.
--
-- @author Daniel Miller
-- @copyright Same as Nmap--See https://nmap.org/book/man-legal.html
-- @class module
-- @name outlib
local tableaux = require "tableaux"
local keys = tableaux.keys
local coroutine = require "coroutine"
local wrap = coroutine.wrap
local yield = coroutine.yield
local table = require "table"
local sort = table.sort
local setmetatable = setmetatable
local ipairs = ipairs
local _ENV = {}
--- Create a table that yields elements sorted by key when iterated over with pairs()
--
-- The returned table is like a sorted view of the original table; it should be
-- treated as read-only, and any new data should be added to the original table
-- instead.
--@param t The table whose data should be used
--@return out A table that can be passed to pairs() to get sorted results
function sorted_by_key(t)
local out = {}
setmetatable(out, {
__pairs = function(_)
local order = keys(t)
sort(order)
return wrap(function()
for i,k in ipairs(order) do
yield(k, t[k])
end
end)
end
})
return out
end
return _ENV

View File

@@ -105,6 +105,7 @@ local libs = {
"omp2", "omp2",
"openssl", "openssl",
"ospf", "ospf",
"outlib",
"packet", "packet",
"pcre", "pcre",
"pgsql", "pgsql",

View File

@@ -3,6 +3,7 @@ local datetime = require "datetime"
local formulas = require "formulas" local formulas = require "formulas"
local math = require "math" local math = require "math"
local nmap = require "nmap" local nmap = require "nmap"
local outlib = require "outlib"
local stdnse = require "stdnse" local stdnse = require "stdnse"
local table = require "table" local table = require "table"
@@ -126,26 +127,6 @@ local function sorted_keys(t)
return ret return ret
end end
--- Return a table that yields elements sorted by key when iterated over with pairs()
-- Should probably put this in a formatting library later.
-- Depends on keys() function defined above.
--@param t The table whose data should be used
--@return out A table that can be passed to pairs() to get sorted results
function sorted_by_key(t)
local out = {}
setmetatable(out, {
__pairs = function(_)
local order = sorted_keys(t)
return coroutine.wrap(function()
for i,k in ipairs(order) do
coroutine.yield(k, t[k])
end
end)
end
})
return out
end
postaction = function() postaction = function()
local skews = nmap.registry.clock_skews local skews = nmap.registry.clock_skews
@@ -184,7 +165,7 @@ postaction = function()
end end
if next(out) then if next(out) then
return sorted_by_key(out) return outlib.sorted_by_key(out)
end end
end end

View File

@@ -1,6 +1,7 @@
local coroutine = require "coroutine" local coroutine = require "coroutine"
local math = require "math" local math = require "math"
local nmap = require "nmap" local nmap = require "nmap"
local outlib = require "outlib"
local shortport = require "shortport" local shortport = require "shortport"
local sslcert = require "sslcert" local sslcert = require "sslcert"
local stdnse = require "stdnse" local stdnse = require "stdnse"
@@ -1070,26 +1071,6 @@ portrule = function (host, port)
return shortport.ssl(host, port) or sslcert.getPrepareTLSWithoutReconnect(port) return shortport.ssl(host, port) or sslcert.getPrepareTLSWithoutReconnect(port)
end end
--- Return a table that yields elements sorted by key when iterated over with pairs()
-- Should probably put this in a formatting library later.
-- Depends on keys() function defined above.
--@param t The table whose data should be used
--@return out A table that can be passed to pairs() to get sorted results
function sorted_by_key(t)
local out = {}
setmetatable(out, {
__pairs = function(_)
local order = sorted_keys(t)
return coroutine.wrap(function()
for i,k in ipairs(order) do
coroutine.yield(k, t[k])
end
end)
end
})
return out
end
action = function(host, port) action = function(host, port)
if not have_ssl then if not have_ssl then
@@ -1129,5 +1110,5 @@ action = function(host, port)
end end
results["least strength"] = least results["least strength"] = least
return sorted_by_key(results) return outlib.sorted_by_key(results)
end end