Frida: Question: How to read a JSON file in Javascript code on Android

Created on 16 Jan 2018  路  3Comments  路  Source: frida/frida

Hello everyone,

I want to load the methods that need to be hooked from a JSON file stored on android device. How do I do that?
I tried using the new File(filePath, mode) in the but it says that it can't find the file or directory. I can't make a file either, it gives the error of Read only file system.

The device is rooted and the file path is /data/local/tmp/file.json.

I also tried sending the file using script.post() but I get only half file, may be because of some size limit.
I will appreciate any help. Let me know if you need more info, as I am new to frida and probably missed some needed info.

Cheers.
Waqar

Most helpful comment

For those of you who are visiting this issue for similar reason as mine, here is an example script to open a text file on android and send it to host.

'use strict';

const fs = require("frida-fs");

Java.perform(function () {
    var readStream = fs.createReadStream("/path/to/file.txt");
    var text = "";
    readStream
        .on('readable', function () {
            var chunk;
            while (null !== (chunk = readStream.read())) {
                text = text.concat(chunk);
            }
        })
        .on('end', function () {
            send(text);
        });
});

Below are roughly the steps I followed:

To compile this code, first install frida-compile. Thats how I did it:
sudo npm install frida-compile -g

And then create a NodeJS project with npm init in the directory you want your project to be. Then install frida-fs using npm install frida-fs. Then make the app.js( or whatever file you want to use as main script) and paste the above code into it.

Compile this script using frida-compile app.js -o payload.js.
Then change in your binding code, use payload.js as your JavaScript file. e.g in mine it looks like this:
script = process.create_script(open("path/to/payload.js").read())

I hope its helpful.

Waqar

All 3 comments

Hi,

If you want a persistent on-device solution take a look at Gadget's script mode.

The File API doesn't support reading just yet (PR welcome), but for non-trivial agents it's a better idea to use frida-compile anyway and use the frida-fs module.

Cheers!

Thanks @oleavr . Another question:
But how do I execute the compiled binary? Do I launch it from python bindings just like the JavaScript or some other way?

UPDATE: My bad, so far according to my understanding it generates a single Javascript file from the nodejs project, which can then be loaded using "create_script(fd.read())" in python binding or its counterparts in other bindings.

Waqar

For those of you who are visiting this issue for similar reason as mine, here is an example script to open a text file on android and send it to host.

'use strict';

const fs = require("frida-fs");

Java.perform(function () {
    var readStream = fs.createReadStream("/path/to/file.txt");
    var text = "";
    readStream
        .on('readable', function () {
            var chunk;
            while (null !== (chunk = readStream.read())) {
                text = text.concat(chunk);
            }
        })
        .on('end', function () {
            send(text);
        });
});

Below are roughly the steps I followed:

To compile this code, first install frida-compile. Thats how I did it:
sudo npm install frida-compile -g

And then create a NodeJS project with npm init in the directory you want your project to be. Then install frida-fs using npm install frida-fs. Then make the app.js( or whatever file you want to use as main script) and paste the above code into it.

Compile this script using frida-compile app.js -o payload.js.
Then change in your binding code, use payload.js as your JavaScript file. e.g in mine it looks like this:
script = process.create_script(open("path/to/payload.js").read())

I hope its helpful.

Waqar

Was this page helpful?
0 / 5 - 0 ratings