Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hooking static methods #1

Closed
TheBigS opened this issue Jan 27, 2014 · 2 comments
Closed

Hooking static methods #1

TheBigS opened this issue Jan 27, 2014 · 2 comments

Comments

@TheBigS
Copy link

TheBigS commented Jan 27, 2014

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
Copy link
Author

TheBigS commented Jan 27, 2014

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?
Also, it may be helpful to others to add an example static method hook to the readme

@TheBigS TheBigS closed this as completed Jan 27, 2014
@crmulliner
Copy link
Owner

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:
#1 (comment)

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants