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] 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