A, goals,

Boss Li: Fenfei, I am myself. I can play as I like. Why bother to use Hook?

Fenfei: a man should be hard on himself.

I have a libtest.so, and when I call it, it will print something with android_log_print, and I want it to print something with a little bootstring. Start work.

  • so hook
  • Dobby

Second, the steps

So let’s call so

Place so under CPP’s peer directory jniLibs. Then run, output:

The 2021-06-11 09:45:11. 185, 17916-18002 / com. Fenfei. Dobbydemo D/mytest: Call directly. 1 2021-06-11 09:45:11. 185, 17916-18002 / com. Fenfei. Dobbydemo D/mytest: Call the from global PTR. 1 2021-06-11 09:45:11. 185, 17916-18002 / com. Fenfei. Dobbydemo D/mytest: The call from the local PTR. 1 2021-06-11 09:45:11. 185, 17916-18002 / com. Fenfei. Dobbydemo D/mytest: call from local ptr2. 1 (definitely failed when compiled with -O0)Copy the code

Our goal is to add some bootlaces to the output.

Dobby

Github.com/jmpews/Dobb… It is a multi-platform Hook library, but it is very good.

Git Clone down.

Place the entire folder under cmakelists. TXT and native-lib. CPP.

Then edit the cMakelists.txt file

# here specify static link, generate a so; Default is ON, # Set (DOBBY_SOURCE_DIR) dobby (${DOBBY_SOURCE_DIR}) # set(DOBBY_SOURCE_DIR) dobby (${DOBBY_SOURCE_DIR}) dobby.out) #end ...... The # target_link_libraries section adds dobby target_link_libraries(# Specifies the target library target library to the log library # included in the NDK. ${log-lib} )Copy the code

Then add the Hook code

#include <android/log.h> #include "Dobby/include/dobby.h" static int (*orig_log_print)(int prio, const char* tag, const char* fmt, ...) ; static int my_libtest_log_print(int prio, const char* tag, const char* fmt, ...) { va_list ap; char buf[1024]; int r; snprintf(buf, sizeof(buf), "[%s] %s", (NULL == tag ? "" : tag), (NULL == fmt ? "" : fmt)); va_start(ap, fmt); r = __android_log_vprint(prio, "Dobby_libtest", buf, ap); va_end(ap); return r; } __attribute__((constructor)) static void ctor() { DobbyHook((void *) DobbySymbolResolver(NULL, "__android_log_print"), (void *) my_libtest_log_print,(void **) &orig_log_print); }Copy the code

Run and experience it.

The 2021-06-11 10:23:12. 175, 30447-30493 / com. Fenfei. Dobbydemo D/Dobby_libtest: [mytest] call directly. 1 2021-06-11 10:23:12. 175, 30447-30493 / com. Fenfei. Dobbydemo D/Dobby_libtest: [mytest] call from global PTR. 1 2021-06-11 10:23:12. 175, 30447-30493 / com. Fenfei. Dobbydemo D/Dobby_libtest: [mytest] call from local PTR. 1 2021-06-11 10:23:12. 175, 30447-30493 / com. Fenfei. Dobbydemo D/Dobby_libtest: [mytest] call from local ptr2. 1 (definitely failed when compiled with -O0)Copy the code

Dobby_libtest: [mytest]

Third, summary

Hook is an enduring topic. In addition to others, the Hook itself is also very meaningful.

Some things, the layman looks very good, but we look inside, that really XXX is not the general good

TIP: The purpose of this article is only one is learning more backward techniques and train of thought, if anyone use this technology to get illegal commercial interests the legal liabilities are their operators, and the author and it doesn’t matter, this paper involves the knowledge of code project can go to my friends to fly star come undone, welcome to join star learn together to explore technology knowledge. Questions can be added to me WX: Fenfei331 discussion.

Wechat public account: Fenfei security, the latest technology dry goods real-time push