欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android hook native函数

程序员文章站 2022-06-28 11:08:49
大概2年前写的代码,今天突然要用到,找了半天,这里记录下 用到的库: https://pan.baidu.com/s/1htuUQX2 ......

大概2年前写的代码,今天突然要用到,找了半天,这里记录下

用到的库: https://pan.baidu.com/s/1htuUQX2

#include <jni.h>
#include <string>
#include <dlfcn.h>

typedef  int  (*  pfnMSHookFunction)(void* result, void* a2, void** a3);

int Aaaaaa(jint a, jint b)
{

    int bRet = 0;
    bRet = a/3;
    return  bRet;
}

extern  "C"
JNIEXPORT

int  Java_aaa_aaa_MainActivity_test(jint a,jint b)
{
    FILE *fp = fopen("1.txt", "rb");
    if (0 != fp)
    {
        printf("111");
    }

  return   Aaaaaa(a, b);
}


typedef FILE*    (*pfnmyfopen)(const char *, const char *);
pfnmyfopen porg = 0;

//fake 函数
FILE    *myfopen(const char * name, const char * flag)
{
    FILE* fret = 0;
    fret =  porg(name,flag);
    return  fret;
}

//这里用的是substrate
void initHook()
{
   void* phandle =dlopen("libhook.so",RTLD_NOW );
    pfnMSHookFunction pmsHook=(pfnMSHookFunction)dlsym(phandle,"MSHookFunction");
    pmsHook((void*)fopen,(void*)myfopen,(void**)&porg);


}

jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
{
    JNIEnv* env = 0;
    jint result = -1;

    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK)
    {
        return -1;
    }

    initHook();

    result = JNI_VERSION_1_4;

    return result;
}