brickproof-adb 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +36 -0
- package/LICENSE +21 -0
- package/README.md +146 -2
- package/package.json +40 -4
- package/src/adb.js +376 -0
- package/src/apps.js +1156 -0
- package/src/cli.js +497 -0
- package/src/config.js +597 -0
- package/src/device.js +669 -0
- package/src/discover.js +315 -0
- package/src/files.js +453 -0
- package/src/hostpath.js +110 -0
- package/src/http.js +314 -0
- package/src/image.js +209 -0
- package/src/main.js +7 -0
- package/src/platform-tools.js +258 -0
- package/src/registry.js +334 -0
- package/src/server.js +161 -0
- package/src/session.js +303 -0
- package/src/stdio.js +51 -0
- package/src/token.js +114 -0
- package/src/ui.js +942 -0
- package/src/version.js +10 -0
package/src/device.js
ADDED
|
@@ -0,0 +1,669 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* device.js — facts read off the device itself.
|
|
4
|
+
*
|
|
5
|
+
* The protected set is derived from the device, never kept as a list of
|
|
6
|
+
* package names: a list protects only the devices it was written against,
|
|
7
|
+
* while a query finds an account stack it has never heard of.
|
|
8
|
+
*
|
|
9
|
+
* Sources differ between firmwares, so each one is probed on its own. A probe
|
|
10
|
+
* that fails must leave a line in notes: a failed source must not look like an
|
|
11
|
+
* absent one. "No role service on this platform" and "the role query failed"
|
|
12
|
+
* are different lines.
|
|
13
|
+
*
|
|
14
|
+
* Probes in use, and why each has its form:
|
|
15
|
+
* - roles: `dumpsys role` (`cmd role` has no getter on SDK 30/31);
|
|
16
|
+
* - installer: resolve-activity with a content:// URI (SDK 29+), file:// as
|
|
17
|
+
* the fallback; every derived value must look like a package name;
|
|
18
|
+
* - locale: marker-split output, so an empty value stays empty (Fire OS 7 has
|
|
19
|
+
* an empty ro.product.locale);
|
|
20
|
+
* - density: `wm density` physical and override read separately, the override
|
|
21
|
+
* wins when set.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
const { adbSh } = require('./adb.js');
|
|
25
|
+
|
|
26
|
+
// The core of the set. Small and conservative on purpose: everything else is
|
|
27
|
+
// read off the device.
|
|
28
|
+
const CORE_PROTECTED = [
|
|
29
|
+
'android',
|
|
30
|
+
'com.android.systemui',
|
|
31
|
+
'com.android.settings',
|
|
32
|
+
'com.android.shell',
|
|
33
|
+
'com.android.keychain',
|
|
34
|
+
'com.android.packageinstaller',
|
|
35
|
+
'com.google.android.packageinstaller',
|
|
36
|
+
'com.android.permissioncontroller',
|
|
37
|
+
'com.google.android.permissioncontroller',
|
|
38
|
+
// The unicode input channel of this server: disabling it breaks adb_text.
|
|
39
|
+
'com.android.adbkeyboard',
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
const CORE_PREFIXES = [
|
|
43
|
+
'com.android.providers.', // settings, media, downloads, telephony...
|
|
44
|
+
'com.android.inputmethod.',
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Name heuristics — only in addition to what is derived, never instead.
|
|
49
|
+
* Narrow on purpose: a bare 'sso' matches `pacprocessor` (proce-sso-r).
|
|
50
|
+
*/
|
|
51
|
+
const ACCOUNT_HINTS = [
|
|
52
|
+
/(^|\.)dcp(\.|$)/i, // com.amazon.dcp, com.amazon.dcp.contracts.*
|
|
53
|
+
/identity\.auth/i, // com.amazon.identity.auth.device.authorization
|
|
54
|
+
/ottsso/i, // com.amazon.tv.ottssocompanionapp
|
|
55
|
+
/ssocompanion/i,
|
|
56
|
+
/accountmanager/i,
|
|
57
|
+
/(^|\.)accounts?(\.|$)/i,
|
|
58
|
+
/\bgsf\b|android\.gms$/i, // the Google stack
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
// An Android package name: letters, digits, underscores, at least one dot, no
|
|
62
|
+
// spaces. Keeps "No activity found", "Unknown command: ..." and other error
|
|
63
|
+
// text out of the set.
|
|
64
|
+
const PKG_RE = /^[A-Za-z][A-Za-z0-9_]*(\.[A-Za-z0-9_]+)+$/;
|
|
65
|
+
|
|
66
|
+
// Looks like BCP-47: ru, en-US, sr-Latn-RS, es-419.
|
|
67
|
+
const LOCALE_RE = /^[a-z]{2,3}(-[A-Za-z]{4})?(-([A-Za-z]{2}|[0-9]{3}))?$/;
|
|
68
|
+
|
|
69
|
+
const MARK = '@@';
|
|
70
|
+
|
|
71
|
+
function looksLikePackage(s) {
|
|
72
|
+
const v = String(s || '').trim();
|
|
73
|
+
return !!v && v !== 'null' && v.length <= 255 && PKG_RE.test(v);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function pkgOf(component) {
|
|
77
|
+
const s = String(component || '').trim();
|
|
78
|
+
if (!s || s === 'null') return null;
|
|
79
|
+
return s.split('/')[0].trim() || null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Split output marked with lines @@key@@. A split with one capture group gives
|
|
84
|
+
* [preamble, key, value, key, ...], so an empty value is unambiguous, which a
|
|
85
|
+
* single regex over the whole output is not.
|
|
86
|
+
*/
|
|
87
|
+
function splitMarked(out) {
|
|
88
|
+
const parts = String(out).split(new RegExp(`^${MARK}([a-z0-9_]+)${MARK}[ \\t]*\\r?$`, 'm'));
|
|
89
|
+
const map = {};
|
|
90
|
+
for (let i = 1; i < parts.length; i += 2) {
|
|
91
|
+
map[parts[i]] = String(parts[i + 1] === undefined ? '' : parts[i + 1]).trim();
|
|
92
|
+
}
|
|
93
|
+
return map;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function markedCommand(queries) {
|
|
97
|
+
return Object.entries(queries)
|
|
98
|
+
.map(([k, c]) => `echo "${MARK}${k}${MARK}"; ${c} 2>/dev/null`)
|
|
99
|
+
.join('; ') + `; echo "${MARK}end${MARK}"`;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Basic device properties, in one call. */
|
|
103
|
+
async function getProps(serial) {
|
|
104
|
+
const out = await adbSh(serial, markedCommand({
|
|
105
|
+
sdk: 'getprop ro.build.version.sdk',
|
|
106
|
+
abilist: 'getprop ro.product.cpu.abilist',
|
|
107
|
+
abi: 'getprop ro.product.cpu.abi',
|
|
108
|
+
model: 'getprop ro.product.model',
|
|
109
|
+
// Locale chain: what the user set (the app runs in it, and splits are
|
|
110
|
+
// chosen by it), then the factory value, then language+region.
|
|
111
|
+
locale_persist: 'getprop persist.sys.locale',
|
|
112
|
+
locale_product: 'getprop ro.product.locale',
|
|
113
|
+
locale_language: 'getprop ro.product.locale.language',
|
|
114
|
+
locale_region: 'getprop ro.product.locale.region',
|
|
115
|
+
// Density: physical and override separately (see the header).
|
|
116
|
+
density_physical: 'wm density | sed -n "s/^ *Physical density: *\\([0-9][0-9]*\\).*/\\1/p" | head -1',
|
|
117
|
+
density_override: 'wm density | sed -n "s/^ *Override density: *\\([0-9][0-9]*\\).*/\\1/p" | head -1',
|
|
118
|
+
density_prop: 'getprop ro.sf.lcd_density',
|
|
119
|
+
}));
|
|
120
|
+
|
|
121
|
+
const m = splitMarked(out);
|
|
122
|
+
const num = v => {
|
|
123
|
+
const n = parseInt(v, 10);
|
|
124
|
+
return Number.isFinite(n) && n > 0 ? n : null;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const sdk = parseInt(m.sdk, 10);
|
|
128
|
+
|
|
129
|
+
// Locale: the first candidate that looks like BCP-47. Garbage is skipped.
|
|
130
|
+
const localeCandidates = [
|
|
131
|
+
m.locale_persist,
|
|
132
|
+
m.locale_product,
|
|
133
|
+
(m.locale_language && m.locale_region) ? `${m.locale_language}-${m.locale_region}` : '',
|
|
134
|
+
m.locale_language,
|
|
135
|
+
];
|
|
136
|
+
let locale = '';
|
|
137
|
+
for (const c of localeCandidates) {
|
|
138
|
+
const v = String(c || '').trim();
|
|
139
|
+
if (v && LOCALE_RE.test(v)) { locale = v; break; }
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const densityPhysical = num(m.density_physical) || num(m.density_prop);
|
|
143
|
+
const densityOverride = num(m.density_override);
|
|
144
|
+
|
|
145
|
+
const abilist = String(m.abilist || '').split(',').map(s => s.trim()).filter(Boolean);
|
|
146
|
+
if (!abilist.length && m.abi) abilist.push(String(m.abi).trim());
|
|
147
|
+
|
|
148
|
+
const notes = [];
|
|
149
|
+
if (!locale) notes.push('device locale not determined - config.<locale> is skipped when installing splits');
|
|
150
|
+
if (!densityPhysical) notes.push('screen density not determined - config.<density> is skipped');
|
|
151
|
+
if (densityOverride && densityPhysical && densityOverride !== densityPhysical)
|
|
152
|
+
notes.push(`Override density ${densityOverride} is set over physical ${densityPhysical} - splits are chosen by the override`);
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
sdk: Number.isFinite(sdk) ? sdk : 0,
|
|
156
|
+
abilist,
|
|
157
|
+
locale,
|
|
158
|
+
model: m.model || '',
|
|
159
|
+
density: densityOverride || densityPhysical || null,
|
|
160
|
+
density_physical: densityPhysical || null,
|
|
161
|
+
density_override: densityOverride || null,
|
|
162
|
+
notes,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Every installed package plus the system and disabled subsets. */
|
|
167
|
+
async function listPackages(serial) {
|
|
168
|
+
const out = await adbSh(serial, markedCommand({
|
|
169
|
+
all: 'pm list packages',
|
|
170
|
+
system: 'pm list packages -s',
|
|
171
|
+
disabled: 'pm list packages -d',
|
|
172
|
+
}));
|
|
173
|
+
|
|
174
|
+
const m = splitMarked(out);
|
|
175
|
+
const section = key => String(m[key] || '').split('\n')
|
|
176
|
+
.map(l => l.trim())
|
|
177
|
+
.filter(l => l.startsWith('package:'))
|
|
178
|
+
.map(l => l.slice('package:'.length).trim())
|
|
179
|
+
.filter(Boolean);
|
|
180
|
+
|
|
181
|
+
const all = section('all');
|
|
182
|
+
return {
|
|
183
|
+
all,
|
|
184
|
+
system: new Set(section('system')),
|
|
185
|
+
disabled: new Set(section('disabled')),
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Account snapshot — the canary of package removal.
|
|
191
|
+
*
|
|
192
|
+
* Only the count and the types: account names from `dumpsys account` never
|
|
193
|
+
* reach the output (personal data, and the before/after comparison does not
|
|
194
|
+
* need them).
|
|
195
|
+
*/
|
|
196
|
+
async function accountSnapshot(serial) {
|
|
197
|
+
let out = '';
|
|
198
|
+
try {
|
|
199
|
+
out = await adbSh(serial,
|
|
200
|
+
'dumpsys account 2>/dev/null | sed -n "s/.*Account {.*type=\\([^}]*\\)}.*/\\1/p"');
|
|
201
|
+
} catch {
|
|
202
|
+
return { available: false, count: null, types: [] };
|
|
203
|
+
}
|
|
204
|
+
const types = out.split('\n').map(s => s.trim()).filter(Boolean);
|
|
205
|
+
if (!types.length) {
|
|
206
|
+
// Empty means either "no accounts" or "dumpsys is not available to the
|
|
207
|
+
// shell"; the header tells them apart.
|
|
208
|
+
let head = '';
|
|
209
|
+
try { head = await adbSh(serial, 'dumpsys account 2>/dev/null | head -n 2'); } catch { /* ignore */ }
|
|
210
|
+
if (!/User UserInfo|Accounts:/i.test(head)) return { available: false, count: null, types: [] };
|
|
211
|
+
}
|
|
212
|
+
const byType = {};
|
|
213
|
+
for (const t of types) byType[t] = (byType[t] || 0) + 1;
|
|
214
|
+
return { available: true, count: types.length, types: byType };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Role holders through `dumpsys role` (the role service has no getter in
|
|
219
|
+
* `cmd role`). The service exists from SDK 29; its absence on older firmware
|
|
220
|
+
* is a fact about the platform, noted separately from a failed query.
|
|
221
|
+
*
|
|
222
|
+
* Every role is taken: a role means "this package performs a system function",
|
|
223
|
+
* the role list is vendor-specific, and picking the "important" ones is the
|
|
224
|
+
* mistake this set exists to avoid.
|
|
225
|
+
*/
|
|
226
|
+
async function roleHolders(serial, sdk) {
|
|
227
|
+
if (!(sdk >= 29))
|
|
228
|
+
return { holders: [], detail: {}, note: `no role service on this platform (SDK ${sdk || '?'} < 29) - roles were not queried, which is normal` };
|
|
229
|
+
|
|
230
|
+
let out = '';
|
|
231
|
+
try {
|
|
232
|
+
out = await adbSh(serial, 'dumpsys role');
|
|
233
|
+
} catch (e) {
|
|
234
|
+
return { holders: [], detail: {}, note: `⚠ dumpsys role failed (${e.message}) - roles NOT included in the protected set` };
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (!/ROLE STATE|roles=|name=android\.app\.role/i.test(out))
|
|
238
|
+
return { holders: [], detail: {}, note: '⚠ dumpsys role returned no role state - roles NOT included in the protected set' };
|
|
239
|
+
|
|
240
|
+
// Blocks of the form { name=android.app.role.HOME \n holders=pkg[,pkg] }.
|
|
241
|
+
// Cut at block boundaries, not with a fixed-size window: the last role has
|
|
242
|
+
// no following name=, and a window would silently lose its holder.
|
|
243
|
+
const detail = {};
|
|
244
|
+
const holders = new Set();
|
|
245
|
+
for (const chunk of String(out).split(/(?=name=android\.app\.role\.)/)) {
|
|
246
|
+
const rm = /^name=(android\.app\.role\.[A-Z_0-9]+)/.exec(chunk);
|
|
247
|
+
if (!rm) continue;
|
|
248
|
+
const hm = /holders=([^\r\n}]+)/.exec(chunk);
|
|
249
|
+
if (!hm) continue;
|
|
250
|
+
const list = hm[1].split(',').map(s => s.trim()).filter(looksLikePackage);
|
|
251
|
+
if (!list.length) continue;
|
|
252
|
+
detail[rm[1].replace('android.app.role.', '')] = list;
|
|
253
|
+
list.forEach(p => holders.add(p));
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (!holders.size)
|
|
257
|
+
return { holders: [], detail: {}, note: 'dumpsys role answered, but the device has no role holders' };
|
|
258
|
+
|
|
259
|
+
return { holders: Array.from(holders).sort(), detail, note: null };
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* A hex address from /proc/net/tcp in readable form. Each 32-bit word is
|
|
264
|
+
* little-endian. IPv4-mapped addresses (::ffff:a.b.c.d) collapse to IPv4,
|
|
265
|
+
* which is how LAN connections appear in tcp6.
|
|
266
|
+
*/
|
|
267
|
+
function decodeAddr(hex) {
|
|
268
|
+
const h = String(hex || '').toUpperCase();
|
|
269
|
+
if (h.length === 8) {
|
|
270
|
+
return (h.match(/../g) || []).reverse().map(b => parseInt(b, 16)).join('.');
|
|
271
|
+
}
|
|
272
|
+
if (h.length === 32) {
|
|
273
|
+
const words = h.match(/.{8}/g);
|
|
274
|
+
if (words[0] === '00000000' && words[1] === '00000000' && words[2] === 'FFFF0000')
|
|
275
|
+
return decodeAddr(words[3]);
|
|
276
|
+
const bytes = words.flatMap(w => (w.match(/../g) || []).reverse());
|
|
277
|
+
const parts = [];
|
|
278
|
+
for (let i = 0; i < 16; i += 2) parts.push((bytes[i] + bytes[i + 1]).toLowerCase());
|
|
279
|
+
return parts.join(':').replace(/(^|:)0+([0-9a-f])/g, '$1$2');
|
|
280
|
+
}
|
|
281
|
+
return h;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function isLoopback(addr) {
|
|
285
|
+
return /^127\./.test(addr) || addr === '::1' || addr === '0:0:0:0:0:0:0:1';
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Packages holding LISTENING TCP sockets.
|
|
290
|
+
*
|
|
291
|
+
* The account canary looks inside the device; a service something outside the
|
|
292
|
+
* device relies on can die while accounts and the launcher look fine. A
|
|
293
|
+
* listening socket is direct evidence that a package serves someone, without
|
|
294
|
+
* knowing the vendor or the package names.
|
|
295
|
+
*
|
|
296
|
+
* ⚠ Only LISTEN counts. An ESTABLISHED row with a high local port is an
|
|
297
|
+
* outbound connection of the app (any player or VPN client has dozens);
|
|
298
|
+
* what strengthens the signal is an inbound connection to the listening port
|
|
299
|
+
* from a non-loopback address.
|
|
300
|
+
*
|
|
301
|
+
* A uid is not a package: many packages can share one (sharedUserId,
|
|
302
|
+
* android.uid.system = 1000). A uid spread over more than SHARED_UID_LIMIT
|
|
303
|
+
* packages is attributed to none of them and reported in `unattributed`.
|
|
304
|
+
*/
|
|
305
|
+
async function netListeners(serial) {
|
|
306
|
+
let out = '';
|
|
307
|
+
try {
|
|
308
|
+
out = await adbSh(serial, markedCommand({
|
|
309
|
+
uidmap: 'pm list packages -U',
|
|
310
|
+
tcp: 'cat /proc/net/tcp /proc/net/tcp6',
|
|
311
|
+
}));
|
|
312
|
+
} catch (e) {
|
|
313
|
+
return { ok: false, byPackage: {}, byUid: {}, unattributed: [],
|
|
314
|
+
note: `⚠ listening sockets not collected (${e.message}) - the network signal is NOT included in the protected set` };
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const m = splitMarked(out);
|
|
318
|
+
if (!String(m.tcp || '').trim())
|
|
319
|
+
return { ok: false, byPackage: {}, byUid: {}, unattributed: [],
|
|
320
|
+
note: '⚠ /proc/net/tcp is empty or not readable by the shell - the network signal is NOT included in the protected set' };
|
|
321
|
+
|
|
322
|
+
const byUid = {};
|
|
323
|
+
for (const line of String(m.uidmap || '').split('\n')) {
|
|
324
|
+
const mm = /^package:(\S+)\s+uid:(\d+)/.exec(line.trim());
|
|
325
|
+
if (mm) (byUid[mm[2]] = byUid[mm[2]] || []).push(mm[1]);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
const listening = {}; // uid -> Set(port)
|
|
329
|
+
const established = []; // {uid, lport, peer}
|
|
330
|
+
for (const line of String(m.tcp || '').split('\n')) {
|
|
331
|
+
const f = line.trim().split(/\s+/);
|
|
332
|
+
if (f.length < 8 || !/^\d+:$/.test(f[0])) continue;
|
|
333
|
+
const [lAddr, lPortHex] = f[1].split(':');
|
|
334
|
+
const [rAddr, rPortHex] = f[2].split(':');
|
|
335
|
+
const state = f[3];
|
|
336
|
+
const uid = f[7];
|
|
337
|
+
const lport = parseInt(lPortHex, 16);
|
|
338
|
+
if (state === '0A') {
|
|
339
|
+
(listening[uid] = listening[uid] || new Set()).add(lport);
|
|
340
|
+
} else if (state === '01') {
|
|
341
|
+
established.push({ uid, lport, peer: decodeAddr(rAddr), rport: parseInt(rPortHex, 16) });
|
|
342
|
+
}
|
|
343
|
+
void lAddr;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const SHARED_UID_LIMIT = 3;
|
|
347
|
+
const byPackage = {};
|
|
348
|
+
const unattributed = [];
|
|
349
|
+
|
|
350
|
+
for (const [uid, ports] of Object.entries(listening)) {
|
|
351
|
+
const owners = byUid[uid] || [];
|
|
352
|
+
const portList = Array.from(ports).sort((a, b) => a - b);
|
|
353
|
+
|
|
354
|
+
const external = [];
|
|
355
|
+
for (const e of established) {
|
|
356
|
+
if (e.uid !== uid || !ports.has(e.lport)) continue;
|
|
357
|
+
if (isLoopback(e.peer)) continue; // a client on the device itself is not an outside dependency
|
|
358
|
+
const tag = `${e.peer} -> :${e.lport}`;
|
|
359
|
+
if (!external.includes(tag)) external.push(tag);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
if (!owners.length || owners.length > SHARED_UID_LIMIT) {
|
|
363
|
+
unattributed.push({
|
|
364
|
+
uid,
|
|
365
|
+
ports: portList,
|
|
366
|
+
serving: external,
|
|
367
|
+
owners: owners.length,
|
|
368
|
+
why: owners.length
|
|
369
|
+
? `${owners.length} packages share this uid - /proc/net/tcp does not show which one is listening`
|
|
370
|
+
: 'no installed package owns this uid (system daemon outside the package model)',
|
|
371
|
+
});
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
for (const pkg of owners) {
|
|
376
|
+
const entry = byPackage[pkg] || { ports: [], serving: [] };
|
|
377
|
+
entry.ports = Array.from(new Set([...entry.ports, ...portList])).sort((a, b) => a - b);
|
|
378
|
+
for (const tag of external) if (!entry.serving.includes(tag)) entry.serving.push(tag);
|
|
379
|
+
byPackage[pkg] = entry;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const note = unattributed.length
|
|
384
|
+
? `ℹ network signal: ${unattributed.length} listening uid${unattributed.length === 1 ? '' : 's'} not attributed to a package ` +
|
|
385
|
+
`(shared uid, or a daemon outside the package model) - these sockets are NOT included in the protected set, see net_unattributed`
|
|
386
|
+
: null;
|
|
387
|
+
|
|
388
|
+
return { ok: true, byPackage, byUid, unattributed, note };
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Which of the given packages serve a client outside the device right now.
|
|
393
|
+
* Pure: works on a netListeners() snapshot.
|
|
394
|
+
*
|
|
395
|
+
* One criterion with two halves: the package LISTENs, and the same local port
|
|
396
|
+
* has an ESTABLISHED connection from a non-loopback address. A listening
|
|
397
|
+
* socket alone is no reason (it would make every torrent client undisableable),
|
|
398
|
+
* an ESTABLISHED row alone is usually outbound, and a loopback peer is a
|
|
399
|
+
* client on the device itself.
|
|
400
|
+
*
|
|
401
|
+
* ⚠ Sockets of a shared uid cannot be attributed, but passing them silently
|
|
402
|
+
* would be the very gap the guard closes: when such a uid serves an outside
|
|
403
|
+
* client and the target package is in it, that is a hit with attributed:
|
|
404
|
+
* false. force_network lifts it like any other hit, and the refusal says the
|
|
405
|
+
* link is unproven.
|
|
406
|
+
*/
|
|
407
|
+
function servingHits(net, packages) {
|
|
408
|
+
const want = new Set((packages || []).filter(Boolean));
|
|
409
|
+
const hits = [];
|
|
410
|
+
if (!net || !want.size) return hits;
|
|
411
|
+
|
|
412
|
+
for (const [pkg, info] of Object.entries(net.byPackage || {})) {
|
|
413
|
+
if (!want.has(pkg)) continue;
|
|
414
|
+
const serving = (info && info.serving) || [];
|
|
415
|
+
if (!serving.length) continue;
|
|
416
|
+
hits.push({
|
|
417
|
+
package: pkg,
|
|
418
|
+
attributed: true,
|
|
419
|
+
ports: (info.ports || []).slice(),
|
|
420
|
+
serving: serving.slice(),
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
const byUid = net.byUid || {};
|
|
425
|
+
for (const u of (net.unattributed || [])) {
|
|
426
|
+
if (!u.serving || !u.serving.length) continue;
|
|
427
|
+
for (const pkg of (byUid[u.uid] || [])) {
|
|
428
|
+
if (!want.has(pkg)) continue;
|
|
429
|
+
if (hits.some(h => h.package === pkg)) continue;
|
|
430
|
+
hits.push({
|
|
431
|
+
package: pkg,
|
|
432
|
+
attributed: false,
|
|
433
|
+
uid: u.uid,
|
|
434
|
+
owners: (byUid[u.uid] || []).length,
|
|
435
|
+
ports: (u.ports || []).slice(),
|
|
436
|
+
serving: u.serving.slice(),
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
return hits.sort((a, b) => a.package.localeCompare(b.package));
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/** The network guard refusal: one text for disable, uninstall, stop and clear. */
|
|
445
|
+
function servingRefusal(hits, what) {
|
|
446
|
+
const lines = hits.map(h => h.attributed
|
|
447
|
+
? ` · ${h.package} - listening on ${h.ports.length > 1 ? 'ports' : 'port'} ${h.ports.join(', ')}, ` +
|
|
448
|
+
`currently serving ${h.serving.join(', ')}`
|
|
449
|
+
: ` · ${h.package} - uid ${h.uid} is listening on ${h.ports.join(', ')} and currently serving ` +
|
|
450
|
+
`${h.serving.join(', ')}. ${h.owners} packages share this uid; /proc/net does not show ` +
|
|
451
|
+
`which one holds the socket, and there is no evidence it is NOT ${h.package}`);
|
|
452
|
+
|
|
453
|
+
const unproven = hits.some(h => !h.attributed);
|
|
454
|
+
|
|
455
|
+
return `REFUSED (${what}): the package is currently serving a client outside the device.\n` +
|
|
456
|
+
lines.join('\n') + '\n' +
|
|
457
|
+
(unproven
|
|
458
|
+
? 'Some hits are unproven: the socket belongs to a shared uid and cannot be tied to one package.\n'
|
|
459
|
+
: '') +
|
|
460
|
+
'Nothing was applied. Repeat with force_network: true to override; ' +
|
|
461
|
+
'`force` does not lift this guard.';
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Packages that REGISTER an account authenticator. `dumpsys account` prints
|
|
466
|
+
* AuthenticatorDescription {type=com.example.account},
|
|
467
|
+
* ComponentInfo{com.example.auth/com.example.auth.AuthenticationService}
|
|
468
|
+
* so the package serving each account type is read, not guessed from its name
|
|
469
|
+
* (an authenticator package may carry no account-like word at all).
|
|
470
|
+
*/
|
|
471
|
+
async function authProviders(serial) {
|
|
472
|
+
let out = '';
|
|
473
|
+
try {
|
|
474
|
+
out = await adbSh(serial, 'dumpsys account 2>/dev/null | grep AuthenticatorDescription');
|
|
475
|
+
} catch (e) {
|
|
476
|
+
return { byPackage: {}, note: `⚠ authenticator providers not collected (${e.message}) - the signal is NOT included in the protected set` };
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
const byPackage = {};
|
|
480
|
+
const re = /AuthenticatorDescription\s*\{\s*type=([^}]*)\}[\s\S]{0,40}?ComponentInfo\{([^/}]+)\//g;
|
|
481
|
+
let mm;
|
|
482
|
+
while ((mm = re.exec(out)) !== null) {
|
|
483
|
+
const type = mm[1].trim();
|
|
484
|
+
const pkg = mm[2].trim();
|
|
485
|
+
if (!looksLikePackage(pkg)) continue;
|
|
486
|
+
const list = byPackage[pkg] = byPackage[pkg] || [];
|
|
487
|
+
if (type && !list.includes(type)) list.push(type);
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
if (!Object.keys(byPackage).length)
|
|
491
|
+
return { byPackage: {}, note: '⚠ dumpsys account lists no authenticator - the signal is NOT included in the protected set' };
|
|
492
|
+
|
|
493
|
+
return { byPackage, note: null };
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Derive the protected set from the device. Each source is probed separately,
|
|
498
|
+
* does not take the others down, and leaves a note when it did not work.
|
|
499
|
+
*/
|
|
500
|
+
async function protectedSet(serial, opts = {}) {
|
|
501
|
+
const props = opts.props || await getProps(serial);
|
|
502
|
+
const sources = {};
|
|
503
|
+
const notes = [];
|
|
504
|
+
|
|
505
|
+
/** Probe a source, validate it, record a failure in notes. */
|
|
506
|
+
async function derive(key, cmd, extract, humanName) {
|
|
507
|
+
let out;
|
|
508
|
+
try {
|
|
509
|
+
out = await adbSh(serial, cmd);
|
|
510
|
+
} catch (e) {
|
|
511
|
+
notes.push(`⚠ ${humanName}: probe failed (${e.message}) - source NOT included in the protected set`);
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
const raw = extract(out);
|
|
515
|
+
const pkg = pkgOf(raw);
|
|
516
|
+
if (!looksLikePackage(pkg)) {
|
|
517
|
+
// An empty answer usually means the subsystem does not exist on this
|
|
518
|
+
// device (no WebView provider on a watch); garbage means it could not be
|
|
519
|
+
// determined. The notes say which.
|
|
520
|
+
const empty = !String(raw || '').trim();
|
|
521
|
+
notes.push(empty
|
|
522
|
+
? `ℹ ${humanName}: the subsystem is absent on this device (empty answer) - source not applicable, not included in the protected set`
|
|
523
|
+
: `⚠ ${humanName}: not determined (answer: ${JSON.stringify(String(raw || '').slice(0, 80))}) - source NOT included in the protected set`);
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
sources[key] = pkg;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
const lastLine = out => String(out).trim().split('\n').filter(Boolean).pop() || '';
|
|
530
|
+
|
|
531
|
+
// Launcher: resolve-activity exists from SDK 28.
|
|
532
|
+
await derive('launcher',
|
|
533
|
+
'cmd package resolve-activity --brief -a android.intent.action.MAIN -c android.intent.category.HOME 2>/dev/null',
|
|
534
|
+
lastLine, 'launcher');
|
|
535
|
+
|
|
536
|
+
// Package installer: content:// works on SDK 29+; file:// does not resolve
|
|
537
|
+
// there and stays as the fallback for older firmware.
|
|
538
|
+
{
|
|
539
|
+
const variants = [
|
|
540
|
+
'cmd package resolve-activity --brief -a android.intent.action.INSTALL_PACKAGE -d content://x/y.apk 2>/dev/null',
|
|
541
|
+
'cmd package resolve-activity --brief -a android.intent.action.INSTALL_PACKAGE -d file:///x.apk 2>/dev/null',
|
|
542
|
+
];
|
|
543
|
+
let found = null;
|
|
544
|
+
for (const v of variants) {
|
|
545
|
+
try {
|
|
546
|
+
const pkg = pkgOf(lastLine(await adbSh(serial, v)));
|
|
547
|
+
if (looksLikePackage(pkg)) { found = pkg; break; }
|
|
548
|
+
} catch { /* try the next form */ }
|
|
549
|
+
}
|
|
550
|
+
if (found) sources.installer = found;
|
|
551
|
+
else notes.push('⚠ package installer: not determined via content:// or file:// - source NOT included in the protected set');
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
// Active IME
|
|
555
|
+
await derive('ime', 'settings get secure default_input_method',
|
|
556
|
+
out => String(out).trim(), 'active IME');
|
|
557
|
+
|
|
558
|
+
// WebView provider
|
|
559
|
+
await derive('webview', 'dumpsys webviewupdate 2>/dev/null | head -n 12',
|
|
560
|
+
out => {
|
|
561
|
+
const m = String(out).match(/Current WebView package \(name, version\):\s*\(([^,\s)]+)/);
|
|
562
|
+
return m ? m[1] : '';
|
|
563
|
+
}, 'WebView provider');
|
|
564
|
+
|
|
565
|
+
// Roles
|
|
566
|
+
const roles = await roleHolders(serial, props.sdk);
|
|
567
|
+
if (roles.holders.length) {
|
|
568
|
+
sources.roles = roles.holders;
|
|
569
|
+
sources.roles_detail = roles.detail;
|
|
570
|
+
}
|
|
571
|
+
if (roles.note) notes.push(roles.note);
|
|
572
|
+
|
|
573
|
+
const pkgs = opts.packages || await listPackages(serial);
|
|
574
|
+
|
|
575
|
+
// Listening sockets and authenticator providers, two derived signals.
|
|
576
|
+
//
|
|
577
|
+
// The line is drawn by reversibility, not importance:
|
|
578
|
+
// • a system package goes into the set (hard to bring back, its loss is
|
|
579
|
+
// hard to notice);
|
|
580
|
+
// • a user package gets an advisory, not a refusal: its APK can be backed
|
|
581
|
+
// up and installed again.
|
|
582
|
+
// Without that line the network signal would make every torrent server or
|
|
583
|
+
// VPN client undisableable, and the authenticator signal every file manager
|
|
584
|
+
// that registers one.
|
|
585
|
+
//
|
|
586
|
+
// ⚠ A user package that serves an outside client right now is still stopped
|
|
587
|
+
// by the network guard in apps.js, which applies regardless of system/user.
|
|
588
|
+
const advisories = [];
|
|
589
|
+
const net = await netListeners(serial);
|
|
590
|
+
if (net.note) notes.push(net.note);
|
|
591
|
+
const auth = await authProviders(serial);
|
|
592
|
+
if (auth.note) notes.push(auth.note);
|
|
593
|
+
|
|
594
|
+
const netSystem = {}, authSystem = {};
|
|
595
|
+
for (const [pkg, info] of Object.entries(net.byPackage)) {
|
|
596
|
+
const where = `listening on port${info.ports.length > 1 ? 's' : ''} ${info.ports.join(', ')}` +
|
|
597
|
+
(info.serving.length ? `; currently serving ${info.serving.join(', ')}` : '');
|
|
598
|
+
if (pkgs.system.has(pkg)) netSystem[pkg] = info;
|
|
599
|
+
else advisories.push({ package: pkg, signal: 'net_listener', system: false, detail: `${where} - something outside the device may depend on it` });
|
|
600
|
+
}
|
|
601
|
+
for (const [pkg, types] of Object.entries(auth.byPackage)) {
|
|
602
|
+
const where = `registers an account authenticator: ${types.join(', ')}`;
|
|
603
|
+
if (pkgs.system.has(pkg)) authSystem[pkg] = types;
|
|
604
|
+
else advisories.push({ package: pkg, signal: 'authenticator', system: false, detail: `${where} - disabling it can take the linked accounts with it` });
|
|
605
|
+
}
|
|
606
|
+
if (Object.keys(netSystem).length) sources.net_listener = netSystem;
|
|
607
|
+
if (net.unattributed && net.unattributed.length) sources.net_unattributed = net.unattributed;
|
|
608
|
+
if (Object.keys(authSystem).length) sources.authenticator = authSystem;
|
|
609
|
+
|
|
610
|
+
// Account stack by name, a supplementary source: it catches libraries that
|
|
611
|
+
// register no authenticator themselves and so are invisible above.
|
|
612
|
+
const accountLike = pkgs.all.filter(p => ACCOUNT_HINTS.some(re => re.test(p)));
|
|
613
|
+
if (accountLike.length) sources.account_like = accountLike;
|
|
614
|
+
|
|
615
|
+
const set = new Set(CORE_PROTECTED);
|
|
616
|
+
for (const p of pkgs.all) {
|
|
617
|
+
if (CORE_PREFIXES.some(pref => p.startsWith(pref))) set.add(p);
|
|
618
|
+
}
|
|
619
|
+
for (const key of ['launcher', 'installer', 'ime', 'webview']) {
|
|
620
|
+
if (sources[key]) set.add(sources[key]);
|
|
621
|
+
}
|
|
622
|
+
for (const p of roles.holders) set.add(p);
|
|
623
|
+
for (const p of Object.keys(netSystem)) set.add(p);
|
|
624
|
+
for (const p of Object.keys(authSystem)) set.add(p);
|
|
625
|
+
for (const p of accountLike) set.add(p);
|
|
626
|
+
|
|
627
|
+
if (accountLike.length)
|
|
628
|
+
notes.push('account_like is a package-name heuristic (supplementary source) - check it by eye in dry_run');
|
|
629
|
+
if (advisories.length)
|
|
630
|
+
notes.push(`${advisories.length} user package${advisories.length === 1 ? '' : 's'} with external-connectivity signals - not protected (reversible via APK backup), see advisories`);
|
|
631
|
+
for (const n of (props.notes || [])) notes.push(n);
|
|
632
|
+
|
|
633
|
+
return {
|
|
634
|
+
packages: Array.from(set).sort(),
|
|
635
|
+
sources,
|
|
636
|
+
advisories,
|
|
637
|
+
notes,
|
|
638
|
+
// The raw network snapshot, for the guard in apps.js, so /proc/net is not
|
|
639
|
+
// read twice in one call. Not part of action=protected: sources already
|
|
640
|
+
// show the same.
|
|
641
|
+
net,
|
|
642
|
+
};
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
/**
|
|
646
|
+
* What is on top now: used by both launch paths and by verify on adb_tap,
|
|
647
|
+
* adb_swipe and adb_key, so it lives here and apps.js and ui.js never require
|
|
648
|
+
* each other.
|
|
649
|
+
*
|
|
650
|
+
* The grep pattern (`mResumedActivity|ResumedActivity`) is the one accepted on
|
|
651
|
+
* devices; `topResumedActivity` (SDK 36) does not match it. The component is
|
|
652
|
+
* extracted by a separate regex; none found is null, not a guess.
|
|
653
|
+
*/
|
|
654
|
+
async function resumedActivity(serial) {
|
|
655
|
+
const resumedLine = (await adbSh(serial,
|
|
656
|
+
'dumpsys activity activities 2>/dev/null | grep -m1 -E "mResumedActivity|ResumedActivity"')).trim();
|
|
657
|
+
const focusLine = (await adbSh(serial,
|
|
658
|
+
'dumpsys window 2>/dev/null | grep -m1 mCurrentFocus')).trim();
|
|
659
|
+
const raw = resumedLine || focusLine;
|
|
660
|
+
const m = /([A-Za-z0-9_.]+\/[A-Za-z0-9_.$]+)/.exec(raw);
|
|
661
|
+
return { raw, component: m ? m[1] : null, resumedLine, focusLine };
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
module.exports = {
|
|
665
|
+
CORE_PROTECTED, CORE_PREFIXES, ACCOUNT_HINTS, PKG_RE, LOCALE_RE,
|
|
666
|
+
getProps, listPackages, accountSnapshot, protectedSet, roleHolders,
|
|
667
|
+
netListeners, servingHits, servingRefusal, authProviders, decodeAddr, isLoopback,
|
|
668
|
+
pkgOf, looksLikePackage, splitMarked, markedCommand, resumedActivity,
|
|
669
|
+
};
|