-
-
Notifications
You must be signed in to change notification settings - Fork 186
/
device.js
71 lines (58 loc) · 1.6 KB
/
device.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
const frida = require('frida')
class Device {
static manager = new frida.DeviceManager()
constructor(dev) {
this.dev = dev
}
/**
*
* @param {string} host
* @returns {Promise<Device>}
*/
static async connect(host) {
const dev = await Action.manager.addRemoteDevice(host)
return new Device(dev)
}
/**
* @returns {Promise<Device>}
*/
static async usb() {
return new Device(await frida.getUsbDevice())
}
static async find(partialId) {
const list = await frida.enumerateDevices()
for (let dev of list)
if (dev.id.startsWith(partialId))
return new Device(dev)
throw new Error(`Unable to find device that matches id == ${partialId}`)
}
/**
*
* @param {string} name bundle id or app name
* @returns {Promise<frida.Session>}
*/
async run(name) {
const apps = await this.dev.enumerateApplications()
const app = apps.find(app => app.name === name || app.identifier === name)
if (!app)
throw new Error(`Unable to find app: ${name}`)
let needsNew = app.pid === 0
if (!needsNew) {
const front = await this.dev.getFrontmostApplication()
if (front && front.pid !== app.pid) {
await this.dev.kill(app.pid)
needsNew = true
}
}
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms))
if (needsNew) {
const pid = await this.dev.spawn(app.identifier)
await this.dev.resume(pid)
await sleep(1000)
const session = await this.dev.attach(pid)
return session
}
return this.dev.attach(app.pid)
}
}
module.exports = Device