Search code examples
javascriptjavaarraysfrida

Constructing a string array with Frida


I'm trying to call a function with Frida that takes a string array as one of its arguments.

public void coolFunction(long value, String[] strArr);

Within Java it gets called like this:

long importantValue = 4L;
String[] importantArr = new String[]{"TEST"};
coolFunction(importantValue, importantArr);

The overload looks like this: .overload('long', '[Ljava.lang.String;')

I could probably create a string array from scratch, but I don't know how to express it in Javascript. What's the Frida equivalent of new String[]{"TEST"}?

Because of that I tried to turn an ArrayList<String> into a String[], which also wasn't successful.

As far as I can tell there are two simple ways to turn ArrayList<String> into String[]:

Attempt #1:

List<String> list = new ArrayList<String>();
list.add("TEST");
String[] stringArray = list.toArray(new String[0]);

If I try to express it with Javascript it looks like this:

var AL_class = Java.use("java.util.ArrayList");
var arrList = AL_class.$new();
arrList.add("TEST");
var stringArray = arrList.toArray(Java.use("[Ljava.lang.String;").$new(0));

This fails with the following error message:

Error: no supported overloads

Attempt #2:

List<String> list = new ArrayList<String>();
list.add("TEST");
Object[] objectArray = list.toArray();
String[] stringArray = (String[]) objectArray;

Javascript:

var AL_class = Java.use("java.util.ArrayList");
var arrList = AL_class.$new();
arrList.add("TEST");
var arrayButAsObject = arrList.toArray();
var stringArray = Java.cast(arrayButAsObject, "[Ljava.lang.String;");

This fails because it assumes that I want to use Javascript's toArray() function.

The solution to this problem is probably very simple but I've been stuck here for quite a while now and can't seem to figure it out. Any help would be appreciated.


Solution

  • Instead of trying to construct a java.util.List and then convert it to an array I would use the Frida function Java.array and directly construct a Java String array:

    var javaStringArray = Java.array('java.lang.String', [ "Test" ]);