-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_authentication_session.sh
executable file
·410 lines (316 loc) · 18.9 KB
/
test_authentication_session.sh
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
##################################################################################
# The MIT License (MIT)
#
# Copyright (c) 2015 Bertrand Martel
#
# 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.
###################################################################################
#!/bin/bash
#title : digest_test.sh
#author : Bertrand Martel
#date : 02/09/2015
#description : test digest authentication / digest session / cookie session
############################################################################
# FOR DIGEST AUTHENTICATION - COOKIE SESSION
# 1) Test authentication success
# 2) Test cookie hsid is enabled
# 3) Test cookie hsid is not valid
# 4) Test opaque invalid
# 5) Test digest-uri invalid
# 6) Test nonce count invalid
# 7) Test nonce count > 1
# 8) Test cookie lifetime [ TESTED MANUALLY ]
# 9) Test nonce lifetime [ TESTED MANUALLY ]
# FOR DIGEST AUTHENTICATION - DIGEST SESSION
# 1) Test authentication success
# 2) Test opaque invalid
# 3) Test digest-uri invalid
# 4) Test nonce count invalid
# 5) Test nonce count > 1
# 6) Test 5 consecutive session key with different nonce count
# 7) Test cookie lifetime [ TESTED MANUALLY ]
# 8) Test nonce lifetime [ TESTED MANUALLY ]
verbose=false
if [ "$1" == "-v" ]; then
verbose=true
fi
protocol="http"
uri="/login"
username="test"
password="test"
method="GET"
cnonce="12345"
test_success=0
test_count=7
function test_success {
test_success=$((test_success + 1))
echo -e "\x1B[01;32m[ ${test_success} / ${test_count} ] [SUCCESS] $1 \x1B[0m"
}
function test_error {
echo -e "\x1B[31m$1 [FAILURE] \x1B[0m"
}
function launch_request {
request_headers=`curl -sk -D - ${protocol}://127.0.0.1:4343/login | grep 'WWW-Authenticate\|Set-Cookie: HSID'`
request_no_header_authenticate=`echo ${request_headers} | grep WWW-Authenticate`
request_no_header_cookie=`echo ${request_headers} | grep Cookie`
if [[ -z $request_no_header_authenticate ]]; then
echo "Expected response with WWW-authenticate header. Exiting ...."
exit 0;
elif [[ -z $request_no_header_cookie ]]; then
echo "Expected response with Cookie header. Exiting ...."
exit 0;
fi
realm=`echo $request_no_header_authenticate | grep -Eo 'realm="[a-zA-Z0-9=\+\/]+"' | sed -e 's/realm="//g' | sed -e 's/"//g'`
nonce=`echo $request_no_header_authenticate | grep -Eo 'nonce="[a-zA-Z0-9=\+\/]+"' | sed -e 's/nonce="//g' | sed -e 's/"//g'`
opaque=`echo $request_no_header_authenticate | grep -Eo 'opaque="[a-zA-Z0-9=\+\/]+"' | sed -e 's/opaque="//g' | sed -e 's/"//g'`
hsid=`echo $request_no_header_cookie | grep -oE 'HSID=[a-zA-Z0-9\/=\+]+' | sed -e 's/HSID=//g'`
current_date=`date +%T`
if $verbose ; then
echo "#####################################"
echo "[$current_date] Received response from server with : "
echo "[$current_date] realm : $realm"
echo "[$current_date] nonce : $nonce"
echo "[$current_date] opaque : $opaque"
echo "[$current_date] hsid : $hsid"
echo "#####################################"
fi
first_part=`echo -n "${username}:${realm}:${password}" | $1 | grep -oE '\w+'`
last_part=`echo -n "${method}:${uri}" | $1 | grep -oE '\w+'`
}
function launch_cookie_digest_auth_request {
######################################################################################################################################
########################### TEST 1 : Test authentication success #####################################################################
######################################################################################################################################
test_name="Test authentication success"
launch_request "$1"
response_digest=`echo -n "${first_part}:${nonce}:00000001:${cnonce}:auth-int:${last_part}" | $1 | grep -oE '\w+'`
authentication_request=`curl -sk -D - --header "Authorization: algorithm="MD5",opaque="${opaque}",digest-uri="${uri}",username="${username}",qop="auth-int",nc=00000001,response=${response_digest},cnonce=${cnonce}" --header "Cookie: HSID=${hsid}; Domain=127.0.0.1; Path=/; HttpOnly" ${protocol}://127.0.0.1:4343${uri} | grep "HTTP/1.1 200"`
if [[ ! -z $authentication_request ]]; then
test_success "${test_name}"
else
test_error "${test_name}"
exit 0;
fi
######################################################################################################################################
########################### TEST 2 : Test cookie hsid is valid## #####################################################################
######################################################################################################################################
test_name="Test cookie hsid is valid"
request_headers=`curl -sk -D - --header "Cookie: HSID=${hsid}; Domain=127.0.0.1; Path=/; HttpOnly" ${protocol}://127.0.0.1:4343/login | grep "HTTP/1.1 200"`
if [[ ! -z $request_headers ]]; then
test_success "${test_name}"
else
test_error "${test_name}"
exit 0;
fi
######################################################################################################################################
########################### TEST 3 : Test cookie hsid is not valid ##################################################################
######################################################################################################################################
test_name="Test cookie hsid is not valid"
request_headers=`curl -sk -D - --header "Cookie: HSID=${hsid}FAKE; Domain=127.0.0.1; Path=/; HttpOnly" ${protocol}://127.0.0.1:4343/login | grep "HTTP/1.1 401"`
if [[ ! -z $request_headers ]]; then
test_success "${test_name}"
else
test_error "${test_name}"
exit 0;
fi
######################################################################################################################################
########################### TEST 4 : Test opaque invalid #############################################################################
######################################################################################################################################
test_name="Test opaque invalid"
launch_request "$1"
response_digest=`echo -n "${first_part}:${nonce}:00000001:${cnonce}:auth-int:${last_part}" | $1 | grep -oE '\w+'`
authentication_request=`curl -sk -D - --header "Authorization: algorithm="MD5",opaque="${opaque}FAKE",digest-uri="${uri}",username="${username}",qop="auth-int",nc=00000001,response=${response_digest},cnonce=${cnonce}" --header "Cookie: HSID=${hsid}; Domain=127.0.0.1; Path=/; HttpOnly" ${protocol}://127.0.0.1:4343${uri} | grep "HTTP/1.1 401"`
if [[ ! -z $authentication_request ]]; then
test_success "${test_name}"
else
test_error "${test_name}"
exit 0;
fi
######################################################################################################################################
########################### TEST 5 : Test digest-uri invalid ########################################################################
######################################################################################################################################
test_name="Test digest-uri invalid"
launch_request "$1"
response_digest=`echo -n "${first_part}:${nonce}:00000001:${cnonce}:auth-int:${last_part}" | $1 | grep -oE '\w+'`
authentication_request=`curl -sk -D - --header "Authorization: algorithm="MD5",opaque="${opaque}",digest-uri="${uri}FAKE",username="${username}",qop="auth-int",nc=00000001,response=${response_digest},cnonce=${cnonce}" --header "Cookie: HSID=${hsid}; Domain=127.0.0.1; Path=/; HttpOnly" ${protocol}://127.0.0.1:4343${uri} | grep "HTTP/1.1 401"`
if [[ ! -z $authentication_request ]]; then
test_success "${test_name}"
else
test_error "${test_name}"
exit 0;
fi
######################################################################################################################################
########################### TEST 6 : Test nonce count invalid ########################################################################
######################################################################################################################################
test_name="Test nonce count invalid"
launch_request "$1"
response_digest=`echo -n "${first_part}:${nonce}:00000001:${cnonce}:auth-int:${last_part}" | $1 | grep -oE '\w+'`
authentication_request=`curl -sk -D - --header "Authorization: algorithm="MD5",opaque="${opaque}",digest-uri="${uri}",username="${username}",qop="auth-int",nc=00000009,response=${response_digest},cnonce=${cnonce}" --header "Cookie: HSID=${hsid}; Domain=127.0.0.1; Path=/; HttpOnly" ${protocol}://127.0.0.1:4343${uri} | grep "HTTP/1.1 401"`
if [[ ! -z $authentication_request ]]; then
test_success "${test_name}"
else
test_error "${test_name}"
exit 0;
fi
######################################################################################################################################
########################### TEST 7 : Test nonce count > 1 ############################################################################
######################################################################################################################################
test_name="Test nonce count > 1"
response_digest=`echo -n "${first_part}:${nonce}:00000002:${cnonce}:auth-int:${last_part}" | $1 | grep -oE '\w+'`
authentication_request=`curl -sk -D - --header "Authorization: algorithm="MD5",opaque="${opaque}",digest-uri="${uri}",username="${username}",qop="auth-int",nc=00000002,response=${response_digest},cnonce=${cnonce}" --header "Cookie: HSID=${hsid}; Domain=127.0.0.1; Path=/; HttpOnly" ${protocol}://127.0.0.1:4343${uri} | grep "HTTP/1.1 200"`
if [[ ! -z $authentication_request ]]; then
test_success "${test_name}"
else
test_error "${test_name}"
exit 0;
fi
}
function launch_request_digest_session {
request_headers=`curl -sk -D - ${protocol}://127.0.0.1:4343/login | grep 'WWW-Authenticate'`
request_no_header_authenticate=`echo ${request_headers} | grep WWW-Authenticate`
if [[ -z $request_no_header_authenticate ]]; then
echo "Expected response with WWW-authenticate header. Exiting ...."
exit 0;
fi
realm=`echo $request_no_header_authenticate | grep -Eo 'realm="[a-zA-Z0-9=\+\/]+"' | sed -e 's/realm="//g' | sed -e 's/"//g'`
nonce=`echo $request_no_header_authenticate | grep -Eo 'nonce="[a-zA-Z0-9=\+\/]+"' | sed -e 's/nonce="//g' | sed -e 's/"//g'`
opaque=`echo $request_no_header_authenticate | grep -Eo 'opaque="[a-zA-Z0-9=\+\/]+"' | sed -e 's/opaque="//g' | sed -e 's/"//g'`
current_date=`date +%T`
if $verbose ; then
echo "#####################################"
echo "[$current_date] Received response from server with : "
echo "[$current_date] realm : $realm"
echo "[$current_date] nonce : $nonce"
echo "[$current_date] opaque : $opaque"
echo "#####################################"
fi
first_part=`echo -n "${username}:${realm}:${password}" | $1 | grep -oE '\w+'`
first_part="$first_part:${nonce}:${cnonce}"
last_part=`echo -n "${method}:${uri}" | $1 | grep -oE '\w+'`
}
function launch_digest_session_digest_auth_request {
######################################################################################################################################
########################### TEST 1 : Test authentication success #####################################################################
######################################################################################################################################
test_name="Test authentication success"
launch_request_digest_session "$1"
response_digest=`echo -n "${first_part}:${nonce}:00000001:${cnonce}:auth-int:${last_part}" | $1 | grep -oE '\w+'`
authentication_request=`curl -sk -D - --header "Authorization: algorithm="MD5",opaque="${opaque}",digest-uri="${uri}",username="${username}",qop="auth-int",nc=00000001,response=${response_digest},cnonce=${cnonce}" ${protocol}://127.0.0.1:4343${uri} | grep "HTTP/1.1 200"`
if [[ ! -z $authentication_request ]]; then
test_success "${test_name}"
else
test_error "${test_name}"
exit 0;
fi
######################################################################################################################################
########################### TEST 2 : Test opaque invalid #############################################################################
######################################################################################################################################
test_name="Test opaque invalid"
launch_request_digest_session "$1"
response_digest=`echo -n "${first_part}:${nonce}:00000001:${cnonce}:auth-int:${last_part}" | $1 | grep -oE '\w+'`
authentication_request=`curl -sk -D - --header "Authorization: algorithm="MD5",opaque="${opaque}FAKE",digest-uri="${uri}",username="${username}",qop="auth-int",nc=00000001,response=${response_digest},cnonce=${cnonce}" ${protocol}://127.0.0.1:4343${uri} | grep "HTTP/1.1 401"`
if [[ ! -z $authentication_request ]]; then
test_success "${test_name}"
else
test_error "${test_name}"
exit 0;
fi
######################################################################################################################################
########################### TEST 3 : Test digest-uri invalid ########################################################################
######################################################################################################################################
test_name="Test digest-uri invalid"
launch_request_digest_session "$1"
response_digest=`echo -n "${first_part}:${nonce}:00000001:${cnonce}:auth-int:${last_part}" | $1 | grep -oE '\w+'`
authentication_request=`curl -sk -D - --header "Authorization: algorithm="MD5",opaque="${opaque}",digest-uri="${uri}FAKE",username="${username}",qop="auth-int",nc=00000001,response=${response_digest},cnonce=${cnonce}" ${protocol}://127.0.0.1:4343${uri} | grep "HTTP/1.1 401"`
if [[ ! -z $authentication_request ]]; then
test_success "${test_name}"
else
test_error "${test_name}"
exit 0;
fi
######################################################################################################################################
########################### TEST 4 : Test nonce count invalid ########################################################################
######################################################################################################################################
test_name="Test nonce count invalid"
launch_request_digest_session "$1"
response_digest=`echo -n "${first_part}:${nonce}:00000001:${cnonce}:auth-int:${last_part}" | $1 | grep -oE '\w+'`
authentication_request=`curl -sk -D - --header "Authorization: algorithm="MD5",opaque="${opaque}",digest-uri="${uri}",username="${username}",qop="auth-int",nc=00000009,response=${response_digest},cnonce=${cnonce}" ${protocol}://127.0.0.1:4343${uri} | grep "HTTP/1.1 401"`
if [[ ! -z $authentication_request ]]; then
test_success "${test_name}"
else
test_error "${test_name}"
exit 0;
fi
######################################################################################################################################
########################### TEST 5 : Test nonce count > 1 ############################################################################
######################################################################################################################################
test_name="Test nonce count > 1"
response_digest=`echo -n "${first_part}:${nonce}:00000002:${cnonce}:auth-int:${last_part}" | $1 | grep -oE '\w+'`
authentication_request=`curl -sk -D - --header "Authorization: algorithm="MD5",opaque="${opaque}",digest-uri="${uri}",username="${username}",qop="auth-int",nc=00000002,response=${response_digest},cnonce=${cnonce}" ${protocol}://127.0.0.1:4343${uri} | grep "HTTP/1.1 200"`
if [[ ! -z $authentication_request ]]; then
test_success "${test_name}"
else
test_error "${test_name}"
exit 0;
fi
######################################################################################################################################
########################### TEST 6 : Test 5 consecutive session key with different nonce count #######################################
######################################################################################################################################
test_name="Test 5 consecutive session key with different nonce count"
launch_request_digest_session "$1"
nonce_count=1
for i in {1..5}
do
response_digest=`echo -n "${first_part}:${nonce}:0000000${nonce_count}:${cnonce}:auth-int:${last_part}" | $1 | grep -oE '\w+'`
authentication_request=`curl -sk -D - --header "Authorization: algorithm="MD5",opaque="${opaque}",digest-uri="${uri}",username="${username}",qop="auth-int",nc=0000000${nonce_count},response=${response_digest},cnonce=${cnonce}" ${protocol}://127.0.0.1:4343${uri} | grep "HTTP/1.1 200"`
if [[ -z $authentication_request ]]; then
test_error "${test_name}"
exit 0;
fi
nonce_count=$((nonce_count+1))
done
test_success "${test_name}"
}
algo="MD5"
session_type="COOKIE"
if [ "$1" == "MD5" ] || [ "$1" == "SHA1" ]; then
algo="$1"
fi
if [ "$2" == "DIGEST" ] || [ "$2" == "COOKIE" ]; then
session_type="$2"
fi
echo "#######################################################"
if [ "$algo" == "MD5" ] && [ "$session_type" == "COOKIE" ]; then
echo "DIGEST AUTHENTICATION WITH COOKIE SESSION - ALGO => MD5"
launch_cookie_digest_auth_request "md5sum"
elif [ "$algo" == "SHA1" ] && [ "$session_type" == "COOKIE" ]; then
echo "DIGEST AUTHENTICATION WITH COOKIE SESSION - ALGO => SHA1"
launch_cookie_digest_auth_request "sha1sum"
elif [ "$algo" == "MD5" ] && [ "$session_type" == "DIGEST" ]; then
test_count=6
launch_digest_session_digest_auth_request "md5sum"
elif [ "$algo" == "SHA1" ] && [ "$session_type" == "DIGEST" ]; then
test_count=6
launch_digest_session_digest_auth_request "sha1sum"
else
echo "Usage : test_authentication_session.sh <algo> <session_type>"
echo "<algo> : MD5 | SHA1 "
echo "<session_type> : COOKIE | DIGEST "
fi
echo "#######################################################"