summaryrefslogtreecommitdiff
path: root/tor-exit-ssl-check
blob: fb850112512a87a1b22199c10023c41bf62dc48e (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
#!/bin/bash

# Copyright (c) 2012, 2013 Peter Palfrader <peter@palfrader.org>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

set -e
set -u

shopt -s extglob

usage() {
	echo "Usage: $0 [-v [-v]] [-d <datadir>] -c [<cert-cachedir>] -C [<control socket>] <torserver-fpr> <targethost>[:<targetport>] [<targethost>[:<targetport>] [...]]"
	echo " o If torserver-fpr is -, a list of fingerprints is read from stdin"
	echo " o with -C set, this script will not launch a tor.  instead it will use the"
	echo "   specified control socket to manipulate an already running Tor as needed."
}

verbose=0
cachedir=""
datadir=""
controlsocket=""
while getopts "vhc:d:C:" OPTION
do
	case "$OPTION" in
		v)
			verbose=$((verbose + 1))
			;;
		h)
			usage
			exit 0
			;;
		c)
			cachedir="$OPTARG"
			;;
		d)
			datadir="$OPTARG"
			;;
		C)
			controlsocket="$OPTARG"
			;;
		*)
			usage >&2
			exit 1
	esac
done
shift $(($OPTIND - 1))

if [ "${1:-}" = "--help" ]; then
	usage
	exit 0
elif [ "$#" -lt 2 ]; then
	usage >&2
	exit 1
fi


torserver="$1"; shift
declare -a targets=("$@")
declare -a used_cache

mapaddr="192.0.2.1"


logpid=""
torpid=""
tmpdir=""

cleanup() {
	[ -z "$torpid" ] || kill "$torpid" || true
	[ -z "$logpid" ] || kill "$logpid" || true
	[ -z "$tmpdir" ] || rm -rf "$tmpdir" || true
}

tmpdir=$(mktemp -d "/tmp/cert-check-XXXXXX")
trap 'cleanup' EXIT

if [ -z "$controlsocket" ]; then

	pidfile="$tmpdir/pid"
	torlog="$tmpdir/log"
	if [ "$verbose" -gt 0 ]; then
		tail -F "$torlog" &
		logpid=$!
	fi

	if command -v tor > /dev/null; then
		tor="tor"
	elif [ -x /usr/sbin/tor ]; then
		tor="/usr/sbin/tor"
	else
		echo >&2 "Cannot find tor executable"
		exit 1
	fi

	if [ "$verbose" -gt 1 ]; then
		loglevel="info"
	else
		loglevel="notice"
	fi

	datadir=${datadir:-$tmpdir/tor}
	controlsocket="$tmpdir/sock"
	cat > "$tmpdir/torrc" << EOF
DataDirectory $datadir
RunAsDaemon 1
SocksPort auto
PidFile $pidfile
Log $loglevel file $torlog
SafeLogging 0
ControlSocket $controlsocket
StrictNodes 1
EOF

	mkdir -p -m 0700 "$datadir"
	if [ "$verbose" -gt 0 ]; then hush=""; else hush="--hush"; fi
	"$tor" $hush -f "$tmpdir/torrc"
	torpid="$(cat $pidfile)"
fi


eatdata() {
	local fn="$1"; shift
	if [ "$verbose" -gt 0 ]; then
		tee "$fn"
	else
		cat > "$fn"
	fi
}
expect_ok() {
	local line
	read code line <&${COPROC[0]}
	line=$(echo "$line" | tr -d '\r')
	if ! [ "$code" = "250" ]; then
		echo >&2 "Tor said '$code $line' - no idea what that means"
		exit 1
	fi
}

sanitize() {
	egrep -v '(Session-ID:|Master-Key:|Start Time:|SSL handshake has read)'
}

direct_fetch_one() {
	local i="$1"

	local host
	host="${targets[$i]}"

	[ "$verbose" = 0 ] || echo "Directly to $host:"
	openssl s_client -no_ticket -showcerts -connect "$host" < /dev/null 2>&1 | eatdata "$tmpdir/cert-direct-$i"
	[ -n "$cachedir" ] && cp "$tmpdir/cert-direct-$i" "$cachedir/tesc3-$host"
	used_cache[$i]=""
}

direct_fetch() {
	local i="$1"

	local host
	host="${targets[$i]}"

	if [ -n "$cachedir" ] && [ -e "$cachedir/tesc3-$host" ] ; then
		[ "$verbose" = 0 ] || echo "Using cached certificate for $host."
		cp "$cachedir/tesc3-$host" "$tmpdir/cert-direct-$i"
		used_cache[$i]="1"
	else
		direct_fetch_one "$i"
	fi

	sanitize < "$tmpdir/cert-direct-$i" > "$tmpdir/cert-direct-$i.filtered"
	[ "$verbose" = 0 ] || echo "===="
}


# set default port
for ((i=0; i < ${#targets[@]}; i++)); do
	host="${targets[$i]}"
	[ "${host%%+([0-9])}" = "$host" ] && targets[$i]="$host:443"
done

[ -n "$cachedir" ] && find "$cachedir" -name "tesc3-*" -mmin +1400 -exec rm '{}' '+'

for ((i=0; i < ${#targets[@]}; i++)); do
	direct_fetch "$i"
done



coproc socat UNIX-CONNECT:"$controlsocket" -
echo 'AUTHENTICATE' >&${COPROC[1]}
expect_ok

echo 'GETINFO net/listeners/socks' >&${COPROC[1]}
read line <&${COPROC[0]}
line=$(echo "$line" | tr -d '\r')
socksport=$( echo "$line" | sed -e 's#^250-net/listeners/socks="127.0.0.1:##; s#"$##' )
if [ "$socksport" = "$line" ] || echo "$socksport" | grep -q '[^0-9]'; then
	echo >&2 "Did not manage to learn listening port from tor."
	echo >&2 "Got '$line' and parsed it into '$socksport'."
	exit 1
fi
expect_ok

cat > "$tmpdir/torsocks.conf" << EOF
server = 127.0.0.1
server_port = $socksport
EOF

errors=0
while : ; do
	if [ "$torserver" = "-" ]; then
		read server
		[ -n "$server" ] || break
	else
		server="$torserver"
	fi
	server="${server// /}"

	# de-base64 if needed
	if [ "$(echo "$server" | wc -c)" = 28 ] ; then
		server="$(echo "$server" | perl -MMIME::Base64 -e 'print unpack("H*", decode_base64(<>))')"
	fi

	[ "$verbose" = 0 ] || echo "Setting ExitNodes $server"
	echo "RESETCONF ExitNodes" >&${COPROC[1]}
	expect_ok
	echo "SETCONF ExitNodes=\$$server" >&${COPROC[1]}
	expect_ok

	for ((i=0; i < ${#targets[@]}; i++)); do
		host="${targets[$i]}"

		rm -f "$tmpdir/cert-tor-$i" "$tmpdir/cert-tor-$i.filtered"

		[ "$verbose" = 0 ] || echo "Via $server to $host:"
		TORSOCKS_CONF_FILE="$tmpdir/torsocks.conf" torify openssl s_client -no_ticket -showcerts -connect "$host" < /dev/null 2>&1 | eatdata "$tmpdir/cert-tor-$i"
		sanitize < "$tmpdir/cert-tor-$i" > "$tmpdir/cert-tor-$i.filtered"

		if [ -n "used_cache[$i]" ] && ! diff "$tmpdir/cert-direct-$i.filtered" "$tmpdir/cert-tor-$i.filtered" > /dev/null; then
			[ "$verbose" = 0 ] || echo "Fetching a non-cached copy of $host's cert."
			direct_fetch_one "$i"
			sanitize < "$tmpdir/cert-direct-$i" > "$tmpdir/cert-direct-$i.filtered"
		fi

		if diff "$tmpdir/cert-direct-$i.filtered" "$tmpdir/cert-tor-$i.filtered" > /dev/null; then
			echo "RESULT[$host]: $server: No real differences."
			[ "$verbose" = 0 ] || diff -U100 "$tmpdir/cert-direct-$i" "$tmpdir/cert-tor-$i" || true
		elif egrep '^connect:errno=' "$tmpdir/cert-tor-$i" > /dev/null; then
			[ "$verbose" -lt 1 ] || diff -U100 "$tmpdir/cert-direct-$i" "$tmpdir/cert-tor-$i" || true
			echo "RESULT[$host]: $server: Connect failed"
			errors=$((errors | 0x02))
		elif egrep '^[0-9]*:error:.*:ssl handshake failure:' "$tmpdir/cert-tor-$i" > /dev/null; then
			[ "$verbose" -lt 1 ] || diff -U100 "$tmpdir/cert-direct-$i" "$tmpdir/cert-tor-$i" || true
			echo "RESULT[$host]: $server: SSL Handshake failed"
			errors=$((errors | 0x04))
		else
			echo "RESULT[$host]: $server: differences!"
			[ "$verbose" = 0 ] || echo "===="
			[ "$verbose" = 0 ] || echo "Diff:"
			diff -U100 "$tmpdir/cert-direct-$i" "$tmpdir/cert-tor-$i" || true
			errors=$((errors | 0x08))
		fi
	done

	[ "$torserver" = "-" ] || break
done
exit "$errors"