diff --git a/nselib/stdnse.lua b/nselib/stdnse.lua index 684e8c1c7..730dd118c 100644 --- a/nselib/stdnse.lua +++ b/nselib/stdnse.lua @@ -1238,4 +1238,19 @@ function filename_escape(s) 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; diff --git a/scripts/ajp-methods.nse b/scripts/ajp-methods.nse index e258c5246..82d1c2df3 100644 --- a/scripts/ajp-methods.nse +++ b/scripts/ajp-methods.nse @@ -41,15 +41,10 @@ portrule = shortport.port_or_service(8009, 'ajp13', 'tcp') local arg_url = stdnse.get_script_args(SCRIPT_NAME .. ".path") or "/" 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 result = {} for _, e in ipairs(t) do - if ( not(contains(filter, e)) ) then + if ( not(stdnse.contains(filter, e)) ) then result[#result + 1] = e end end diff --git a/scripts/dns-nsec3-enum.nse b/scripts/dns-nsec3-enum.nse index 8ab8e6f91..43f4b8821 100644 --- a/scripts/dns-nsec3-enum.nse +++ b/scripts/dns-nsec3-enum.nse @@ -207,16 +207,6 @@ local function get_first(tbl) 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 -- returns the list of new ranges 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 local h1 = string.lower(remove_suffix(nsec3.dname,domain)) 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) stdnse.print_debug("nexthash " .. h1 .. " " .. h2) end diff --git a/scripts/duplicates.nse b/scripts/duplicates.nse index ed7bbdf96..d4b0c12e5 100644 --- a/scripts/duplicates.nse +++ b/scripts/duplicates.nse @@ -61,19 +61,6 @@ dependencies = {"ssl-cert", "ssh-hostkey", "nbstat"} hostrule = 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) -- Handle SSL-certificates @@ -82,7 +69,7 @@ local function processSSLCerts(tab) for host, v in pairs(tab) do for port, sha1 in pairs(v) do 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) end end @@ -111,7 +98,7 @@ local function processSSHKeys(tab) hostkeys[fp] = {} end -- discard duplicate IPs - if not contains(hostkeys[fp], ip) then + if not stdnse.contains(hostkeys[fp], ip) then table.insert(hostkeys[fp], ip) end end @@ -135,12 +122,12 @@ local function processNBStat(tab) local results, mac_table, name_table = {}, {}, {} for host, v in pairs(tab) do 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) end 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) end end @@ -171,7 +158,7 @@ local function processMAC(tab) if ( host.mac_addr ) then mac = stdnse.format_mac(host.mac_addr) 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) end end diff --git a/scripts/hostmap-robtex.nse b/scripts/hostmap-robtex.nse index 16eebf674..61f1f9f69 100644 --- a/scripts/hostmap-robtex.nse +++ b/scripts/hostmap-robtex.nse @@ -39,7 +39,7 @@ function parse_robtex_response (data) local result = {} for domain in string.gmatch(data, " 0 and stdnse.strjoin("\n", output) or nil 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) local result = {} local _, e, f for _, e in ipairs(t) do - if not contains(filter, e) then + if not stdnse.contains(filter, e) then result[#result + 1] = e end end diff --git a/scripts/ms-sql-tables.nse b/scripts/ms-sql-tables.nse index 598b89c08..72f7e91f1 100644 --- a/scripts/ms-sql-tables.nse +++ b/scripts/ms-sql-tables.nse @@ -102,16 +102,6 @@ dependencies = {"ms-sql-brute", "ms-sql-empty-password"} hostrule = mssql.Helper.GetHostrule_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 status, result, dbs, tables @@ -188,7 +178,7 @@ local function process_instance( instance ) end 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 FROM %s..syscolumns sc, %s..sysobjects so, %s..systypes st WHERE so.id = sc.id AND sc.xtype=st.xtype AND diff --git a/scripts/ssh-hostkey.nse b/scripts/ssh-hostkey.nse index 109d0c480..ba4d671ee 100644 --- a/scripts/ssh-hostkey.nse +++ b/scripts/ssh-hostkey.nse @@ -125,19 +125,6 @@ portrule = shortport.port_or_service(22, "ssh") 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 --@param host nmap host table --@param key host key table @@ -188,7 +175,7 @@ local function check_keys(host, keys, f) end end 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]) table.insert(keys_from_file, ("%s %s"):format(parts[2], parts[3])) else @@ -324,19 +311,6 @@ local function portaction(host, port) 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) local function postaction() local hostkeys = {} @@ -357,7 +331,7 @@ local function postaction() } end -- discard duplicate IPs - if not contains(hostkeys[fp], ip) then + if not stdnse.contains(hostkeys[fp], ip) then table.insert(hostkeys[fp], ip) end end