Skip to content

Commit 94ef475

Browse files
authored
Merge pull request #2461 from akabe1/3.2
Add mTLS new feature to support scans with client authentication
2 parents f84e8c0 + 51ab05e commit 94ef475

5 files changed

Lines changed: 71 additions & 18 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* Compatible to GNU grep 3.8
4747
* Don't use external pwd command anymore
4848
* Doesn't hang anymore when there's no local resolver
49+
* Added --mtls feature to support client authentication
4950

5051

5152
### Features implemented / improvements in 3.0

CREDITS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ Full contribution, see git log.
164164
* Jonas Schäfer
165165
- XMPP server patch
166166

167+
* Maurizio Siddu
168+
- added --mTLS feature
169+
167170
* Marcin Szychowski
168171
- Quick'n'dirty client certificate support
169172

doc/testssl.1.html

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

doc/testssl.1.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ The same can be achieved by setting the environment variable `WARNINGS`.
113113

114114
`--reqheader <header>` This can be used to add additional HTTP request headers in the correct format `Headername: headercontent`. This parameter can be called multiple times if required. For example: `--reqheader 'Proxy-Authorization: Basic dGVzdHNzbDpydWxlcw==' --reqheader 'ClientID: 0xDEADBEAF'`. REQHEADER is the corresponding environment variable.
115115

116+
`--mtls <path_to_client_cert>` This can be set to provide a file containing a client certificatete and a private key (not encrypted) in PEM format, which is used when a mutual TLS authentication is required by the remote server. MTLS is the equivalent environment variable.
117+
116118

117119
### SPECIAL INVOCATIONS
118120

testssl.sh

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ XMPP_HOST=""
389389
PROXYIP="" # $PROXYIP:$PROXPORT is your proxy if --proxy is defined ...
390390
PROXYPORT="" # ... and openssl has proxy support
391391
PROXY="" # Once check_proxy() executed it contains $PROXYIP:$PROXPORT
392+
MTLS="" # mTLS authentication with client certificate and private key
392393
VULN_COUNT=0
393394
SERVICE="" # Is the server running an HTTP server, SMTP, POP or IMAP?
394395
URI=""
@@ -2316,6 +2317,12 @@ s_client_options() {
23162317
fi
23172318
# $keyopts may be set as an environment variable to enable client authentication (see PR #1383)
23182319
tm_out "$options $keyopts"
2320+
2321+
# In case of mutual TLS authentication is required by the server
2322+
# Note: the PEM certificate file must contain: client certificate and certificate key (not encrypted)
2323+
if [[ -n "$MTLS" ]]; then
2324+
options+=" -cert $MTLS"
2325+
fi
23192326
}
23202327

23212328
###### check code starts here ######
@@ -2375,10 +2382,14 @@ service_detection() {
23752382
out " $SERVICE, thus skipping HTTP specific checks"
23762383
fileout "${jsonID}" "INFO" "$SERVICE, thus skipping HTTP specific checks"
23772384
;;
2378-
*) if [[ "$CLIENT_AUTH" == required ]]; then
2379-
out " certificate-based authentication => skipping all HTTP checks"
2380-
echo "certificate-based authentication => skipping all HTTP checks" >$TMPFILE
2381-
fileout "${jsonID}" "INFO" "certificate-based authentication => skipping all HTTP checks"
2385+
*) if [[ ! -z $MTLS ]]; then
2386+
out " not identified, but mTLS authentication is set ==> trying HTTP checks"
2387+
SERVICE=HTTP
2388+
fileout "${jsonID}" "DEBUG" "Couldn't determine service -- ASSUME_HTTP set"
2389+
elif [[ "$CLIENT_AUTH" == required ]] && [[ -z $MTLS ]]; then
2390+
out " certificate-based authentication without providing client certificate and private key => skipping all HTTP checks"
2391+
echo "certificate-based authentication without providing client certificate and private key => skipping all HTTP checks" >$TMPFILE
2392+
fileout "${jsonID}" "INFO" "certificate-based authentication without providing client certificate and private key => skipping all HTTP checks"
23822393
else
23832394
out " Couldn't determine what's running on port $PORT"
23842395
if "$ASSUME_HTTP"; then
@@ -2430,6 +2441,7 @@ run_http_header() {
24302441
local url redirect
24312442
local jsonID="HTTP_status_code"
24322443
local spaces=" "
2444+
local cert_option=""
24332445

24342446
HEADERFILE=$TEMPDIR/$NODEIP.http_header.txt
24352447
if [[ $NR_HEADER_FAIL -eq 0 ]]; then
@@ -2444,12 +2456,17 @@ run_http_header() {
24442456

24452457
pr_bold " HTTP Status Code "
24462458
[[ -z "$1" ]] && url="/" || url="$1"
2447-
tm_out "$GET_REQ11" | $OPENSSL s_client $(s_client_options "$OPTIMAL_PROTO $BUGS -quiet -ign_eof -connect $NODEIP:$PORT $PROXY $SNI") >$HEADERFILE 2>$ERRFILE &
2459+
2460+
# Set -cert option value if mTLS authentication is selected
2461+
if [[ ! -z "$MTLS" ]]; then
2462+
cert_option="-cert $MTLS"
2463+
fi
2464+
tm_out "$GET_REQ11" | $OPENSSL s_client $(s_client_options "$OPTIMAL_PROTO $BUGS $cert_option -quiet -ign_eof -connect $NODEIP:$PORT $PROXY $SNI") >$HEADERFILE 2>$ERRFILE &
24482465
wait_kill $! $HEADER_MAXSLEEP
24492466
if [[ $? -eq 0 ]]; then
24502467
# Issue HTTP GET again as it properly finished within $HEADER_MAXSLEEP and didn't hang.
24512468
# Doing it again in the foreground to get an accurate header time
2452-
tm_out "$GET_REQ11" | $OPENSSL s_client $(s_client_options "$OPTIMAL_PROTO $BUGS -quiet -ign_eof -connect $NODEIP:$PORT $PROXY $SNI") >$HEADERFILE 2>$ERRFILE
2469+
tm_out "$GET_REQ11" | $OPENSSL s_client $(s_client_options "$OPTIMAL_PROTO $BUGS $cert_option -quiet -ign_eof -connect $NODEIP:$PORT $PROXY $SNI") >$HEADERFILE 2>$ERRFILE
24532470
NOW_TIME=$(date "+%s")
24542471
HTTP_TIME=$(awk -F': ' '/^date:/ { print $2 } /^Date:/ { print $2 }' $HEADERFILE)
24552472
HTTP_AGE=$(awk -F': ' '/^[aA][gG][eE]: / { print $2 }' $HEADERFILE)
@@ -2601,7 +2618,7 @@ run_http_date() {
26012618
local spaces=" "
26022619
jsonID="HTTP_clock_skew"
26032620

2604-
if [[ $SERVICE != HTTP ]] || [[ "$CLIENT_AUTH" == required ]]; then
2621+
if [[ $SERVICE != HTTP ]] || { [[ "$CLIENT_AUTH" == required ]] && [[ -z "$MTLS" ]]; }; then
26052622
return 0
26062623
fi
26072624
if [[ ! -s $HEADERFILE ]]; then
@@ -6710,6 +6727,12 @@ sub_session_resumption() {
67106727
local sess_data=$(mktemp $TEMPDIR/sub_session_data_resumption.$NODEIP.XXXXXX)
67116728
local -a rw_line
67126729
local protocol="$1"
6730+
local cert_option=""
6731+
6732+
# Set -cert option value if mTLS authentication is selected
6733+
if [[ ! -z "$MTLS" ]]; then
6734+
cert_option="-cert $MTLS"
6735+
fi
67136736

67146737
if [[ "$2" == ID ]]; then
67156738
local byID=true
@@ -6721,7 +6744,8 @@ sub_session_resumption() {
67216744
return 1
67226745
fi
67236746
fi
6724-
[[ "$CLIENT_AUTH" == required ]] && return 6
6747+
# Return 6 if client authentication is required and none PEM file (containing client certificate+private key) is provided
6748+
[[ "$CLIENT_AUTH" == required ]] && [[ -z "$MTLS" ]] && return 6
67256749
if ! "$HAS_TLS13" && "$HAS_NO_SSL2"; then
67266750
addcmd+=" -no_ssl2"
67276751
else
@@ -6738,7 +6762,7 @@ sub_session_resumption() {
67386762
addcmd+=" $protocol"
67396763
fi
67406764

6741-
$OPENSSL s_client $(s_client_options "$STARTTLS $BUGS -connect $NODEIP:$PORT $PROXY $SNI $addcmd -sess_out $sess_data") </dev/null &>$tmpfile
6765+
$OPENSSL s_client $(s_client_options "$STARTTLS $BUGS -connect $NODEIP:$PORT $PROXY $SNI $cert_option $addcmd -sess_out $sess_data") </dev/null &>$tmpfile
67426766
ret1=$?
67436767
if [[ $ret1 -ne 0 ]]; then
67446768
# MacOS and LibreSSL return 1 here, that's why we need to check whether the handshake contains e.g. a certificate
@@ -6756,7 +6780,7 @@ sub_session_resumption() {
67566780
# [[ ! $(<$sess_data) =~ -----.*\ SSL\ SESSION\ PARAMETERS----- ]]
67576781
ret=2
67586782
else
6759-
$OPENSSL s_client $(s_client_options "$STARTTLS $BUGS -connect $NODEIP:$PORT $PROXY $SNI $addcmd -sess_in $sess_data") </dev/null >$tmpfile 2>$ERRFILE
6783+
$OPENSSL s_client $(s_client_options "$STARTTLS $BUGS -connect $NODEIP:$PORT $PROXY $SNI $cert_option $addcmd -sess_in $sess_data") </dev/null >$tmpfile 2>$ERRFILE
67606784
ret2=$?
67616785
if [[ $DEBUG -ge 2 ]]; then
67626786
echo -n "$ret1, $ret2, "
@@ -17037,9 +17061,9 @@ run_renego() {
1703717061
[[ $DEBUG -ge 1 ]] && out ", no renegotiation support in TLS 1.3 only servers"
1703817062
outln
1703917063
fileout "$jsonID" "OK" "not vulnerable, TLS 1.3 only" "$cve" "$cwe"
17040-
elif [[ "$CLIENT_AUTH" == required ]]; then
17041-
prln_warning "client x509-based authentication prevents this from being tested"
17042-
fileout "$jsonID" "WARN" "client x509-based authentication prevents this from being tested"
17064+
elif [[ "$CLIENT_AUTH" == required ]] && [[ -z "$MTLS" ]]; then
17065+
prln_warning "not having provided client certificate and private key file, the client x509-based authentication prevents this from being tested"
17066+
fileout "$jsonID" "WARN" "not having provided client certificate and private key file, the client x509-based authentication prevents this from being tested"
1704317067
sec_client_renego=1
1704417068
else
1704517069
# We will need $ERRFILE for mitigation detection
@@ -17210,7 +17234,7 @@ run_crime() {
1721017234
fileout "$jsonID" "OK" "not vulnerable" "$cve" "$cwe"
1721117235
fi
1721217236
else
17213-
if [[ $SERVICE == HTTP ]] || [[ "$CLIENT_AUTH" == required ]]; then
17237+
if [[ $SERVICE == HTTP ]] || [[ "$CLIENT_AUTH" == required ]] || [[ ! -z "$MTLS" ]]; then
1721417238
pr_svrty_high "VULNERABLE (NOT ok)"
1721517239
fileout "$jsonID" "HIGH" "VULNERABLE" "$cve" "$cwe" "$hint"
1721617240
else
@@ -17269,8 +17293,13 @@ sub_breach_helper() {
1726917293
local get_command="$1"
1727017294
local detected_compression=""
1727117295
local -i was_killed=0
17296+
local cert_option=""
1727217297

17273-
safe_echo "$get_command" | $OPENSSL s_client $(s_client_options "$OPTIMAL_PROTO $BUGS -quiet -ign_eof -connect $NODEIP:$PORT $PROXY $SNI") 1>$TMPFILE 2>$ERRFILE &
17298+
# Set -cert option value if mTLS authentication is selected
17299+
if [[ ! -z "$MTLS" ]]; then
17300+
cert_option="-cert $MTLS"
17301+
fi
17302+
safe_echo "$get_command" | $OPENSSL s_client $(s_client_options "$OPTIMAL_PROTO $BUGS $cert_option -quiet -ign_eof -connect $NODEIP:$PORT $PROXY $SNI") 1>$TMPFILE 2>$ERRFILE &
1727417303
wait_kill $! $HEADER_MAXSLEEP
1727517304
was_killed=$? # !=0 when it was killed
1727617305
detected_compression=$(grep -ia ^Content-Encoding: $TMPFILE)
@@ -17320,9 +17349,9 @@ run_breach() {
1732017349

1732117350
[[ $VULN_COUNT -le $VULN_THRESHLD ]] && outln && pr_headlineln " Testing for BREACH (HTTP compression) vulnerability " && outln
1732217351
pr_bold " BREACH"; out " ($cve) "
17323-
if [[ "$CLIENT_AUTH" == required ]]; then
17324-
prln_warning "client x509-based authentication prevents this from being tested"
17325-
fileout "$jsonID" "WARN" "client x509-based authentication prevents this from being tested" "$cve" "$cwe"
17352+
if [[ "$CLIENT_AUTH" == required ]] && [[ -z "$MTLS" ]]; then
17353+
prln_warning "not having provided client certificate and private key file, the client x509-based authentication prevents this from being tested"
17354+
fileout "$jsonID" "WARN" "not having provided client certificate and private key file, the client x509-based authentication prevents this from being tested" "$cve" "$cwe"
1732617355
return 7
1732717356
fi
1732817357

@@ -20507,6 +20536,7 @@ tuning / connect options (most also can be preset via environment variables):
2050720536
--ids-friendly skips a few vulnerability checks which may cause IDSs to block the scanning IP
2050820537
--phone-out allow to contact external servers for CRL download and querying OCSP responder
2050920538
--add-ca <CA files|CA dir> path to <CAdir> with *.pem or a comma separated list of CA files to include in trust check
20539+
--mtls <CLIENT CERT file> path to <CLIENT CERT> file, it must be in PEM format and contain client certificate with certificate key (not encrypted)
2051020540
--basicauth <user:pass> provide HTTP basic auth information.
2051120541
--reqheader <header> add custom http request headers
2051220542

@@ -23814,6 +23844,10 @@ parse_cmd_line() {
2381423844
OPENSSL_TIMEOUT="$(parse_opt_equal_sign "$1" "$2")"
2381523845
[[ $? -eq 0 ]] && shift
2381623846
;;
23847+
--mtls|--mtls=*)
23848+
MTLS="$(parse_opt_equal_sign "$1" "$2")"
23849+
[[ $? -eq 0 ]] && shift
23850+
;;
2381723851
--connect-timeout|--connect-timeout=*)
2381823852
CONNECT_TIMEOUT="$(parse_opt_equal_sign "$1" "$2")"
2381923853
[[ $? -eq 0 ]] && shift
@@ -23892,6 +23926,17 @@ parse_cmd_line() {
2389223926
grep -q 'BEGIN CERTIFICATE' "$fname" || fatal_cmd_line "\"$fname\" is not CA file in PEM format" $ERR_RESOURCE
2389323927
done
2389423928

23929+
# Check if mTLS has been selected, and if the correct client auth PEM file has been provided by user
23930+
if [[ ! -z "$MTLS" ]]; then
23931+
if [[ -f $MTLS ]]; then
23932+
grep -q 'BEGIN CERTIFICATE' "$MTLS" || fatal_cmd_line "\"$MTLS\" is not a client certificate file in PEM format" $ERR_RESOURCE
23933+
grep -q 'BEGIN PRIVATE KEY\|BEGIN RSA PRIVATE KEY' "$MTLS" || fatal_cmd_line "\"$MTLS\" the not encrypted private key is missing in the specified PEM file" $ERR_RESOURCE
23934+
MTLS=$MTLS
23935+
else
23936+
[[ -s "$MTLS" ]] || fatal_cmd_line "the specified client certificate file \"$MTLS\" does not exist" $ERR_RESOURCE
23937+
fi
23938+
fi
23939+
2389523940
"$FAST" && pr_warning "\n'--fast' can have some undesired side effects thus it is not recommended to use anymore\n"
2389623941
"$SSL_NATIVE" && pr_warning "\nusage of '--ssl-native' is not recommended as it will return incomplete and may even return incorrect results\n"
2389723942

0 commit comments

Comments
 (0)