I am having an issue with a JavaScript script to be executed in Frida. In this case, I am intercepting an input from a function where I receive an array of bytes. I need to convert this array to a string, modify it, and then convert it back to an array of bytes before returning the value.

I managed to make the first step work correctly, which is converting the array of bytes into a string and modifying the string. However, the problem arises when I try to convert the string back into an array of bytes; it throws an error. I tried using the Java native function getBytes(), but it gives the error below:

TypeError: not a function

My code is:

//result is byte[]
let StringJava = Java.use("java.lang.String");
var json = StringJava.$new(result);
try {
  let arg1 = StringJava.$new(": 0,");
  let arg2 = StringJava.$new(": 1000,");
  json = json.replace(arg1,arg2);
  let newByte = json.getBytes(); //error in line
  return newByte;
} catch (error) {
  console.log("error:", error);

Is there a simple way for me to convert this string back into an array of bytes?


1 Answer 1


The only problem of your code seems to that frida wants to help you too much:

Before you call json = json.replace(arg1,arg2); json is an instance of the Java type java.lang.String.

When you assign the return value of the replace method to json, frida seems to automatically convert the Java String to a JavaScript string (I assume for convenience).

If you then try to call getBytes() on the JavaScript string it fails because JavaScript strings don't have this method. As workaround you could change the line to json = StringJava.$new(json).getBytes(), however that would require a lot of unnecessary conversions.

In the end I would recommend to do the whole string replacement with JavaScript code and only for calling getBytes() make it a Java string.

My complete and working test script is this:

Java.perform(() => {

    function test(result) {
        let StringJava = Java.use("java.lang.String");
        var json = String(result); // may be unnecessary, just to make sure json is a JavaScript string
        try {
            let arg1 = ": 0,";
            let arg2 = ": 1000,";
            json = json.replace(arg1, arg2);
            return StringJava.$new(json).getBytes();
        } catch (error) {
            console.log("error:", error);

    var bytes = test('{ "a": 0, "b": 1 }');
    // do something with bytes


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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