/nselib/http.lua
Lua | 2886 lines | 2566 code | 58 blank | 262 comment | 97 complexity | 470a24c1ec40b61a76841dcb544e9963 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, Apache-2.0, LGPL-2.0, LGPL-2.1, MIT
Large files files are truncated, but you can click here to view the full file
- ---Implements the HTTP client protocol in a standard form that Nmap scripts can
- -- take advantage of.
- --
- -- Because HTTP has so many uses, there are a number of interfaces to this library.
- -- The most obvious and common ones are simply <code>get</code>, <code>post</code>,
- -- and <code>head</code>; or, if more control is required, <code>generic_request</code>
- -- can be used. These functions do what one would expect. The <code>get_url</code>
- -- helper function can be used to parse and retrieve a full URL.
- --
- -- HTTPS support is transparent. The library uses <code>comm.tryssl</code> to
- -- determine whether SSL is required for a request.
- --
- -- These functions return a table of values, including:
- -- * <code>status-line</code> - A string representing the status, such as "HTTP/1.1 200 OK". In case of an error, a description will be provided in this line.
- -- * <code>status</code>: The HTTP status value; for example, "200". If an error occurs during a request, then this value is going to be nil.
- -- * <code>header</code> - An associative array representing the header. Keys are all lowercase, and standard headers, such as 'date', 'content-length', etc. will typically be present.
- -- * <code>rawheader</code> - A numbered array of the headers, exactly as the server sent them. While header['content-type'] might be 'text/html', rawheader[3] might be 'Content-type: text/html'.
- -- * <code>cookies</code> - A numbered array of the cookies the server sent. Each cookie is a table with the following keys: <code>name</code>, <code>value</code>, <code>path</code>, <code>domain</code>, and <code>expires</code>.
- -- * <code>body</code> - The full body, as returned by the server.
- --
- -- If a script is planning on making a lot of requests, the pipelining functions can
- -- be helpful. <code>pipeline_add</code> queues requests in a table, and
- -- <code>pipeline</code> performs the requests, returning the results as an array,
- -- with the responses in the same order as the queries were added. As a simple example:
- --<code>
- -- -- Start by defining the 'all' variable as nil
- -- local all = nil
- --
- -- -- Add two 'GET' requests and one 'HEAD' to the queue. These requests are not performed
- -- -- yet. The second parameter represents the 'options' table, which we don't need.
- -- all = http.pipeline_add('/book', nil, all)
- -- all = http.pipeline_add('/test', nil, all)
- -- all = http.pipeline_add('/monkeys', nil, all, 'HEAD')
- --
- -- -- Perform all three requests as parallel as Nmap is able to
- -- local results = http.pipeline('nmap.org', 80, all)
- --</code>
- --
- -- At this point, <code>results</code> is an array with three elements. Each element
- -- is a table containing the HTTP result, as discussed above.
- --
- -- One more interface provided by the HTTP library helps scripts determine whether or not
- -- a page exists. The <code>identify_404</code> function will try several URLs on the
- -- server to determine what the server's 404 pages look like. It will attempt to identify
- -- customized 404 pages that may not return the actual status code 404. If successful,
- -- the function <code>page_exists</code> can then be used to determine whether or not
- -- a page existed.
- --
- -- Some other miscellaneous functions that can come in handy are <code>response_contains</code>,
- -- <code>can_use_head</code>, and <code>save_path</code>. See the appropriate documentation
- -- for them.
- --
- -- The response to each function is typically a table with the following keys:
- -- * <code>status-line</code>: The HTTP status line; for example, "HTTP/1.1 200 OK" (note: this is followed by a newline). In case of an error, a description will be provided in this line.
- -- * <code>status</code>: The HTTP status value; for example, "200". If an error occurs during a request, then this value is going to be nil.
- -- * <code>header</code>: A table of header values, where the keys are lowercase and the values are exactly what the server sent
- -- * <code>rawheader</code>: A list of header values as "name: value" strings, in the exact format and order that the server sent them
- -- * <code>cookies</code>: A list of cookies that the server is sending. Each cookie is a table containing the keys <code>name</code>, <code>value</code>, and <code>path</code>. This table can be sent to the server in subsequent responses in the <code>options</code> table to any function (see below).
- -- * <code>body</code>: The body of the response
- -- * <code>location</code>: a list of the locations of redirects that were followed.
- --
- -- Many of the functions optionally allow an 'options' table. This table can alter the HTTP headers
- -- or other values like the timeout. The following are valid values in 'options' (note: not all
- -- options will necessarily affect every function):
- -- * <code>timeout</code>: A timeout used for socket operations.
- -- * <code>header</code>: A table containing additional headers to be used for the request. For example, <code>options['header']['Content-Type'] = 'text/xml'</code>
- -- * <code>content</code>: The content of the message (content-length will be added -- set header['Content-Length'] to override). This can be either a string, which will be directly added as the body of the message, or a table, which will have each key=value pair added (like a normal POST request).
- -- * <code>cookies</code>: A list of cookies as either a string, which will be directly sent, or a table. If it's a table, the following fields are recognized: <code>name</code>, <code>value</code>, <code>path</code>, <code>expires</code>. Only <code>name</code> and <code>value</code> fields are required.
- -- * <code>auth</code>: A table containing the keys <code>username</code> and <code>password</code>, which will be used for HTTP Basic authentication.
- -- If a server requires HTTP Digest authentication, then there must also be a key <code>digest</code>, with value <code>true</code>.
- -- If a server requires NTLM authentication, then there must also be a key <code>ntlm</code>, with value <code>true</code>.
- -- * <code>bypass_cache</code>: Do not perform a lookup in the local HTTP cache.
- -- * <code>no_cache</code>: Do not save the result of this request to the local HTTP cache.
- -- * <code>no_cache_body</code>: Do not save the body of the response to the local HTTP cache.
- -- * <code>redirect_ok</code>: Closure that overrides the default redirect_ok used to validate whether to follow HTTP redirects or not. False, if no HTTP redirects should be followed. Alternatively, a number may be passed to change the number of redirects to follow.
- -- The following example shows how to write a custom closure that follows 5 consecutive redirects, without the safety checks in the default redirect_ok:
- -- <code>
- -- redirect_ok = function(host,port)
- -- local c = 5
- -- return function(url)
- -- if ( c==0 ) then return false end
- -- c = c - 1
- -- return true
- -- end
- -- end
- -- </code>
- --
- -- @args http.max-cache-size The maximum memory size (in bytes) of the cache.
- --
- -- @args http.useragent The value of the User-Agent header field sent with
- -- requests. By default it is
- -- <code>"Mozilla/5.0 (compatible; Nmap Scripting Engine; http://nmap.org/book/nse.html)"</code>.
- -- A value of the empty string disables sending the User-Agent header field.
- --
- -- @args http.pipeline If set, it represents the number of HTTP requests that'll be
- -- sent on one connection. This can be set low to make debugging easier, or it
- -- can be set high to test how a server reacts (its chosen max is ignored).
- -- @args http.max-pipeline If set, it represents the number of outstanding HTTP requests
- -- that should be pipelined. Defaults to <code>http.pipeline</code> (if set), or to what
- -- <code>getPipelineMax</code> function returns.
- --
- -- TODO
- -- Implement cache system for http pipelines
- --
- local base64 = require "base64"
- local bin = require "bin"
- local bit = require "bit"
- local comm = require "comm"
- local coroutine = require "coroutine"
- local nmap = require "nmap"
- local os = require "os"
- local sasl = require "sasl"
- local stdnse = require "stdnse"
- local string = require "string"
- local table = require "table"
- local url = require "url"
- local smbauth = require "smbauth"
- local unicode = require "unicode"
- _ENV = stdnse.module("http", stdnse.seeall)
- ---Use ssl if we have it
- local have_ssl, openssl = pcall(require,'openssl')
- USER_AGENT = stdnse.get_script_args('http.useragent') or "Mozilla/5.0 (compatible; Nmap Scripting Engine; http://nmap.org/book/nse.html)"
- local MAX_REDIRECT_COUNT = 5
- -- Recursively copy a table.
- -- Only recurs when a value is a table, other values are copied by assignment.
- local function tcopy (t)
- local tc = {};
- for k,v in pairs(t) do
- if type(v) == "table" then
- tc[k] = tcopy(v);
- else
- tc[k] = v;
- end
- end
- return tc;
- end
- --- Recursively copy into a table any elements from another table whose key it
- -- doesn't have.
- local function table_augment(to, from)
- for k, v in pairs(from) do
- if type( to[k] ) == 'table' then
- table_augment(to[k], from[k])
- else
- to[k] = from[k]
- end
- end
- end
- --- Get a value suitable for the Host header field.
- -- See RFC 2616 sections 14.23 and 5.2.
- local function get_host_field(host, port)
- return stdnse.get_hostname(host)
- end
- -- Skip *( SP | HT ) starting at offset. See RFC 2616, section 2.2.
- -- @return the first index following the spaces.
- -- @return the spaces skipped over.
- local function skip_space(s, offset)
- local _, i, space = s:find("^([ \t]*)", offset)
- return i + 1, space
- end
- -- Get a token starting at offset. See RFC 2616, section 2.2.
- -- @return the first index following the token, or nil if no token was found.
- -- @return the token.
- local function get_token(s, offset)
- -- All characters except CTL and separators.
- local _, i, token = s:find("^([^()<>@,;:\\\"/%[%]?={} \0\001-\031\127]+)", offset)
- if i then
- return i + 1, token
- else
- return nil
- end
- end
- -- Get a quoted-string starting at offset. See RFC 2616, section 2.2. crlf is
- -- used as the definition for CRLF in the case of LWS within the string.
- -- @return the first index following the quoted-string, or nil if no
- -- quoted-string was found.
- -- @return the contents of the quoted-string, without quotes or backslash
- -- escapes.
- local function get_quoted_string(s, offset, crlf)
- local result = {}
- local i = offset
- assert(s:sub(i, i) == "\"")
- i = i + 1
- while i <= s:len() do
- local c = s:sub(i, i)
- if c == "\"" then
- -- Found the closing quote, done.
- return i + 1, table.concat(result)
- elseif c == "\\" then
- -- This is a quoted-pair ("\" CHAR).
- i = i + 1
- c = s:sub(i, i)
- if c == "" then
- -- No character following.
- error(string.format("\\ escape at end of input while parsing quoted-string."))
- end
- -- Only CHAR may follow a backslash.
- if c:byte(1) > 127 then
- error(string.format("Unexpected character with value > 127 (0x%02X) in quoted-string.", c:byte(1)))
- end
- else
- -- This is qdtext, which is TEXT except for '"'.
- -- TEXT is "any OCTET except CTLs, but including LWS," however "a CRLF is
- -- allowed in the definition of TEXT only as part of a header field
- -- continuation." So there are really two definitions of quoted-string,
- -- depending on whether it's in a header field or not. This function does
- -- not allow CRLF.
- c = s:sub(i, i)
- if c ~= "\t" and c:match("^[\0\001-\031\127]$") then
- error(string.format("Unexpected control character in quoted-string: 0x%02X.", c:byte(1)))
- end
- end
- result[#result + 1] = c
- i = i + 1
- end
- return nil
- end
- -- Get a ( token | quoted-string ) starting at offset.
- -- @return the first index following the token or quoted-string, or nil if
- -- nothing was found.
- -- @return the token or quoted-string.
- local function get_token_or_quoted_string(s, offset, crlf)
- if s:sub(offset, offset) == "\"" then
- return get_quoted_string(s, offset)
- else
- return get_token(s, offset)
- end
- end
- -- Returns the index just past the end of LWS.
- local function skip_lws(s, pos)
- local _, e
- while true do
- while string.match(s, "^[ \t]", pos) do
- pos = pos + 1
- end
- _, e = string.find(s, "^\r?\n[ \t]", pos)
- if not e then
- return pos
- end
- pos = e + 1
- end
- end
- ---Validate an 'options' table, which is passed to a number of the HTTP functions. It is
- -- often difficult to track down a mistake in the options table, and requires fiddling
- -- with the http.lua source, but this should make that a lot easier.
- local function validate_options(options)
- local bad = false
- if(options == nil) then
- return true
- end
- for key, value in pairs(options) do
- if(key == 'timeout') then
- if(type(tonumber(value)) ~= 'number') then
- stdnse.debug1('http: options.timeout contains a non-numeric value')
- bad = true
- end
- elseif(key == 'header') then
- if(type(value) ~= 'table') then
- stdnse.debug1("http: options.header should be a table")
- bad = true
- end
- elseif(key == 'content') then
- if(type(value) ~= 'string' and type(value) ~= 'table') then
- stdnse.debug1("http: options.content should be a string or a table")
- bad = true
- end
- elseif(key == 'cookies') then
- if(type(value) == 'table') then
- for _, cookie in ipairs(value) do
- for cookie_key, cookie_value in pairs(cookie) do
- if(cookie_key == 'name') then
- if(type(cookie_value) ~= 'string') then
- stdnse.debug1("http: options.cookies[i].name should be a string")
- bad = true
- end
- elseif(cookie_key == 'value') then
- if(type(cookie_value) ~= 'string') then
- stdnse.debug1("http: options.cookies[i].value should be a string")
- bad = true
- end
- elseif(cookie_key == 'path') then
- if(type(cookie_value) ~= 'string') then
- stdnse.debug1("http: options.cookies[i].path should be a string")
- bad = true
- end
- elseif(cookie_key == 'expires') then
- if(type(cookie_value) ~= 'string') then
- stdnse.debug1("http: options.cookies[i].expires should be a string")
- bad = true
- end
- else
- stdnse.debug1("http: Unknown field in cookie table: %s", cookie_key)
- bad = true
- end
- end
- end
- elseif(type(value) ~= 'string') then
- stdnse.debug1("http: options.cookies should be a table or a string")
- bad = true
- end
- elseif(key == 'auth') then
- if(type(value) == 'table') then
- if(value['username'] == nil or value['password'] == nil) then
- stdnse.debug1("http: options.auth should contain both a 'username' and a 'password' key")
- bad = true
- end
- else
- stdnse.debug1("http: options.auth should be a table")
- bad = true
- end
- elseif (key == 'digestauth') then
- if(type(value) == 'table') then
- local req_keys = {"username","realm","nonce","digest-uri","response"}
- for _,k in ipairs(req_keys) do
- if not value[k] then
- stdnse.debug1("http: options.digestauth missing key: %s",k)
- bad = true
- break
- end
- end
- else
- bad = true
- stdnse.debug1("http: options.digestauth should be a table")
- end
- elseif (key == 'ntlmauth') then
- stdnse.debug1("Proceeding with ntlm message")
- elseif(key == 'bypass_cache' or key == 'no_cache' or key == 'no_cache_body') then
- if(type(value) ~= 'boolean') then
- stdnse.debug1("http: options.bypass_cache, options.no_cache, and options.no_cache_body must be boolean values")
- bad = true
- end
- elseif(key == 'redirect_ok') then
- if(type(value)~= 'function' and type(value)~='boolean' and type(value) ~= 'number') then
- stdnse.debug1("http: options.redirect_ok must be a function or boolean or number")
- bad = true
- end
- else
- stdnse.debug1("http: Unknown key in the options table: %s", key)
- end
- end
- return not(bad)
- end
- -- The following recv functions, and the function <code>next_response</code>
- -- follow a common pattern. They each take a <code>partial</code> argument
- -- whose value is data that has been read from the socket but not yet used in
- -- parsing, and they return as their second return value a new value for
- -- <code>partial</code>. The idea is that, for example, in reading from the
- -- socket to get the Status-Line, you will probably read too much and read part
- -- of the header. That part (the "partial") has to be retained when you go to
- -- parse the header. The common use pattern is this:
- -- <code>
- -- local partial
- -- status_line, partial = recv_line(socket, partial)
- -- ...
- -- header, partial = recv_header(socket, partial)
- -- ...
- -- </code>
- -- On error, the functions return <code>nil</code> and the second return value
- -- is an error message.
- -- Receive a single line (up to <code>\n</code>).
- local function recv_line(s, partial)
- local _, e
- local status, data
- local pos
- partial = partial or ""
- pos = 1
- while true do
- _, e = string.find(partial, "\n", pos, true)
- if e then
- break
- end
- status, data = s:receive()
- if not status then
- return status, data
- end
- pos = #partial
- partial = partial .. data
- end
- return string.sub(partial, 1, e), string.sub(partial, e + 1)
- end
- local function line_is_empty(line)
- return line == "\r\n" or line == "\n"
- end
- -- Receive up to and including the first blank line, but return everything up
- -- to and not including the final blank line.
- local function recv_header(s, partial)
- local lines = {}
- partial = partial or ""
- while true do
- local line
- line, partial = recv_line(s, partial)
- if not line then
- return line, partial
- end
- if line_is_empty(line) then
- break
- end
- lines[#lines + 1] = line
- end
- return table.concat(lines), partial
- end
- -- Receive until the connection is closed.
- local function recv_all(s, partial)
- local parts
- partial = partial or ""
- parts = {partial}
- while true do
- local status, part = s:receive()
- if not status then
- break
- else
- parts[#parts + 1] = part
- end
- end
- return table.concat(parts), ""
- end
- -- Receive exactly <code>length</code> bytes. Returns <code>nil</code> if that
- -- many aren't available.
- local function recv_length(s, length, partial)
- local parts, last
- partial = partial or ""
- parts = {}
- last = partial
- length = length - #last
- while length > 0 do
- local status
- parts[#parts + 1] = last
- status, last = s:receive()
- if not status then
- return nil
- end
- length = length - #last
- end
- -- At this point length is 0 or negative, and indicates the degree to which
- -- the last read "overshot" the desired length.
- if length == 0 then
- return table.concat(parts) .. last, ""
- else
- return table.concat(parts) .. string.sub(last, 1, length - 1), string.sub(last, length)
- end
- end
- -- Receive until the end of a chunked message body, and return the dechunked
- -- body.
- local function recv_chunked(s, partial)
- local chunks, chunk
- local chunk_size
- local pos
- chunks = {}
- repeat
- local line, hex, _, i
- line, partial = recv_line(s, partial)
- if not line then
- return nil, partial
- end
- pos = 1
- pos = skip_space(line, pos)
- -- Get the chunk-size.
- _, i, hex = string.find(line, "^([%x]+)", pos)
- if not i then
- return nil, string.format("Chunked encoding didn't find hex; got %q.", string.sub(line, pos, pos + 10))
- end
- pos = i + 1
- chunk_size = tonumber(hex, 16)
- if not chunk_size or chunk_size < 0 then
- return nil, string.format("Chunk size %s is not a positive integer.", hex)
- end
- -- Ignore chunk-extensions that may follow here.
- -- RFC 2616, section 2.1 ("Implied *LWS") seems to allow *LWS between the
- -- parts of a chunk-extension, but that is ambiguous. Consider this case:
- -- "1234;a\r\n =1\r\n...". It could be an extension with a chunk-ext-name
- -- of "a" (and no value), and a chunk-data beginning with " =", or it could
- -- be a chunk-ext-name of "a" with a value of "1", and a chunk-data
- -- starting with "...". We don't allow *LWS here, only ( SP | HT ), so the
- -- first interpretation will prevail.
- chunk, partial = recv_length(s, chunk_size, partial)
- if not chunk then
- return nil, partial
- end
- chunks[#chunks + 1] = chunk
- line, partial = recv_line(s, partial)
- if not line then
- -- this warning message was initially an error but was adapted
- -- to support broken servers, such as the Citrix XML Service
- stdnse.debug2("Didn't find CRLF after chunk-data.")
- elseif not string.match(line, "^\r?\n") then
- return nil, string.format("Didn't find CRLF after chunk-data; got %q.", line)
- end
- until chunk_size == 0
- return table.concat(chunks), partial
- end
- -- Receive a message body, assuming that the header has already been read by
- -- <code>recv_header</code>. The handling is sensitive to the request method
- -- and the status code of the response.
- local function recv_body(s, response, method, partial)
- local connection_close, connection_keepalive
- local version_major, version_minor
- local transfer_encoding
- local content_length
- local err
- partial = partial or ""
- -- First check for Connection: close and Connection: keep-alive. This is
- -- necessary to handle some servers that don't follow the protocol.
- connection_close = false
- connection_keepalive = false
- if response.header.connection then
- local offset, token
- offset = 0
- while true do
- offset, token = get_token(response.header.connection, offset + 1)
- if not offset then
- break
- end
- if string.lower(token) == "close" then
- connection_close = true
- elseif string.lower(token) == "keep-alive" then
- connection_keepalive = true
- end
- end
- end
- -- The HTTP version may also affect our decisions.
- version_major, version_minor = string.match(response["status-line"], "^HTTP/(%d+)%.(%d+)")
- -- See RFC 2616, section 4.4 "Message Length".
- -- 1. Any response message which "MUST NOT" include a message-body (such as
- -- the 1xx, 204, and 304 responses and any response to a HEAD request) is
- -- always terminated by the first empty line after the header fields...
- --
- -- Despite the above, some servers return a body with response to a HEAD
- -- request. So if an HTTP/1.0 server returns a response without Connection:
- -- keep-alive, or any server returns a response with Connection: close, read
- -- whatever's left on the socket (should be zero bytes).
- if string.upper(method) == "HEAD"
- or (response.status >= 100 and response.status <= 199)
- or response.status == 204 or response.status == 304 then
- if connection_close or (version_major == "1" and version_minor == "0" and not connection_keepalive) then
- return recv_all(s, partial)
- else
- return "", partial
- end
- end
- -- 2. If a Transfer-Encoding header field (section 14.41) is present and has
- -- any value other than "identity", then the transfer-length is defined by
- -- use of the "chunked" transfer-coding (section 3.6), unless the message
- -- is terminated by closing the connection.
- if response.header["transfer-encoding"]
- and response.header["transfer-encoding"] ~= "identity" then
- return recv_chunked(s, partial)
- end
- -- The Citrix XML Service sends a wrong "Transfer-Coding" instead of
- -- "Transfer-Encoding".
- if response.header["transfer-coding"]
- and response.header["transfer-coding"] ~= "identity" then
- return recv_chunked(s, partial)
- end
- -- 3. If a Content-Length header field (section 14.13) is present, its decimal
- -- value in OCTETs represents both the entity-length and the
- -- transfer-length. The Content-Length header field MUST NOT be sent if
- -- these two lengths are different (i.e., if a Transfer-Encoding header
- -- field is present). If a message is received with both a
- -- Transfer-Encoding header field and a Content-Length header field, the
- -- latter MUST be ignored.
- if response.header["content-length"] and not response.header["transfer-encoding"] then
- content_length = tonumber(response.header["content-length"])
- if not content_length then
- return nil, string.format("Content-Length %q is non-numeric", response.header["content-length"])
- end
- return recv_length(s, content_length, partial)
- end
- -- 4. If the message uses the media type "multipart/byteranges", and the
- -- transfer-length is not otherwise specified, then this self-delimiting
- -- media type defines the transfer-length. [sic]
- -- Case 4 is unhandled.
- -- 5. By the server closing the connection.
- return recv_all(s, partial)
- end
- -- Sets response["status-line"] and response.status.
- local function parse_status_line(status_line, response)
- local version, status, reason_phrase
- response["status-line"] = status_line
- version, status, reason_phrase = string.match(status_line,
- "^HTTP/(%d%.%d) *(%d+) *(.*)\r?\n$")
- if not version then
- return nil, string.format("Error parsing status-line %q.", status_line)
- end
- -- We don't have a use for the version; ignore it.
- response.status = tonumber(status)
- if not response.status then
- return nil, string.format("Status code is not numeric: %s", status)
- end
- return true
- end
- -- Sets response.header and response.rawheader.
- local function parse_header(header, response)
- local pos
- local name, words
- local s, e
- response.header = {}
- response.rawheader = stdnse.strsplit("\r?\n", header)
- pos = 1
- while pos <= #header do
- -- Get the field name.
- e, name = get_token(header, pos)
- if not name or e > #header or string.sub(header, e, e) ~= ":" then
- return nil, string.format("Can't get header field name at %q", string.sub(header, pos, pos + 30))
- end
- pos = e + 1
- -- Skip initial space.
- pos = skip_lws(header, pos)
- -- Get non-space words separated by LWS, then join them with a single space.
- words = {}
- while pos <= #header and not string.match(header, "^\r?\n", pos) do
- s = pos
- while not string.match(header, "^[ \t]", pos) and
- not string.match(header, "^\r?\n", pos) do
- pos = pos + 1
- end
- words[#words + 1] = string.sub(header, s, pos - 1)
- pos = skip_lws(header, pos)
- end
- -- Set it in our table.
- name = string.lower(name)
- if response.header[name] then
- response.header[name] = response.header[name] .. ", " .. table.concat(words, " ")
- else
- response.header[name] = table.concat(words, " ")
- end
- -- Next field, or end of string. (If not it's an error.)
- s, e = string.find(header, "^\r?\n", pos)
- if not e then
- return nil, string.format("Header field named %q didn't end with CRLF", name)
- end
- pos = e + 1
- end
- return true
- end
- -- Parse the contents of a Set-Cookie header field. The result is an array
- -- containing tables of the form
- --
- -- { name = "NAME", value = "VALUE", Comment = "...", Domain = "...", ... }
- --
- -- Every key except "name" and "value" is optional.
- --
- -- This function attempts to support the cookie syntax defined in RFC 2109
- -- along with the backwards-compatibility suggestions from its section 10,
- -- "HISTORICAL". Values need not be quoted, but if they start with a quote they
- -- will be interpreted as a quoted string.
- local function parse_set_cookie(s)
- local cookies
- local name, value
- local _, pos
- cookies = {}
- pos = 1
- while true do
- local cookie = {}
- -- Get the NAME=VALUE part.
- pos = skip_space(s, pos)
- pos, cookie.name = get_token(s, pos)
- if not cookie.name then
- return nil, "Can't get cookie name."
- end
- pos = skip_space(s, pos)
- if pos > #s or string.sub(s, pos, pos) ~= "=" then
- return nil, string.format("Expected '=' after cookie name \"%s\".", cookie.name)
- end
- pos = pos + 1
- pos = skip_space(s, pos)
- if string.sub(s, pos, pos) == "\"" then
- pos, cookie.value = get_quoted_string(s, pos)
- else
- _, pos, cookie.value = string.find(s, "([^;]*)[ \t]*", pos)
- pos = pos + 1
- end
- if not cookie.value then
- return nil, string.format("Can't get value of cookie named \"%s\".", cookie.name)
- end
- pos = skip_space(s, pos)
- -- Loop over the attributes.
- while pos <= #s and string.sub(s, pos, pos) == ";" do
- pos = pos + 1
- pos = skip_space(s, pos)
- pos, name = get_token(s, pos)
- if not name then
- return nil, string.format("Can't get attribute name of cookie \"%s\".", cookie.name)
- end
- pos = skip_space(s, pos)
- if pos <= #s and string.sub(s, pos, pos) == "=" then
- pos = pos + 1
- pos = skip_space(s, pos)
- if string.sub(s, pos, pos) == "\"" then
- pos, value = get_quoted_string(s, pos)
- else
- -- account for the possibility of the expires attribute being empty or improperly formatted
- local last_pos = pos
- if string.lower(name) == "expires" then
- -- For version 0 cookies we must allow one comma for "expires".
- _, pos, value = string.find(s, "([^,]*,[^;,]*)[ \t]*", pos)
- else
- _, pos, value = string.find(s, "([^;,]*)[ \t]*", pos)
- end
- -- account for the possibility of the expires attribute being empty or improperly formatted
- if ( not(pos) ) then
- _, pos, value = s:find("([^;]*)", last_pos)
- end
- pos = pos + 1
- end
- if not value then
- return nil, string.format("Can't get value of cookie attribute \"%s\".", name)
- end
- else
- value = true
- end
- cookie[name:lower()] = value
- pos = skip_space(s, pos)
- end
- cookies[#cookies + 1] = cookie
- if pos > #s then
- break
- end
- if string.sub(s, pos, pos) ~= "," then
- return nil, string.format("Syntax error after cookie named \"%s\".", cookie.name)
- end
- pos = pos + 1
- pos = skip_space(s, pos)
- end
- return cookies
- end
- -- Read one response from the socket <code>s</code> and return it after
- -- parsing.
- local function next_response(s, method, partial)
- local response
- local status_line, header, body
- local status, err
- partial = partial or ""
- response = {
- status=nil,
- ["status-line"]=nil,
- header={},
- rawheader={},
- body=""
- }
- status_line, partial = recv_line(s, partial)
- if not status_line then
- return nil, partial
- end
- status, err = parse_status_line(status_line, response)
- if not status then
- return nil, err
- end
- header, partial = recv_header(s, partial)
- if not header then
- return nil, partial
- end
- status, err = parse_header(header, response)
- if not status then
- return nil, err
- end
- body, partial = recv_body(s, response, method, partial)
- if not body then
- return nil, partial
- end
- response.body = body
- -- We have the Status-Line, header, and body; now do any postprocessing.
- response.cookies = {}
- if response.header["set-cookie"] then
- response.cookies, err = parse_set_cookie(response.header["set-cookie"])
- if not response.cookies then
- -- Ignore a cookie parsing error.
- response.cookies = {}
- end
- end
- return response, partial
- end
- --- Tries to extract the max number of requests that should be made on
- -- a keep-alive connection based on "Keep-Alive: timeout=xx,max=yy" response
- -- header.
- --
- -- If the value is not available, an arbitrary value is used. If the connection
- -- is not explicitly closed by the server, this same value is attempted.
- --
- -- @param response The http response - Might be a table or a raw response
- -- @return The max number of requests on a keep-alive connection
- local function getPipelineMax(response)
- -- Allow users to override this with a script-arg
- local pipeline = stdnse.get_script_args({'http.pipeline', 'pipeline'})
- if(pipeline) then
- return tonumber(pipeline)
- end
- if response then
- if response.header and response.header.connection ~= "close" then
- if response.header["keep-alive"] then
- local max = string.match( response.header["keep-alive"], "max=(%d*)")
- if(max == nil) then
- return 40
- end
- return tonumber(max)
- else
- return 40
- end
- end
- end
- return 1
- end
- --- Builds a string to be added to the request mod_options table
- --
- -- @param cookies A cookie jar just like the table returned parse_set_cookie.
- -- @param path If the argument exists, only cookies with this path are included to the request
- -- @return A string to be added to the mod_options table
- local function buildCookies(cookies, path)
- local cookie = ""
- if type(cookies) == 'string' then return cookies end
- for _, ck in ipairs(cookies or {}) do
- local ckpath = ck["path"]
- if not path or not ckpath
- or ckpath == path
- or ckpath:sub(-1) == "/" and ckpath == path:sub(1, ckpath:len())
- or ckpath .. "/" == path:sub(1, ckpath:len()+1)
- then
- cookie = cookie .. ck["name"] .. "=" .. ck["value"] .. "; "
- end
- end
- return cookie:gsub("; $","")
- end
- -- HTTP cache.
- -- Cache of GET and HEAD requests. Uses <"host:port:path", record>.
- -- record is in the format:
- -- result: The result from http.get or http.head
- -- last_used: The time the record was last accessed or made.
- -- get: Was the result received from a request to get or recently wiped?
- -- size: The size of the record, equal to #record.result.body.
- local cache = {size = 0};
- local function check_size (cache)
- local max_size = tonumber(stdnse.get_script_args({'http.max-cache-size', 'http-max-cache-size'}) or 1e6);
- local size = cache.size;
- if size > max_size then
- stdnse.debug1(
- "Current http cache size (%d bytes) exceeds max size of %d",
- size, max_size);
- table.sort(cache, function(r1, r2)
- return (r1.last_used or 0) < (r2.last_used or 0);
- end);
- for i, record in ipairs(cache) do
- if size <= max_size then break end
- local result = record.result;
- if type(result.body) == "string" then
- size = size - record.size;
- record.size, record.get, result.body = 0, false, "";
- end
- end
- cache.size = size;
- end
- stdnse.debug2("Final http cache size (%d bytes) of max size of %d",
- size, max_size);
- return size;
- end
- -- Unique value to signal value is being retrieved.
- -- Also holds <mutex, thread> pairs, working thread is value
- local WORKING = setmetatable({}, {__mode = "v"});
- local function lookup_cache (method, host, port, path, options)
- if(not(validate_options(options))) then
- return nil
- end
- options = options or {};
- local bypass_cache = options.bypass_cache; -- do not lookup
- local no_cache = options.no_cache; -- do not save result
- local no_cache_body = options.no_cache_body; -- do not save body
- if type(port) == "table" then port = port.number end
- local key = stdnse.get_hostname(host)..":"..port..":"..path;
- local mutex = nmap.mutex(tostring(lookup_cache)..key);
- local state = {
- mutex = mutex,
- key = key,
- method = method,
- bypass_cache = bypass_cache,
- no_cache = no_cache,
- no_cache_body = no_cache_body,
- };
- while true do
- mutex "lock";
- local record = cache[key];
- if bypass_cache or record == nil or method ~= record.method then
- WORKING[mutex] = coroutine.running();
- cache[key], state.old_record = WORKING, record;
- return nil, state;
- elseif record == WORKING then
- local working = WORKING[mutex];
- if working == nil or coroutine.status(working) == "dead" then
- -- thread died before insert_cache could be called
- cache[key] = nil; -- reset
- end
- mutex "done";
- else
- mutex "done";
- record.last_used = os.time();
- return tcopy(record.result), state;
- end
- end
- end
- local function response_is_cacheable(response)
- -- if response.status is nil, then an error must have occurred during the request
- -- and we probably don't want to cache the response
- if not response.status then
- return false
- end
- -- 206 Partial Content. RFC 2616, 1.34: "...a cache that does not support the
- -- Range and Content-Range headers MUST NOT cache 206 (Partial Content)
- -- responses."
- if response.status == 206 then
- return false
- end
- -- RFC 2616, 13.4. "A response received with any [status code other than 200,
- -- 203, 206, 300, 301 or 410] (e.g. status codes 302 and 307) MUST NOT be
- -- returned in a reply to a subsequent request unless there are cache-control
- -- directives or another header(s) that explicitly allow it."
- -- We violate the standard here and allow these other codes to be cached,
- -- with the exceptions listed below.
- -- 401 Unauthorized. Caching this would prevent us from retrieving it later
- -- with the correct credentials.
- if response.status == 401 then
- return false
- end
- return true
- end
- local function insert_cache (state, response)
- local key = assert(state.key);
- local mutex = assert(state.mutex);
- if response == nil or state.no_cache or not response_is_cacheable(response) then
- cache[key] = state.old_record;
- else
- local record = {
- result = tcopy(response),
- last_used = os.time(),
- method = state.method,
- size = type(response.body) == "string" and #response.body or 0,
- };
- response = record.result; -- only modify copy
- cache[key], cache[#cache+1] = record, record;
- if state.no_cache_body then
- response.body = "";
- end
- if type(response.body) == "string" then
- cache.size = cache.size + #response.body;
- check_size(cache);
- end
- end
- mutex "done";
- end
- -- Return true if the given method requires a body in the request. In case no
- -- body was supplied we must send "Content-Length: 0".
- local function request_method_needs_content_length(method)
- return method == "POST"
- end
- -- For each of the following request functions, <code>host</code> may either be
- -- a string or a table, and <code>port</code> may either be a number or a
- -- table.
- --
- -- The format of the return value is a table with the following structure:
- -- {status = 200, status-line = "HTTP/1.1 200 OK", header = {}, rawheader = {}, body ="<html>...</html>"}
- -- The header table has an entry for each received header with the header name
- -- being the key. The table also has an entry named "status" which contains the
- -- http status code of the request.
- -- In case of an error, the status is nil and status-line describes the problem.
- local function http_error(status_line)
- return {
- status = nil,
- ["status-line"] = status_line,
- header = {},
- rawheader = {},
- body = nil,
- }
- end
- --- Build an HTTP request from parameters and return it as a string.
- --
- -- @param host The host this request is intended for.
- -- @param port The port this request is intended for.
- -- @param method The method to use.
- -- @param path The path for the request.
- -- @param options A table of options, which may include the keys:
- -- * <code>header</code>: A table containing additional headers to be used for the request.
- -- * <code>content</code>: The content of the message (content-length will be added -- set header['Content-Length'] to override)
- -- * <code>cookies</code>: A table of cookies in the form returned by <code>parse_set_cookie</code>.
- -- * <code>auth</code>: A table containing the keys <code>username</code> and <code>password</code>.
- -- @return A request string.
- -- @see generic_request
- local function build_request(host, port, method, path, options)
- if(not(validate_options(options))) then
- return nil
- end
- options = options or {}
- -- Private copy of the options table, used to add default header fields.
- local mod_options = {
- header = {
- Connection = "close",
- Host = get_host_field(host, port),
- ["User-Agent"] = USER_AGENT
- }
- }
- if options.cookies then
- local cookies = buildCookies(options.cookies, path)
- if #cookies > 0 then
- mod_options.header["Cookie"] = cookies
- end
- end
- if options.auth and not (options.auth.digest or options.auth.ntlm) then
- local username = options.auth.username
- local password = options.auth.password
- local credentials = "Basic " .. base64.enc(username .. ":" .. password)
- mod_options.header["Authorization"] = credentials
- end
- if options.digestauth then
- local order = {"username", "realm", "nonce", "digest-uri", "algorithm", "response", "qop", "nc", "cnonce"}
- local no_quote = {algorithm=true, qop=true, nc=true}
- local creds = {}
- for _,k in ipairs(order) do
- local v = options.digestauth[k]
- if v then
- if no_quote[k] then
- table.insert(creds, ("%s=%s"):format(k,v))
- else
- if k == "digest-uri" then
- table.insert(creds, ('%s="%s"'):format("uri",v))
- else
- table.insert(creds, ('%s="%s"'):format(k,v))
- end
- end
- end
- end
- local credentials = "Digest "..table.concat(creds, ", ")
- mod_options.header["Authorization"] = credentials
- end
- if options.ntlmauth then
- mod_options.header["Authorization"] = "NTLM " .. base64.enc(options.ntlmauth)
- end
- local body
- -- Build a form submission from a table, like "k1=v1&k2=v2".
- if type(options.content) == "table" then
- local parts = {}
- local k, v
- for k, v in pairs(options.content) do
- parts[#parts + 1] = url.escape(k) .. "=" .. url.escape(v)
- end
- body = table.concat(parts, "&")
- mod_options.header["Content-Type"] = "application/x-www-form-urlencoded"
- elseif options.content then
- body = options.content
- elseif request_method_needs_content_length(method) then
- body = ""
- end
- if body then
- mod_options.header["Content-Length"] = #body
- end
- -- Add any other header fields into the local copy.
- table_augment(mod_options, options)
- -- We concat this string manually to allow null bytes in requests
- local request_line = method.." "..path.." HTTP/1.1"
- local header = {}
- for name, value in pairs(mod_options.header) do
- -- we concat this string manually to allow null bytes in requests
- header[#header + 1] = name..": "..value
- end
- return request_line .. "\r\n" .. stdnse.strjoin("\r\n", header) .. "\r\n\r\n" .. (body or "")
- end
- --- Send a string to a host and port and return the HTTP result. This function
- -- is like <code>generic_request</code>, to be used when you have a ready-made
- -- request, not a collection of request parameters.
- --
- -- @param host The host to connect to.
- -- @param port The port to connect to.
- -- @param options A table of other parameters. It may have any of these fields:
- -- * <code>timeout</code>: A timeout used for socket operations.
- -- * <code>header</code>: A table containing additional headers to be used for the request.
- -- * <code>content</code>: The content of the message (content-length will be added -- set header['Content-Length'] to override)
- -- * <code>cookies</code>: A table of cookies in the form returned by <code>parse_set_cookie</code>.
- -- * <code>auth</code>: A table containing the keys <code>username</code> and <code>password</code>.
- -- @return A response table, see module documentation for description.
- -- @see generic_request
- local function request(host, port, data, options)
- if(not(validate_options(options))) then
- return http_error("Options failed to validate.")
- end
- local method
- local header
- local response
- options = options or {}
- if type(port) == 'table' then
- if port.protocol and port.protocol ~= 'tcp' then
- stdnse.debug1("http.request() supports the TCP protocol only, your request to %s cannot be completed.", host)
- return http_error("Unsupported protocol.")
- end
- end
- method = string.match(data, "^(%S+)")
- local socket, partial, opts = comm.tryssl(host, port, data, { timeout = options.timeout })
- if not socket then
- return http_error("Error creating socket.")
- end
- repeat
- response, partial = next_response(socket, method, partial)
- if not response then
- return http_error("There was an error in next_response function.")
- end
- -- See RFC 2616, sections 8.2.3 and 10.1.1, for the 100 Continue status.
- -- Sometimes a server will tell us to "go ahead" with a POST body before
- -- sending the real response. If we got one of those, skip over it.
- until not (response.status >= 100 and response.status <= 199)
- socket:close()
- -- if SSL was used to retrieve the URL mark this in the response
- response.ssl = ( opts == 'ssl' )
- return response
- end
- ---Do a single request with a given method. The response is returned as the standard
- -- response table (see the module documentation).
- --
- -- The <code>get</code>, <code>head</code>, and <code>post</code> functions are simple
- -- wrappers around <code>generic_request</code>.
- --
- -- Any 1XX (informational) responses are discarded.
- --
- -- @param host The host to connect to.
- -- @param port The port to connect to.
- -- @param method The method to use; for example, 'GET', 'HEAD', etc.
- -- @param path The path to retrieve.
- -- @param options [optional] A table that lets the caller control socket timeouts, HTTP headers, and other parameters. For full documentation, see the module documentation (above).
- -- @return A response table, see module documentation for description.
- -- @see request
- function generic_request(host, port, method, path, options)
- if(not(validate_options(options))) then
- return http_error("Options failed to validate.")
- end
- local digest_auth = options and options.auth and options.auth.digest
- local ntlm_auth = options and options.auth and options.auth.ntlm
- if (digest_auth or ntlm_auth) and not have_ssl then
- stdnse.debug1("http: digest and ntlm auth require openssl.")
- end
- if digest_auth and have_ssl then
- -- If we want to do digest authentication, we have to make an initial
- -- request to get realm, nonce and other fields.
- local options_with_auth_removed = tcopy(options)
- options_with_auth_removed["auth"] = nil
- local r = generic_request(host, port, method, path, options_with_auth_removed)
- local h = r.header['www-authenticate']
- if not r.status or (h and not string.find(h:lower(), "digest.-realm")) then
- stdnse.debug1("http: the target doesn't support digest auth or there was an error during request.")
- return http_error("The target doesn't support digest auth or there was an error during request.")
- end
- -- Compute the response hash
- local dmd5 = sasl.DigestMD5:new(h, options.auth.username, options.auth.password, method, path)
- local _, digest_table = dmd5:calcDigest()
- options.digestauth = digest_table
- end
- if ntlm_auth and have_ssl then
- local custom_options = tcopy(options) -- to be sent with the type 1 request
- custom_options["auth"] = nil -- removing the auth options
- -- let's check if the target supports ntlm with a simple get request.
- -- Setting a timeout here other than nil messes up the authentication if this is the first device sending
- -- a request to the server. Don't know why.
- custom_options.timeout = nil
- local response = generic_request(host, port, method, path, custom_options)
- local authentication_header = response.header['www-authenticate']
- -- get back the timeout option.
- custom_options.timeout = options.timeout
- custom_options.header = options.header or {}
- custom_options.header["Connection"] = "Keep-Alive" -- Keep-Alive headers are needed for authentication.
- if (not authentication_header) or (not response.status) or (not string.find(authentication_header:lower(), "ntlm")) then
- stdnse.debug1("http: the target doesn't support NTLM or there was an error during request.")
- return http_error("The target doesn't support NTLM or there was an error during request.")
- end
- -- ntlm works with three messages. we send a request, it sends
- -- a challenge, we respond to the challenge.
- local hostname = options.auth.hostname or "localhost" -- the hostname to be sent
- local workstation_name = options.auth.workstation_name or "NMAP" -- the workstation name to be sent
- local username = options.auth.username -- the username as specified
- local auth_blob = "NTLMSSP\x00" .. -- NTLM signature
- "\x01\x00\x00\x00" .. -- NTLM Type 1 message
- bin.pack("<I", 0xa208b207) .. -- flags 56, 128, Version, Extended Security, Always Sign, Workstation supplied, Domain Supplied, NTLM Key, OEM, Unicode
- bin.pack("<SSISSI",#workstation_name, #workstation_name, 40 + #hostname, #hostname, #hostname, 40) .. -- Supplied Domain and Workstation
- bin.pack("CC<S", -- OS version info
- 5, 1, 2600) .. -- 5.1.2600
- "\x00\x00\x00\x0f" .. -- OS version info end (static 0x0000000f)
- hostname.. -- HOST NAME
- workstation_name --WORKSTATION name
- custom_options.ntlmauth = auth_blob
- -- check if the protocol is tcp
- if type(port) == 'table' then
- if port.protocol and port.protocol ~= 'tcp' then
- stdnse.debug1("NTLM authentication supports the TCP protocol only, your request to %s cannot be completed.", host)
- return http_error("Unsupported protocol.")
- end
- end
- -- tryssl uses ssl if needed. sends the type 1 message.
- local socket, partial, opts = comm.tryssl(host, port, build_request(host, port, method, path, custom_options), { timeout = options.timeout })
- if not socket then
- return http_error("Could not create socket to send type 1 message.")
- end
- repeat
- response, partial = next_response(socket, method, partial)
- if not response then
- return http_error("There was error in receivin…
Large files files are truncated, but you can click here to view the full file