Files
Operation-Lovecraft-Fallen-…/frida_hooks/capture_chacha_key.js
T
2026-04-15 22:24:44 -07:00

155 lines
5.2 KiB
JavaScript

// capture_chacha_key.js - Hook ChaCha20 to capture session encryption key
// Hooks the AVX path at RVA 0xc254d4 (the scalar path at 0xc25118 gets 0 calls on modern CPUs)
//
// ChaCha20 state layout (64 bytes):
// [0..15] "expand 32-byte k" (sigma constants: 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574)
// [16..47] 32-byte key
// [48..51] 32-bit counter
// [52..63] 12-byte nonce (or 8-byte nonce + 4-byte extended counter)
//
// Function signature (from research):
// rcx = state pointer (sigma + key)
// rdx = output buffer
// r8 = nonce/counter
(function() {
'use strict';
var BASE = Process.getModuleByName('Paralogue-Win64-Shipping.exe').base;
// ChaCha20 AVX path entry (confirmed used on modern CPUs)
var CHACHA_AVX_RVA = 0xc254d4;
// ChaCha20 scalar path (fallback, rarely used)
var CHACHA_SCALAR_RVA = 0xc25118;
// Function entry with CPU feature check
var CHACHA_ENTRY_RVA = 0xc250e8;
// "expand 32-byte k" as 4 uint32 LE values
var SIGMA = [0x61707865, 0x3320646e, 0x79622d32, 0x6b206574];
var capturedKeys = {};
var callCount = 0;
var gameKeyFound = false;
function readState(ptr) {
// Read the full 64-byte ChaCha20 state
try {
var state = ptr.readByteArray(64);
if (!state) return null;
return new Uint8Array(state);
} catch(e) {
return null;
}
}
function toHex(arr, start, len) {
var hex = '';
for (var i = start; i < start + len && i < arr.length; i++) {
hex += ('0' + arr[i].toString(16)).slice(-2);
}
return hex;
}
function checkSigma(state) {
// Check if first 16 bytes match "expand 32-byte k"
var dv = new DataView(state.buffer, state.byteOffset, state.byteLength);
return dv.getUint32(0, true) === SIGMA[0] &&
dv.getUint32(4, true) === SIGMA[1] &&
dv.getUint32(8, true) === SIGMA[2] &&
dv.getUint32(12, true) === SIGMA[3];
}
function extractKeyInfo(state) {
var key = toHex(state, 16, 32);
var counter = toHex(state, 48, 4);
var nonce = toHex(state, 52, 12);
return {key: key, counter: counter, nonce: nonce};
}
// Hook the function entry point (which dispatches to AVX or scalar)
var chachaEntry = BASE.add(CHACHA_ENTRY_RVA);
send({t: 'info', msg: 'Hooking ChaCha20 entry at ' + chachaEntry});
Interceptor.attach(chachaEntry, {
onEnter: function(args) {
callCount++;
var statePtr = args[0]; // rcx = state pointer
var state = readState(statePtr);
if (!state) return;
if (!checkSigma(state)) {
// Not a standard ChaCha20 state, might be different calling convention
// Try reading from different offsets
return;
}
var info = extractKeyInfo(state);
var keyHash = info.key;
// Only report each unique key once
if (!capturedKeys[keyHash]) {
capturedKeys[keyHash] = true;
// Read the nonce/counter from r8 as well
var r8 = this.context.r8;
var r8Data = null;
try {
r8Data = toHex(new Uint8Array(r8.readByteArray(16)), 0, 16);
} catch(e) {}
send({
t: 'chacha_key',
key: info.key,
counter: info.counter,
nonce: info.nonce,
r8_data: r8Data,
call_num: callCount,
state_ptr: statePtr.toString()
});
// Also send the full 64-byte state as binary
send({t: 'chacha_state', call_num: callCount}, statePtr.readByteArray(64));
}
}
});
// Also try hooking the AVX path directly as backup
var chachaAVX = BASE.add(CHACHA_AVX_RVA);
send({t: 'info', msg: 'Hooking ChaCha20 AVX at ' + chachaAVX});
Interceptor.attach(chachaAVX, {
onEnter: function(args) {
// At the AVX path entry, args might be different since we're mid-function
// The state pointer should still be in rcx or on the stack
// Try rcx first
var statePtr = this.context.rcx;
var state = readState(statePtr);
if (!state || !checkSigma(state)) {
// Try the stack - the entry function may have saved rcx
return;
}
var info = extractKeyInfo(state);
if (!capturedKeys[info.key + '_avx']) {
capturedKeys[info.key + '_avx'] = true;
send({
t: 'chacha_key_avx',
key: info.key,
counter: info.counter,
nonce: info.nonce,
call_num: callCount
});
}
}
});
send({t: 'ready', msg: 'ChaCha20 key capture hooks installed. Waiting for encryption calls...'});
// Periodic status report
var statusInterval = setInterval(function() {
var numKeys = Object.keys(capturedKeys).length;
send({t: 'status', calls: callCount, unique_keys: numKeys});
}, 10000);
})();