Skip to content

Instantly share code, notes, and snippets.

@circleous
Last active July 27, 2021 08:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save circleous/86f9cc90194164e43a07493f430a198b to your computer and use it in GitHub Desktop.
Save circleous/86f9cc90194164e43a07493f430a198b to your computer and use it in GitHub Desktop.
Inline Hook Android
#include <stdio.h>
#include <jni.h>
#include <android/log.h>
#include <dlfcn.h>
#include <sys/types.h>
#include <stdlib.h>
//#include "TKHooklib.h"
#define LOG_TAG "HOOK"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
typedef int (*TK_InlineHookFunction)(void *, void *, void** );
TK_InlineHookFunction pTK_InlineHookFunction_t = NULL;
char *getMoney = "_ZN7LKModel8getMoneyEv";
int (*orig_getMoney)(void);
int hook_getMoney(void)
{
LOGI("getMoney()");
return 1337;
}
void* lookup_symbol(char* libraryname, char* symbolname)
{
void *imagehandle = dlopen(libraryname, RTLD_GLOBAL | RTLD_NOW);
if (imagehandle != NULL)
{
dlerror(); //clear
void * sym = dlsym(imagehandle, symbolname);
if (sym != NULL)
{
return sym;
}
else
{
LOGI("(lookup_symbol) %s", symbolname);
LOGE("dlerror: %s", dlerror());
return NULL;
}
}
else
{
LOGI("(lookup_symbol) dlerror: %s",dlerror());
return NULL;
}
}
void doHook()
{
void * pGetMoney = lookup_symbol("libcocos2dlua.so", getMoney);
int ret = pTK_InlineHookFunction_t(pGetMoney, (void*)&hook_getMoney, (void**)&orig_getMoney);
LOGI("Hook Money %s", ret == 0 ? "success" : "failed");
}
jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
jint result = -1;
void *handle;
handle = dlopen("/data/data/com.nova.DemonSouls/lib/libTKHooklib.so", RTLD_NOW);
if (handle == NULL)
{
LOGE("dlopen [%s TKHooklib.so]", dlerror());
goto exit;
}
dlerror();
pTK_InlineHookFunction_t = dlsym(handle, "TK_InlineHookFunction");
if (pTK_InlineHookFunction_t == NULL)
{
LOGE("dlsym [%s TK_InlineHookFunction]", dlerror());
goto exit;
}
doHook();
result = JNI_VERSION_1_4;
exit:
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment