-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathjwtcat.py
executable file
·318 lines (259 loc) · 9.48 KB
/
jwtcat.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
#!/usr/bin/env python3
# Copyright (C) 2017 - 2020 Alexandre Teyar
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import json
import logging
import os
import signal
import sys
import time
from datetime import datetime, timedelta
from itertools import chain, product
import coloredlogs
import jwt
from tqdm import tqdm
logger = logging.getLogger(__name__)
coloredlogs.install(level='DEBUG', milliseconds=True)
def parse_args():
"""This function parses the command line.
Returns:
[object] -- The parsed arguments
"""
parser = argparse.ArgumentParser(
description="A CPU-based JSON Web Token (JWT) cracker",
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
subparsers = parser.add_subparsers(
dest='attack_mode',
title="Attack-mode"
)
subparsers.required = True
brute_force_subparser = subparsers.add_parser(
"brute-force",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
brute_force_subparser.add_argument(
"-c", "--charset",
default="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
dest="charset",
help="User-defined charset",
type=str,
required=False,
)
brute_force_subparser.add_argument(
"--increment-min",
default=1,
dest="increment_min",
help="Start incrementing at X",
type=int,
required=False,
)
brute_force_subparser.add_argument(
"--increment-max",
default=8,
dest="increment_max",
help="Stop incrementing at X",
type=int,
required=False,
)
cve_subparser = subparsers.add_parser(
"vulnerable",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
wordlist__subparser = subparsers.add_parser(
"wordlist",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
# Set the UTF-8 encoding and ignore error mode to avoid issues with the wordlist
wordlist__subparser.add_argument(
"-w", "--wordlist",
default=argparse.SUPPRESS,
dest="wordlist",
help="Wordlist of private key candidates",
required=True,
type=argparse.FileType(
'r',
encoding='UTF-8',
errors='ignore'
)
)
parser.add_argument(
"-lL", "--log-level",
default=logging.INFO,
dest="log_level",
# TODO: Improve how to retrieve all log levels
choices=[
'DEBUG',
'INFO',
],
help="Set the logging level",
type=str,
required=False,
)
parser.add_argument(
"-o", "--outfile",
dest="outfile",
help="Define outfile for recovered private keys",
required=False,
type=argparse.FileType(
'w+',
encoding='UTF-8',
errors='ignore'
)
)
parser.add_argument(
"--potfile-disable",
action='store_true',
default=False,
dest="potfile_disable",
help="Do not write potfile",
required=False,
)
parser.add_argument(
"--potfile-path",
default='jwtpot.potfile',
dest="potfile",
help="Specific path to potfile",
required=False,
type=argparse.FileType(
'a+',
encoding='UTF-8',
errors='ignore'
)
)
# parser.add_argument(
# "-tF", "--jwt-file",
# default=argparse.SUPPRESS,
# dest="token_file",
# help="File with JSON Web Tokens to attack",
# required=False,
# type=argparse.FileType(
# 'r',
# encoding='UTF-8',
# errors='ignore'
# )
# )
parser.add_argument(
default=argparse.SUPPRESS,
dest="token",
help="JSON Web Token to attack",
type=str
)
return parser.parse_args()
def bruteforce(charset, minlength, maxlength):
"""This function generates all the different possible combination in a given character space.
Arguments:
charset {string} -- The charset used to generate all possible candidates
minlength {integer} -- The minimum length for candiates generation
maxlength {integer} -- The maximum length for candiates generation
Returns:
[type] -- All the possible candidates
"""
return (''.join(candidate)
for candidate in chain.from_iterable(product(charset, repeat=i)
for i in range(minlength, maxlength + 1)))
def run(token, candidate):
"""This function checks if a candidate can decrypt a JWT token.
Arguments:
token {string} -- An encrypted JWT token to test
candidate {string} -- A candidate word for decoding
Returns:
[boolean] -- Result of the decoding attempt
"""
try:
payload = jwt.decode(token, candidate, algorithms=["HS512", "HS256", "H384"])
return True
except jwt.exceptions.DecodeError:
logger.debug(f"DecodingError: {candidate}")
return False
except jwt.exceptions.InvalidTokenError:
logger.debug(f"InvalidTokenError: {candidate}")
return False
except Exception as ex:
logger.exception(f"Exception: {ex}")
sys.exit(1)
def is_vulnerable(args):
"""This function checks a JWT token against a well-known vulnerabilities.
Arguments:
args {object} -- The command-line arguments
"""
headers = jwt.get_unverified_header(args.token)
if headers['alg'] == "HS256":
logging.info("JWT vulnerable to HS256 guessing attacks")
elif headers['alg'] == "HS512":
logging.info("JWT vulnerable to HS512 guessing attacks")
elif headers['alg'] == "HS384":
logging.info("JWT vulnerable to HS384 guessing attacks")
elif headers['alg'] == "None":
logging.info("JWT vulnerable to CVE-2018-1000531")
def hs256_attack(args):
"""This function passes down different candidates to the run() function and is required
to handle different types of guessing attack.
Arguments:
args {object} -- The command-line arguments
"""
headers = jwt.get_unverified_header(args.token)
if not headers['alg'] == "HS256" and not headers['alg'] == "HS512" and not headers['alg'] == "HS384":
logging.error("JWT signed using an algorithm other than HS256 or HS512 or HS384")
else:
tqdm_disable = True if args.log_level == "DEBUG" else False
if args.attack_mode == "brute-force":
# Count = ....
for candidate in tqdm(bruteforce(args.charset, args.increment_min, args.increment_max), disable=tqdm_disable):
if run(args.token, candidate):
return candidate
return None
elif args.attack_mode == "wordlist":
word_count = len(open(args.wordlist.name, "r",
encoding="utf-8",
errors="ignore").readlines())
for entry in tqdm(args.wordlist, disable=tqdm_disable, total=word_count):
if run(args.token, entry.rstrip()):
return entry.rstrip()
return None
def main():
try:
args = parse_args()
logger.setLevel(args.log_level)
start_time = time.time()
if args.attack_mode == "vulnerable":
is_vulnerable(args)
elif args.attack_mode in ('brute-force', 'wordlist'):
logger.warning(
"For attacking complex JWT, it is best to use compiled, GPU accelerated password crackers such as Hashcat and John the Ripper which offer more advanced techniques such as raw brute forcing, rules-based, and mask attacks.")
logger.info(
"Pour yourself a cup (or two) of ☕ as this operation might take a while depending on the size of your wordlist.")
candidate = hs256_attack(args)
if candidate:
logger.info(f"Private key found: {candidate}")
if args.outfile:
args.outfile.write(f"{args.token}:{candidate}\n")
logging.info(f"Private key saved to: {args.outfile.name}")
# Save the private secret into a file in case sys.stdout is unresponsive
if not args.potfile_disable:
args.potfile.write(f"{args.token}:{candidate}\n")
else:
logger.info(
"The private key was not found in this wordlist. Consider using a bigger wordlist or other types of attacks.")
end_time = time.time()
elapsed_time = end_time - start_time
logger.info(f"Finished in {elapsed_time} sec")
except KeyboardInterrupt:
logger.error("CTRL+C pressed, exiting...")
# Not sure if necessary
# args.wordlist.close()
elapsed_time = time.time() - start_time
logger.info(f"Interrupted after {elapsed_time} sec")
except Exception as e:
logger.error(f"{e}")
if __name__ == "__main__":
main()