1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-09 06:01:28 +00:00

Consolidate "contains" functions into stdnse.contains

These implementations were all functionally identical. The replacement
has an extra feature of returning the index where the value was found,
currently unused.
This commit is contained in:
dmiller
2014-01-16 22:57:33 +00:00
parent 197f28265f
commit 726b259b20
9 changed files with 28 additions and 109 deletions

View File

@@ -1238,4 +1238,19 @@ function filename_escape(s)
end end
end end
--- Check for the presence of a value in a table
--@param tab the table to search into
--@param item the searched value
--@return Boolean true if the item was found, false if not
--@return The index or key where the value was found, or nil
function contains(tab, item)
for k, val in pairs(tab) do
if val == item then
return true, k
end
end
return false, nil
end
return _ENV; return _ENV;

View File

@@ -41,15 +41,10 @@ portrule = shortport.port_or_service(8009, 'ajp13', 'tcp')
local arg_url = stdnse.get_script_args(SCRIPT_NAME .. ".path") or "/" local arg_url = stdnse.get_script_args(SCRIPT_NAME .. ".path") or "/"
local UNINTERESTING_METHODS = { "GET", "HEAD", "POST", "OPTIONS" } local UNINTERESTING_METHODS = { "GET", "HEAD", "POST", "OPTIONS" }
local function contains(t, elem)
for _, e in ipairs(t) do if e == elem then return true end end
return false
end
local function filter_out(t, filter) local function filter_out(t, filter)
local result = {} local result = {}
for _, e in ipairs(t) do for _, e in ipairs(t) do
if ( not(contains(filter, e)) ) then if ( not(stdnse.contains(filter, e)) ) then
result[#result + 1] = e result[#result + 1] = e
end end
end end

View File

@@ -207,16 +207,6 @@ local function get_first(tbl)
end end
end end
-- convenience function , check if table cointains an element
local function table_contains(tbl,element)
for _, value in pairs(tbl) do
if value == element then
return true
end
end
return false
end
-- queries the domain and parses the results -- queries the domain and parses the results
-- returns the list of new ranges -- returns the list of new ranges
local function query_for_hashes(host,subdomain,domain) local function query_for_hashes(host,subdomain,domain)
@@ -228,7 +218,7 @@ local function query_for_hashes(host,subdomain,domain)
for _, nsec3 in ipairs(auth_filter(result, "NSEC3")) do for _, nsec3 in ipairs(auth_filter(result, "NSEC3")) do
local h1 = string.lower(remove_suffix(nsec3.dname,domain)) local h1 = string.lower(remove_suffix(nsec3.dname,domain))
local h2 = string.lower(nsec3.hash.base32) local h2 = string.lower(nsec3.hash.base32)
if not table_contains(all_results,"nexthash " .. h1 .. " " .. h2) then if not stdnse.contains(all_results,"nexthash " .. h1 .. " " .. h2) then
table.insert(all_results, "nexthash " .. h1 .. " " .. h2) table.insert(all_results, "nexthash " .. h1 .. " " .. h2)
stdnse.print_debug("nexthash " .. h1 .. " " .. h2) stdnse.print_debug("nexthash " .. h1 .. " " .. h2)
end end

View File

@@ -61,19 +61,6 @@ dependencies = {"ssl-cert", "ssh-hostkey", "nbstat"}
hostrule = function() return true end hostrule = function() return true end
postrule = function() return true end postrule = function() return true end
--- check for the presence of a value in a table
--@param tab the table to search into
--@param item the searched value
--@return a boolean indicating whether the value has been found or not
local function contains(tab, item)
for _, val in pairs(tab) do
if val == item then
return true
end
end
return false
end
local function processSSLCerts(tab) local function processSSLCerts(tab)
-- Handle SSL-certificates -- Handle SSL-certificates
@@ -82,7 +69,7 @@ local function processSSLCerts(tab)
for host, v in pairs(tab) do for host, v in pairs(tab) do
for port, sha1 in pairs(v) do for port, sha1 in pairs(v) do
ssl_certs[sha1] = ssl_certs[sha1] or {} ssl_certs[sha1] = ssl_certs[sha1] or {}
if ( not contains(ssl_certs[sha1], host.ip) ) then if ( not stdnse.contains(ssl_certs[sha1], host.ip) ) then
table.insert(ssl_certs[sha1], host.ip) table.insert(ssl_certs[sha1], host.ip)
end end
end end
@@ -111,7 +98,7 @@ local function processSSHKeys(tab)
hostkeys[fp] = {} hostkeys[fp] = {}
end end
-- discard duplicate IPs -- discard duplicate IPs
if not contains(hostkeys[fp], ip) then if not stdnse.contains(hostkeys[fp], ip) then
table.insert(hostkeys[fp], ip) table.insert(hostkeys[fp], ip)
end end
end end
@@ -135,12 +122,12 @@ local function processNBStat(tab)
local results, mac_table, name_table = {}, {}, {} local results, mac_table, name_table = {}, {}, {}
for host, v in pairs(tab) do for host, v in pairs(tab) do
mac_table[v.mac] = mac_table[v.mac] or {} mac_table[v.mac] = mac_table[v.mac] or {}
if ( not(contains(mac_table[v.mac], host.ip)) ) then if ( not(stdnse.contains(mac_table[v.mac], host.ip)) ) then
table.insert(mac_table[v.mac], host.ip) table.insert(mac_table[v.mac], host.ip)
end end
name_table[v.server_name] = name_table[v.server_name] or {} name_table[v.server_name] = name_table[v.server_name] or {}
if ( not(contains(name_table[v.server_name], host.ip)) ) then if ( not(stdnse.contains(name_table[v.server_name], host.ip)) ) then
table.insert(name_table[v.server_name], host.ip) table.insert(name_table[v.server_name], host.ip)
end end
end end
@@ -171,7 +158,7 @@ local function processMAC(tab)
if ( host.mac_addr ) then if ( host.mac_addr ) then
mac = stdnse.format_mac(host.mac_addr) mac = stdnse.format_mac(host.mac_addr)
mac_table[mac] = mac_table[mac] or {} mac_table[mac] = mac_table[mac] or {}
if ( not(contains(mac_table[mac], host.ip)) ) then if ( not(stdnse.contains(mac_table[mac], host.ip)) ) then
table.insert(mac_table[mac], host.ip) table.insert(mac_table[mac], host.ip)
end end
end end

View File

@@ -39,7 +39,7 @@ function parse_robtex_response (data)
local result = {} local result = {}
for domain in string.gmatch(data, "<span id=\"dns[0-9]+\"><a href=\"//[a-z]+.robtex.com/([^\"]-)%.html\"") do for domain in string.gmatch(data, "<span id=\"dns[0-9]+\"><a href=\"//[a-z]+.robtex.com/([^\"]-)%.html\"") do
if not table.contains(result, domain) then if not stdnse.contains(result, domain) then
table.insert(result, domain) table.insert(result, domain)
end end
end end
@@ -60,12 +60,3 @@ action = function (host)
end end
return output_tab return output_tab
end end
function table.contains (table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end

View File

@@ -73,19 +73,6 @@ local add_key_to_registry = function(host, port, path, affid)
table.insert(nmap.registry["http-affiliate-id"][site], affid) table.insert(nmap.registry["http-affiliate-id"][site], affid)
end end
--- check for the presence of a value in a table
--@param tab the table to search into
--@param item the searched value
--@return a boolean indicating whether the value has been found or not
local function contains(tab, item)
for _, val in pairs(tab) do
if val == item then
return true
end
end
return false
end
portaction = function(host, port) portaction = function(host, port)
local result = {} local result = {}
local url_path = stdnse.get_script_args("http-affiliate-id.url-path") or "/" local url_path = stdnse.get_script_args("http-affiliate-id.url-path") or "/"
@@ -121,7 +108,7 @@ local function postaction()
siteids[id] = {} siteids[id] = {}
end end
-- discard duplicate IPs -- discard duplicate IPs
if not contains(siteids[id], site) then if not stdnse.contains(siteids[id], site) then
table.insert(siteids[id], site) table.insert(siteids[id], site)
end end
end end

View File

@@ -122,21 +122,11 @@ action = function(host, port)
return #output > 0 and stdnse.strjoin("\n", output) or nil return #output > 0 and stdnse.strjoin("\n", output) or nil
end end
local function contains(t, elem)
local _, e
for _, e in ipairs(t) do
if e == elem then
return true
end
end
return false
end
function filter_out(t, filter) function filter_out(t, filter)
local result = {} local result = {}
local _, e, f local _, e, f
for _, e in ipairs(t) do for _, e in ipairs(t) do
if not contains(filter, e) then if not stdnse.contains(filter, e) then
result[#result + 1] = e result[#result + 1] = e
end end
end end

View File

@@ -102,16 +102,6 @@ dependencies = {"ms-sql-brute", "ms-sql-empty-password"}
hostrule = mssql.Helper.GetHostrule_Standard() hostrule = mssql.Helper.GetHostrule_Standard()
portrule = mssql.Helper.GetPortrule_Standard() portrule = mssql.Helper.GetPortrule_Standard()
local function table_contains( tbl, val )
for k,v in pairs(tbl) do
if ( v == val ) then
return true
end
end
return false
end
local function process_instance( instance ) local function process_instance( instance )
local status, result, dbs, tables local status, result, dbs, tables
@@ -188,7 +178,7 @@ local function process_instance( instance )
end end
for k, v in pairs(dbs.rows) do for k, v in pairs(dbs.rows) do
if ( not( table_contains( done_dbs, v[1] ) ) ) then if ( not( stdnse.contains( done_dbs, v[1] ) ) ) then
local query = [[ SELECT so.name 'table', sc.name 'column', st.name 'type', sc.length local query = [[ SELECT so.name 'table', sc.name 'column', st.name 'type', sc.length
FROM %s..syscolumns sc, %s..sysobjects so, %s..systypes st FROM %s..syscolumns sc, %s..sysobjects so, %s..systypes st
WHERE so.id = sc.id AND sc.xtype=st.xtype AND WHERE so.id = sc.id AND sc.xtype=st.xtype AND

View File

@@ -125,19 +125,6 @@ portrule = shortport.port_or_service(22, "ssh")
postrule = function() return (nmap.registry.sshhostkey ~= nil) end postrule = function() return (nmap.registry.sshhostkey ~= nil) end
--- check for the presence of a value in a table
--@param tab the table to search into
--@param item the searched value
--@return a boolean indicating whether the value has been found or not
local function contains(tab, item)
for _, val in pairs(tab) do
if val == item then
return true
end
end
return false
end
--- put hostkey in the nmap registry for usage by other scripts --- put hostkey in the nmap registry for usage by other scripts
--@param host nmap host table --@param host nmap host table
--@param key host key table --@param key host key table
@@ -188,7 +175,7 @@ local function check_keys(host, keys, f)
end end
end end
else else
if contains(possible_host_names, parts[1]) then if stdnse.contains(possible_host_names, parts[1]) then
stdnse.print_debug(2, "Found an entry that matches: %s", parts[1]) stdnse.print_debug(2, "Found an entry that matches: %s", parts[1])
table.insert(keys_from_file, ("%s %s"):format(parts[2], parts[3])) table.insert(keys_from_file, ("%s %s"):format(parts[2], parts[3]))
else else
@@ -324,19 +311,6 @@ local function portaction(host, port)
end end
end end
--- check for the presence of a value in a table
--@param tab the table to search into
--@param item the searched value
--@return a boolean indicating whether the value has been found or not
local function contains(tab, item)
for _, val in pairs(tab) do
if val == item then
return true
end
end
return false
end
--- iterate over the list of gathered keys and look for duplicate hosts (sharing the same hostkeys) --- iterate over the list of gathered keys and look for duplicate hosts (sharing the same hostkeys)
local function postaction() local function postaction()
local hostkeys = {} local hostkeys = {}
@@ -357,7 +331,7 @@ local function postaction()
} }
end end
-- discard duplicate IPs -- discard duplicate IPs
if not contains(hostkeys[fp], ip) then if not stdnse.contains(hostkeys[fp], ip) then
table.insert(hostkeys[fp], ip) table.insert(hostkeys[fp], ip)
end end
end end