mirror of
https://github.com/nmap/nmap.git
synced 2025-12-11 10:19:03 +00:00
Rework shortport.lua to simplify some code and remove leading underscores from
parameter names.
This commit is contained in:
@@ -22,40 +22,31 @@ end
|
|||||||
|
|
||||||
--- Return a portrule that returns true when given an open port matching a
|
--- Return a portrule that returns true when given an open port matching a
|
||||||
-- single port number or a list of port numbers.
|
-- single port number or a list of port numbers.
|
||||||
-- @param port A single port number or a list of port numbers.
|
-- @param ports A single port number or a list of port numbers.
|
||||||
-- @param _proto The protocol or list of protocols to match against, default
|
-- @param protos The protocol or list of protocols to match against, default
|
||||||
-- <code>"tcp"</code>.
|
-- <code>"tcp"</code>.
|
||||||
-- @param _state A state or list of states to match against, default
|
-- @param states A state or list of states to match against, default
|
||||||
-- {<code>"open"</code>, <code>"open|filtered"</code>}.
|
-- {<code>"open"</code>, <code>"open|filtered"</code>}.
|
||||||
-- @return Function for the portrule.
|
-- @return Function for the portrule.
|
||||||
-- @usage portrule = shortport.portnumber({80, 443})
|
-- @usage portrule = shortport.portnumber({80, 443})
|
||||||
portnumber = function(port, _proto, _state)
|
portnumber = function(ports, protos, states)
|
||||||
local port_table, state_table, proto_table
|
protos = protos or "tcp"
|
||||||
local proto = _proto or "tcp"
|
states = states or {"open", "open|filtered"}
|
||||||
local state = _state or {"open", "open|filtered"}
|
|
||||||
|
|
||||||
if(type(port) == "number") then
|
if type(ports) ~= "table" then
|
||||||
port_table = {port}
|
ports = {ports}
|
||||||
elseif(type(port) == "table") then
|
end
|
||||||
port_table = port
|
if type(protos) ~= "table" then
|
||||||
end
|
protos = {protos}
|
||||||
|
end
|
||||||
if(type(state) == "string") then
|
if type(states) ~= "table" then
|
||||||
state_table = {state}
|
states = {states}
|
||||||
elseif(type(state) == "table") then
|
end
|
||||||
state_table = state
|
|
||||||
end
|
|
||||||
|
|
||||||
if(type(proto) == "string") then
|
|
||||||
proto_table = {proto}
|
|
||||||
elseif(type(proto) == "table") then
|
|
||||||
proto_table = proto
|
|
||||||
end
|
|
||||||
|
|
||||||
return function(host, port)
|
return function(host, port)
|
||||||
return includes(state_table, port.state)
|
return includes(ports, port.number)
|
||||||
and includes(port_table, port.number)
|
and includes(protos, port.protocol)
|
||||||
and includes(proto_table, port.protocol)
|
and includes(states, port.state)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -68,40 +59,31 @@ end
|
|||||||
-- determined by Nmap's version scan or (if no version scan information is
|
-- determined by Nmap's version scan or (if no version scan information is
|
||||||
-- available) the service assigned to the port in <code>nmap-services</code>
|
-- available) the service assigned to the port in <code>nmap-services</code>
|
||||||
-- (e.g. <code>"http"</code> for TCP port 80).
|
-- (e.g. <code>"http"</code> for TCP port 80).
|
||||||
-- @param service Service name or a list of names to run against.
|
-- @param services Service name or a list of names to run against.
|
||||||
-- @param _proto The protocol or list of protocols to match against, default
|
-- @param protos The protocol or list of protocols to match against, default
|
||||||
-- <code>"tcp"</code>.
|
-- <code>"tcp"</code>.
|
||||||
-- @param _state A state or list of states to match against, default
|
-- @param states A state or list of states to match against, default
|
||||||
-- {<code>"open"</code>, <code>"open|filtered"</code>}.
|
-- {<code>"open"</code>, <code>"open|filtered"</code>}.
|
||||||
-- @return Function for the portrule.
|
-- @return Function for the portrule.
|
||||||
-- @usage portrule = shortport.service("ftp")
|
-- @usage portrule = shortport.service("ftp")
|
||||||
service = function(service, _proto, _state)
|
service = function(services, protos, states)
|
||||||
local service_table, state_table, proto_table
|
protos = protos or "tcp"
|
||||||
local state = _state or {"open", "open|filtered"}
|
states = states or {"open", "open|filtered"}
|
||||||
local proto = _proto or "tcp"
|
|
||||||
|
|
||||||
if(type(service) == "string") then
|
if type(services) ~= "table" then
|
||||||
service_table = {service}
|
services = {services}
|
||||||
elseif(type(service) == "table") then
|
end
|
||||||
service_table = service
|
if type(protos) ~= "table" then
|
||||||
end
|
protos = {protos}
|
||||||
|
end
|
||||||
if(type(state) == "string") then
|
if type(states) ~= "table" then
|
||||||
state_table = {state}
|
states = {states}
|
||||||
elseif(type(state) == "table") then
|
end
|
||||||
state_table = state
|
|
||||||
end
|
|
||||||
|
|
||||||
if(type(proto) == "string") then
|
|
||||||
proto_table = {proto}
|
|
||||||
elseif(type(proto) == "table") then
|
|
||||||
proto_table = proto
|
|
||||||
end
|
|
||||||
|
|
||||||
return function(host, port)
|
return function(host, port)
|
||||||
return includes(state_table, port.state)
|
return includes(services, port.service)
|
||||||
and includes(service_table, port.service)
|
and includes(protos, port.protocol)
|
||||||
and includes(proto_table, port.protocol)
|
and includes(states, port.state)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -114,26 +96,17 @@ end
|
|||||||
-- scripts explicitly try to run against the well-known ports, but want also to
|
-- scripts explicitly try to run against the well-known ports, but want also to
|
||||||
-- run against any other port which was discovered to run the named service.
|
-- run against any other port which was discovered to run the named service.
|
||||||
-- @usage portrule = shortport.port_or_service(22,"ssh").
|
-- @usage portrule = shortport.port_or_service(22,"ssh").
|
||||||
-- @param _port A single port number or a list of port numbers.
|
-- @param ports A single port number or a list of port numbers.
|
||||||
-- @param _service Service name or a list of names to run against.
|
-- @param services Service name or a list of names to run against.
|
||||||
-- @param proto The protocol or list of protocols to match against, default
|
-- @param protos The protocol or list of protocols to match against, default
|
||||||
-- <code>"tcp"</code>.
|
-- <code>"tcp"</code>.
|
||||||
-- @param _state A state or list of states to match against, default
|
-- @param states A state or list of states to match against, default
|
||||||
-- {<code>"open"</code>, <code>"open|filtered"</code>}.
|
-- {<code>"open"</code>, <code>"open|filtered"</code>}.
|
||||||
-- @return Function for the portrule.
|
-- @return Function for the portrule.
|
||||||
port_or_service = function(_port, _service, proto, _state)
|
port_or_service = function(ports, services, protos, states)
|
||||||
local state = _state or {"open", "open|filtered"}
|
|
||||||
local state_table
|
|
||||||
|
|
||||||
if(type(state) == "string") then
|
|
||||||
state_table = {state}
|
|
||||||
elseif(type(state) == "table") then
|
|
||||||
state_table = state
|
|
||||||
end
|
|
||||||
|
|
||||||
return function(host, port)
|
return function(host, port)
|
||||||
local port_checker = portnumber(_port, proto, state_table)
|
local port_checker = portnumber(ports, protos, states)
|
||||||
local service_checker = service(_service, proto, state_table)
|
local service_checker = service(services, protos, states)
|
||||||
return port_checker(host, port) or service_checker(host, port)
|
return port_checker(host, port) or service_checker(host, port)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user