crmulliner / ddi

ddi - Dynamic Dalvik Instrumentation Toolkit
http://www.mulliner.org/android/
395 stars 159 forks source link

Hooking static methods #1

Closed TheBigS closed 10 years ago

TheBigS commented 10 years ago

I'm trying to hook a static method that wraps Android.Log in this application I'm playing with. I couldn't find any examples that show how to hook a static method. I took a guess at the callback function signature so I think that is where my issue might be, but I'm not sure. Little help?

I do the hook setup and the method signature is:

Logger;->i(Ljava/lang/String;Ljava/langString;)V

So I specify 2 as the number of parameters (since there is no this object for static invocations).

I've also tried setting infoHook.sm = 1 after the dalvik_hook_setup call but that didn't help either.

I then make my callback function look like this:

static void* Logger_i(JNIEnv *env, jclass clazz, jobject tag, jobject message) {
  jvalue args[2];
  args[0].l = tag;
  args[1].l = message;

  dalvik_prepare(&d, &infoHook, env);
  (*env)->CallStaticVoidMethodA(env, clazz, infoHook.mid, args);
  dalvik_postcall(&d, &infoHook);

  // ... snip printers that just log the tag/message to my .log file
}
TheBigS commented 10 years ago

Ah nevermind, I wrote a simple test application that just had the following:

I changed my hooking code to latch onto this method call and it seems to be working. I think the wrapper class Logger I found in the application I'm playing with is just never used by the application. The hook method won't get called if the real method never gets called, doh!

Outstanding question though, when is it necessary to set the dalvik_hook_t.sm flag? Also, it may be helpful to others to add an example static method hook to the readme

crmulliner commented 10 years ago

On 01/27/2014 09:28 AM, TheBigS wrote:

Ah Nevermind, I wrote a simple test application that just had the following:

  • A class Logger with a static method public static void i(String tag, String message) which just calls android.util.Log.i(tag, message)
  • An onCreate() that kicked off a thread that called Logger.i("Test", "The time is: " + new Date().toString()); every 2 seconds

I changed my hooking code to latch onto this method call and it seems to be working. I think the wrapper class Logger I found in the application I'm playing with is just never used by the application. The hook method won't get called if the real method never gets called, doh!

Outstanding question though, when is it necessary to set the dalvik_hook_t.sm flag?

is is used in dalvik_prepare(). if you want to call the original method from your hook you need to set "sm = 1" if the hooked method is static


Reply to this email directly or view it on GitHub: https://github.com/crmulliner/ddi/issues/1#issuecomment-33371534

Collin R. Mulliner collin@mulliner.org KeyID 0x5D89EEED jabber: collin@jabber.ccc.de skype: crmcrm web:www.mulliner.org finger:collin@betaversion.net Failure is not an option; it comes bundled with your Microsoft products.