-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.py
More file actions
330 lines (281 loc) · 9.7 KB
/
web.py
File metadata and controls
330 lines (281 loc) · 9.7 KB
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
"""Local Flask UI for jwtmod.
Binds to 127.0.0.1 by default. Security testing helper — do not deploy.
"""
from __future__ import annotations
import argparse
import json
from flask import Flask, jsonify, render_template, request
from jwtmod import (
JWTError,
decode_jwt,
diff as diff_tokens,
info as info_token,
forge_none,
forge_signed,
forge_jwk_embed,
forge_jku,
forge_kid,
forge_rs_to_hs_confusion,
forge_strip_signature,
forge_psychic_es256,
load_pem,
sign as sign_jwt,
verify as verify_jwt,
default_wordlist_path,
iter_wordlist,
try_secrets,
)
app = Flask(__name__)
@app.get("/")
def index():
return render_template("index.html")
@app.get("/wordlist")
def bundled_wordlist():
with open(default_wordlist_path(), "r", encoding="utf-8") as f:
return f.read(), 200, {"Content-Type": "text/plain; charset=utf-8"}
def _parse_sets(raw):
out = []
if not raw:
return out
if isinstance(raw, str):
for line in raw.splitlines():
line = line.strip()
if not line:
continue
k, _, v = line.partition("=")
out.append((k.strip(), v))
return out
if isinstance(raw, list):
for item in raw:
if isinstance(item, list) and len(item) == 2:
out.append((str(item[0]), str(item[1])))
elif isinstance(item, str) and "=" in item:
k, _, v = item.partition("=")
out.append((k.strip(), v))
return out
return out
def _common_kw(data):
return {
"sets": _parse_sets(data.get("sets")),
"removes": data.get("removes") or [],
"merge_json": data.get("merge_json") if isinstance(data.get("merge_json"), dict) else None,
"presets": data.get("presets") or [],
}
def _err(msg, code=400):
return jsonify(error=msg), code
def _need(data, *keys):
for k in keys:
if not data.get(k):
return _err(f"{k} is required")
return None
@app.post("/api/decode")
def api_decode():
data = request.get_json(silent=True) or {}
token = (data.get("token") or "").strip()
if not token:
return _err("token is required")
try:
header, payload, sig = decode_jwt(token)
except JWTError as e:
return _err(str(e))
return jsonify(header=header, payload=payload, signature=sig)
@app.post("/api/forge")
def api_forge():
"""Backwards-compatible alg:none forge endpoint."""
data = request.get_json(silent=True) or {}
token = (data.get("token") or "").strip()
if not token:
return _err("token is required")
try:
nh, np_, nt, extra = forge_none(
token,
**_common_kw(data),
alg_literal=data.get("alg_literal", "none"),
keep_header_extras=bool(data.get("keep_header_extras")),
trailing_dot=bool(data.get("trailing_dot", True)),
)
except JWTError as e:
return _err(str(e))
return jsonify(header=nh, payload=np_, token=nt, extra=extra)
@app.post("/api/sign")
def api_sign():
data = request.get_json(silent=True) or {}
token = (data.get("token") or "").strip()
alg = data.get("alg") or ""
if not token or not alg:
return _err("token and alg required")
try:
if alg.startswith("HS"):
secret = data.get("secret")
if secret is None:
return _err("secret required for HS*")
key = secret.encode("utf-8") if isinstance(secret, str) else bytes(secret)
elif alg == "none":
key = None
else:
pem = data.get("key_pem")
if not pem:
return _err("key_pem required for RS/PS/ES")
key = load_pem(pem)
nh, np_, nt, extra = forge_signed(
token, alg=alg, key=key, kid=data.get("kid"),
keep_header_extras=bool(data.get("keep_header_extras")),
**_common_kw(data),
)
except JWTError as e:
return _err(str(e))
return jsonify(header=nh, payload=np_, token=nt, extra=extra)
@app.post("/api/verify")
def api_verify():
data = request.get_json(silent=True) or {}
token = (data.get("token") or "").strip()
alg = data.get("alg") or ""
if not token or not alg:
return _err("token and alg required")
try:
if alg.startswith("HS"):
secret = data.get("secret") or ""
key = secret.encode("utf-8") if isinstance(secret, str) else bytes(secret)
elif alg == "none":
key = None
else:
pem = data.get("key_pem")
if not pem:
return _err("key_pem required")
key = load_pem(pem)
ok = verify_jwt(token, alg, key)
except JWTError as e:
return _err(str(e))
return jsonify(valid=bool(ok))
@app.post("/api/info")
def api_info():
data = request.get_json(silent=True) or {}
token = (data.get("token") or "").strip()
if not token:
return _err("token required")
try:
return jsonify(info_token(token))
except JWTError as e:
return _err(str(e))
@app.post("/api/diff")
def api_diff():
data = request.get_json(silent=True) or {}
a = (data.get("token_a") or "").strip()
b = (data.get("token_b") or "").strip()
if not a or not b:
return _err("token_a and token_b required")
try:
return jsonify(diff_tokens(a, b))
except JWTError as e:
return _err(str(e))
@app.post("/api/brute")
def api_brute():
data = request.get_json(silent=True) or {}
token = (data.get("token") or "").strip()
if not token:
return _err("token required")
raw = data.get("wordlist")
if raw:
secrets = (line.strip() for line in raw.splitlines() if line.strip() and not line.startswith("#"))
else:
secrets = iter_wordlist(default_wordlist_path())
try:
found = try_secrets(token, secrets, alg=data.get("alg"),
max_tries=data.get("max_tries"))
except JWTError as e:
return _err(str(e))
if found is None:
return jsonify(found=False)
try:
return jsonify(found=True, secret=found.decode("utf-8"))
except UnicodeDecodeError:
return jsonify(found=True, secret_hex=found.hex())
@app.post("/api/attack/jwk-embed")
def api_attack_jwk_embed():
data = request.get_json(silent=True) or {}
token = (data.get("token") or "").strip()
if not token:
return _err("token required")
try:
nh, np_, nt, extra = forge_jwk_embed(token, alg=data.get("alg", "RS256"), **_common_kw(data))
except JWTError as e:
return _err(str(e))
return jsonify(header=nh, payload=np_, token=nt, extra=extra)
@app.post("/api/attack/jku")
def api_attack_jku():
data = request.get_json(silent=True) or {}
token = (data.get("token") or "").strip()
url = data.get("url") or ""
if not token or not url:
return _err("token and url required")
try:
nh, np_, nt, extra = forge_jku(token, jku_url=url, alg=data.get("alg", "RS256"), **_common_kw(data))
except JWTError as e:
return _err(str(e))
return jsonify(header=nh, payload=np_, token=nt, extra=extra)
@app.post("/api/attack/kid")
def api_attack_kid():
data = request.get_json(silent=True) or {}
token = (data.get("token") or "").strip()
value = data.get("value")
if not token or value is None:
return _err("token and value required")
secret = data.get("secret") or ""
key = secret.encode("utf-8") if isinstance(secret, str) else bytes(secret)
try:
nh, np_, nt, extra = forge_kid(
token, kid_value=value, secret=key, alg=data.get("alg", "HS256"), **_common_kw(data),
)
except JWTError as e:
return _err(str(e))
return jsonify(header=nh, payload=np_, token=nt, extra=extra)
@app.post("/api/attack/rs-to-hs")
def api_attack_rs_to_hs():
data = request.get_json(silent=True) or {}
token = (data.get("token") or "").strip()
pem = data.get("public_pem")
if not token or not pem:
return _err("token and public_pem required")
try:
nh, np_, nt, extra = forge_rs_to_hs_confusion(
token, public_pem=pem, hs_alg=data.get("alg", "HS256"), **_common_kw(data),
)
except JWTError as e:
return _err(str(e))
return jsonify(header=nh, payload=np_, token=nt, extra=extra)
@app.post("/api/attack/strip-sig")
def api_attack_strip_sig():
data = request.get_json(silent=True) or {}
token = (data.get("token") or "").strip()
if not token:
return _err("token required")
try:
nh, np_, nt, extra = forge_strip_signature(
token, alg_literal=data.get("alg_literal"),
trailing_dot=bool(data.get("trailing_dot", False)),
**_common_kw(data),
)
except JWTError as e:
return _err(str(e))
return jsonify(header=nh, payload=np_, token=nt, extra=extra)
@app.post("/api/attack/psychic-es256")
def api_attack_psychic_es256():
data = request.get_json(silent=True) or {}
token = (data.get("token") or "").strip()
if not token:
return _err("token required")
try:
nh, np_, nt, extra = forge_psychic_es256(token, **_common_kw(data))
except JWTError as e:
return _err(str(e))
return jsonify(header=nh, payload=np_, token=nt, extra=extra)
def main() -> int:
parser = argparse.ArgumentParser(description="jwtmod local web UI")
parser.add_argument("--host", default="127.0.0.1")
parser.add_argument("--port", type=int, default=5000)
parser.add_argument("--debug", action="store_true")
args = parser.parse_args()
app.run(host=args.host, port=args.port, debug=args.debug)
return 0
if __name__ == "__main__":
raise SystemExit(main())