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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
| 'use strict';
const logFilePath = "./log.txt"; const logFile = new File(logFilePath, "w");
function logLine(s) { logFile.write(s + "\n"); logFile.flush(); console.log(s); }
function findPythonModule() { const mods = Process.enumerateModulesSync(); for (const m of mods) { const n = m.name.toLowerCase(); if (n.startsWith('python3') && n.endsWith('.dll')) return m; if (n.startsWith('python') && n.endsWith('.dll')) return m; } throw new Error('python3*.dll not found. Use: frida -f python.exe -l hook_number_compare.js'); }
const pymod = findPythonModule(); logLine('[*] using python module: ' + pymod.name + ' ' + pymod.base);
function exp(name) { try { const addr = Module.getExportByName(pymod.name, name); logLine(`[+] export ${name} => ${addr}`); return addr; } catch (e) { logLine(`[-] export ${name} not found`); return null; } }
const symbols = { PyUnicode_AsUTF8: exp('PyUnicode_AsUTF8'), PyObject_Repr: exp('PyObject_Repr'), PyObject_Str: exp('PyObject_Str'), PyLong_Check: exp('PyLong_Check'), PyLong_AsLongLong: exp('PyLong_AsLongLong'), PyLong_AsUnsignedLongLongMask: exp('PyLong_AsUnsignedLongLongMask'), PyLong_FromString: exp('PyLong_FromString'), PyLong_FromUnicodeObject: exp('PyLong_FromUnicodeObject'), PyNumber_Add: exp('PyNumber_Add'), PyNumber_InPlaceAdd: exp('PyNumber_InPlaceAdd'), PyNumber_Subtract: exp('PyNumber_Subtract'), PyNumber_Multiply: exp('PyNumber_Multiply'), PyNumber_And: exp('PyNumber_And'), PyNumber_Or: exp('PyNumber_Or'), PyNumber_Xor: exp('PyNumber_Xor'), PyNumber_Lshift: exp('PyNumber_Lshift'), PyNumber_Rshift: exp('PyNumber_Rshift'), PyNumber_InPlaceRshift: exp('PyNumber_InPlaceRshift'), PyNumber_Power: exp('PyNumber_Power'), PyNumber_Remainder: exp('PyNumber_Remainder'), PyNumber_Index: exp('PyNumber_Index'), PyObject_RichCompare: exp('PyObject_RichCompare'), PyObject_RichCompareBool: exp('PyObject_RichCompareBool') };
function tryCreateNativeFunction(addr, retType, argTypes, friendlyName) { if (!addr) return null; try { const nf = new NativeFunction(addr, retType, argTypes); logLine(`[+] NativeFunction created for ${friendlyName}`); return nf; } catch (e) { logLine(`[-] Failed creating NativeFunction for ${friendlyName}: ${e.message}`); return null; } }
const PyUnicode_AsUTF8 = tryCreateNativeFunction(symbols.PyUnicode_AsUTF8, 'pointer', ['pointer'], 'PyUnicode_AsUTF8'); const PyObject_Repr = tryCreateNativeFunction(symbols.PyObject_Repr, 'pointer', ['pointer'], 'PyObject_Repr'); const PyObject_Str = tryCreateNativeFunction(symbols.PyObject_Str, 'pointer', ['pointer'], 'PyObject_Str'); const PyLong_Check = tryCreateNativeFunction(symbols.PyLong_Check, 'int', ['pointer'], 'PyLong_Check'); const PyLong_AsLongLong = tryCreateNativeFunction(symbols.PyLong_AsLongLong, 'int64', ['pointer'], 'PyLong_AsLongLong'); const PyLong_AsUnsignedLongLongMask = tryCreateNativeFunction(symbols.PyLong_AsUnsignedLongLongMask, 'uint64', ['pointer'], 'PyLong_AsUnsignedLongLongMask');
function toHexFixed(num, width = 8) { try { let big; if (typeof num === 'bigint') big = num; else if (typeof num === 'number') big = BigInt(num >>> 0); else if (typeof num === 'string') { if (/^0x[0-9a-fA-F]+$/.test(num)) big = BigInt(num); else if (/^-?\d+$/.test(num)) { big = BigInt(num); if (big < 0n) { const bits = BigInt(width * 4); big = (big & ((1n << bits) - 1n)); } } else return num; } else return `<hex error: ${typeof num}>`; let hex = big.toString(16); if (hex.length < width) hex = hex.padStart(width, '0'); return '0x' + hex; } catch (e) { return `<hex error: ${e.message}>`; } }
function isDecimalString(s) { return typeof s === 'string' && /^-?\d+$/.test(s); } function isHexString(s) { return typeof s === 'string' && /^0x[0-9a-fA-F]+$/.test(s); }
function formatForLogging(s, width = 8) { if (s === undefined || s === null) return s; if (isHexString(s)) return s; if (isDecimalString(s)) return toHexFixed(s, width); const m = /(-?\d+)$/.exec(String(s).trim()); if (m) return toHexFixed(m[1], width); return s; }
function safeUtf8FromPyStr(pyStrPtr) { if (!pyStrPtr || pyStrPtr.isNull()) return '<utf8 NULL>'; try { if (!PyUnicode_AsUTF8) return '<no PyUnicode_AsUTF8>'; const cstr = PyUnicode_AsUTF8(pyStrPtr); if (cstr.isNull()) return '<utf8 NULL>'; return Memory.readUtf8String(cstr); } catch { return '<utf8 read error>'; } }
function objToStr(pyObjPtr) { if (!pyObjPtr || pyObjPtr.isNull()) return '<NULL>'; try { if (PyLong_Check) { const isLong = PyLong_Check(pyObjPtr); if (isLong === 1) { if (PyLong_AsUnsignedLongLongMask) { const u = PyLong_AsUnsignedLongLongMask(pyObjPtr); return toHexFixed(u); } if (PyLong_AsLongLong) { const v = PyLong_AsLongLong(pyObjPtr); return toHexFixed(v); } } } if (PyObject_Str) { const sObj = PyObject_Str(pyObjPtr); if (sObj && !sObj.isNull()) return safeUtf8FromPyStr(sObj); } if (PyObject_Repr) { const r = PyObject_Repr(pyObjPtr); if (r && !r.isNull()) return safeUtf8FromPyStr(r); } return '<unrepresentable>'; } catch (e) { return `<repr error: ${e.message}>`; } }
if (symbols.PyLong_FromUnicodeObject) { Interceptor.attach(symbols.PyLong_FromUnicodeObject, { onEnter(args) { this.sRaw = objToStr(args[0]); this.base = args[1].toInt32(); }, onLeave(retval) { const got = objToStr(retval); logLine(`[PyLong_FromUnicodeObject] int(s, base=${this.base}) s=${this.sRaw} -> ${formatForLogging(got)}`); } }); }
if (symbols.PyLong_FromString) { Interceptor.attach(symbols.PyLong_FromString, { onEnter(args) { try { this.s = Memory.readUtf8String(args[0]); } catch (e) { this.s = '<read error>'; } this.base = args[2].toInt32(); }, onLeave(retval) { const got = objToStr(retval); logLine(`[PyLong_FromString] int(s, base=${this.base}) s=${this.s} -> ${formatForLogging(got)}`); } }); }
function symbolFor(name) { const map = { PyNumber_Add: '+', PyNumber_InPlaceAdd: '+=', PyNumber_Subtract: '-', PyNumber_Multiply: '*', PyNumber_And: '&', PyNumber_Or: '|', PyNumber_Xor: '^', PyNumber_Lshift: '<<', PyNumber_Rshift: '>>', PyNumber_InPlaceRshift: '>>=', PyNumber_Power: '**', PyNumber_Remainder: '%', PyNumber_Index: 'index' }; return map[name] || '?'; }
function hookPyNumber(name, ptr) { if (!ptr) return; Interceptor.attach(ptr, { onEnter(args) { this.aPtr = args[0]; this.bPtr = args[1]; this.aRaw = objToStr(this.aPtr); this.bRaw = objToStr(this.bPtr); this.a = formatForLogging(this.aRaw, 8); this.b = formatForLogging(this.bRaw, 8);
try { this.cPtr = args[2]; if (this.cPtr && !this.cPtr.isNull()) { this.cRaw = objToStr(this.cPtr); this.c = formatForLogging(this.cRaw, 8); } else { this.cPtr = null; this.cRaw = null; this.c = null; } } catch (e) { this.cPtr = null; this.cRaw = null; this.c = null; } }, onLeave(retval) {
let aStr = this.aRaw.length > 32 ? "num" : this.a; let bStr = this.bRaw.length > 32 ? "num" : this.b; const outRaw = objToStr(retval); let outFmt = outRaw.length > 32 ? "num" : formatForLogging(outRaw, 8);
if (name === 'PyNumber_Power') { let modStr = this.c ? (this.cRaw.length > 32 ? "num" : this.c) : "None"; logLine(`[${name}] pow(${aStr}, ${bStr}, ${modStr}) = ${outFmt}`); return; }
logLine(`[${name}] ${aStr} ${symbolFor(name)} ${bStr} = ${outFmt}`); } }); }
[ 'PyNumber_Add', 'PyNumber_InPlaceAdd', 'PyNumber_Subtract', 'PyNumber_Multiply', 'PyNumber_And', 'PyNumber_Or', 'PyNumber_Xor', 'PyNumber_Lshift', 'PyNumber_Rshift', 'PyNumber_InPlaceRshift', 'PyNumber_Remainder', 'PyNumber_Index', 'PyNumber_Power' ].forEach(n => hookPyNumber(n, symbols[n]));
const cmpOps = ['<', '<=', '==', '!=', '>', '>='];
if (symbols.PyObject_RichCompareBool) { Interceptor.attach(symbols.PyObject_RichCompareBool, { onEnter(args) { this.ptrA = args[0]; this.ptrB = args[1]; this.op = args[2].toInt32(); this.aRaw = objToStr(this.ptrA); this.bRaw = objToStr(this.ptrB); this.a = formatForLogging(this.aRaw, 8); this.b = formatForLogging(this.bRaw, 8); }, onLeave(retval) { const opStr = cmpOps[this.op] || `op(${this.op})`; if (this.ptrA.equals(this.ptrB)) return; if (this.aRaw === this.bRaw && this.op === 2) return; logLine(`[CompareBool] ${this.a} ${opStr} ${this.b} -> ${retval.toInt32()}`); } }); }
if (symbols.PyObject_RichCompare) { Interceptor.attach(symbols.PyObject_RichCompare, { onEnter(args) { this.aRaw = objToStr(args[0]); this.bRaw = objToStr(args[1]); this.op = args[2].toInt32(); this.a = formatForLogging(this.aRaw, 8); this.b = formatForLogging(this.bRaw, 8); }, onLeave(retval) { logLine(`[CompareObj] ${this.a} ${cmpOps[this.op]} ${this.b} -> ${objToStr(retval)}`); } }); }
logLine(`\n✅ Hooks installed on ${pymod.name}`); logLine(`🧮 PyNumber_* operations + Compare ops are now being traced in HEX...\n`);
|