Skip to content

Commit

Permalink
Add enumerateApplications, and make enumerateProcesses return an object
Browse files Browse the repository at this point in the history
This is a breaking change to enumerateProcesses, but makes more sense
and is more consistent with the app equivalent.
  • Loading branch information
pimterry committed Apr 30, 2024
1 parent 4cf7055 commit 0f501b6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
9 changes: 8 additions & 1 deletion README.md
Expand Up @@ -163,8 +163,15 @@ The exact parameters returned may vary and will depend on the specific target sy

### `fridaClient.enumerateProcesses()`

Returns a promise for an array of `[pid: number, processName: string]` pairs. You can use this to query the currently running processes that can be targeted on your local machine.
Returns a promise for an array of `{ pid: number, processName: string }` objects. You can use this to query the currently running processes that can be targeted from the target Frida server.

### `fridaClient.enumerateApplications()`

Returns a promise for an array of `{ pid: number | null, id: string, name: string }` objects. You can use this to query all installed applications (running or not) that can be targeted on a target mobile device.

The pid will be `null` if the app is not running, or a numeric process id otherwise. The id is the internal app identifier (e.g. Android package id) while the name is the user-visible application name.

This will always return an empty list on non-mobile devices.

### `fridaClient.injectIntoProcess(pid: number, script: string)`

Expand Down
35 changes: 30 additions & 5 deletions src/index.ts
Expand Up @@ -52,7 +52,8 @@ export async function connect(options:

interface HostSession {
QuerySystemParameters(): Promise<DBusVariantDict>;
EnumerateProcesses(arg: {}): Promise<Array<[number, string]>>;
EnumerateProcesses(arg: {}): Promise<Array<[pid: number, name: string]>>;
EnumerateApplications(arg: {}): Promise<Array<[id: string, name: string, pid: number | 0]>>;
Attach(pid: number, options: {}): Promise<[string]>;
Spawn(program: string, options: [
hasArgv: boolean,
Expand Down Expand Up @@ -107,14 +108,38 @@ export class FridaSession {
return parseDBusVariantDict(rawMetadata);
}


/**
* List all running processes accessible to the target Frida server. Returns an array
* of [pid, process name] pairs.
* of { pid, name } objects.
*/
async enumerateProcesses(): Promise<Array<{
pid: number,
name: string
}>> {
const hostSession = await this.getHostSession();
return (await hostSession.EnumerateProcesses({})).map((proc) => ({
pid: proc[0],
name: proc[1]
}));
}

/**
* List all installed applications accessible on the target Frida server. Returns an array of
* { pid, id, name } objects, where pid is null if the application is not currently running.
*
* This is only applicable to mobile devices, and will return an empty array everywhere else.
*/
async enumerateProcesses(): Promise<Array<[number, string]>> {
async enumerateApplications(): Promise<Array<{
pid: number | null,
id: string,
name: string
}>> {
const hostSession = await this.getHostSession();
return hostSession.EnumerateProcesses({});
return (await hostSession.EnumerateApplications({})).map((proc) => ({
pid: proc[2] || null, // Not running = 0. We map it to null.
id: proc[0],
name: proc[1]
}));
}

/**
Expand Down
14 changes: 12 additions & 2 deletions test/test.spec.ts
Expand Up @@ -35,18 +35,28 @@ describe("Frida-JS", () => {
spawnedProc = undefined;
})

it("can connect to Frida and list targets", async () => {
it("can connect to Frida and list target processes", async () => {
fridaClient = await connect();
const processes = await fridaClient.enumerateProcesses();

expect(processes.length).to.be.greaterThan(0);

if (isNode) {
const thisProcess = processes.find(([pid]) => pid === process.pid)!;
const thisProcess = processes.find(({ pid }) => pid === process.pid)!;
expect(thisProcess[1]).to.equal('node');
}
});

it("can connect to Frida and list target apps", async () => {
fridaClient = await connect();

const processes = await fridaClient.enumerateApplications();

// This should work, but won't actually return anything in local testing
// because it's only applicable to mobile devices.
expect(processes.length).to.equal(0);
});

it("can connect to a Frida instance by address", async () => {
try {
await connect({ host: '127.0.0.1:12345' });
Expand Down

0 comments on commit 0f501b6

Please sign in to comment.