From df32ab6f9de78e5692dc3da47a47df56dd006d22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Notin?= Date: Sun, 9 Aug 2026 21:23:53 +0200 Subject: [PATCH 1/3] Add TLS client certificate request script --- nselib/asn1.lua | 59 ++++++++++++- nselib/tls.lua | 48 +++++++++++ scripts/script.db | 1 + scripts/tls-client-cert-request.nse | 123 ++++++++++++++++++++++++++++ 4 files changed, 230 insertions(+), 1 deletion(-) create mode 100644 scripts/tls-client-cert-request.nse diff --git a/nselib/asn1.lua b/nselib/asn1.lua index 2620024ef..c406cd8b1 100644 --- a/nselib/asn1.lua +++ b/nselib/asn1.lua @@ -10,11 +10,12 @@ -- @name asn1 -- --- Version 0.3 +-- Version 0.4 -- Created 01/12/2010 - v0.1 - Created by Patrik Karlsson -- Revised 01/28/2010 - v0.2 - Adapted to create a framework for SNMP, LDAP and future protocols -- Revised 02/02/2010 - v0.3 - Changes: o Re-designed so that ASN1Encoder and ASN1Decoder are separate classes -- o Each script or library should now create its own Encoder and Decoder instance +-- Revised 08/09/2026 - v0.4 - Added X.509 Name decoding. -- local math = require "math" @@ -30,6 +31,23 @@ BERCLASS = { Private = 192 } +-- Common X.509 distinguished-name attribute OIDs and their RFC 4514 names. +X509_NAME_OIDS = { + ["2.5.4.3"] = "CN", ["2.5.4.4"] = "SN", ["2.5.4.5"] = "serialNumber", + ["2.5.4.6"] = "C", ["2.5.4.7"] = "L", ["2.5.4.8"] = "ST", + ["2.5.4.10"] = "O", ["2.5.4.11"] = "OU", + ["0.9.2342.19200300.100.1.25"] = "DC", + ["1.2.840.113549.1.9.1"] = "emailAddress", +} + +local function decode_x509_name_string(_, encoded, length, position) + return encoded:sub(position, position + length - 1), position + length +end + +local function decode_x509_name_set(self, encoded, length, position) + return self:decodeSeq(encoded, length, position) +end + --- The decoder class -- ASN1Decoder = { @@ -251,6 +269,45 @@ ASN1Decoder = { } +--- +-- Decodes a DER-encoded X.509 Name into a human-readable distinguished name. +-- Unknown attribute OIDs are rendered in dotted-decimal notation. If the Name +-- cannot be decoded, returns its hexadecimal DER representation. +-- @param der DER-encoded X.509 Name +-- @return string Distinguished name +function decodeX509Name(der) + local decoder = ASN1Decoder:new() + decoder:setStopOnError(true) + decoder:registerTagDecoders({ + ["\x31"] = decode_x509_name_set, -- SET OF RelativeDistinguishedName + ["\x0c"] = decode_x509_name_string, -- UTF8String + ["\x13"] = decode_x509_name_string, -- PrintableString + ["\x14"] = decode_x509_name_string, -- T61String + ["\x16"] = decode_x509_name_string, -- IA5String + }) + local ok, name = pcall(decoder.decode, decoder, der, 1) + if not ok or type(name) ~= "table" then + return stdnse.tohex(der) + end + + local rdns = {} + for _, rdn in ipairs(name) do + local attributes = {} + for _, attribute in ipairs(rdn) do + local oid, value = attribute[1], attribute[2] + if type(oid) == "table" and type(value) == "string" then + local oid_text = table.concat(oid, ".") + attributes[#attributes + 1] = ("%s = %s"):format( + X509_NAME_OIDS[oid_text] or oid_text, value) + end + end + if #attributes > 0 then + rdns[#rdns + 1] = table.concat(attributes, " + ") + end + end + return #rdns > 0 and table.concat(rdns, ", ") or stdnse.tohex(der) +end + --- The encoder class -- ASN1Encoder = { diff --git a/nselib/tls.lua b/nselib/tls.lua index 7838f57cb..167f4f9d4 100644 --- a/nselib/tls.lua +++ b/nselib/tls.lua @@ -246,6 +246,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 = { @@ -1560,6 +1571,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 diff --git a/scripts/script.db b/scripts/script.db index d1d683173..c7a4b5501 100644 --- a/scripts/script.db +++ b/scripts/script.db @@ -576,6 +576,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/tls-client-cert-request.nse b/scripts/tls-client-cert-request.nse new file mode 100644 index 000000000..c4059c449 --- /dev/null +++ b/scripts/tls-client-cert-request.nse @@ -0,0 +1,123 @@ +local asn1 = require "asn1" +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: +-- | Acceptable Certificate Authorities: +-- | C = US, ST = California, L = San Francisco, O = BadSSL, CN = BadSSL Client Root Certificate Authority +-- | Client Certificate Types: +-- | rsa_sign +-- | dss_sign +-- | ecdsa_sign +-- | 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] = + asn1.decodeX509Name(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 From 24cc8e958c8edc9e0a6b2005e13ca9ae06401c0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Notin?= Date: Sun, 9 Aug 2026 21:24:19 +0200 Subject: [PATCH 2/3] Share TLS record iterator --- nselib/tls.lua | 27 +++++++++++++++++++++++++++ scripts/ssl-dh-params.nse | 27 +-------------------------- scripts/ssl-enum-ciphers.nse | 26 +------------------------- 3 files changed, 29 insertions(+), 51 deletions(-) diff --git a/nselib/tls.lua b/nselib/tls.lua index 167f4f9d4..9a12dda79 100644 --- a/nselib/tls.lua +++ b/nselib/tls.lua @@ -2094,6 +2094,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/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 dd63dc9ec..0c244d0e3 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 From 4b2d2c5cc5d9f1c3d0e3695ba71baad75d650dcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Notin?= Date: Sun, 9 Aug 2026 22:07:20 +0200 Subject: [PATCH 3/3] Decode certificate authority names with OpenSSL --- nse_nsock.cc | 6 +++ nse_ssl_cert.cc | 34 +++++++++++++++++ nse_ssl_cert.h | 1 + nselib/asn1.lua | 59 +---------------------------- scripts/tls-client-cert-request.nse | 7 ++-- 5 files changed, 45 insertions(+), 62 deletions(-) diff --git a/nse_nsock.cc b/nse_nsock.cc index 2162f221d..7602083b2 100644 --- a/nse_nsock.cc +++ b/nse_nsock.cc @@ -904,6 +904,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 @@ -1182,6 +1187,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 e7bdce4e5..a1ce78d65 100644 --- a/nse_ssl_cert.cc +++ b/nse_ssl_cert.cc @@ -542,6 +542,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/asn1.lua b/nselib/asn1.lua index c406cd8b1..2620024ef 100644 --- a/nselib/asn1.lua +++ b/nselib/asn1.lua @@ -10,12 +10,11 @@ -- @name asn1 -- --- Version 0.4 +-- Version 0.3 -- Created 01/12/2010 - v0.1 - Created by Patrik Karlsson -- Revised 01/28/2010 - v0.2 - Adapted to create a framework for SNMP, LDAP and future protocols -- Revised 02/02/2010 - v0.3 - Changes: o Re-designed so that ASN1Encoder and ASN1Decoder are separate classes -- o Each script or library should now create its own Encoder and Decoder instance --- Revised 08/09/2026 - v0.4 - Added X.509 Name decoding. -- local math = require "math" @@ -31,23 +30,6 @@ BERCLASS = { Private = 192 } --- Common X.509 distinguished-name attribute OIDs and their RFC 4514 names. -X509_NAME_OIDS = { - ["2.5.4.3"] = "CN", ["2.5.4.4"] = "SN", ["2.5.4.5"] = "serialNumber", - ["2.5.4.6"] = "C", ["2.5.4.7"] = "L", ["2.5.4.8"] = "ST", - ["2.5.4.10"] = "O", ["2.5.4.11"] = "OU", - ["0.9.2342.19200300.100.1.25"] = "DC", - ["1.2.840.113549.1.9.1"] = "emailAddress", -} - -local function decode_x509_name_string(_, encoded, length, position) - return encoded:sub(position, position + length - 1), position + length -end - -local function decode_x509_name_set(self, encoded, length, position) - return self:decodeSeq(encoded, length, position) -end - --- The decoder class -- ASN1Decoder = { @@ -269,45 +251,6 @@ ASN1Decoder = { } ---- --- Decodes a DER-encoded X.509 Name into a human-readable distinguished name. --- Unknown attribute OIDs are rendered in dotted-decimal notation. If the Name --- cannot be decoded, returns its hexadecimal DER representation. --- @param der DER-encoded X.509 Name --- @return string Distinguished name -function decodeX509Name(der) - local decoder = ASN1Decoder:new() - decoder:setStopOnError(true) - decoder:registerTagDecoders({ - ["\x31"] = decode_x509_name_set, -- SET OF RelativeDistinguishedName - ["\x0c"] = decode_x509_name_string, -- UTF8String - ["\x13"] = decode_x509_name_string, -- PrintableString - ["\x14"] = decode_x509_name_string, -- T61String - ["\x16"] = decode_x509_name_string, -- IA5String - }) - local ok, name = pcall(decoder.decode, decoder, der, 1) - if not ok or type(name) ~= "table" then - return stdnse.tohex(der) - end - - local rdns = {} - for _, rdn in ipairs(name) do - local attributes = {} - for _, attribute in ipairs(rdn) do - local oid, value = attribute[1], attribute[2] - if type(oid) == "table" and type(value) == "string" then - local oid_text = table.concat(oid, ".") - attributes[#attributes + 1] = ("%s = %s"):format( - X509_NAME_OIDS[oid_text] or oid_text, value) - end - end - if #attributes > 0 then - rdns[#rdns + 1] = table.concat(attributes, " + ") - end - end - return #rdns > 0 and table.concat(rdns, ", ") or stdnse.tohex(der) -end - --- The encoder class -- ASN1Encoder = { diff --git a/scripts/tls-client-cert-request.nse b/scripts/tls-client-cert-request.nse index c4059c449..6c70bb715 100644 --- a/scripts/tls-client-cert-request.nse +++ b/scripts/tls-client-cert-request.nse @@ -1,4 +1,3 @@ -local asn1 = require "asn1" local nmap = require "nmap" local shortport = require "shortport" local sslcert = require "sslcert" @@ -20,12 +19,12 @@ TLS 1.3 handshake decryption, so this script intentionally negotiates TLS 1.2. -- @output -- 443/tcp open https -- | tls-client-cert-request: --- | Acceptable Certificate Authorities: --- | C = US, ST = California, L = San Francisco, O = BadSSL, CN = BadSSL Client Root Certificate Authority -- | 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 @@ -107,7 +106,7 @@ action = function(host, port) end for _, authority in ipairs(message.certificate_authorities) do output["Acceptable Certificate Authorities"][#output["Acceptable Certificate Authorities"] + 1] = - asn1.decodeX509Name(authority) + nmap.socket.parse_ssl_name(authority) end return output elseif message.type == "server_hello_done" then