APP逆向-hook框架frida

发布时间 2023-10-04 16:59:58作者: simon_T

1hook框架frida

# 搜出位置---》找到了代码---》你确定他就是吗?
# 我们现在不确定代码有没有走咱们找的地方

# 借助于另一个东西,帮咱们确认,它确实走了它

# hook框架---》通过hook(钩子)--》拦截 把我找的函数hook住,看看有没有执行
Hook 框架是一种技术,用于在运行时拦截和修改应用程序的行为。
通过 Hook,你可以劫持应用程序的方法调用、修改参数、篡改返回值等,以达到对应用程序的修改、增强或调试的目的

# 常见的有:
Xposed Framework:Xposed 是一个功能强大的开源 Hook 框架,可以在不修改应用程序源代码的情况下,对应用程序进行各种修改。它允许你编写模块来拦截和修改应用程序的方法调用,修改应用程序的行为和逻辑。

Frida(咱们讲):Frida 是一个跨平台的动态 Hook 框架,支持安卓和其他操作系统。它提供了一个强大的 JavaScript API,可以在运行时对应用程序进行 Hook,包括方法拦截、参数修改、调用注入等。Frida 可以用于安全研究、逆向工程和应用程序调试等方面。

1.1 下载安装(电脑,手机)

#搭建frida的hook环境---》电脑上hook手机---》电脑要配置,手机要配置

# 电脑端一定要安装python解释器---》3.9 我用的,建议你们也是3.9
# 手机端要跟电脑端【版本一致】
####版本一定要对应

1.1.1 电脑端配置

# 下载两个模块---》最新版
pip install frida           # 16.1.3
pip install frida-tools     # 12.2.1

1.1.2 手机端配置

# 去frida官网,下载对应版本的 frida-server  ----》16.1.3
	-https://github.com/frida/frida/releases
# 下的frida-server跟手机架构对应
	-adb shell getprop ro.product.cpu.abi
    -arm64-v8a
    
# 根据手机平台下载:
# 压缩包=---解压---》看到frida-server-16.1.3-android-arm64
# 推送到手机上---》不要把压缩包放到手机上
	adb push frida-server-16.1.3-android-arm64   /data/local/tmp
    
# 去手机那个目录下看看有没有

# 按照命令
	adb shell  # 进入手机命令行
    su  #切换为root用户
    cd /data/local/tmp  # 切换到目录下
    chmod 755 frida-server-16.1.3-android-arm64  # 加入执行权限
    ls -al  # 查看文件
    

image-20230816221452901

1.2 启动并hook-app程序

# 刚刚反编译:找到这个  SecurityUtil.encodeMD5(str3),觉得密码加密用的它
# 但是不确定--》现在要用frida,hook这个函数,看一下是不是真的走了

1.2.1 通过frida,打印出当前手机所有的进程和前台进程(固定代码)

# 前置条件一:
	手机端启动frida-server
    进入到手机路径下  ./frida-server  # 你的名字一定要对

# 前置条件二:
	在电脑端要做端口转发---》命令行中执行---》电脑
    adb forward tcp:27042 tcp:27042
	adb forward tcp:27043 tcp:27043
# 然后才能hook
import frida

# 获取设备信息
rdev = frida.get_remote_device()

# 枚举所有的进程
processes = rdev.enumerate_processes()
for process in processes:
    print(process)

# 获取在前台运行的APP
front_app = rdev.get_frontmost_application()
print(front_app)

1.3 hook某智赢app-pwd加密算法

# hook-->encodeMD5--在登录的时候,有没有走它--》只要走了它--》hook代码执行了---可以修改内容
# hook返回值是明文---》抓包看到也是明文

# 确定了,密码加密,就会走encodeMD5

import frida
import sys
# 连接手机设备
rdev = frida.get_remote_device()

# 包名:com.che168.autotradercloud
# 车智赢+
session = rdev.attach("车智赢+")  # app名字

# ----上面固定------以后只会动src中代码
# src 是字符串,写js代码
scr = """
Java.perform(function () {
    //找到类 反编译的首行+类名:com.autohome.ahkit.utils下的
    var SecurityUtil = Java.use("com.autohome.ahkit.utils.SecurityUtil");

    //替换类中的方法
    SecurityUtil.encodeMD5.implementation = function(str){
        console.log("传入的参数(未加密之前的):",str);
        var res = this.encodeMD5(str); //调用原来的函数
        console.log("返回值(加密后的字符串):",res);
        return str;  // 没加密的
    }
});
"""


# -----下面固定---以后不会动
script = session.create_script(scr)

def on_message(message, data):
    print(message, data)

script.on("message", on_message)
script.load()
sys.stdin.read()

hook方式(两种,python,js)

# hook 安卓程序函数的两种方式
	-spawn:
    需要在应用程序启动的早期阶段进行 Hook。
    需要访问和修改应用程序的内部状态,例如应用程序的全局变量、静态变量等。
    需要 Hook 应用程序的初始化过程,以实现对应用程序的自定义初始化逻辑。
    需要在应用程序的上下文中执行代码,并与其他模块或库进行交互。
    
    -attach:刚刚写的
    需要对已经运行的应用程序进行 Hook,即动态地连接到正在运行的进程。
    需要在应用程序运行时拦截和修改特定的方法调用。
    需要实时监视和修改应用程序的行为,例如参数修改、返回值篡改等。
    需要对应用程序进行调试和分析,以查找潜在的问题和漏洞。

1.5.1 attach方案(刚讲了)

import frida
import sys
# 连接手机设备
rdev = frida.get_remote_device()

# 包名:com.che168.autotradercloud
# 车智赢+
session = rdev.attach("车智赢+")  # app名字

# ----上面固定------以后只会动src中代码
# src 是字符串,写js代码
scr = """
Java.perform(function () {
    //找到类 反编译的首行+类名:com.autohome.ahkit.utils下的
    var SecurityUtil = Java.use("com.autohome.ahkit.utils.SecurityUtil");

    //替换类中的方法
    SecurityUtil.encodeMD5.implementation = function(str){
        console.log("传入的参数(未加密之前的):",str);
        var res = this.encodeMD5(str); //调用原来的函数
        console.log("返回值(加密后的字符串):",res);
        return str;  // 没加密的
    }
});
"""


# -----下面固定---以后不会动
script = session.create_script(scr)

def on_message(message, data):
    print(message, data)

script.on("message", on_message)
script.load()
sys.stdin.read()

1.5.2 spawn方案

# 自动重启app,适用于在应用程序启动的早期阶段进行

import frida
import sys

rdev = frida.get_remote_device()
pid = rdev.spawn(["com.che168.autotradercloud"])
session = rdev.attach(pid)

scr = """
Java.perform(function () {
    // 包.类
    var SecurityUtil = Java.use("com.autohome.ahkit.utils.SecurityUtil");
    SecurityUtil.encodeMD5.implementation = function(str){
        console.log("明文:",str);
        var res = this.encodeMD5(str);
        console.log("md5加密结果=",res);
        return "305eb636-eb15-4e24-a29d-9fd60fbc91bf";
    }
});
"""
script = session.create_script(scr)


def on_message(message, data):
    print(message, data)


script.on("message", on_message)
script.load()
rdev.resume(pid)
sys.stdin.read()

1.5.3 hook可以使用js代码写

# 之前用python写的
# 现在可以用js代码写--》attach和spawn
// hook代码如下
Java.perform(function () {
    //找到类 反编译的首行+类名:com.autohome.ahkit.utils下的
    var SecurityUtil = Java.use("com.autohome.ahkit.utils.SecurityUtil");

    //替换类中的方法
    SecurityUtil.encodeMD5.implementation = function(str){
        console.log("传入的参数(未加密之前的):",str);
        var res = this.encodeMD5(str); //调用原来的函数
        console.log("返回值(加密后的字符串):",res);
        return str;  // 没加密的
    }
});

// 执行是,选择用attach还是  spwan方案


// attach 方案执行命令:   frida -UF -l 6-hook-pwd-attach.js

// spwan 方案执行命令:  frida -U -f com.che168.autotradercloud -l 6-hook-pwd-attach.js