逆向 | frida android hook 入门总结

逆向 | frida android hook 入门总结

最近在备课,整理到这一块儿了,顺带就把以前的东西整理一下。
比较好的参考文章:

https://www.jianshu.com/p/0fa6138fafc9  # hook重载函数的几种写法
https://blog.csdn.net/zhy025907/article/details/89512096  # hook方法汇总***
https://www.52pojie.cn/thread-1128884-1-1.html   # frida入门总结
https://frida.re/docs/examples/android/    # 官方文档

自己画的原理图,差不多是这么一回事儿,可能不够准确,欢迎师傅们修正:
image
image

安装环境

我是懒狗,直接截图自己的ppt了
image
image
image
image
image
image

测试代码:

随便写个helloworld的apk。然后里面新建个线程不停log.d

hook用的js:

Java.perform(function(){  
    var Log = Java.use('android.util.Log'); //获得Log类
    send(Log);  
    Log.d("Mz1", 'test');   // 直接调用这个函数
    Log.d.overload('java.lang.String', 'java.lang.String').implementation = function(tag, s){
    	send(tag + s);
    	var result = this.d("Mz1", "hook!");
    	return result;
    }
});

用python注入:

import frida  #导入frida模块
import sys    #导入sys模块

# js 代码
jscode = """
Java.perform(function(){  
    var Log = Java.use('android.util.Log'); //获得Log类
    send(Log);  
    Log.d("Mz1", 'test');   // 直接调用这个函数
    Log.d.overload('java.lang.String', 'java.lang.String').implementation = function(tag, s){
    	send(tag + s);
    	var result = this.d("Mz1", "hook!");
    	return result;
    }
});
"""

def on_message(message,data): #js中执行send函数后要回调的函数
    print(message)
    


# 得到设备并劫持进程com.mz.helloworld
#(开始用get_usb_device函数用来获取设备,但是一直报错找不到设备,改用get_remote_device函数即可解决这个问题)
device = frida.get_remote_device()

# attach方式启动
# process = device.attach('com.mz.helloworld')   # 这个方法在模拟器上有点问题

# 或者spawn方式启动:
pid = device.spawn(["com.mz.helloworld"])
device.resume(pid)
process = device.attach(pid)


 
script = process.create_script(jscode) #创建js脚本
script.on('message',on_message) #加载回调函数,也就是js中执行send函数规定要执行的python函数
script.load() #加载脚本
sys.stdin.read()

posted @ 2022-09-07 13:44  Mz1  阅读(412)  评论(0编辑  收藏  举报