0x01 Hook整体框架代码

  1. import frida,sys
  2. jscode = """
  3. Java.perform(function () {
  4. // hook构造函数
  5. var auto = Java.use('com.dongchedi.cisn.policy.AutoPrivacyPolicyDialog');
  6. auto.$init.implementation = function(obj){
  7. send("Hook start...");
  8. return this.$init(obj);
  9. }
  10. // hook普通函数
  11. var test = Java.use('com.dongchedi.cisn.policy.Utils');
  12. test.getCalc.implementation = function(a,b){
  13. console.log("Hook start....");
  14. return this.getCalc(a,b);
  15. }
  16. // hook重载函数
  17. var add = Java.use('com.dongchedi.cisn.policy.Utils');
  18. add.addCalc.overload("int","java.lang.String").implementation = function(a,b){
  19. console.log("Hook start....");
  20. return this.getCalc(a,b);
  21. }
  22. // 调用其它类方法
  23. var cls = Java.use('com.dongchedi.cisn.policy.Cls');
  24. var test = Java.use('com.dongchedi.cisn.policy.Utils');
  25. test.getCalc.implementation = function(a,b){
  26. console.log("Hook start....");
  27. var c = cls.new();
  28. send(c.test());
  29. // 修改类的值
  30. test.name.value = 123;
  31. return this.getCalc(a,b);
  32. }
  33. });
  34. """
  35. def message(message,data):
  36. if message["type"] == 'send':
  37. print("[*] {0}".format(message['payload']))
  38. else:
  39. print(message)
  40. # process = frida.get_remote_device().attach('pingan2')
  41. # 当使用get_remote_device需要开启端口转发,语句如下:
  42. # adb forward tcp:27043 tcp:27043
  43. # adb forward tcp:27042 tcp:27042
  44. # 很多情况下进程名与包名并不相同,需要使用frida-ps查看进程名进行hook
  45. process = frida.get_usb_device().attach('com.dongchedi.cisn.android')
  46. script = process.create_script(jscode)
  47. script.on("message",message)
  48. script.load()
  49. sys.stdin.read()

0x02 Hook native层代码

1)有导出的so层函数

可以直接在导出表查看导出函数名,进行hook
图片.png
hook脚本:

  1. var str_name_so = "libcyberpeace.so"; //要hook的so名
  2. var str_name_func = "Java_com_testjava_jack_pingan2_cyberpeace_CheckString";//要hook的函数名
  3. var n_addr_func = Module.findExportByName(str_name_so , str_name_func);
  4. console.log("func addr is ---" + n_addr_func);
  5. Interceptor.attach(n_addr_func, {
  6. //在hook函数之前执行的语句
  7. onEnter: function(args)
  8. {
  9. console.log("hook on enter")
  10. },
  11. //在hook函数之后执行的语句
  12. onLeave:function(retval)
  13. {
  14. console.log("hook on leave");
  15. console.log("return:"+retval); //返回值
  16. retval.replace(1); //替换返回值为100
  17. }
  18. });

图片.png

2)无导出的so层函数

无法通过函数名找到该函数,只能通过字符串或者上下级调用手工找到偏移地址。
hook脚本:

  1. var str_name_so = "libcyberpeace.so"; //要hook的so名
  2. var n_addr_func_offset = 0x000006F0; //需要hook的函数的偏移
  3. var n_addr_so = Module.findBaseAddress(str_name_so);//加载到内存后 函数地址 = so地址+函数偏移
  4. var n_addr_func = parseInt(n_addr_so, 16) + n_addr_func_offset;
  5. var n_addr_func = new NativePointer(n_addr_func);
  6. console.log("func addr is ---" + n_addr_func);
  7. Interceptor.attach(n_addr_func, {
  8. //在hook函数之前执行的语句
  9. onEnter: function(args)
  10. {
  11. console.log("hook on enter")
  12. },
  13. //在hook函数之后执行的语句
  14. onLeave:function(retval)
  15. {
  16. console.log("hook on leave");
  17. console.log("return:"+retval); //返回值
  18. retval.replace(1); //替换返回值为100
  19. }
  20. });