Android hook native function, androidnative
The code I wrote about two years ago suddenly needs to be used today. I searched for it for half a day and recorded it here.
Library used: 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" JNIEXPORTint 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; // The fake function FILE * myfopen (const char * name, const char * flag) {FILE * fret = 0; fret = porg (name, flag ); return fret;} // substratevoid initHook () {void * phandle = dlopen ("libhook. so ", RTLD_NOW); pfnMSHookFunction pmsHook = (pfnMSHookFunction) dlsym (phandle," MSHookFunction "); pmsHoo K (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 ;}