3

I'm learning frida and trying to hook a function that looks like:

`.method public final fn()[B`

It returns a byte array. Here's my code:

Java.perform(function () {
    var test = Java.use("com...");
    test.fn.overload().implementation = function () {
        var ret = this.fn();
        console.log("how to write here?");
        return ret;
    };
});

How to print the ret variable returned by the function? It's a byte[]. I tried console.log but it only prints a [object], and hexdump complains 'expected a pointer'. How can I print the array?

3

 

var ret = this.fn();
var buffer = Java.array('byte', ret);
console.log(buffer.length);
var result = "";
for(var i = 0; i < buffer.length; ++i){
    result+= (String.fromCharCode(buffer[i]));
}
console.log(result);
2

Frida's java byte array element representation is signed number currently. Therefore, your byte array may contain negative number. We should edit accepted answer to reflect this:

var buffer = Java.array('byte', ret);
var result = "";
for(var i = 0; i < buffer.length; ++i){
    result += (String.fromCharCode(buffer[i] & 0xff)); // here!!
}
console.log(result);

Related https://stackoverflow.com/a/9609447/9304999 and https://github.com/frida/frida/issues/289

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.