Search code examples
frida

frida modify static param as ArrayList


public class OBDSportsModelManager {
    public static ArrayList<DataArray> mDiagnosisCommand;
    public boolean getData() {
        mDiagnosisCommand = new ArrayList<>();
        for (String dataArray : this.commandIDs) {
            mDiagnosisCommand.add(new DataArray(dataArray));
        }
        return true;
    }
}

I want to add some more item to the 'mDiagnosisCommand',

by using this code:

sports.getData.implementation = function(){
    Log.v("hook-sports", "try to add obd commands!");
    var ret = this.getData();
    var DataArray = Java.use("com.obd2.comm.DataArray");
    var items = DataArray.$new("0x00,0x00,0x00,0x00,0x00,0x42");

    this.mDiagnosisCommand.add(items);  // not working!!!

    Log.v("hook-sports", "hook done!");

    return ret;
}

but doesn't works well.

I googled frida ArrayList add items without any help.


Solution

  • You have two problems:

    1. You are using this.mDiagnosisCommand but the field is a static field, therefore it belongs to the class OBDSportsModelManager and not to the class instance this.
    2. By calling this.mDiagnosisCommand you only get the Frida object representing this field, not the field value itself. If you want the ArrayList referenced by a field you have to add .value.

    Considering both problems the following lines should work (after correcting the class name):

    // correct the class name in the next line
    var cls = Java.use("<full.name.to>.OBDSportsModelManager"); 
    cls.mDiagnosisCommand.value.add(items);