File size: 3,104 Bytes
13ca229
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Whole-library x-ref scan.
//
// Goal: find which IL2CPP methods actually *read* a given string literal (e.g. the
// JSON key "day_token"). Method bodies are opaque, but on AArch64 a constant address
// is loaded with `adrp Xd, page` + `add Xd, Xd, #off`, so scanning the whole
// .so for ADRP+ADD pairs and resolving them against Il2CppDumper's string table
// gives us the code sites — and ScriptMethod addresses turn those into method names.
const fs = require('fs');

const WS = 'D:/tqhyg/Project/Prism Step';
const so = fs.readFileSync(WS + '/backup/apk/raw/libil2cpp.so');
const j = JSON.parse(fs.readFileSync(WS + '/dumps/script.json', 'utf8'));

const WANT = (process.argv.slice(2).length ? process.argv.slice(2)
  : ['token', 'day_token', 'data', 'result', 'status', 'code', 'error', 'message', 'success']);

// string address -> value
const strByAddr = new Map();
for (const s of j.ScriptString) strByAddr.set(s.Address, s.Value);
// reverse: value -> addresses, only for wanted values
const addrWanted = new Map(); // address -> value
for (const [addr, v] of strByAddr) {
  if (WANT.includes(v)) addrWanted.set(addr, v);
}
console.log('wanted literals found in table:');
for (const [a, v] of addrWanted) console.log('  ', JSON.stringify(v), '@0x' + a.toString(16));

// method address -> name (sorted for lookup)
const methods = j.ScriptMethod.map(m => ({ a: m.Address, n: m.Name })).sort((x, y) => x.a - y.a);
function methodAt(addr) {
  let lo = 0, hi = methods.length - 1, best = null;
  while (lo <= hi) {
    const mid = (lo + hi) >> 1;
    if (methods[mid].a <= addr) { best = methods[mid]; lo = mid + 1; } else hi = mid - 1;
  }
  if (best && addr - best.a < 0x800) return best;
  return null;
}

const hits = new Map(); // targetAddr -> Set(codeOffset)
let pendingReg = -1, pendingPage = 0, pendingPc = 0;
const n = (so.length / 4) | 0;
for (let k = 0; k < n; k++) {
  const i = so.readUInt32LE(k * 4);
  if ((i & 0x9f000000) === 0x90000000) {
    const rd = i & 0x1f;
    const immlo = (i >>> 29) & 3;
    const immhi = (i >>> 5) & 0x7ffff;
    let imm = (immhi << 2) | immlo;
    if (imm & (1 << 20)) imm -= 1 << 21;
    pendingReg = rd;
    pendingPage = ((k * 4) & ~0xfff) + (imm << 12);
    pendingPc = k * 4;
    continue;
  }
  if (pendingReg >= 0 && (i & 0xff800000) === 0x91000000) {
    const rn = (i >>> 5) & 0x1f;
    if (rn === pendingReg) {
      const imm12 = (i >>> 10) & 0xfff;
      const sh = (i >>> 22) & 1;
      const target = pendingPage + (sh ? imm12 << 12 : imm12);
      if (addrWanted.has(target)) {
        if (!hits.has(target)) hits.set(target, new Set());
        hits.get(target).add(pendingPc);
      }
      pendingReg = -1;
    }
    continue;
  }
  if (k * 4 - pendingPc > 16) pendingReg = -1;
}

console.log('\n=== x-ref sites ===');
for (const [addr, offs] of hits) {
  console.log(JSON.stringify(addrWanted.get(addr)) + ' @0x' + addr.toString(16));
  for (const o of offs) {
    const m = methodAt(o);
    console.log('   code@0x' + o.toString(16) + '  ' + (m ? ('in ' + m.n + ' (+0x' + (o - m.a).toString(16) + ')') : '(no method)'));
  }
}