diff --git a/nse_nsock.cc b/nse_nsock.cc index 33582647f..66070f3a7 100644 --- a/nse_nsock.cc +++ b/nse_nsock.cc @@ -916,6 +916,11 @@ int l_parse_ssl_certificate(lua_State *L) { return luaL_error(L, "SSL is not available"); } + +int l_parse_ssl_name(lua_State *L) +{ + return luaL_error(L, "SSL is not available"); +} #endif /* Set the local address for socket operations. The two optional parameters @@ -1195,6 +1200,7 @@ LUALIB_API int luaopen_nsock (lua_State *L) {"new", l_new}, {"sleep", l_sleep}, {"parse_ssl_certificate", l_parse_ssl_certificate}, + {"parse_ssl_name", l_parse_ssl_name}, {"get_stats", l_get_stats}, {NULL, NULL} }; diff --git a/nse_ssl_cert.cc b/nse_ssl_cert.cc index f6c2c9597..e0f5254d3 100644 --- a/nse_ssl_cert.cc +++ b/nse_ssl_cert.cc @@ -544,6 +544,40 @@ int l_parse_ssl_certificate(lua_State *L) return parse_ssl_cert(L, cert); } +int l_parse_ssl_name(lua_State *L) +{ + BIO *out; + X509_NAME *name; + size_t l; + const char *der; + + der = luaL_checklstring(L, 1, &l); + if (der == NULL) { + lua_pushnil(L); + return 1; + } + + name = d2i_X509_NAME(NULL, (const unsigned char **) &der, l); + if (name == NULL) { + lua_pushnil(L); + return 1; + } + + out = BIO_new(BIO_s_mem()); + if (out == NULL || X509_NAME_print_ex(out, name, 0, XN_FLAG_ONELINE) < 0) { + BIO_free_all(out); + X509_NAME_free(name); + lua_pushnil(L); + return 1; + } + char *value = NULL; + long len = BIO_get_mem_data(out, &value); + lua_pushlstring(L, value, len); + BIO_free_all(out); + X509_NAME_free(name); + return 1; +} + int l_get_ssl_certificate(lua_State *L) { SSL *ssl; diff --git a/nse_ssl_cert.h b/nse_ssl_cert.h index 788585790..b95b59657 100644 --- a/nse_ssl_cert.h +++ b/nse_ssl_cert.h @@ -65,6 +65,7 @@ int l_get_ssl_certificate(lua_State *L); int l_parse_ssl_certificate(lua_State *L); +int l_parse_ssl_name(lua_State *L); void nse_nsock_init_ssl_cert(lua_State *L); #endif diff --git a/nselib/tls.lua b/nselib/tls.lua index 29cfe357c..c5ee68220 100644 --- a/nselib/tls.lua +++ b/nselib/tls.lua @@ -251,6 +251,17 @@ SignatureAlgorithms = { gostr34102012_512 = 65, } +-- RFC 5246, section 7.4.4. Client Certificate Type values. +ClientCertificateTypes = { + rsa_sign = 1, + dss_sign = 2, + rsa_fixed_dh = 3, + dss_fixed_dh = 4, + ecdsa_sign = 64, + rsa_fixed_ecdh = 65, + ecdsa_fixed_ecdh = 66, +} + --- -- TLS v1.3 Signature Algorithms SignatureSchemes = { @@ -1565,6 +1576,43 @@ handshake_parse = { return b, j end, + -- RFC 5246, section 7.4.4. This is deliberately limited to the TLS 1.2 + -- format. TLS 1.3 CertificateRequest messages use a different layout + -- and are encrypted; TLS 1.3 handshake decryption is not implemented. + certificate_request = function (buffer, j, msg_end, protocol) + local b = { certificate_types = {}, signature_algorithms = {}, + certificate_authorities = {} } + local types, signature_algorithms, authorities + + types, j = unpack(">s1", buffer, j) + for i = 1, #types do + b.certificate_types[#b.certificate_types + 1] = + find_key(ClientCertificateTypes, types:byte(i)) or types:byte(i) + end + + -- The signature_algorithms field was added in TLS 1.2. + if protocol == "TLSv1.2" then + signature_algorithms, j = unpack(">s2", buffer, j) + for i = 1, #signature_algorithms, 2 do + local hash, signature = signature_algorithms:byte(i, i + 1) + b.signature_algorithms[#b.signature_algorithms + 1] = { + hash = find_key(HashAlgorithms, hash) or hash, + signature = find_key(SignatureAlgorithms, signature) or signature, + } + end + end + + authorities, j = unpack(">s2", buffer, j) + local i = 1 + while i <= #authorities do + local authority + authority, i = unpack(">s2", authorities, i) + b.certificate_authorities[#b.certificate_authorities + 1] = authority + end + + return b, j + end, + NewSessionTicket = function (buffer, j, msg_end, protocol) -- Need 4 bytes for parsing. local have = #buffer - j + 1 @@ -2056,6 +2104,33 @@ function record_buffer(sock, buffer, i) return true, buffer end +--- +-- Returns an iterator that yields TLS records read from a socket. +-- @param sock Connected socket +-- @return function Iterator returning a record, or nil and an error string +function record_iter(sock) + local buffer = "" + local i = 1 + local fragment + return function() + local record + i, record = record_read(buffer, i, fragment) + if not record then + local status, err + status, buffer, err = record_buffer(sock, buffer, i) + if not status then + return nil, err + end + i, record = record_read(buffer, i, fragment) + if not record then + return nil, "done" + end + end + fragment = record.fragment + return record + end +end + -- Get a server_name for use with the TLS Server Name Indication extension. -- -- This returns the value of the script argument "tls.servername" if given. Otherwise, it diff --git a/scripts/script.db b/scripts/script.db index fca72dbbf..404bc8646 100644 --- a/scripts/script.db +++ b/scripts/script.db @@ -583,6 +583,7 @@ Entry { filename = "telnet-ntlm-info.nse", categories = { "default", "discovery" Entry { filename = "tftp-enum.nse", categories = { "discovery", "intrusive", } } Entry { filename = "tftp-version.nse", categories = { "default", "safe", "version", } } Entry { filename = "tls-alpn.nse", categories = { "default", "discovery", "safe", } } +Entry { filename = "tls-client-cert-request.nse", categories = { "default", "discovery", "safe", } } Entry { filename = "tls-nextprotoneg.nse", categories = { "default", "discovery", "safe", } } Entry { filename = "tls-ticketbleed.nse", categories = { "safe", "vuln", } } Entry { filename = "tn3270-screen.nse", categories = { "discovery", "safe", } } diff --git a/scripts/ssl-dh-params.nse b/scripts/ssl-dh-params.nse index dcdf8d590..a4af3be25 100644 --- a/scripts/ssl-dh-params.nse +++ b/scripts/ssl-dh-params.nse @@ -531,31 +531,6 @@ local function ctx_log(level, protocol, fmt, ...) end --- returns a function that yields a new tls record each time it is called -local function get_record_iter(sock) - local buffer = "" - local i = 1 - local fragment - return function () - local record - i, record = tls.record_read(buffer, i, fragment) - if record == nil then - local status, err - status, buffer, err = tls.record_buffer(sock, buffer, i) - if not status then - return nil, err - end - i, record = tls.record_read(buffer, i, fragment) - if record == nil then - return nil, "done" - end - end - fragment = record.fragment - return record - end -end - - local function get_server_response(host, port, t) local timeout = stdnse.get_timeout(host, 10000, 5000) @@ -591,7 +566,7 @@ local function get_server_response(host, port, t) end -- Read response. - local get_next_record = get_record_iter(sock) + local get_next_record = tls.record_iter(sock) local records = {} while true do local record diff --git a/scripts/ssl-enum-ciphers.nse b/scripts/ssl-enum-ciphers.nse index 4aa206c29..48f8fc81c 100644 --- a/scripts/ssl-enum-ciphers.nse +++ b/scripts/ssl-enum-ciphers.nse @@ -322,30 +322,6 @@ local function ctx_log(level, protocol, fmt, ...) return stdnse.debug(level, "(%s) " .. fmt, protocol, ...) end --- returns a function that yields a new tls record each time it is called -local function get_record_iter(sock) - local buffer = "" - local i = 1 - local fragment - return function () - local record - i, record = tls.record_read(buffer, i, fragment) - if record == nil then - local status, err - status, buffer, err = tls.record_buffer(sock, buffer, i) - if not status then - return nil, err - end - i, record = tls.record_read(buffer, i, fragment) - if record == nil then - return nil, "done" - end - end - fragment = record.fragment - return record - end -end - local function try_params(host, port, t) -- Use Nmap's own discovered timeout plus 5 seconds for host processing @@ -384,7 +360,7 @@ local function try_params(host, port, t) end -- Read response. - local get_next_record = get_record_iter(sock) + local get_next_record = tls.record_iter(sock) local records = {} while true do local record diff --git a/scripts/tls-client-cert-request.nse b/scripts/tls-client-cert-request.nse new file mode 100644 index 000000000..6c70bb715 --- /dev/null +++ b/scripts/tls-client-cert-request.nse @@ -0,0 +1,122 @@ +local nmap = require "nmap" +local shortport = require "shortport" +local sslcert = require "sslcert" +local stdnse = require "stdnse" +local tls = require "tls" + +description = [[ +Collects the acceptable certificate authorities and signature algorithms from a +TLS 1.2 server's client-certificate request. + +TLS 1.3 encrypts this message. Nmap's NSE TLS library does not yet implement +TLS 1.3 handshake decryption, so this script intentionally negotiates TLS 1.2. +]] + +--- +-- @usage +-- nmap --script tls-client-cert-request +-- +-- @output +-- 443/tcp open https +-- | tls-client-cert-request: +-- | Client Certificate Types: +-- | rsa_sign +-- | dss_sign +-- | ecdsa_sign +-- | Acceptable Certificate Authorities: +-- |_ C = US, ST = California, L = San Francisco, O = BadSSL, CN = BadSSL Client Root Certificate Authority +-- | Signature Algorithms: +-- | sha512-rsa +-- | sha512-dsa +-- | sha512-ecdsa +-- | sha384-rsa +-- | sha384-dsa +-- | sha384-ecdsa +-- | sha256-rsa +-- | sha256-dsa +-- | sha256-ecdsa +-- | sha224-rsa +-- | sha224-dsa +-- | sha224-ecdsa +-- | sha1-rsa +-- | sha1-dsa +-- |_ sha1-ecdsa + +author = "Clément Notin" +license = "Same as Nmap--See https://nmap.org/book/man-legal.html" +categories = {"default", "discovery", "safe"} +dependencies = {"https-redirect"} + +portrule = function(host, port) + return port.protocol == "tcp" and + (shortport.ssl(host, port) or sslcert.getPrepareTLSWithoutReconnect(port)) +end + +action = function(host, port) + local status, sock, err + local specialized = sslcert.getPrepareTLSWithoutReconnect(port) + if specialized then + status, sock = specialized(host, port) + else + sock = nmap.new_socket() + status, err = sock:connect(host, port) + end + if not status then + stdnse.debug1("Connection to server failed: %s", sock or err) + return nil + end + + sock:set_timeout(((host.times and host.times.timeout) or 5) * 1000 + 5000) + local server_name = tls.servername(host) + local hello = tls.client_hello({ + protocol = "TLSv1.2", + record_protocol = "TLSv1.2", + extensions = { + elliptic_curves = tls.EXTENSION_HELPERS.elliptic_curves(tls.DEFAULT_ELLIPTIC_CURVES), + ec_point_formats = tls.EXTENSION_HELPERS.ec_point_formats({"uncompressed"}), + server_name = server_name and tls.EXTENSION_HELPERS.server_name(server_name), + }, + }) + status, err = sock:send(hello) + if not status then + stdnse.debug1("Could not send TLS ClientHello: %s", err) + sock:close() + return nil + end + + local next_record = tls.record_iter(sock) + while true do + local record, read_err = next_record() + if not record then + stdnse.debug1("Could not read TLS record: %s", read_err) + break + end + if record.type == "handshake" then + for _, message in ipairs(record.body) do + if message.type == "certificate_request" then + sock:close() + local output = { + ["Client Certificate Types"] = message.certificate_types, + ["Signature Algorithms"] = {}, + ["Acceptable Certificate Authorities"] = {}, + } + for _, algorithm in ipairs(message.signature_algorithms) do + output["Signature Algorithms"][#output["Signature Algorithms"] + 1] = + ("%s-%s"):format(algorithm.hash, algorithm.signature) + end + for _, authority in ipairs(message.certificate_authorities) do + output["Acceptable Certificate Authorities"][#output["Acceptable Certificate Authorities"] + 1] = + nmap.socket.parse_ssl_name(authority) + end + return output + elseif message.type == "server_hello_done" then + sock:close() + return nil + end + end + elseif record.type == "alert" then + break + end + end + sock:close() +end