2
0
mirror of https://github.com/mozilla/cipherscan.git synced 2024-11-17 04:13:40 +01:00
cipherscan/CiphersScan.sh

231 lines
6.4 KiB
Bash
Raw Normal View History

2013-07-17 20:49:22 +02:00
#!/usr/bin/env bash
DOBENCHMARK=0
BENCHMARKITER=30
OPENSSLBIN="./openssl"
2013-11-20 15:30:52 +01:00
#OPENSSLBIN="/usr/bin/openssl"
TIMEOUT=10
CIPHERSUITE="ALL:COMPLEMENTOFALL"
2013-12-09 16:40:23 +01:00
TARGET=$1
VERBOSE=0
ALLCIPHERS=0
OUTPUTFORMAT="terminal"
2013-07-17 20:49:22 +02:00
REQUEST="GET / HTTP/1.1
Host: $TARGET
Connection: close
2013-07-17 20:49:22 +02:00
"
2013-12-09 16:40:23 +01:00
usage() {
echo -e "usage: $0 <target:port>
$0 attempts to connect to a target site using all the ciphersuites it knowns.
Julien Vehent [:ulfr] - https://github.com/jvehent/cipherscan
example: $ ./CipherScan.sh www.google.com:443
Use only one of the options below:
-v\tincrease verbosity
-a\ttest all known ciphers individually at the end
-json\toutput results in json format
OpenSSL path can be changed in the OPENSSLBIN variable
Benchmarking can be enabled in the DOBENCHMARK variable
"
exit 1
}
2013-07-17 20:49:22 +02:00
verbose() {
if [ $VERBOSE -eq 1 ];then
echo $@
fi
}
# Connect to a target host with the selected ciphersuite
test_cipher_on_target() {
local sslcommand=$@
2013-11-20 16:30:45 +01:00
cipher=""
protocols=""
pfs=""
for tls_version in "-ssl2" "-ssl3" "-tls1" "-tls1_1" "-tls1_2"
do
local tmp=$(mktemp)
$sslcommand $tls_version 1>"$tmp" 2>/dev/null << EOF
2013-07-17 20:49:22 +02:00
$REQUEST
EOF
2013-11-20 16:30:45 +01:00
current_cipher=$(grep "New, " $tmp|awk '{print $5}')
current_pfs=$(grep 'Server Temp Key' $tmp|awk '{print $4$5$6$7}')
current_protocol=$(grep -E "^\s+Protocol\s+:" $tmp|awk '{print $3}')
if [[ -z "$current_protocol" || "$current_cipher" == '(NONE)' ]]; then
# connection failed, try again with next TLS version
continue
fi
# connection succeeded, add TLS version to positive results
if [ -z "$protocols" ]; then
protocols=$current_protocol
else
protocols="$protocols,$current_protocol"
fi
cipher=$current_cipher
pfs=$current_pfs
# grab the cipher and PFS key size
rm "$tmp"
done
# if cipher is empty, that means none of the TLS version worked with
# the current cipher
if [ -z "$cipher" ]; then
verbose "handshake failed, no ciphersuite was returned"
result='ConnectionFailure'
return 2
2013-11-20 16:30:45 +01:00
# if cipher contains NONE, the cipher wasn't accepted
elif [ "$cipher" == '(NONE) ' ]; then
result="$cipher $protocols $pfs"
2013-07-17 20:49:22 +02:00
verbose "handshake failed, server returned ciphersuite '$result'"
return 1
2013-11-20 16:30:45 +01:00
# the connection succeeded
2013-07-17 20:49:22 +02:00
else
2013-11-20 16:30:45 +01:00
result="$cipher $protocols $pfs"
2013-07-17 20:49:22 +02:00
verbose "handshake succeeded, server returned ciphersuite '$result'"
return 0
fi
}
# Calculate the average handshake time for a specific ciphersuite
bench_cipher() {
local ciphersuite="$1"
local sslcommand="timeout $TIMEOUT $OPENSSLBIN s_client -connect $TARGET -cipher $ciphersuite"
2013-07-17 20:49:22 +02:00
local t="$(date +%s%N)"
verbose "Benchmarking handshake on '$TARGET' with ciphersuite '$ciphersuite'"
for i in $(seq 1 $BENCHMARKITER); do
$sslcommand 2>/dev/null 1>/dev/null << EOF
$REQUEST
EOF
if [ $? -gt 0 ]; then
break
fi
2013-07-17 20:49:22 +02:00
done
# Time interval in nanoseconds
local t="$(($(date +%s%N) - t))"
verbose "Benchmarking done in $t nanoseconds"
# Microseconds
cipherbenchms="$((t/1000/$BENCHMARKITER))"
2013-07-17 20:49:22 +02:00
}
# Connect to the target and retrieve the chosen cipher
2013-11-20 16:30:45 +01:00
# recursively until the connection fails
2013-07-17 20:49:22 +02:00
get_cipher_pref() {
2013-12-09 16:16:45 +01:00
[ "$OUTPUTFORMAT" == "terminal" ] && echo -n '.'
2013-07-17 20:49:22 +02:00
local ciphersuite="$1"
local sslcommand="timeout $TIMEOUT $OPENSSLBIN s_client -connect $TARGET -cipher $ciphersuite"
2013-07-17 20:49:22 +02:00
verbose "Connecting to '$TARGET' with ciphersuite '$ciphersuite'"
test_cipher_on_target "$sslcommand"
local success=$?
# If the connection succeeded with the current cipher, benchmark and store
if [ $success -eq 0 ]; then
2013-11-20 16:30:45 +01:00
cipherspref=("${cipherspref[@]}" "$result")
2013-08-07 16:40:03 +02:00
pciph=$(echo $result|awk '{print $1}')
get_cipher_pref "!$pciph:$ciphersuite"
2013-07-17 20:49:22 +02:00
return 0
fi
}
2013-12-09 16:40:23 +01:00
2013-12-09 16:16:45 +01:00
display_results_in_terminal() {
# Display the results
ctr=1
for cipher in "${cipherspref[@]}"; do
pciph=$(echo $cipher|awk '{print $1}')
if [ $DOBENCHMARK -eq 1 ]; then
bench_cipher "$pciph"
r="$ctr $cipher $cipherbenchms"
else
r="$ctr $cipher"
fi
results=("${results[@]}" "$r")
ctr=$((ctr+1))
done
if [ $DOBENCHMARK -eq 1 ]; then
header="prio ciphersuite protocols pfs_keysize avg_handshake_microsec"
else
header="prio ciphersuite protocols pfs_keysize"
fi
ctr=0
for result in "${results[@]}"; do
if [ $ctr -eq 0 ]; then
echo $header
ctr=$((ctr+1))
fi
echo $result|grep -v '(NONE)'
done|column -t
}
2013-12-09 16:40:23 +01:00
2013-12-09 16:16:45 +01:00
display_results_in_json() {
# Display the results in json
ctr=0
echo -n "{\"target\":\"$TARGET\",\"date\":\"$(date -R)\",\"ciphersuite\": ["
for cipher in "${cipherspref[@]}"; do
[ $ctr -gt 0 ] && echo -n ','
echo -n "{\"cipher\":\"$(echo $cipher|awk '{print $1}')\","
echo -n "\"protocols\":[\"$(echo $cipher|awk '{print $2}'|sed 's/,/","/g')\"],"
pfs=$(echo $cipher|awk '{print $3}')
[ "$pfs" == "" ] && pfs="None"
echo -n "\"pfs\":\"$pfs\"}"
ctr=$((ctr+1))
done
echo ']}'
}
2013-07-17 20:49:22 +02:00
2013-12-09 16:40:23 +01:00
[[ -z $1 || "$1" == "-h" || "$1" == "--help" ]] && usage
2013-07-17 20:49:22 +02:00
if [ ! -z $2 ]; then
if [ "$2" == "-v" ]; then
VERBOSE=1
echo "Loading $($OPENSSLBIN ciphers -v $CIPHERSUITE 2>/dev/null|grep Kx|wc -l) ciphersuites from $(echo -n $($OPENSSLBIN version 2>/dev/null))"
2013-07-17 20:49:22 +02:00
$OPENSSLBIN ciphers ALL 2>/dev/null
2013-12-09 16:40:23 +01:00
elif [ "$2" == "-a" ]; then
ALLCIPHERS=1
2013-12-09 16:40:23 +01:00
elif [ "$2" == "-json" ]; then
2013-12-09 16:16:45 +01:00
OUTPUTFORMAT="json"
2013-12-09 16:40:23 +01:00
else
echo "ERROR: unknown option '$2'"; echo
usage
2013-12-09 16:16:45 +01:00
fi
2013-07-17 20:49:22 +02:00
fi
cipherspref=();
results=()
# Call to the recursive loop that retrieves the cipher preferences
get_cipher_pref $CIPHERSUITE
2013-07-17 20:49:22 +02:00
2013-12-09 16:16:45 +01:00
if [ "$OUTPUTFORMAT" == "json" ]; then
display_results_in_json
else
2013-12-09 16:16:45 +01:00
echo
display_results_in_terminal
fi
2013-11-20 16:47:59 +01:00
# If asked, test every single cipher individually
if [ $ALLCIPHERS -gt 0 ]; then
echo; echo "All accepted ciphersuites"
2013-11-20 16:47:59 +01:00
for c in $($OPENSSLBIN ciphers -v ALL:COMPLEMENTOFALL 2>/dev/null |awk '{print $1}'|sort|uniq); do
r="fail"
osslcommand="timeout $TIMEOUT $OPENSSLBIN s_client -connect $TARGET -cipher $c"
test_cipher_on_target "$osslcommand"
2013-11-20 16:47:59 +01:00
if [ $? -eq 0 ]; then
r="pass"
fi
2013-11-20 16:47:59 +01:00
echo "$c $r"|awk '{printf "%-35s %s\n",$1,$2}'
done
fi