Skip to content
valerio costamagna edited this page Aug 27, 2015 · 1 revision

The following snippet code define two patch methods. The patch method's first argument must be an Object type (it represents the reference to the this Object) followed by the original method's arguments.

ARTDroid allows to call the original method implementation from a patch method using Java reflection. (ARTDroid hooks all Java reflection calls, for tech details refer to the blog post)

//called when the target application call getDeviceId API's method
public static String getDeviceId(Object thiz) {
    String imeistring = "";
    Log.d(TAG, "FAKE GETDEVICEID !!");
    Log.d(TAG, "obj =: " + thiz.toString() + thiz.getClass().getName());
    Method m = null;
    try {       
        //call the original method, this call is managed by ARTDroid
        m = thiz.getClass().getMethod("getDeviceId", null);
        imeistring = (String) m.invoke(thiz, null);
        return imeistring + "!!!w00t!!!!";
    } catch (Exception e) {
        [...]
    } 
    
}

//called when the target application call openFileOutput API's method
public  static FileOutputStream openFileOutput(Object thiz, String s, int i) {
    Log.d(TAG, "FAKE OPEN FILE OUTPUT name : " + s + " mode: " + i);
    Log.d(TAG, "obj =: " + thiz.toString() + thiz.getClass().getName());
    Method m = null;
    FileOutputStream res = null;
    try {
        //call the original method, this call is managed by ARTDroid
        m = thiz.getClass().getMethod("openFileOutput",String.class, int.class);
        res = (FileOutputStream) m.invoke(thiz,s,i);
        return res;
    } catch (Exception e) {
		[...]	
	}
}

As demo, the DEX file "examples/classes.dex" defines three hooks against getDeviceId , openFileOutput , sendTextMessage Android API's methods. Creating and adding new patch method requires the following steps:

  1. write your code in Java (as the snippet above) you can use any Android API's methods as "normal"
  2. create the DEX file and push it to the "/data/local/tmp/dex/target.dex" directory. Please, before remember to delete the old DEX file (if any) from "/data/local/tmp/dex/opt"

Ok, almost done. please refer to the use library page to register the just created patch methods into the native component.

Clone this wiki locally