home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Networking / nmap-5.00-setup.exe / nselib / shortport.lua < prev    next >
Text File  |  2009-07-06  |  4KB  |  113 lines

  1. --- Functions for building short portrules.
  2. --
  3. -- Since portrules are mostly the same for many scripts, this
  4. -- module provides functions for the most common tests.
  5. -- @copyright Same as Nmap--See http://nmap.org/book/man-legal.html
  6.  
  7. module(... or "shortport", package.seeall)
  8.  
  9. ---
  10. -- See if a table contains a value.
  11. -- @param t A table repesenting a set.
  12. -- @param value The value to check for.
  13. -- @return True if <code>t</code> contains <code>value</code>, false otherwise.
  14. local function includes(t, value)
  15.     for _, elem in ipairs(t) do
  16.         if elem == value then
  17.             return true
  18.         end
  19.     end
  20.     return false
  21. end
  22.  
  23. --- Return a portrule that returns true when given an open port matching a
  24. -- single port number or a list of port numbers.
  25. -- @param ports A single port number or a list of port numbers.
  26. -- @param protos The protocol or list of protocols to match against, default
  27. -- <code>"tcp"</code>.
  28. -- @param states A state or list of states to match against, default
  29. -- {<code>"open"</code>, <code>"open|filtered"</code>}.
  30. -- @return Function for the portrule.
  31. -- @usage portrule = shortport.portnumber({80, 443})
  32. portnumber = function(ports, protos, states)
  33.     protos = protos or "tcp"
  34.     states = states or {"open", "open|filtered"}
  35.  
  36.     if type(ports) ~= "table" then
  37.         ports = {ports}
  38.     end
  39.     if type(protos) ~= "table" then
  40.         protos = {protos}
  41.     end
  42.     if type(states) ~= "table" then
  43.         states = {states}
  44.     end
  45.  
  46.     return function(host, port)
  47.         return includes(ports, port.number)
  48.             and includes(protos, port.protocol)
  49.             and includes(states, port.state)
  50.     end
  51. end
  52.  
  53. --- Return a portrule that returns true when given an open port with a
  54. -- service name matching a single service name or a list of service
  55. -- names.
  56. --
  57. -- A service name is something like <code>"http"</code>, <code>"https"</code>,
  58. -- <code>"smtp"</code>, or <code>"ftp"</code>. These service names are
  59. -- determined by Nmap's version scan or (if no version scan information is
  60. -- available) the service assigned to the port in <code>nmap-services</code>
  61. -- (e.g. <code>"http"</code> for TCP port 80). 
  62. -- @param services Service name or a list of names to run against.
  63. -- @param protos The protocol or list of protocols to match against, default
  64. -- <code>"tcp"</code>.
  65. -- @param states A state or list of states to match against, default
  66. -- {<code>"open"</code>, <code>"open|filtered"</code>}.
  67. -- @return Function for the portrule.
  68. -- @usage portrule = shortport.service("ftp")
  69. service = function(services, protos, states)
  70.     protos = protos or "tcp"
  71.     states = states or {"open", "open|filtered"}
  72.  
  73.     if type(services) ~= "table" then
  74.         services = {services}
  75.     end
  76.     if type(protos) ~= "table" then
  77.         protos = {protos}
  78.     end
  79.     if type(states) ~= "table" then
  80.         states = {states}
  81.     end
  82.  
  83.     return function(host, port)
  84.         return includes(services, port.service)
  85.             and includes(protos, port.protocol)
  86.             and includes(states, port.state)
  87.     end
  88. end
  89.  
  90. --- Return a portrule that returns true when given an open port matching
  91. -- either a port number or service name.
  92. --
  93. -- This function is a combination of the <code>portnumber</code> and
  94. -- <code>service</code> functions. The port and service may be single values or
  95. -- a list of values as in those functions. This function exists because many
  96. -- scripts explicitly try to run against the well-known ports, but want also to
  97. -- run against any other port which was discovered to run the named service.
  98. -- @usage portrule = shortport.port_or_service(22,"ssh"). 
  99. -- @param ports A single port number or a list of port numbers.
  100. -- @param services Service name or a list of names to run against.
  101. -- @param protos The protocol or list of protocols to match against, default
  102. -- <code>"tcp"</code>.
  103. -- @param states A state or list of states to match against, default
  104. -- {<code>"open"</code>, <code>"open|filtered"</code>}.
  105. -- @return Function for the portrule.
  106. port_or_service = function(ports, services, protos, states)
  107.     return function(host, port)
  108.         local port_checker = portnumber(ports, protos, states)
  109.         local service_checker = service(services, protos, states)
  110.         return port_checker(host, port) or service_checker(host, port)
  111.     end
  112. end
  113.