Last active
March 11, 2026 16:50
-
-
Save dlenski/4639dc139dd68bda1794d212e2a91507 to your computer and use it in GitHub Desktop.
Simple CLI to decode and dump JWTs (https://en.wikipedia.org/wiki/JSON_Web_Token) and similar strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| """ | |
| A simple CLI to decode and dump JWTs (https://en.wikipedia.org/wiki/JSON_Web_Token), | |
| or other similar strings consisting of URL-safe, unpadded base64 blobs separated | |
| by '.'. | |
| Example: | |
| $ ./jwtish.py e30.foob.barz.aGVsbG8 | |
| 0 (JSON ): {} | |
| 1 (3B ): 7e8a1b | |
| 2 (3B ): 6daaf3 | |
| 3 (string): 'hello' | |
| """ | |
| import json | |
| import base64 | |
| import argparse | |
| p = argparse.ArgumentParser(description=__doc__.split('\n\n')[0]) | |
| p.add_argument('-b', '--brief', action='store_true', help="Don't print full hex-dump of binaries longer than 32 bytes") | |
| p.add_argument('-p', '--pretty', action='store_true', help="Pretty-print JSON") | |
| p.add_argument('jwtish', nargs='+') | |
| args = p.parse_args() | |
| for ii, jwtish in enumerate(args.jwtish): | |
| if ii > 0: | |
| print() | |
| bits = jwtish.split('.') | |
| for jj, bit in enumerate(bits): | |
| bit += '=' * (4 - len(bit) % 4) | |
| binary = base64.urlsafe_b64decode(bit) | |
| try: | |
| string = binary.decode() | |
| try: | |
| js = json.loads(string) | |
| typ = 'JSON' | |
| if args.pretty: | |
| dump = json.dumps(js, indent=2) | |
| else: | |
| dump = string | |
| except json.JSONDecodeError: | |
| js = None | |
| typ = 'string' | |
| dump = repr(string) | |
| except UnicodeDecodeError: | |
| string = js = None | |
| typ = f'{len(binary)}B' | |
| if args.brief and len(binary) > 32: | |
| dump = binary[:16].hex() + "..." + binary[-16:].hex() | |
| else: | |
| dump = binary.hex() | |
| print(f'{jj} ({typ:6s}): {dump}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment