Skip to content

Commit 56782a6

Browse files
committed
Refined pattern to allow for whitespace, restricted characters in keys/values to allowed ranges
1 parent adf5395 commit 56782a6

File tree

2 files changed

+107
-2
lines changed

2 files changed

+107
-2
lines changed

jwt/generate_jwt.py

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env python3
2+
3+
from base64 import urlsafe_b64encode as b64encode
4+
import re
5+
from random import randbytes
6+
from typing import Generator, Optional
7+
from enum import Enum
8+
from argparse import ArgumentParser
9+
10+
11+
PADDING_CHARS = ('', "\t", "\n", ' ')
12+
13+
14+
class JSONTypes(Enum):
15+
STRING = 1
16+
NUMBER = 2
17+
OBJECT = 3
18+
ARRAY = 4
19+
BOOL = 5
20+
NULL = 6
21+
22+
23+
def leading_json_as_base64() -> Generator:
24+
for c in range(0x01, 0xf4):
25+
for d in range(0x01, 0xf4):
26+
for e in PADDING_CHARS:
27+
for f in PADDING_CHARS:
28+
for g in PADDING_CHARS:
29+
for h in PADDING_CHARS:
30+
padding = e + f + g + h
31+
yield b64('{' + padding + '"' + chr(c) + chr(d))
32+
33+
34+
def trailing_json_as_base64() -> Generator:
35+
for json_type in JSONTypes:
36+
if json_type == JSONTypes.STRING:
37+
for c in range(0x01, 0xf4):
38+
for d in range(0x01, 0xf4):
39+
for output in output_trailing_json(chr(c) + chr(d) + '"'):
40+
yield output
41+
elif json_type == JSONTypes.NUMBER:
42+
for c in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'e', '.', '-', ' ', "\t", ':']:
43+
for d in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'e', '.', '-', ' ', "\t", ':']:
44+
for e in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0']:
45+
for output in output_trailing_json(c + d + e):
46+
yield output
47+
elif json_type == JSONTypes.OBJECT:
48+
for c in range(0x01, 0xf4):
49+
for d in range(0x01, 0xf4):
50+
for output in output_trailing_json(chr(c) + chr(d) + '}'):
51+
yield output
52+
elif json_type == JSONTypes.ARRAY:
53+
for c in range(0x01, 0xf4):
54+
for d in range(0x01, 0xf4):
55+
for output in output_trailing_json(chr(c) + chr(d) + ']'):
56+
yield output
57+
elif json_type == JSONTypes.BOOL:
58+
for c in PADDING_CHARS:
59+
for b in ["true", "false"]:
60+
for output in output_trailing_json(c + b):
61+
yield output
62+
elif json_type == JSONTypes.NULL:
63+
for c in PADDING_CHARS:
64+
for output in output_trailing_json(c + "null"):
65+
yield output
66+
67+
68+
def output_trailing_json(obj: str) -> Generator:
69+
for slide in range(0, 2):
70+
for e in PADDING_CHARS:
71+
for f in PADDING_CHARS:
72+
for g in PADDING_CHARS:
73+
for h in PADDING_CHARS:
74+
padding = e + f + g + h
75+
yield b64(('A' * slide) + obj + padding + '}')
76+
77+
78+
def b64(text: str) -> str:
79+
return b64encode(text.encode('utf-8')).decode('utf-8')
80+
81+
82+
def main() -> None:
83+
parser = ArgumentParser(description="Generate JWT base64 strings")
84+
add_args(parser)
85+
args = parser.parse_args()
86+
87+
if args.leading:
88+
for token in leading_json_as_base64():
89+
print(token)
90+
return
91+
92+
if args.trailing:
93+
for token in trailing_json_as_base64():
94+
print(token.rstrip('='))
95+
return
96+
97+
98+
def add_args(parser: ArgumentParser) -> None:
99+
parser.add_argument('--leading', action='store_true')
100+
parser.add_argument('--trailing', action='store_true')
101+
102+
103+
if __name__ == '__main__':
104+
main()
105+

jwt/patterns.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ patterns:
77
description: "JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties."
88
regex:
99
pattern: |
10-
ey[A-Za-z0-9-_]{12,}[Q90]={0,2}\.ey[A-Za-z0-9-_]{12,}[Q90]={0,2}\.?[A-Za-z0-9-_=]*
10+
e(?:y[I-J]|yL[CD]|w[koA][JKgi])[A-Za-z0-9_-]{10,}(?:[0-59JKdgilsw-z]fQ|[3HXn]0|[1BJVlpx]9)={0,2}\.e(?:y[I-J]|yL[CD]|w[koA][JKgi])[A-Za-z0-9_-]{10,}(?:[0-59JKdgilsw-z]fQ|[3HXn]0|[1BJVlpx]9)={0,2}\.?[A-Za-z0-9_-]*={0,2}
1111
start: |
1212
[^0-9A-Za-z_.-]|\A
1313
end: |
14-
[^0-9A-Za-z_.-]|\z
14+
[^0-9A-Za-z_.=-]|\z
1515
1616
expected:
1717
- name: owasp-juice-shop.ts

0 commit comments

Comments
 (0)