首页
社区
课程
招聘
[原创]Android Java虚拟机拦截技术分析
2013-10-10 21:38 34779

[原创]Android Java虚拟机拦截技术分析

2013-10-10 21:38
34779
标 题: 【原创】Android Java虚拟机拦截技术分析
作 者: 蟑螂一号  
时 间: 2013-10-10,21:44:03
    最近反编译金山毒霸,分析其广告拦截功能是如何实现的。根据金山毒霸的介绍,采用了java虚拟机拦截技术,带着好奇去研究了一下。在查看代码过程中,会看到许多被hook的Java类,例如ActivityThread、ServiceManager等。在反编译的源码中,我主要追踪了有关iphonesubinfo服务拦截的实现。并通过代码测试通过,分享一下成果。
  1.Java虚拟机拦截技术总体流程。
     1)首先通过ptrace注入so到远程进程,例如金山就是注入libksrootclient.so到远程进程。
     2)通过ptrace_call在远程进程中调用libksrootclient中的某一个静态方法,该方法主要完成加载封装的Java功能类jar包,并调用jar包中的类方法,完成进程中Java层的hook工作。比如金山毒霸中的加载jar包:ksremote.jar
  2.关键技术
     1)so注入
         so注入式Java虚拟机拦截技术的第一步,注入成功与否决定了后面的拦截功能。
     2)实现拦截Java API功能模块,并导出jar包。
          比如如果想监控程序是否读取手机IMEI/ismi、电话号码、sim卡号。通过源码分析可知,获取这些信息需要TelephonyManager类的相关方法,方法中主要是通过获取iphonesubinfo服务类提供功能。因此,如果拦截到了iphonesubinfo服务的功能,就可以成功获取应用是否读取设备信息。
        在java层,获取服务是通过ServiceManager的getService方法获取,分析getService方法,该方法首先会从sCache中查询是否存在服务,如果存在服务,就直接返回。因此,如果修改sCache中iphonesubinfo的ibinder引用,就可以实现拦截功能。
    3)在注入的so中通过JNI动态加载jar包,并执行jar包中的关键类,完成Java层Hook。
         JNI动态加载jar包原理可以查看贴子"Android中JNI方式调用Jar包"。
     关于android中拦截技术总结:
     1.android中API拦截技术有两种模式,一种是拦截C层API,另一种是拦截Java层API.
     2.C层拦截API
        在C层拦截API中最常研究的方式是拦截libbinder.so中的ioctl调用。在binder通信中,用户空间和内核空间需要ioctl系统调用,如果拦截该系统调用,然后对参数解析,可以实现lbe的主动防御功能。前提是必须非常熟悉binder机制以及binder的数据传输格式。
     3.Java虚拟机拦截技术(Java层拦截)
         Java层拦截需要自己实现关键方法的拦截,该中方式的拦截不需要对参数进行大规模解析。
例如我在实现拦截读取imei的过程中,只需在拦截Java  api处直接返回false,应用获取的imei变成null。

阿里云助力开发者!2核2G 3M带宽不限流量!6.18限时价,开 发者可享99元/年,续费同价!

收藏
点赞3
打赏
分享
最新回复 (20)
雪    币: 185
活跃值: (25)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
whnet 2013-10-10 22:34
2
0
楼主是否可以详细 讲一下这个java拦截如何去做。

比如说你最后提到的java api 取imei 的这个。
雪    币: 16
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
轩轩hlq 2013-10-11 00:10
3
0
这个有点深,从头开始,学习下
雪    币: 341
活跃值: (133)
能力值: ( LV7,RANK:110 )
在线值:
发帖
回帖
粉丝
地狱怪客 2 2013-10-11 08:19
4
0
名字很霸道
雪    币: 257
活跃值: (3623)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
蟑螂一号 1 2013-10-11 09:24
5
0
你可以去反编译金山毒霸看看,拦截imei这个主要是你需要写一个jar包,这个jar包封装了获取imei的代理类,注入的时候需要把远程进程的代理类替换了。
雪    币: 185
活跃值: (25)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
whnet 2013-10-11 09:54
6
0
-0- 好吧。我的理解 中。 注入都是so 文件的 那就需要so再去dexclassloader 这个jar 包?

那逆向就需要去看so 么。 。。这个能力不够啊。

代理类这个概念还不是特别理解。
雪    币: 76
活跃值: (55)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
yaneng 2013-10-11 10:01
7
0
这种功能只能在root的手机中使用吧?!
雪    币: 257
活跃值: (3623)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
蟑螂一号 1 2013-10-11 10:32
8
0
只能root,不然没法注入
雪    币: 65
活跃值: (53)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
yaojunhap 2013-10-11 15:51
9
0
如果是注入的话,直接注入bionic的C库怎么样
雪    币: 257
活跃值: (3623)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
蟑螂一号 1 2013-10-11 17:04
10
0
可以注入啊,但是你解析函数参数问题就大了。注入bionic c的话分析应用访问的文件和访问的网络,那还可以接收,其他的隐私拦截获取,太有困难了。
雪    币: 208
活跃值: (40)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
justlovemm 2013-10-12 09:41
11
0
果断下载、收藏加回帖。
多谢楼主!
雪    币: 65
活跃值: (53)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
yaojunhap 2013-10-12 11:19
12
0
隐私窃取走TaintDroid分析的路怎么样,不过它也不能分析.so,正在设法改造中
雪    币: 257
活跃值: (3623)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
蟑螂一号 1 2013-10-12 11:49
13
0
taintdroid不能追踪第三方so泄露的隐私信息,不过可以改造,工作量还是蛮大的
雪    币: 245
活跃值: (13)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
八十客车 2013-10-12 23:13
14
0
谢谢楼主分享
雪    币: 245
活跃值: (13)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
八十客车 2013-10-15 23:03
15
0
谢谢楼主分享
雪    币: 19
活跃值: (25)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
Aipsa 2013-10-16 17:34
16
0
Mark: "original" Android Java virtual machine to intercept technical analysis
Author: cockroaches. 1
Time: 2013-10-10, 21:44:03
Recent decompiled jinshan drug gangsters, analyze its advertising is how to implement interception function. According to the introduction of jinshan drug gangsters, USES the Java virtual machine to intercept technology, with a curious to study. In the process of check code, will see many hook Java classes, such as ActivityThread, ServiceManager, etc. In the compilation of source code, I mainly tracked about iphonesubinfo intercept service implementation. And test pass through the code and share achievements.
1. The Java virtual machine to intercept technology overall process.
1) first by ptrace injection so to the remote process, for example, jinshan is injected libksrootclient. So to the remote process.
2) in remote process call through ptrace_call libksrootclient in a static method, the method is mainly finished loading encapsulate Java function class jars, and call the class method in jars, finish the hook Java layer in the process of work. As in the loading of the jinshan drug gangsters jars: ksremote jar
2. The key technology
1) so injection
So the first step to intercept injection Java virtual machine technology, injection or not determines the success at the back of the interception function.
2) implement interception Java API function module, and export the jar package.
Such as mobile phone IMEI/reading if you want to monitor whether ismi, phone number and sim card number. Through source code analysis, access to the information need of TelephonyManager class related method, method of main is by getting iphonesubinfo service class provides functions. So if intercepted by iphonesubinfo service function, can successfully get application whether read equipment information.
In Java layer, access to services is by ServiceManager getService method of obtaining, analyzing getService method, this method will first query whether there is any service from sCache, if there is a service that is returned directly. Therefore, if the alteration of sCache iphonesubinfo ibinder references, can realize interception function.
3) through JNI in injection so dynamic loading jars, and implement the key classes of jars, complete Hook Java layer.
JNI dynamic loading jars principle can view the posts "Android JNI calling jar package".
About android intercept technology in summary:
1. The android API interceptor technology has two modes, one is to intercept C layer API, another kind is to intercept the Java layer API.
2. C layer intercept API
In C layer intercepts API interception is the most common research way libbinder. So the ioctl call. In binder communication, user space and kernel space need the ioctl system call, if the system calls, then the argument parsing, can achieve the function of lbe active defense. The premise is must be very familiar with binder mechanism and binder data transmission formats.
3. The Java virtual machine to intercept technology (Java layer intercept)
Java layer intercepts the need to realize the key method of interception, the way of intercepting the parameters on a large scale does not need to parse.
For example, I in the process of implement interception read imei, simply by intercepting directly returns false, where the Java API application access the imei become null.
雪    币: 5
活跃值: (359)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
king少 2013-10-30 14:13
17
0
谢谢楼主的分享,想看看金山的那个JAR包,可是下了新版和旧版里面都没有找到。

是否可以给提供下 。
雪    币: 257
活跃值: (3623)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
蟑螂一号 1 2013-10-30 14:34
18
0
这英语水平太高了吧
雪    币: 23
活跃值: (13)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
俺是小号 2014-8-15 16:40
19
0
请问 ksremote.jar 这个如何对java层的封装,能相信讲解一下吗,我首先在c层注入,在ioctl中发现自己对那些数据解析根本没办法,只好放在java层来做了。
雪    币: 45
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
丘比龙 2014-8-15 17:16
20
0
注入so使用DexClassLoader加载jar包。hook里面的方法即可。java hook简单的做法就是对象的替换。替换ServiceManager中的sCache中的Binder对象。
或者使用xposed的做法hook java方法。即transact方法。实现起来不难。
雪    币: 1906
活跃值: (712)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
tigerwood 2014-11-26 09:26
21
0
都是N人啊,学习了
游客
登录 | 注册 方可回帖
返回