local nmap = require('nmap') local stdnse = require('stdnse') description = [[ Scan the given host using NSE's connect() function. ]] --- -- @usage -- nmap -sK --script=connectscan -- -- @output -- Scanned at 2011-04-25 10:33:41 CEST for 1s -- PORT STATE SERVICE REASON -- 21/tcp closed ftp script-set -- 22/tcp closed ssh script-set -- 23/tcp open telnet script-set -- 25/tcp closed smtp script-set -- 53/tcp closed domain script-set -- 80/tcp open http script-set -- 05/31/2014: initial version author = "Jacek Wielemborek" license = "Same as Nmap--See http://nmap.org/book/man-legal.html" categories = {"safe", "discovery"} scanrule = function(host) return true end --- Return the list of TCP ports to scan -- @return ports an array of port objects local function getports(host) local ports = {} local port = nil repeat port = nmap.get_ports(host, port, "tcp", "unknown") if port then table.insert(ports, port) end until not port return ports end action = function(host) local ports = getports(host) for _, port in ipairs(ports) do local sock = nmap.new_socket() sock:set_timeout(500) local constatus, conerr = sock:connect(host, port) sock:close() if (constatus) then nmap.set_port_state(host, port, "open", nmap.reasons.syn_ack) else if (conerr == "ERROR") then nmap.set_port_state(host, port, "closed", nmap.reasons.conn_refused) elseif (conerr == "TIMEOUT") then nmap.set_port_state(host, port, "filtered", nmap.reasons.no_response) else stdnse.print_debug("%s: unknown error when scanning port %d: %s", SCRIPT_NAME, port, conerr) end end end end