ADB Device
An Android phone over USB, with no adb installed: the page speaks the Android Debug Bridge protocol itself, and the local agent only claims the phone's USB debugging interface and moves the bytes. It lists the apps (and pulls their APKs into the APK tool), browses files (an app's private files too, with run-as, for debuggable apps), opens a shell, captures logcat, takes screenshots, installs APKs and makes TCP tunnels like adb forward and adb reverse.
The phone allows USB debugging only from a key it has approved. The keys are kept in this browser and managed in Settings; import ~/.android/adbkey to be accepted by phones that already trust your computer's adb. Only one program can use the debugging interface at a time: stop the adb server (adb kill-server) first.
Contents
- The protocol
- The library: AdbDevice, AdbKeys, AdbShell (lib/adb.js)
- Tunnels: AdbTunnels (lib/adb-tunnels.js)
1The protocol
Every message is a 24-byte header (command, two arguments, data length, data checksum, the command with every bit flipped) followed by the data. The host sends CNXN; the phone answers AUTH with a 20-byte token. The host signs it with its RSA key (PKCS#1 v1.5, as if the token were a SHA-1 digest); if the phone doesn't know the key it asks again, and the host sends its public key, which the phone shows to the user for approval. The phone then sends its own CNXN with a banner of properties and features.
Everything else is a stream: OPEN names a service (shell:, exec:getprop, sync:, tcp:8080, reverse:forward:…), OKAY accepts it, WRTE carries data (each acknowledged by an OKAY before the next) and CLSE ends it. Files go over the sync: service, with its own requests (LIST, STAT, RECV, SEND).
2The library: AdbDevice, AdbKeys, AdbShell (lib/adb.js)
lib/adb.js is the protocol with no user interface, as a JavaScript module. It imports lib/crypto/md5.js, lib/base64.js and lib/pem.js itself, keeps its keys in localStorage and makes them with WebCrypto. The transport is any object with read(maxLength) (a Promise of 1 to maxLength bytes), write(bytes) and close(): a USB bulk interface, a TCP socket to adbd, or a fake device for tests (tests/adb-mock.js).
import { AdbDevice, AdbKeys, AdbShell } from "../lib/adb.js";
AdbDevice: the connection
| Method | What it does |
| AdbDevice.connect(transport, key, { onAuth }) | Connects and authenticates; resolves with the device. onAuth() is called when the user has to allow the key on the phone. |
| device.banner, device.auth, device.maxData | { state, props, features } from the phone's banner; "signature" (already trusted), "approved" or "none"; the largest message. |
| device.exec(command), device.shell(command) | A command's whole output, as a Uint8Array (exec: has no terminal; shell: goes through the shell). |
| device.open(service) | An AdbStream: ondata, onclose, write(bytes), close(), read(length), readAll(), takeQueued(), local, remote, closed. |
| device.service(name) | The reply of an adbd request service such as reverse:list-forward; throws with the phone's message on failure. |
| device.onopen, device.onclose, device.close() | Streams the phone opens (adb reverse): return false to refuse; the connection breaking; closing it. |
| device.sync() | An AdbSync, for files: list(path) → [{ name, mode, size, time, dir, link }], stat(path), pull(path, onProgress) → Uint8Array, push(path, bytes, mode, onProgress), close(). |
| AdbDevice.parseBanner(text) | "device::ro.product.model=…;features=…" → { state, props, features }. |
| AdbDevice.USB_INTERFACE | The ADB USB interface: class 0xff, subclass 0x42, protocol 1. |
AdbKeys: the keys kept in this browser
| Method | What it does |
| AdbKeys.loadKey() | The active key (a new 2048-bit RSA key the first time), with its Android public key line. |
| AdbKeys.list(), AdbKeys.create(name), AdbKeys.setActive(id), AdbKeys.rename(id, name), AdbKeys.remove(id), AdbKeys.forgetKey() | The stored keys (without their private parts), a new one, the one used to connect, its name (the phone shows it), deleting one or the active one. |
| AdbKeys.noteDevice(id, serial, { model, how }), AdbKeys.forgetDevice(id, serial) | The record of which phones a key connected to, and how they authorized it. |
| AdbKeys.importPem(text, name), AdbKeys.exportPem(id), AdbKeys.publicKeyFile(id) | An adbkey file in (PKCS#8 or PKCS#1 PEM) and out (PKCS#8 PEM); the adbkey.pub line. |
| AdbKeys.fingerprint(publicKeyLine) | The MD5 fingerprint the phone shows in its prompt. |
| AdbKeys.sign(key, token), AdbKeys.androidPublicKey(key, name) | The token signature, and the key in Android's format (libmincrypt's RSAPublicKey, in Base64, then the name). |
| AdbKeys.STORAGE_KEY | The localStorage key of the key store. |
AdbShell: reading the output of shell commands
| Method | What it does |
| AdbShell.parseGetprop, parsePackageList, parsePackagePaths, parsePackageDump, parseLs | The output of getprop, pm list packages -f, pm path, dumpsys package and ls -la, as data. |
| AdbShell.quote(text), AdbShell.runAsError(text), AdbShell.logcatLevel(line) | One shell word in single quotes; run-as's complaint in words; the level letter of a logcat -v threadtime line. |
import { AdbDevice, AdbKeys, AdbShell } from "../lib/adb.js";
const key = await AdbKeys.loadKey();
const device = await AdbDevice.connect(transport, key, { onAuth: () => console.log("Allow the key on the phone") });
const props = AdbShell.parseGetprop(new TextDecoder().decode(await device.exec("getprop")));
const files = await device.sync();
const apk = await files.pull("/data/app/~~x==/com.example-1/base.apk");
await files.close();
3Tunnels: AdbTunnels (lib/adb-tunnels.js)
import { AdbTunnels } from "../lib/adb-tunnels.js"; makes TCP tunnels over a connected AdbDevice, with the agent (lib/agent.js) doing the TCP side. It has no imports: the device and the agent connection are given to it.
| Method | What it does |
| new AdbTunnels({ device, agent, onchange }) | A tunnel set. device() and agent() return the current device and agent connection; onchange() is called when rules or traffic change. |
| tunnels.addForward({ bind, port, remote }) | The agent listens on bind:port (0: a free port); each connection opens remote on the phone (tcp:8080, localabstract:name, jdwp:pid…). |
| tunnels.addReverse({ remote, host, port }) | The phone listens on remote (tcp:8080, tcp:0 for a free port); each connection comes back and the agent connects it to host:port. |
| tunnels.removeForward(rule), tunnels.removeReverse(rule), tunnels.refreshReverse() | Removing rules; reading the phone's reverse rules (those not made here have ours: false). |
| tunnels.attach(device), tunnels.stop() | Routing the streams a newly connected device opens; dropping every rule when the device goes away. |
| tunnels.forwards, tunnels.reverses | The rules: { id, kind, bind, port, remote, host, ours, error, stats: { open, total, up, down } }. |
import { AdbTunnels } from "../lib/adb-tunnels.js";
const tunnels = new AdbTunnels({ device: () => device, agent: () => agent, onchange: () => console.log(tunnels.forwards) });
tunnels.attach(device);
await tunnels.addForward({ bind: "127.0.0.1", port: 9222, remote: "localabstract:chrome_devtools_remote" });