Files
2026-04-15 22:24:44 -07:00

208 lines
6.0 KiB
Python

#!/usr/bin/env python3
"""
run_key_capture.py - Capture ChaCha20 session key from game.
Writes key to captures/chacha_session_key.json for the proxy to read.
Usage:
1. Start tcp_proxy_decrypt.py
2. Launch game
3. Run this script (attaches to game)
4. Connect to server in-game
5. Key is captured and saved automatically
"""
import frida
import sys
import os
import time
import json
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
JS_PATH = os.path.join(SCRIPT_DIR, 'capture_session_key.js')
PROCESS_NAME = 'Paralogue-Win64-Shipping.exe'
OUTPUT_DIR = os.path.join(SCRIPT_DIR, 'captures')
os.makedirs(OUTPUT_DIR, exist_ok=True)
KEY_FILE = os.path.join(OUTPUT_DIR, 'chacha_session_key.json')
captured_keys = []
def on_message(message, data):
if message['type'] == 'error':
print(f'[!] {message["description"]}')
return
p = message.get('payload', {})
if not isinstance(p, dict):
return
t = p.get('t', '')
if t == 'ready':
print(f'[*] {p["msg"]}')
elif t == 'info':
print(f'[*] {p["msg"]}')
elif t == 'error':
print(f'[!] {p["msg"]}')
elif t == 'hit':
hook = p.get('hook', '?')
count = p.get('count', 0)
extra = ''
if 'rcx' in p:
extra = f' rcx={p["rcx"]} rdx={p["rdx"]} r8={p["r8"]}'
print(f'[HIT] {hook} #{count}{extra}')
elif t == 'status':
print(f'[*] Hits: {p.get("hits","none")} | Keys: {p.get("keys",0)}')
elif t == 'key':
key = p.get('key', '')
nonce = p.get('nonce', '')
callers = p.get('callers', [])
print(f'\n[KEY] {key}')
print(f' Nonce: {nonce}')
if callers:
print(f' Callers: {" <- ".join(callers)}')
entry = {
'key_hex': key,
'nonce_hex': nonce,
'counter_hex': '00000000',
'callers': callers,
'timestamp': time.time()
}
captured_keys.append(entry)
with open(KEY_FILE, 'w') as f:
json.dump(captured_keys, f, indent=2)
print(f' -> Saved to {KEY_FILE}')
elif t == 'packet' and data:
n = p.get('n', 0)
off = p.get('offset', 0)
ln = p.get('len', 0)
enc = p.get('enc', 0)
plaintext = bytes(data)
direction = 'SEND' if enc == 1 else 'RECV'
print(f' [{direction}] #{n:3d} off={off:6d} len={ln:5d} pt={plaintext[:24].hex()}{"..." if ln > 24 else ""}')
# Try parsing as protobuf for interesting messages
if ln > 4:
try:
tag = plaintext[0]
fn = tag >> 3
wt = tag & 7
if fn > 0 and fn < 20 and wt in (0, 2):
# Try decode string fields
text = ''
for b in plaintext:
if 32 <= b < 127:
text += chr(b)
else:
text += '.'
if any(c.isalpha() for c in text):
print(f' ascii: {text[:60]}')
except:
pass
elif t == 'keystream' and data:
off = p.get('offset', 0)
ln = p.get('len', 0)
ks = bytes(data)
# Save keystream to file incrementally
ks_file = os.path.join(OUTPUT_DIR, 'keystream.bin')
# Write at correct offset
with open(ks_file, 'r+b' if os.path.exists(ks_file) else 'wb') as f:
f.seek(off)
f.write(ks)
elif t == 'status':
gc = p.get('calls', 0)
print(f'[*] {gc} calls')
elif t == 'chacha_key':
key = p['key']
nonce = p['nonce']
counter = p['counter']
callers = p.get('callers', [])
elapsed = p.get('elapsed_ms', 0)
source = p.get('source', '?')
is_game = p.get('is_game', False)
call_num = p.get('call_num', 0)
tag = ' *** GAME SOCKET ***' if is_game else ''
print(f'\n{"="*60}')
print(f' ChaCha20 KEY #{len(captured_keys)+1} (from {source}, call #{call_num}){tag}')
print(f' Key (32B): {key}')
print(f' Nonce (12B): {nonce}')
print(f' Counter (4B): {counter}')
print(f' Callers: {" <- ".join(callers)}')
print(f'{"="*60}\n')
entry = {
'key_hex': key,
'nonce_hex': nonce,
'counter_hex': counter,
'source': source,
'is_game': is_game,
'callers': callers,
'timestamp': time.time()
}
captured_keys.append(entry)
# Save all keys
with open(KEY_FILE, 'w') as f:
json.dump(captured_keys, f, indent=2)
print(f' -> Saved to {KEY_FILE}')
print(f' -> Proxy will auto-load this key')
elif t == 'chacha_state_bin' and data:
raw = bytes(data)
idx = len(captured_keys)
path = os.path.join(OUTPUT_DIR, f'chacha_state_{idx}.bin')
with open(path, 'wb') as f:
f.write(raw)
def main():
print('=' * 60)
print(' ChaCha20 Session Key Capture')
print('=' * 60)
with open(JS_PATH, 'r') as f:
js_code = f.read()
print(f'[*] Looking for {PROCESS_NAME}...')
session = None
while not session:
try:
for proc in frida.get_local_device().enumerate_processes():
if proc.name == PROCESS_NAME:
print(f'[*] Found PID {proc.pid}, attaching...')
session = frida.attach(proc.pid)
break
except Exception as e:
print(f'[!] {e}')
if not session:
time.sleep(1)
script = session.create_script(js_code)
script.on('message', on_message)
script.load()
print('[*] Hooks active. Connect to a server in-game.')
print('[*] Press Ctrl+C to stop.\n')
try:
while True:
time.sleep(0.5)
except KeyboardInterrupt:
pass
print(f'\n[*] Captured {len(captured_keys)} unique keys.')
session.detach()
print('[*] Done.')
if __name__ == '__main__':
main()