This repository has been archived by the owner on Nov 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 228
/
utils.js
104 lines (83 loc) · 2.45 KB
/
utils.js
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
const frida = require('frida')
const { DeviceNotFoundError, AppAttachError } = require('./error')
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
async function retry(operation, options) {
if (typeof operation !== 'function') throw new Error('operation should be a function')
const opt = options || {}
let times = opt.retry || 10
const interval = opt.interval || 200
while (--times > 0) {
try {
return operation()
} catch (ignored) {
console.log(ignored)
}
await sleep(interval)
}
throw new Error('max retry exceed')
}
class FridaUtil {
static isUSB(dev) {
return dev && ['tether', 'usb'].indexOf(dev.type) > -1
}
static async getDevice(id) {
const list = await frida.enumerateDevices()
const dev = list.find(d => d.id === id && FridaUtil.isUSB(d))
if (dev) return dev
throw new DeviceNotFoundError(id)
}
// spawn and wait until it's ready
static async spawn(dev, app) {
const pid = await dev.spawn([app.identifier])
const session = await dev.attach(pid)
await dev.resume(pid)
const probeScript = await session.createScript(`
Module.ensureInitialized('Foundation'); rpc.exports.ok = function() { return true }`)
await probeScript.load()
try {
const ok = await retry(probeScript.exports.ok.bind(probeScript.exports))
if (!ok) throw new AppAttachError(app.identifier)
} catch (ex) {
if (/FBSOpenApplicationErrorDomain error 7/.exec(ex)) throw Error('device is locked')
console.error(ex)
await session.detach()
throw new AppAttachError(app.identifier)
}
return session
}
}
function serializeIcon(icon) {
if (!icon) return icon
const { pixels, height, width, rowstride } = icon
return { width, height, rowstride, pixels: pixels.toString('base64') }
}
function serializeDevice(dev) {
const { name, id, icon } = dev
return { name, id, icon: serializeIcon(icon) }
}
function serializeApp(app) {
const { name, id, smallIcon, largeIcon, identifier } = app
return {
name,
id,
identifier,
smallIcon: serializeIcon(smallIcon),
largeIcon: serializeIcon(largeIcon),
}
}
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = Math.random() * 16 | 0, v = c === 'x' ? r : ((r & 0x3) | 0x8)
return v.toString(16)
})
}
module.exports = {
FridaUtil,
serializeDevice,
serializeApp,
sleep,
retry,
uuidv4,
}