diff --git a/nselib/comm.lua b/nselib/comm.lua index 3ba0fa49e..c0126d9e3 100644 --- a/nselib/comm.lua +++ b/nselib/comm.lua @@ -58,51 +58,51 @@ end -- Makes sure that opts exists and the default proto is there local initopts = function(opts) - if not opts then - opts = {} - end + if not opts then + opts = {} + end - if not opts.proto then - opts.proto = "tcp" - end + if not opts.proto then + opts.proto = "tcp" + end - return opts + return opts end -- Sets up the socket and connects to host:port local setup_connect = function(host, port, opts) - local sock = nmap.new_socket() + local sock = nmap.new_socket() - local connect_timeout, request_timeout = get_timeouts(host, opts) + local connect_timeout, request_timeout = get_timeouts(host, opts) - sock:set_timeout(connect_timeout) + sock:set_timeout(connect_timeout) - local status, err = sock:connect(host, port, opts.proto) + local status, err = sock:connect(host, port, opts.proto) - if not status then - return status, err - end + if not status then + return status, err + end - sock:set_timeout(request_timeout) + sock:set_timeout(request_timeout) - return true, sock + return true, sock end local read = function(sock, opts) - local response, status + local response, status - if opts.lines then - status, response = sock:receive_lines(opts.lines) - return status, response - end - - if opts.bytes then - status, response = sock:receive_bytes(opts.bytes) - return status, response - end - - status, response = sock:receive() + if opts.lines then + status, response = sock:receive_lines(opts.lines) return status, response + end + + if opts.bytes then + status, response = sock:receive_bytes(opts.bytes) + return status, response + end + + status, response = sock:receive() + return status, response end --- This function simply connects to the specified port number on the @@ -117,14 +117,14 @@ end -- @return Status (true or false). -- @return Data (if status is true) or error string (if status is false). get_banner = function(host, port, opts) - opts = initopts(opts) - opts.recv_before = true - local socket, nothing, correct, banner = tryssl(host, port, "", opts) - if socket then - socket:close() - return true, banner - end - return false, banner + opts = initopts(opts) + opts.recv_before = true + local socket, nothing, correct, banner = tryssl(host, port, "", opts) + if socket then + socket:close() + return true, banner + end + return false, banner end --- This function connects to the specified port number on the specified @@ -140,28 +140,28 @@ end -- @return Status (true or false). -- @return Data (if status is true) or error string (if status is false). exchange = function(host, port, data, opts) - opts = initopts(opts) + opts = initopts(opts) - local status, sock = setup_connect(host, port, opts) - local ret + local status, sock = setup_connect(host, port, opts) + local ret - if not status then - -- sock is an error message in this case - return status, sock - end + if not status then + -- sock is an error message in this case + return status, sock + end - status, ret = sock:send(data) - - if not status then - sock:close() - return status, ret - end - - status, ret = read(sock, opts) + status, ret = sock:send(data) + if not status then sock:close() - return status, ret + end + + status, ret = read(sock, opts) + + sock:close() + + return status, ret end --- This function uses shortport.ssl to check if the port is a likely SSL port @@ -181,18 +181,18 @@ end -- @return Best option ("tcp" or "ssl") -- @return Worst option ("tcp" or "ssl") local function bestoption(port) - if type(port) == 'table' then - if port.protocol == "udp" then - stdnse.debug2("DTLS (SSL over UDP) is not supported") - return "udp", "udp" - end - if port.version and port.version.service_tunnel and port.version.service_tunnel == "ssl" then return "ssl","tcp" end - if port.version and port.version.name_confidence and port.version.name_confidence > 6 then return "tcp","ssl" end - if is_ssl(port) then return "ssl","tcp" end - elseif type(port) == 'number' then - if is_ssl({number=port, protocol="tcp", state="open", version={}}) then return "ssl","tcp" end + if type(port) == 'table' then + if port.protocol == "udp" then + stdnse.debug2("DTLS (SSL over UDP) is not supported") + return "udp", "udp" end - return "tcp","ssl" + if port.version and port.version.service_tunnel and port.version.service_tunnel == "ssl" then return "ssl","tcp" end + if port.version and port.version.name_confidence and port.version.name_confidence > 6 then return "tcp","ssl" end + if is_ssl(port) then return "ssl","tcp" end + elseif type(port) == 'number' then + if is_ssl({number=port, protocol="tcp", state="open", version={}}) then return "ssl","tcp" end + end + return "tcp","ssl" end --- This function opens a connection, sends the first data payload and @@ -214,24 +214,24 @@ end -- of the first receive (before sending data) function opencon(host, port, data, opts) local status, sd = setup_connect(host, port, opts) - if not status then - sd:close() - return nil, nil, nil - end + if not status then + sd:close() + return nil, nil, nil + end - local response, early_resp; - if opts and opts.recv_before then status, early_resp = read(sd, opts) end - if data and #data > 0 then - sd:send(data) - status, response = sd:receive() - else - response = early_resp - end - if not status then - sd:close() - return nil, response, early_resp - end - return sd, response, early_resp + local response, early_resp; + if opts and opts.recv_before then status, early_resp = read(sd, opts) end + if data and #data > 0 then + sd:send(data) + status, response = sd:receive() + else + response = early_resp + end + if not status then + sd:close() + return nil, response, early_resp + end + return sd, response, early_resp end --- This function tries to open a connection based on the best @@ -260,21 +260,21 @@ function tryssl(host, port, data, opts) Impossible to test the connection for the correct protocol!" ) end - local opt1, opt2 = bestoption(port) - local best = opt1 - if opts.proto=="udp" then - stdnse.debug2("DTLS (SSL over UDP) is not supported") - end - opts.proto = opt1 - local sd, response, early_resp = opencon(host, port, data, opts) - -- Try the second option (If udp, then both options are the same; skip it) - if not sd and opt1 ~= "udp" then - opts.proto = opt2 - sd, response, early_resp = opencon(host, port, data, opts) - best = opt2 - end - if not sd then best = "none" end - return sd, response, best, early_resp + local opt1, opt2 = bestoption(port) + local best = opt1 + if opts.proto=="udp" then + stdnse.debug2("DTLS (SSL over UDP) is not supported") + end + opts.proto = opt1 + local sd, response, early_resp = opencon(host, port, data, opts) + -- Try the second option (If udp, then both options are the same; skip it) + if not sd and opt1 ~= "udp" then + opts.proto = opt2 + sd, response, early_resp = opencon(host, port, data, opts) + best = opt2 + end + if not sd then best = "none" end + return sd, response, best, early_resp end local unittest = require "unittest"