初探Frida

Frida是什么?

背景

最近解决安卓抓包问题的时候,了解到Frida可以解决SSL Pinning的问题,于是对Frida产生了兴趣,看了看文档,感觉很好用,在这里记录下学习笔记。

Frida是什么?

It’s Greasemonkey for native apps, or, put in more technical terms, it’s a dynamic code instrumentation toolkit. It lets you inject snippets of JavaScript or your own library into native apps on Windows, macOS, GNU/Linux, iOS, watchOS, tvOS, Android, FreeBSD, and QNX. Frida also provides you with some simple tools built on top of the Frida API. These can be used as-is, tweaked to your needs, or serve as examples of how to use the API.

Frida是一个动态插桩工具,可以在平台原生APP插入JS代码,动态调试,而且是全平台支持(Linux/MacOS/Windows/Android/iOS)。

更形象地说,Frida好比是Greasemonkey,Greasemonkey可以让你在网页中插入JS,Frida是在二进制环境中插入JS。

Frida相关工具

名称 安装 描述
frida-tools pip3 install frida-tools Frida CLI,包括frida/frida-ps/frida-trace等
frida-server https://github.com/frida/frida/releases 用于远程连接
Frida Python Binding pip3 install frida Frida Python库
Frida Node.js Binding npm install frida Frida Node.js库

Android安装Frida

在安卓上安装Frida,需要有root权限,安装过程如下:

  • pip3 install frida-tools
  • 下载frida-server
  • unxz frida-server.xz
  • adb root
  • adb push frida-server /data/local/tmp/
  • adb shell "chmod 755 /data/local/tmp/frida-server"
  • adb shell "/data/local/tmp/frida-server &"
  • 执行frida-ps -U ,如果输出Android上的进程,则安装成功

代码示例

这是官网的Android示例,其中jscode 是注入到目标进程中的JS代码。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import frida, sys

def on_message(message, data):
    if message['type'] == 'send':
        print("[*] {0}".format(message['payload']))
    else:
        print(message)

jscode = """
Java.perform(() => {
  // Function to hook is defined here
  const MainActivity = Java.use('com.example.seccon2015.rock_paper_scissors.MainActivity');

  // Whenever button is clicked
  const onClick = MainActivity.onClick;
  onClick.implementation = function (v) {
    // Show a message to know that the function got called
    send('onClick');

    // Call the original onClick handler
    onClick.call(this, v);

    // Set our values after running the original onClick handler
    this.m.value = 0;
    this.n.value = 1;
    this.cnt.value = 999;

    // Log to the console that it's done, and we should have the flag!
    console.log('Done:' + JSON.stringify(this.cnt));
  };
});
"""

process = frida.get_usb_device().attach('com.example.seccon2015.rock_paper_scissors')
script = process.create_script(jscode)
script.on('message', on_message)
print('[*] Running CTF')
script.load()
sys.stdin.read()

代码的运行过程如下:

  • 在安卓下,需要先通过get_usb_device 得到一个DeviceManager 实例,代表实际的安卓设备
  • DeviceManager 实例调用attach 或者spwan 方法得到一个Session 实例,代表实际的目标进程
  • Session 实例调用create_script 方法得到一个Script 实例,代表注入到目标进程的脚本
  • Script 实例通过on 方法接收JS的消息,通过load 方法执行JS代码

Frida的[JavaScript API]功能非常强大,最常用的是Java.perform()

Objection

objection is a runtime mobile exploration toolkit, powered by Frida, built to help you assess the security posture of your mobile applications, without needing a jailbreak.

既然提到了Frida,那就不得不再说一下objection。Objection是基于Frida的移动平台运行时调试工具,集成了很多好用的功能,用于动态调试。

比如,绕过SSL Pinning,只需要执行:

1
2
$ objection -g <APP Package Name> explore
$ android sslpinning disable

再比如,为APP设置单独的代理:

1
2
$ objection -g <APP Package Name> explore
$ android proxy <Host> <Port>

总结

在Linux上,有丰富的Debug工具,比如stracegdb 等,可以很方便的安装;而在安卓上,安装类似的Debug工具比较麻烦,而Frida和objection把常用的Debug功能集成到一起,非常方便。

使用 Hugo 构建
主题 StackJimmy 设计