目录
- 0x00 文中用到的工具
- 0x01 hook示例的安装与分析
- 安装
- 源代码分析
- 0x02 frida自带的Messages机制与进程交互
- 0x03 Javascript代码构造与执行
- 方法一:获取calc()返回值
- 方法二:修改cnt的值为1000
- 0x04 总结
博客同步:访问
0x00 文中用到的工具
- Frida
- jadx-gui 一个强大的android反编译工具
- genymotion模拟器
- Python2.7以及frida-python库
- radare2 反汇编器
- pycharm
0x01 hook示例的安装与分析
Frida官网给我们了一个ctf的示例,就以此为例子,开始学习frida在android逆向的使用。
rps.apk 下载地址
安装
使用genymotion等类似android模拟器安装好打开,发现这是一个石头剪刀布的游戏应用,简单的玩了一下,没什么特别的,直接分析代码吧,看看到底想干什么。
源代码分析
使用jadx-gui反编译,发现app没有加壳和混淆,当然一来就加壳和混淆的话对我们就太不友好了,接下分析就简单了,直接看java代码。当然也可以使用androidkiller,jeb等其他强大的反编译工具。
在MainActivity中找到OnCreate()方法,可以看到只是简单的声明了button控件以及对应的监听器。
1
2
3
4
5
6
7
8
9
10
11
|
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.P = (Button) findViewById(R. id .button);
this.S = (Button) findViewById(R. id .button3);
this.r = (Button) findViewById(R. id .buttonR);
this.P.setOnClickListener(this);
this.r.setOnClickListener(this);
this.S.setOnClickListener(this);
this.flag = 0 ;
}
|
继续查看button的onclick方法,可以看出cpu是通过随机数组出的,其判断输赢的方法在this.showMessageTask中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public void onClick(View v) {
if (this.flag ! = 1 ) {
this.flag = 1 ;
((TextView) findViewById(R. id .textView3)).setText("");
TextView tv = (TextView) findViewById(R. id .textView);
TextView tv2 = (TextView) findViewById(R. id .textView2);
this.m = 0 ;
this.n = new Random().nextInt( 3 ); / / 随机数 0 , 1 , 2
tv2.setText(new String[]{ "CPU: Paper" , "CPU: Rock" , "CPU: Scissors" }[this.n]); / / 随机出石头,剪刀,布
if (v = = this.P) {
tv.setText( "YOU: Paper" );
this.m = 0 ;
}
if (v = = this.r) {
tv.setText( "YOU: Rock" );
this.m = 1 ;
}
if (v = = this.S) {
tv.setText( "YOU: Scissors" );
this.m = 2 ;
}
this.handler.postDelayed(this.showMessageTask, 1000 ); / / 输赢判断方法
}
}
|
跟进分析showMessageTask,可以看到如果赢了mainActivity.cnt会+1,但是一旦输了cnt就会置0,而获取flag的要求是我们得获胜1000次,...... :(
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
|
private final Runnable showMessageTask = new Runnable() {
public void run() {
TextView tv3 = (TextView) MainActivity.this.findViewById(R. id .textView3);
MainActivity mainActivity;
/ / 我方:布 CPU:石头 or 我方:石头 CUP:剪刀 ,则为赢
if (MainActivity.this.n - MainActivity.this.m = = 1 ) {
mainActivity = MainActivity.this;
mainActivity.cnt + + ;
tv3.setText( "WIN! +" + String.valueOf(MainActivity.this.cnt));
/ / 反过来当然是输咯
} else if (MainActivity.this.m - MainActivity.this.n = = 1 ) {
MainActivity.this.cnt = 0 ;
tv3.setText( "LOSE +0" );
/ / 一样则打平
} else if (MainActivity.this.m = = MainActivity.this.n) {
tv3.setText( "DRAW +" + String.valueOf(MainActivity.this.cnt));
/ / 我布 cup:剪刀
} else if (MainActivity.this.m < MainActivity.this.n) {
MainActivity.this.cnt = 0 ;
tv3.setText( "LOSE +0" );
} else {
mainActivity = MainActivity.this;
mainActivity.cnt + + ;
tv3.setText( "WIN! +" + String.valueOf(MainActivity.this.cnt));
}
/ / 获胜 1000 次则能够获取flag
if ( 1000 = = MainActivity.this.cnt) {
tv3.setText( "SECCON{" + String.valueOf((MainActivity.this.cnt + MainActivity.this.calc()) * 107 ) + "}" );
}
MainActivity.this.flag = 0 ;
}
};
|
简单分析一下获取flag需要的条件,总结有3个办法:
接下来就用frida,使用后两种思路来解这个简单的示例。但在这之前得先了解Frida自带的Messages机制,了解frida怎么从通过一个python脚本发送和接收message消息是一个提升理解frida的好方法。
0x02 frida自带的Messages机制与进程交互
先来看看一个Messages的模板,这里用到的语言分别是python和javascript,他们之间的关系是python作为载体,javascript作为在android中真正执行代码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import frida, sys
/ / hook代码,采用javascript编写
jscode = """
//javascript代码,重点
"""
/ / 自定义回调函数
def on_message(message, data):
if message[ 'type' ] = = 'send' :
print ( "[*] {0}" . format (message[ 'payload' ]))
else :
print (message)
#重点的4行代码
process = frida.get_usb_device().attach( '应用完整包名' )
script = process.create_script(jscode)
script.on( 'message' , on_message)
script.load()
sys.stdin.read()
|
当然如果是对此简单的使用,只需要编写jscode,以及填写你要hook的应用完整包名就行了,不过如果单纯只会用可能在以后会被模板限制,所以一探究竟还是很有必要。
可以在cmd中,使用python终端的help()函数找到frida库的源代码的绝对路径。
接下来就来具体看看这几句代码做了什么事情。
1
2
3
4
5
|
process = frida.get_usb_device().attach( '应用完整包名' )
script = process.create_script(jscode)
script.on( 'message' , on_message)
script.load()
sys.stdin.read()
|
首先使用了frida.get_usb_device(),返回了一个_get_device函数,跟进_get_device方法。
1
2
|
def get_usb_device(timeout = 0 ):
return _get_device( lambda device: device. type = = 'tether' , timeout)
|
在_get_device中,通过get_device_manager()实例化DeviceManager类,并调用该类中的enumerate_devices()方法。
1
2
3
4
5
6
7
8
9
10
|
def _get_device(predicate, timeout):
mgr = get_device_manager() / / 获取设备管理
def find_matching_device(): / / 寻找匹配设备
usb_devices = [device for device in mgr.enumerate_devices() if predicate(device)]
if len (usb_devices) > 0 :
return usb_devices[ 0 ]
else :
return None
device = find_matching_device()
...省略
|
get_device_manager()代码
1
2
3
4
5
6
|
def get_device_manager():
global _device_manager
if _device_manager is None :
from . import core
_device_manager = core.DeviceManager(_frida.DeviceManager())
return _device_manager
|
DeviceManager中enumerate_devices()方法,可以看到enumerate_devices()方法实际上是返回了一个Device()类的实例化对象List。
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
|
class DeviceManager( object ):
def __init__( self , impl):
self ._impl = impl
def __repr__( self ):
return repr ( self ._impl)
/ / 返回了一个Device()类的实例化。
def enumerate_devices( self ):
return [Device(device) for device in self ._impl.enumerate_devices()]
def add_remote_device( self , host):
return Device( self ._impl.add_remote_device(host))
def remove_remote_device( self , host):
self ._impl.remove_remote_device(host)
def get_device( self , device_id):
devices = self ._impl.enumerate_devices()
if device_id is None :
return Device(devices[ 0 ])
for device in devices:
if device. id = = device_id:
return Device(device)
raise _frida.InvalidArgumentError( "unable to find device with id %s" % device_id)
def on( self , signal, callback):
self ._impl.on(signal, callback)
def off( self , signal, callback):
self ._impl.off(signal, callback)
|
继续跟进Device类中的,就找到了attach()方法。在attach方法这是设置断点,看看传入的数据。
接下来提供的“应用完整名”是通过self._pid_of()函数去找到对应的进程号pid,然后将pid后通过Session类初始化。到此第一句代码过程就算是明白了,最终得到的是一个对应进程号pid的Session实例化对象process。
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
|
class Device( object ):
def __init__( self , device):
self . id = device. id
self .name = device.name
self .icon = device.icon
self . type = device. type
self ._impl = device
def __repr__( self ):
return repr ( self ._impl)
...节省空间删除部分方法,详细内容可自行查看源码
def kill( self , target):
self ._impl.kill( self ._pid_of(target))
/ / 返回了一个Session的实例化对象
def attach( self , target):
return Session( self ._impl.attach( self ._pid_of(target)))
def inject_library_file( self , target, path, entrypoint, data):
return self ._impl.inject_library_file( self ._pid_of(target), path, entrypoint, data)
def inject_library_blob( self , target, blob, entrypoint, data):
return self ._impl.inject_library_blob( self ._pid_of(target), blob, entrypoint, data)
def on( self , signal, callback):
self ._impl.on(signal, callback)
def off( self , signal, callback):
self ._impl.off(signal, callback)
def _pid_of( self , target):
if isinstance (target, numbers.Number):
return target
else :
return self .get_process(target).pid
|
第二句,紧接着process.create_script(jscode),可以看到它返回一个Script类的实例化,参数不确定。
1
2
|
def create_script( self , * args, * * kwargs):
return Script( self ._impl.create_script( * args, * * kwargs))
|
跟进Script类,可以找到on()方法,在on方法中可以设置自定义回调函数。
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
|
class Script( object ):
def __init__( self , impl):
self .exports = ScriptExports( self )
self ._impl = impl
self ._on_message_callbacks = []
self ._log_handler = self ._on_log
self ._pending = {}
self ._next_request_id = 1
self ._cond = threading.Condition()
impl.on( 'destroyed' , self ._on_destroyed)
impl.on( 'message' , self ._on_message)
...节省空间删除部分类方法,详细内容可自行查看源码
def load( self ):
self ._impl.load()
/ / 设置自定义回调函数
def on( self , signal, callback):
if signal = = 'message' :
self ._on_message_callbacks.append(callback)
else :
self ._impl.on(signal, callback)
在IDE中可以看到_on_message_callbacks中存放的on_message函数地址。
|
接下来调用load()方法,在服务端就启动javascript脚本了,至于在frida-server服务端怎么执行的,可逆向研究一下frida-server,它才是真正的核心。
0x03 Javascript代码构造与执行
现在就来使用frida实现刚刚试想的方法。
方法一:获取calc()返回值
第一种思路就是直接获取calc的返回值,从native函数定义上知道它的返回值是int类型,当然直接获取calc函数的返回值是解出问题最简单的方法。
1
|
public native int calc();
|
那怎么获取calc()函数的返回值呢,这个函数在MainActivity类中,直接引用该类下的calc()方法,不就ok了吗,原理是这样,下面就来构造一下Javascript代码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/ / Java.Perform 开始执行JavaScript脚本。
Java.perform(function () {
/ / 定义变量MainActivity,Java.use指定要使用的类
var MainActivity = Java.use( 'com.example.seccon2015.rock_paper_scissors.MainActivity' );
/ / hook该类下的onCreate方法,重新实现它
MainActivity.onCreate.implementation = function () {
send( "Hook Start..." );
/ / 调用calc()方法,获取返回值
var returnValue = this.calc();
send( "Return:" + returnValue);
var result = ( 1000 + returnValue) * 107 ;
/ / 解出答案
send( "Flag:" + "SECCON{" + result.toString() + "}" );
}
});
|
JavaScript代码就是这样,如果不是很理解,学习一下JavaScript基础即可,下面看看完整的python脚本。
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
|
import frida, sys
def on_message(message, data):
if message[ 'type' ] = = 'send' :
print ( "[*] {0}" . format (message[ 'payload' ]))
else :
print (message)
jscode = """
Java.perform(function () {
var MainActivity = Java.use('com.example.seccon2015.rock_paper_scissors.MainActivity');
MainActivity.onCreate.implementation = function () {
send("Hook Start...");
var returnValue = this.calc();
send("Return:"+returnValue);
var result = (1000+returnValue)*107;
send("Flag:"+"SECCON{"+result.toString()+"}");
}
});
"""
process = frida.get_usb_device().attach( 'com.example.seccon2015.rock_paper_scissors' )
script = process.create_script(jscode)
script.on( 'message' , on_message)
script.load()
sys.stdin.read()
|
接下来运行一下,看看能否成功。
步骤如下:
- 启动模拟器,使用adb push将对应架构的frida-server文件push到模拟器中
/data/local/tmp目录下。
- adb shell 进入/data/local/tmp目录,启动frida-server。
- 开启端口转发
adb forward tcp:27043 tcp:27043
adb forward tcp:27042 tcp:27042
- 启动应用后,在命令行等执行python脚本。
因为hook的是应用的onCreate方法,执行python脚本的前提是应用首先启动,这样才能attach到该应用,所以还得返回模拟器桌面重新启动应用,这样它才会执行hook的onCreate()方法,结果如下。
方法二:修改cnt的值为1000
第二种思路也比较简单,我们需要修改cnt的值,但如果直接修改cnt的初始值为1000的话,在游戏中可能存在不确定因素,比如输了会置0,赢了cnt值就变成1001了,所以还得控制一下输赢,而输赢的条件是电脑出什么,所以最终hook的方法就在onClick中。
从onClick()中可以知道,控制输赢的在于修改this.n 和 this.m的值,再来看看源代码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public void onClick(View v) {
if (this.flag ! = 1 ) {
this.flag = 1 ;
((TextView) findViewById(R. id .textView3)).setText("");
TextView tv = (TextView) findViewById(R. id .textView);
TextView tv2 = (TextView) findViewById(R. id .textView2);
this.m = 0 ;
/ / 控制电脑出拳
this.n = new Random().nextInt( 3 );
tv2.setText(new String[]{ "CPU: Paper" , "CPU: Rock" , "CPU: Scissors" }[this.n]);
if (v = = this.P) {
tv.setText( "YOU: Paper" );
this.m = 0 ;
}
if (v = = this.r) {
tv.setText( "YOU: Rock" );
this.m = 1 ;
}
if (v = = this.S) {
tv.setText( "YOU: Scissors" );
this.m = 2 ;
}
this.handler.postDelayed(this.showMessageTask, 1000 );
}
|
来看JavaScript代码怎么写吧
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
Java.perform(function () {
var MainActivity = Java.use( 'com.example.seccon2015.rock_paper_scissors.MainActivity' );
/ / hook onClick方法,此处要注意的是onClick方法是传递了一个View参数v
MainActivity.onClick.implementation = function (v) {
send( "Hook Start..." );
/ / 调用onClick,模拟点击事件
this.onClick(v);
/ / 修改参数
this.n.value = 0 ;
this.m.value = 2 ;
this.cnt.value = 999 ;
send( "Success!" )
}
});
|
完整python代码
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
|
import frida, sys
def on_message(message, data):
if message[ 'type' ] = = 'send' :
print ( "[*] {0}" . format (message[ 'payload' ]))
else :
print (message)
jscode = """
Java.perform(function () {
var MainActivity = Java.use('com.example.seccon2015.rock_paper_scissors.MainActivity');
MainActivity.onClick.implementation = function (v) {
send("Hook Start...");
this.onClick(v);
this.n.value = 0;
this.m.value = 2;
this.cnt.value = 999;
send("Success!")
}
});
"""
process = frida.get_usb_device().attach( 'com.example.seccon2015.rock_paper_scissors' )
script = process.create_script(jscode)
script.on( 'message' , on_message)
script.load()
sys.stdin.read()
|
执行python脚本,任意点击按钮,答案就出来了。
当然,如果so中的calc()函数算法不难的前提,直接使用ida pro或者radare2分析汇编代码也是可以的。这里给出用radare2反汇编出来的代码。可以看到,calc()函数就单纯的返回了int值7。
0x04 总结
- 一般分析流程
1.反编译apk,分析代码寻找hook点。
2.编写js代码,调用类的方法或者替换。
3.在python中执行即可。
下面一篇会更详细介绍frida的使用。
https://bbs.pediy.com/thread-227232.htm
初识Frida--Android逆向之Java层hook (一)相关推荐
- 初识Frida--Android逆向之Java层hook (二)
目录 初识Frida--Android逆向之Java层hook (二) apk的安装与分析 流程分析 hook点分析 JavaScript代码构造与执行 0x00 hook getMac() 0x01 ...
- 转载:使用 Frida 来 hook 加固的 Android 应用的 java 层
Android 加固应用Hook方式 --- Frida:https://github.com/xiaokanghub/Android 转载:使用 frida 来 hook 加固的 Android 应 ...
- 安卓逆向之JAVA层静态分析(愤怒的小鸟去广告,充值破解)
安卓逆向之JAVA层静态分析(愤怒的小鸟去广告,充值破解) 前言 今天女票跟我吐槽,自己闲来无事下个愤怒的小鸟玩玩,结果又是实名认证,又是广告啥的,瞬间没心情玩了,我听后大惊,还有这种事.跟女票说:& ...
- 安卓逆向_21 --- Java层和so层的反调试( IDA 动态调试 JNI_OnLoad、init_array下断)
1. 安卓程序动态调试条件 安卓程序动态调试条件 ( 2个满足1个即可 ): 1. 在 AndroidMainfest.xml ---> application 标签下,设置或者添加属性 and ...
- Android平台下Dalvik层hook框架ddi的研究
通过adbi,可以对native层的所有代码进行hook.但对于Android系统来说,这还远远不够,因为很多应用都还是在Dalvik虚拟机中运行的. 那么,有没有什么办法可以对Dalvik虚拟机中跑 ...
- Android 逆向分析大全
转载:Android 逆向分析大全:https://www.jianshu.com/p/a12d04fc748f 1. 概述 1.1 分析步骤 通用逆向分析步骤 1. 了解该模块正向编程相关方法 2. ...
- 安卓逆向_24( 一 ) --- Hook 框架 frida( Hook Java层 和 so层) )
From:Hook 神器家族的 Frida 工具使用详解:https://blog.csdn.net/FlyPigYe/article/details/90258758 详解 Hook 框架 frid ...
- frida hook java层常用模板
文章转载于 安卓逆向菜鸟修炼记(微信公众号),个人感觉很实用,记录下来方便回顾,想看原文的请移步公众号. 1.JAVA层HOOK普通方法 import frida, sysjscode =" ...
- 5、frida进阶-Android逆向之旅---Hook神器家族的Frida工具使用详解
本文转载自:https://www.cnblogs.com/qwangxiao/p/9255328.html 一.前言 在逆向过程中有一个Hook神器是必不可少的工具,之前已经介绍了Xposed和Su ...
最新文章
- OpenCV中与matlab中相对应的函数
- 基于图像的单目三维网格重建
- EF Core查询标签TagWith
- Django(三)框架之第二篇
- hive开启kerberos-beeline连接
- python多重继承_Python多重继承
- [ACL17]A Minimal Span-Based Neural Constituency Parser(一个最小化的基于跨度的神经句法分析器)...
- 你想学的都在这里!传智播客java就业班教程
- html5对浏览器支持的情况及开发工具
- m3u8在线播放工具,在线播放地址
- 作为应聘者 面试结束时应该问面试官一些什么问题呢
- TP5.1实现数组内容的分页和数据渲染
- 医院挂号系统代码_人脸识别+身份绑定!高科技精准打击医院号贩子
- 微信小程序(视图与逻辑)
- DOS命令-格式化磁盘
- 【算法•日更•第十二期】信息奥赛一本通1585:【例 1】Amount of Degrees题解
- 通达信资金净流入公式_通达信资金净入净出指标公式
- c语言 cdma编码正交的8位码片,CDMA编码
- 本地存储Cookie、Storage、indexDB、ServiceWork离线访问网站
- bat批处理adb screencap 获取安卓设备截图以日期为文件保存至电脑
热门文章
- Makefile写法
- jsp用include指令引入html时的乱码问题
- hdu 2897(威佐夫博奕变形)
- CentOS 6.5系统安装配置图解教程(详细图文)
- 新个人项目-- 拼图游戏
- Javascript 调用XML制作连动下拉框
- Python进阶_wxpy学习:机器人对象
- Python:import详解
- [云炬创业管理笔记]第一章测试5
- 【壁纸】(可商用) 70枚壁纸高清免费