mirror of
https://github.com/nmap/nmap.git
synced 2026-08-28 12:31:12 +00:00
Move caching code to datafiles lib
Scripts no longer need to implement caching of datafiles tables in the
registry, since the datafiles.lua library keeps its own cache in the
registry. A side-effect is that scripts should not change the tables
returned by datafiles.parse_{protocols,rpc,services,mac_prefixes}(), as
doing so will affect all other scripts that use those functions.
This commit is contained in:
parent
959d9a67d3
commit
b868e7f3ce
6 changed files with 61 additions and 74 deletions
|
|
@ -99,16 +99,11 @@ local function matches(addr, pattern)
|
|||
end
|
||||
|
||||
local function get_manuf(mac)
|
||||
if not nmap.registry.mac then
|
||||
local catch = function() return end
|
||||
local try = nmap.new_try(catch)
|
||||
-- Create the table in the registry so we can share between scripts
|
||||
nmap.registry.mac = {}
|
||||
nmap.registry.mac.prefixes = try(datafiles.parse_mac_prefixes())
|
||||
end
|
||||
local catch = function() return "Unknown" end
|
||||
local try = nmap.new_try(catch)
|
||||
local mac_prefixes = try(datafiles.parse_mac_prefixes())
|
||||
local prefix = string.upper(string.format("%02x%02x%02x", mac[1], mac[2], mac[3]))
|
||||
local manuf = nmap.registry.mac.prefixes[prefix] or "Unknown"
|
||||
return manuf
|
||||
return mac_prefixes[prefix] or "Unknown"
|
||||
end
|
||||
|
||||
local function format_mac(mac)
|
||||
|
|
|
|||
|
|
@ -67,18 +67,13 @@ end
|
|||
local function get_mac_addr( mac )
|
||||
local catch = function() return end
|
||||
local try = nmap.new_try(catch)
|
||||
-- Build the MAC prefix lookup table
|
||||
if not nmap.registry.lltd_discovery then
|
||||
-- Create the table in the registry so we can share between script instances
|
||||
nmap.registry.lltd_discovery = {}
|
||||
nmap.registry.lltd_discovery.mac_prefixes = try(datafiles.parse_mac_prefixes())
|
||||
end
|
||||
local mac_prefixes = try(datafiles.parse_mac_prefixes())
|
||||
|
||||
if mac:len() ~= 6 then
|
||||
return "Unknown"
|
||||
else
|
||||
local prefix = string.upper(string.format("%02x%02x%02x", mac:byte(1), mac:byte(2), mac:byte(3)))
|
||||
local manuf = nmap.registry.lltd_discovery.mac_prefixes[prefix] or "Unknown"
|
||||
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 )
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -100,18 +100,13 @@ action = function(host)
|
|||
return stdnse.format_output(false, user_name)
|
||||
end
|
||||
|
||||
-- Build the MAC prefix lookup table
|
||||
if not nmap.registry.nbstat then
|
||||
-- Create the table in the registry so we can share between script instances
|
||||
nmap.registry.nbstat = {}
|
||||
nmap.registry.nbstat.mac_prefixes = try(datafiles.parse_mac_prefixes())
|
||||
end
|
||||
local mac_prefixes = try(datafiles.parse_mac_prefixes())
|
||||
|
||||
-- Format the Mac address in the standard way
|
||||
if(#statistics >= 6) then
|
||||
-- MAC prefixes are matched on the first three bytes, all uppercase
|
||||
prefix = string.upper(string.format("%02x%02x%02x", statistics:byte(1), statistics:byte(2), statistics:byte(3)))
|
||||
manuf = nmap.registry.nbstat.mac_prefixes[prefix]
|
||||
manuf = mac_prefixes[prefix]
|
||||
if manuf == nil then
|
||||
manuf = "unknown"
|
||||
end
|
||||
|
|
|
|||
|
|
@ -184,18 +184,13 @@ end
|
|||
function get_mac_addr( mac )
|
||||
local catch = function() return end
|
||||
local try = nmap.new_try(catch)
|
||||
-- Build the MAC prefix lookup table
|
||||
if not nmap.registry.snmp_interfaces then
|
||||
-- Create the table in the registry so we can share between script instances
|
||||
nmap.registry.snmp_interfaces = {}
|
||||
nmap.registry.snmp_interfaces.mac_prefixes = try(datafiles.parse_mac_prefixes())
|
||||
end
|
||||
local mac_prefixes = try(datafiles.parse_mac_prefixes())
|
||||
|
||||
if mac:len() ~= 6 then
|
||||
return "Unknown"
|
||||
else
|
||||
local prefix = string.upper(string.format("%02x%02x%02x", mac:byte(1), mac:byte(2), mac:byte(3)))
|
||||
local manuf = nmap.registry.snmp_interfaces.mac_prefixes[prefix] or "Unknown"
|
||||
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 )
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -22,7 +22,17 @@ license = "Same as Nmap--See http://nmap.org/book/man-legal.html"
|
|||
categories = { "safe" }
|
||||
|
||||
|
||||
portrule = function() return true end
|
||||
local svc_table
|
||||
|
||||
portrule = function()
|
||||
local status
|
||||
status, svc_table = datafiles.parse_services()
|
||||
if not status then
|
||||
return false --Can't check if we don't have a table!
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
hostrule = function() return true end
|
||||
|
||||
-- the hostrule is only needed to warn
|
||||
|
|
@ -85,7 +95,7 @@ servicechecks = {
|
|||
['ncacn_http'] = function(host, port) return true end,
|
||||
}
|
||||
|
||||
local function checkService(host, port)
|
||||
portaction = function(host, port)
|
||||
local ok = false
|
||||
|
||||
if ( port.version.name_confidence <= 3 ) then
|
||||
|
|
@ -98,9 +108,9 @@ local function checkService(host, port)
|
|||
ok = servicechecks[port.service](host, port)
|
||||
end
|
||||
if ( not(ok) and port.service and
|
||||
( port.service == nmap.registry[SCRIPT_NAME]['services'][port.protocol][port.number] or
|
||||
"unknown" == nmap.registry[SCRIPT_NAME]['services'][port.protocol][port.number] or
|
||||
not(nmap.registry[SCRIPT_NAME]['services'][port.protocol][port.number]) ) ) then
|
||||
( port.service == svc_table[port.protocol][port.number] or
|
||||
"unknown" == svc_table[port.protocol][port.number] or
|
||||
not(svc_table[port.protocol][port.number]) ) ) then
|
||||
ok = true
|
||||
end
|
||||
if ( not(ok) ) then
|
||||
|
|
@ -108,24 +118,6 @@ local function checkService(host, port)
|
|||
end
|
||||
end
|
||||
|
||||
local function loadTables()
|
||||
for _, proto in ipairs({"tcp","udp"}) do
|
||||
if ( not(nmap.registry[SCRIPT_NAME]['services'][proto]) ) then
|
||||
local status, svc_table = datafiles.parse_services(proto)
|
||||
if ( status ) then
|
||||
nmap.registry[SCRIPT_NAME]['services'][proto] = svc_table
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
portaction = function(host, port)
|
||||
nmap.registry[SCRIPT_NAME] = nmap.registry[SCRIPT_NAME] or {}
|
||||
nmap.registry[SCRIPT_NAME]['services'] = nmap.registry[SCRIPT_NAME]['services'] or {}
|
||||
loadTables()
|
||||
return checkService(host, port)
|
||||
end
|
||||
|
||||
local Actions = {
|
||||
hostrule = hostaction,
|
||||
portrule = portaction
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue