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

使用过程中一点疑问 #17

Closed
BiteFoo opened this issue Aug 28, 2018 · 6 comments
Closed

使用过程中一点疑问 #17

BiteFoo opened this issue Aug 28, 2018 · 6 comments

Comments

@BiteFoo
Copy link

BiteFoo commented Aug 28, 2018

我在使用的过程中遇到一个问题,没想明白,所以来提问。
首先我写了一个测试demo,如下代码:
写了一个jni的调用,

  int test_add(int a,int b){

    return a+b;
}

 jint
Java_com_famgy_xhooktest_NativeHandler_test_1add(JNIEnv* env, jobject obj,jint a,jint b)
{

//在这里调用test_add方法
    return test_add(a,b);
}
  

使用xhook写一个Hook模块,如下代码,
hook.so

int (*ori_testadd)(jint,jint);

static int my_test_add(jint a,jint b){
    __android_log_print(ANDROID_LOG_ERROR, "XHOOK====>> before ","a=%d ,b=%d", a,b);
    a=20;
    __android_log_print(ANDROID_LOG_ERROR, "XHOOK====>> after ","a=%d ,b=%d", a,b);
    return ori_testadd(a,b);
}

xhook_register(".*/libxhooktest\\.so$", "test_add", my_test_add,(void**)&ori_testadd);
    xhook_enable_debug(1);//开启debug模式,查看信息  
xhook_refresh(1);  

运行demo后,实际能hook到xhooktest.so中的test_add方法,
test1的日志:

05-20 13:37:35.649 30579-30598/com.famgy.famgyelfhook I/xhook: init OK: /data/app/com.famgy.famgyelfhook-1/lib/arm/libxhooktest.so (REL ELF_HASH PLT:96 DYN:88 ANDROID:0)
    hooking test_add in /data/app/com.famgy.famgyelfhook-1/lib/arm/libxhooktest.so
    found test_add at symidx: 3 (ELF_HASH)
05-20 13:37:35.649 30579-30598/com.famgy.famgyelfhook E/xhook: sym  equals ,r_sym=3 ,symidx=3
    type   equals ,r_type=22 ,XH_ELF_R_GENERIC_JUMP_SLOT=22
    方法不是plt类型 is_plt=1 ,r_type=22,XH_ELF_R_GENERIC_GLOB_DAT=21,XH_ELF_R_GENERIC_ABS=2
05-20 13:37:35.649 30579-30598/com.famgy.famgyelfhook I/xhook: found test_add at .rel.plt offset: 0x2fe8
05-20 13:37:35.651 30579-30598/com.famgy.famgyelfhook I/xhook: XH_HK_OK 0xb3406fe8: 0xb3404775 -> 0xb35066b5 test_add /data/app/com.famgy.famgyelfhook-1/lib/arm/libxhooktest.so
05-20 13:37:35.654 30579-30598/com.famgy.famgyelfhook I/xhook: map refreshed
05-20 13:37:35.847 30579-30579/com.famgy.famgyelfhook E/XHOOK====>> before: a=10 ,b=6
05-20 13:37:35.847 30579-30579/com.famgy.famgyelfhook E/XHOOK====>> after: a=20 ,b=6    

这里的调用关系为,首先通过xhook来注册hook函数,接着调用hooktest.so中的函数test_add。这个test_add函数在hooktest.so中的,通过jni方法调用。

但是现在 有个问题是,按照上面的步骤,写另一个hooktest2.so的时候,调用里边的函数tt_add,如下

代码:
hook.so


void (*ori_testadd)(int,int);

 static void my_dohook(JNIEnv* env, jobject obj,jint sync)
{
    if(sync ==1)
    {
        __android_log_print(ANDROID_LOG_INFO, "XHOOK ====>>  before param ","sync=%d", sync);
        sync =5555;
    }

     __android_log_print(ANDROID_LOG_INFO, "XHOOK ====>>  change param ","sync=%d", sync);
    orii_dohook(env,obj,sync);
}


static int register_hook()
{

      xhook_register(".*/libtest\\.so$", "tt_add", my_test_add,(void**)&ori_testadd);
     xhook_enable_debug(1);
    xhook_refresh(1);
    return 0;

}    

hooktest2.so的代码:

void tt_add(int a,int b)
{

    __android_log_print(ANDROID_LOG_DEBUG, "XHOOK_TEST====> ","tt_add>>>>  a=%d , b =%d   c =a+b=%d",a,b,(a+b));

}  


void Java_itsec_testerxhook_MainActivity_dohooktest(JNIEnv* env, jobject obj,jint sync)
{
    (void)env;
    (void)obj;

    __android_log_print(ANDROID_LOG_DEBUG, "XHOOK_TEST====> ","Java_itsec_testerxhook_MainActivity_dohooktest >>>> ");

    __android_log_print(ANDROID_LOG_DEBUG, "XHOOK_TEST====> ","Java_itsec_testerxhook_MainActivity_dohooktest >>>>  sync=%d",sync);

    tt_add(10,2);

}    

test2日志如下:


05-20 13:18:15.606 29891-29909/itsec.testerxhook I/xhook: init OK: /data/app/itsec.testerxhook-1/lib/arm/libtest.so (REL ELF_HASH PLT:224 DYN:64 ANDROID:0)
    hooking tt_add in /data/app/itsec.testerxhook-1/lib/arm/libtest.so
    found tt_add at symidx: 3 (ELF_HASH)
05-20 13:18:15.606 29891-29909/itsec.testerxhook E/xhook: hooking tt_add in /data/app/itsec.testerxhook-1/lib/arm/libtest.so
    sym not equals ,r_sym=1 ,symidx=3 ,plt: is_plt=1
     
    sym not equals ,r_sym=4 ,symidx=3 ,plt: is_plt=1
     
    sym not equals ,r_sym=13 ,symidx=3 ,plt: is_plt=1
     
    sym not equals ,r_sym=15 ,symidx=3 ,plt: is_plt=1
     
    sym not equals ,r_sym=16 ,symidx=3 ,plt: is_plt=1   

请问下这时什么问题导致的?

@BiteFoo
Copy link
Author

BiteFoo commented Aug 28, 2018

补充下,test2中的my_test_add函数:

static void  my_test_add(int a,int b){
    __android_log_print(ANDROID_LOG_ERROR, "XHOOK====>> before ","a=%d ,b=%d", a,b);
    a=200;
    __android_log_print(ANDROID_LOG_ERROR, "XHOOK====>> after ","a=%d ,b=%d", a,b);
     ori_testadd(a,b);
}

@caikelun
Copy link
Collaborator

你的意思是app中hook第一个点能成功,再加一个点就失败了?

你需要在调用xhook_refresh之前一次性注册完所有的hook点(用xhook_register),一旦调用过xhook_refresh之后,就不能调用xhook_register了(如果调用会有警告输出)。

这么设计主要是性能方面的考虑,xhook内部会维护一个map,用于记录所有注册的hook点、已hook成功的点、当前进程加载的so列表等信息,避免在下次调用xhook_refresh时执行重复的不必要的hook。

@BiteFoo
Copy link
Author

BiteFoo commented Aug 28, 2018

我没完全表述清楚,不好意思。test1和test2是两个不同的app,test1主要是我用来测试是否支持hook自身模块内函数功能,也是就Hook test_add函数。test1测试的时候是可以的。然后我把这个功能用在我的test2上,test2是我的app,测试hook tt_add函数,发现不能实现hook。

@caikelun
Copy link
Collaborator

xhook只支持PLT/GOT hook方式,即hook调用者so中的对外调用点,不支持对包含目标函数实现的so本身进行hook。可以看下这个:https://github.com/iqiyi/xHook/blob/master/docs/overview/android_plt_hook_overview.zh-CN.md

@BiteFoo
Copy link
Author

BiteFoo commented Aug 28, 2018

谢谢,刚重新分析了,是这样,再次谢谢你的回答 。

@BiteFoo BiteFoo closed this as completed Aug 28, 2018
@caikelun
Copy link
Collaborator

不客气。

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