From a285f82299e8b46b5c680f0b9fc98d503099cacf Mon Sep 17 00:00:00 2001 From: kris Date: Mon, 28 Jul 2008 01:47:26 +0000 Subject: [PATCH] o The NSE Comm library now defaults to trying to read as many bytes as are available rather than lines if neither the "bytes" nor "lines" options are given. Thanks to Brandon for reporting a problem which he noticed in the dns-test-open-recursion script. [Kris] --- CHANGELOG | 5 +++++ docs/scripting.xml | 5 +++-- nselib/comm.lua | 39 ++++++++++++------------------------- scripts/HTTP_open_proxy.nse | 2 +- scripts/HTTPtrace.nse | 2 +- scripts/PPTPversion.nse | 2 +- scripts/iax2Detect.nse | 2 +- scripts/nbstat.nse | 2 +- 8 files changed, 25 insertions(+), 34 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 3e7540864..86facdd1f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -65,6 +65,11 @@ o Nmap's Nsock-utilizing subsystems (DNS, NSE, version detection) have been o Added --ip-options support for the connect() scan (-sT). [Kris] +o The NSE Comm library now defaults to trying to read as many bytes as are + available rather than lines if neither the "bytes" nor "lines" options are + given. Thanks to Brandon for reporting a problem which he noticed in the + dns-test-open-recursion script. [Kris] + o Changed the order preference of timing ping propes. [Michael] o Enabled nmap to switch between multiple types of timing pings during port diff --git a/docs/scripting.xml b/docs/scripting.xml index 3a41d0047..dac86f4ba 100644 --- a/docs/scripting.xml +++ b/docs/scripting.xml @@ -1651,8 +1651,9 @@ if(s) code_to_be_done_on_match end The relevant indexes for this table are bytes, lines, proto and timeout. bytes is used to provide the minimum number of bytes required for a read. lines - does the same, but for the minimum number of lines. proto is used - to set the protocol to communicate with, defaulting to "tcp" if not provided. + does the same, but for the minimum number of lines. If neither are provided, these + functions attempt to read as many bytes as are available. proto + is used to set the protocol to communicate with, defaulting to "tcp" if not provided. timeout is used to set the socket timeout (see the socket function set_timeout() for details). diff --git a/nselib/comm.lua b/nselib/comm.lua index 48ad74cfb..4f6cf69c5 100644 --- a/nselib/comm.lua +++ b/nselib/comm.lua @@ -27,10 +27,10 @@ module(..., package.seeall) -- proto: Specifies the protocol to be used with the connect() call -- timeout: Sets the socket's timeout with nmap.set_timeout() -- --- If neither lines nor bytes are specified, the calls read as many lines --- as possible. If only bytes if specified, then it only tries to read that --- many bytes. Likewise, it only lines if specified, then it only tries to --- read that many lines. If they're both specified, the lines value is used. +-- If neither lines nor bytes are specified, the calls attempt to read as many +-- bytes as possible. If only bytes is specified, then it only tries to read +-- that many bytes. Likewise, it only lines if specified, then it only tries +-- to read that many lines. If they're both specified, the lines value is used. -- ------ @@ -71,39 +71,24 @@ local setup_connect = function(host, port, opts) return status, err end + -- If nothing is given, specify bytes=1 so NSE reads everything + if not opts.lines and not opts.bytes then + opts.bytes = 1 + end + return true, sock end local read = function(sock, opts) - local line, response, status + local response, status if opts.lines then status, response = sock:receive_lines(opts.lines) return status, response - elseif opts.bytes then - status, response = sock:receive_bytes(opts.bytes) - return status, response end - response = "" - - while true do - status, line = sock:receive_lines(1) - - if not status then - break - end - - response = response .. line - end - - -- Either we reached the end of the stream, or we got all we could - -- within the socket timeout - if line == "EOF" or (line == "TIMEOUT" and response ~= "") then - return true, response - end - - return false, line + status, response = sock:receive_bytes(opts.bytes) + return status, response end get_banner = function(host, port, opts) diff --git a/scripts/HTTP_open_proxy.nse b/scripts/HTTP_open_proxy.nse index 75e81134b..a7ed1fa0e 100644 --- a/scripts/HTTP_open_proxy.nse +++ b/scripts/HTTP_open_proxy.nse @@ -47,7 +47,7 @@ action = function(host, port) -- Ask proxy to open www.google.com local req = "GET http://www.google.com HTTP/1.0\r\nHost: www.google.com\r\n\r\n" - local status, result = comm.exchange(host, port, req, {proto=port.protocol, timeout=10000}) + local status, result = comm.exchange(host, port, req, {lines=1,proto=port.protocol, timeout=10000}) if not status then return diff --git a/scripts/HTTPtrace.nse b/scripts/HTTPtrace.nse index 9f35972a5..87802d4ed 100644 --- a/scripts/HTTPtrace.nse +++ b/scripts/HTTPtrace.nse @@ -79,7 +79,7 @@ portrule = shortport.port_or_service({80, 8080}, "http") action = function(host, port) local cmd = "TRACE / HTTP/1.0\r\n\r\n" - local status, response = comm.exchange(host, port, cmd, {timeout=5000}) + local status, response = comm.exchange(host, port, cmd, {lines=1,timeout=5000}) if not status then return diff --git a/scripts/PPTPversion.nse b/scripts/PPTPversion.nse index ade1fc806..950dd659d 100644 --- a/scripts/PPTPversion.nse +++ b/scripts/PPTPversion.nse @@ -53,7 +53,7 @@ action = function(host, port) payload = payload .. "\000\000\000\000" -- padding for vendor name local try = nmap.new_try() - local response = try(comm.exchange(host, port, payload, {bytes=1, timeout=5000})) + local response = try(comm.exchange(host, port, payload, {timeout=5000})) local result diff --git a/scripts/iax2Detect.nse b/scripts/iax2Detect.nse index 28026e1ed..4b53e7c3b 100644 --- a/scripts/iax2Detect.nse +++ b/scripts/iax2Detect.nse @@ -20,7 +20,7 @@ action = function(host, port) poke = poke .. string.char(0x00, 0x00, 0x00, 0x00) poke = poke .. string.char(0x00, 0x00, 0x06, 0x1e) - local status, recv = comm.exchange(host, port, poke, {bytes=1,proto=port.protocol,timeout=10000}) + local status, recv = comm.exchange(host, port, poke, {proto=port.protocol,timeout=10000}) if not status then return diff --git a/scripts/nbstat.nse b/scripts/nbstat.nse index e34cb3782..5f863382a 100644 --- a/scripts/nbstat.nse +++ b/scripts/nbstat.nse @@ -65,7 +65,7 @@ action = function(host) "\065\065\065\065\065\000\000\033" .. "\000\001" - local status, result = comm.exchange(host, 137, data, {bytes=1, proto="udp", timeout=5000}) + local status, result = comm.exchange(host, 137, data, {proto="udp", timeout=5000}) if (not status) then return