1、Cydia_Substrate 框架简介
官网地址:http://www.cydiasubstrate.com/
Demo地址:https://github.com/zencodex/cydia-android-hook
官方教程:http://www.cydiasubstrate.com/id/20cf4700-6379-4a14-9bc2-853fde8cc9d1
SDK下载地址:http://asdk.cydiasubstrate.com/zips/cydia_substrate-r2.zip
使用方法
下面以官网的一个实例来说明cydia substrate的使用方法。该实例是实现将多个接口组件颜色修改为紫罗兰色。
需要安装:http://www.cydiasubstrate.com/download/com.saurik.substrate.apk
步骤一:创建一个空的Android工程。由于创建的工程将以插件的形式被加载,所以不需要activity。将SDK中的substrate-api.jar复制到project/libs文件夹中。
步骤二:配置Manifest文件
(1)需要指定权限:cydia.permission.SUBSTRATE
(2)添加meta标签,name为cydia.permission.SUBSTRATE,value为下一步中创建的类名.Main
- <manifest xmlns:android="http://schemas.android.com/apk/res/android">
- <application>
- <meta-data android:name="com.saurik.substrate.main" android:value=".Main"/>
- </application>
- <uses-permission android:name="cydia.permission.SUBSTRATE"/>
- </manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> <application> <meta-data android:name="com.saurik.substrate.main" android:value=".Main"/> </application> <uses-permission android:name="cydia.permission.SUBSTRATE"/> </manifest>
步骤三:创建一个类,类名为Main。类中包含一个static方法initialize,当插件被加载的时候,该方法中的代码就会运行,完成一些必要的初始化工作。
- import com.saurik.substrate.MS;
- public class Main {
- static void initialize() {
- // ... code to run when extension is loaded
- }
- }
import com.saurik.substrate.MS; public class Main { static void initialize() { // ... code to run when extension is loaded } }
步骤四:为了实现HOOK,达到修改目标类中的代码的目的,我们需要得到目标类的一个实例,如示例中的resources。
- public class Main {
- static void initialize() {
- MS.hookClassLoad("android.content.res.Resources", new MS.ClassLoadHook() {
- public void classLoaded(Class<?> resources) {
- // ... code to modify the class when loaded
- }
- });
- }
- }
public class Main { static void initialize() { MS.hookClassLoad("android.content.res.Resources", new MS.ClassLoadHook() { public void classLoaded(Class<?> resources) { // ... code to modify the class when loaded } }); } }
步骤五:通过MS.MethodHook实例实现原代码的修改。
为了调用原来代码中的方法,我们需要创建一个MS.MethodPointer类的实例,它可以在任何时候运行原来的代码。
在这里我们通过对原代码中resources对象原始代码的调用和修改,将所有绿色修改成了紫罗兰色。
- public void classLoaded(Class<?> resources) {
- Method getColor;
- try {
- getColor = resources.getMethod("getColor", Integer.TYPE);
- } catch (NoSuchMethodException e) {
- getColor = null;
- }
- if (getColor != null) {
- final MS.MethodPointer old = new MS.MethodPointer();
- MS.hookMethod(resources, getColor, new MS.MethodHook() {
- public Object invoked(Object resources, Object... args)
- throws Throwable
- {
- int color = (Integer) old.invoke(resources, args);
- return color & ~0x0000ff00 | 0x00ff0000;
- }
- }, old);
- }
- }
public void classLoaded(Class<?> resources) { Method getColor; try { getColor = resources.getMethod("getColor", Integer.TYPE); } catch (NoSuchMethodException e) { getColor = null; } if (getColor != null) { final MS.MethodPointer old = new MS.MethodPointer(); MS.hookMethod(resources, getColor, new MS.MethodHook() { public Object invoked(Object resources, Object... args) throws Throwable { int color = (Integer) old.invoke(resources, args); return color & ~0x0000ff00 | 0x00ff0000; } }, old); } }
安装运行,重启系统后发现很多字体颜色都变了。
示例中MS.hookMethod的代码可以改成:
- MS.hookMethod(resources, getColor, new MS.MethodAlteration<Resources, Integer>() {
- public Integer invoked(Resources resources, Object... args)
- throws Throwable
- {
- int color = invoke(resources, args);
- return color & ~0x0000ff00 | 0x00ffee00;
- }
- });
MS.hookMethod(resources, getColor, new MS.MethodAlteration<Resources, Integer>() { public Integer invoked(Resources resources, Object... args) throws Throwable { int color = invoke(resources, args); return color & ~0x0000ff00 | 0x00ffee00; } });
2、短信监控实例
在下面的例子中我们实现了短信监听功能,将短信发送人、接收人以及短信内容打印出来:
- <span style="font-size:12px;">import java.lang.reflect.Method;
- import android.app.PendingIntent;
- import android.util.Log;
- import com.saurik.substrate.MS;
- public class Main {
- static void initialize() {
- MS.hookClassLoad("android.telephony.SmsManager", new MS.ClassLoadHook() {
- @Override
- public void classLoaded(Class<?> SmsManager) {
- //code to modify the class when loaded
- Method sendTextMessage;
- try {
- sendTextMessage = SmsManager.getMethod("sendTextMessage",
- new Class[]{String.class,String.class,String.class,PendingIntent.class,PendingIntent.class});
- } catch (NoSuchMethodException e) {
- sendTextMessage = null;
- }
- MS.hookMethod(SmsManager, sendTextMessage, new MS.MethodAlteration() {
- public Object invoked(Object _this,Object... _args) throws Throwable{
- Log.i("SMSHOOK","SEND_SMS");
- Log.i("SMSHOOK","destination:"+_args[0]);
- Log.i("SMSHOOK","source:"+_args[1]);
- Log.i("SMSHOOK","text:"+_args[2]);
- return invoke(_this, _args);
- }
- });
- }
- });
- }
- }</span>
<span style="font-size:12px;">import java.lang.reflect.Method; import android.app.PendingIntent; import android.util.Log; import com.saurik.substrate.MS; public class Main { static void initialize() { MS.hookClassLoad("android.telephony.SmsManager", new MS.ClassLoadHook() { @Override public void classLoaded(Class<?> SmsManager) { //code to modify the class when loaded Method sendTextMessage; try { sendTextMessage = SmsManager.getMethod("sendTextMessage", new Class[]{String.class,String.class,String.class,PendingIntent.class,PendingIntent.class}); } catch (NoSuchMethodException e) { sendTextMessage = null; } MS.hookMethod(SmsManager, sendTextMessage, new MS.MethodAlteration() { public Object invoked(Object _this,Object... _args) throws Throwable{ Log.i("SMSHOOK","SEND_SMS"); Log.i("SMSHOOK","destination:"+_args[0]); Log.i("SMSHOOK","source:"+_args[1]); Log.i("SMSHOOK","text:"+_args[2]); return invoke(_this, _args); } }); } }); } }</span>
运行步骤
- ROOT过的手机一部,没有ROOT的,请自行搜索方法。
- Eclipse 上安装 Android Substrate SDK,参考:http://www.cydiasubstrate.com/id/73e45fe5-4525-4de7-ac14-6016652cc1b8/
- 手机端去下载安装 Cydia Substrate http://www.cydiasubstrate.com/download/com.saurik.substrate.apk
- 按照章节1上面的例程,编译本代码后安装到手机
- 手机上运行Substrate,界面上点击“Link Substrate Files”,再点击 “Restart System(Soft)”(这些都是Hook 步骤)
- 最后再编写一个Test.apk, 调用hook 后的class,检验hook 结果。