Skip to content

Instantly share code, notes, and snippets.

@oleavr
Last active January 8, 2024 16:21
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save oleavr/a22d675b76e7509cd2c9 to your computer and use it in GitHub Desktop.
Save oleavr/a22d675b76e7509cd2c9 to your computer and use it in GitHub Desktop.
How to consume npm modules from Frida agent scripts

Install Node.js 5.x, then:

npm install frida co uuid

and run:

node app.js
'use strict';
const uuid = require('uuid');
rpc.exports = {
init() {
Interceptor.attach(Module.findExportByName('libsystem_kernel.dylib', 'open'), {
onEnter(args) {
send(['open', Memory.readUtf8String(args[0])]);
}
});
return uuid.v4();
}
};
'use strict';
const co = require('co');
const frida = require('frida');
let session, script;
co(function *() {
const source = yield frida.load(require.resolve('./agent.js'));
const device = yield frida.getUsbDevice();
const pid = yield device.spawn(['com.atebits.Tweetie2']);
session = yield device.attach(pid);
script = yield session.createScript(source);
script.events.listen('message', onMessage);
yield script.load();
const api = yield script.getExports();
const id = yield api.init();
console.log('Got ID:', id);
yield device.resume(pid);
})
.catch(onError);
function onError(error) {
console.error(error.stack);
}
function onMessage(message, data) {
if (message.type === 'send') {
console.log(message.payload);
} else if (message.type === 'error') {
console.error(message.stack);
} else {
console.log(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment