frida是平台原生app的Greasemonkey,说的专业一点,就是一种动态插桩工具,可以插入一些代码到原生app的内存空间去,(动态地监视和修改其行为),这些原生平台可以是Win、Mac、Linux、Android或者iOS。
Mac系统的frida版本要与Android中的版本号保持一致。
升级Mac系统中的frida为最新版本的命令:
sudo pip install --upgrade frida
或者安装指定版本的frida,这里安装frida的12.1.2版本:
pip install frida==12.1.2
我的root设备是Android4.4.4系统
下载frida-server到电脑中,地址:frida-server
选择合适的版本,我这里下载的是frida-server-12.1.2-android-arm.xz
下载到电脑要解压这个.xz文件,Mac系统如果不能识别这个文件,可到App Store安装一个叫"The Unarchiver"的工具。
解压后修改文件名为"frida-server",并复制文件到手机对应的目录下:
adb push frida-server /data/local/tmp/
继续在终端输入命令,为frida-server添加执行权限,并启动手机中的frida-server:
$ adb shell
shell@hammerhead:/ $ su
root@hammerhead:/ # cd /data/local/tmp/
root@hammerhead:/data/local/tmp # chmod 777 frida-server
root@hammerhead:/data/local/tmp # ./frida-server &
手机会重启,重启之后,mac新开一个终端输入命令检查手机上的frida是否运行成功:
frida-ps -U
成功则会打印手机中运行的进程:
PID Name
----- ----------------------------------------------
25851 adb
25767 adbd
31402 android.process.acore
31675 android.process.media
187 bridgemgrd
32228 com.android.defcontainer
...
...
console.log(Java.use("android.util.Log").getStackTraceString(Java.use("java.lang.Exception").$new()));
https://www.52pojie.cn/forum.php?mod=viewthread&tid=931872
import frida
import sys
rdev = frida.get_remote_device()
front_app = rdev.get_frontmost_application()
print (front_app)
import frida
import sys
rdev = frida.get_remote_device()
processes = rdev.enumerate_processes()
for processe in processes:
print (processe)
import frida
import sys
rdev = frida.get_remote_device()
session = rdev.attach("com.mfw.roadbook") # 也可以使用attach(pid)的方式
modules = session.enumerate_modules()
for module in modules:
# print (module)
if module.name=="libmfw.so":
export_funcs = module.enumerate_exports()
for export_func in export_funcs:
print ("\t%s\t%s"%(export_func.name,hex(export_func.relative_address)))
https://github.com/iGio90/FridaAndroidInjector
操作命令:
frida -U --no-pause -f package_name -l hook_RegisterNatives.js
frida -U -f com.kuaishou.android.common.kwguard //直接注入APP 在APP里面操作
操作案列:
https://juejin.im/post/5b1cc2b85188257d8c7d726c
https://www.anquanke.com/post/id/197657#h3-1
内存堆搜索与执行,提取内存信息。执行一些hook函数
https://jianshu.com/p/bab4f4714d98
//在堆上查找实例化的对象,示例代码如下!
Java.choose("b3nac.injuredandroid", {
onMatch: function (instance) {
console.log("Found instance: " + instance);
console.log("Result of secret func: " + instance.decrypt());
},
onComplete: function () { }
});
如果是自定义的实体类参数:比如okhttp的Request
var Request = Java.use('okhttp3.Request');
var Java_Request = Java.cast(arguments[j], Request);
console.log(Java_Request.url());
var Map = Java.use('java.util.HashMap');
var args_map = Java.cast(arguments[j], Map);
console.log(args_map.toString());
将参数强制转换成类对象,并打印里面的值
或者使用javascript里面的value
var1.data.value
https://langgithub.github.io/2019/08/01/frida%E4%BD%BF%E7%94%A8%E6%80%BB%E7%BB%93/#okhttp%E4%B8%80%E8%88%AChook%E6%96%B9%E5%BC%8F
function abc(){
var base_address=Module.findBaseAddress('libcms.so')
if (base_address!=null){
console.log("param:ok");
var str;
Java.perform(function () {
str = Java.use("java.lang.String");
});
Interceptor.attach(base_address.add(0x16e19), {
onEnter: function (args) {
// console.log("param1>>>>>>>" + args[0].readCString());
// console.log("param1>>>>>>>" + Memory.readUtf16String(args[0]));
// readAnsiString
console.log("hook success");
var s3 = Java.cast(args[3], str);
var s5 = Java.cast(args[5], str);
console.log("param2>>>>>>>" + args[2].toInt32());
console.log("param3>>>>>>>" + s3);
const length = Java.vm.getEnv().getArrayLength(args[4]);
var array=[];
for(var i=0;i<length;i++){
var obj=Java.vm.getEnv().getObjectArrayElement (args[4],i)
var result=Java.vm.getEnv().stringFromJni(obj)
array.push(result);
}
console.log("param4>>>>>>>" + JSON.stringify(array));
console.log("param5>>>>>>>" + s5);
},
onLeave: function (retval) {
}
});
}
}
abc();
错误1
$frida-ps -Ua
Failed to enumerate applications: the connection is closed
我遇到的错误1和错误2,都是因为手机的Frida版本和Mac电脑的Frida版本不同导致的,安装相同版本的Frida就可解决。
使用frida强制启动chrome报错
$ frida -U --no-pause -f com.android.chrome
Traceback (most recent call last):
File "/Users/king/Documents/PythonProject/Tornado/test/venv/bin/frida", line 11, in <module>
sys.exit(main())
File "/Users/king/Documents/PythonProject/Tornado/test/venv/lib/python2.7/site-packages/frida_tools/repl.py", line 23, in main
from prompt_toolkit.shortcuts import create_prompt_application, create_output, create_eventloop
ImportError: cannot import name create_prompt_application
解决:
pip install ‘prompt-toolkit==1.0.15’
错误2:
Failed to spawn: unable to connect to remote frida-server
frida-server未启动
参考:
https://bbs.pediy.com/thread-252520.htm
Frida的操作教程:https://github.com/iddoeldor/frida-snippets#hook-overloads