本文主要是介绍Hook框架xposed的简单demo,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原文地址:http://blog.csdn.net/coc_k/article/details/52584059?locationNum=2&fps=1
简介:Xposed框架是一款可以在不修改APK的情况下影响程序运行的框架服务,通过替换/system/bin/app_process程序控制zygote进程,使得app_process在启动过程中会加载XposedBridge.jar这个jar包,从而完成对Zygote进程及其创建的虚拟机的劫持。
Github地址:https://github.com/rovo89/Xposed
首先我们Hook无参数的函数
1.建立一个空工程,编写获取序列号的简单例子
1.获取手机状态需要设置权限
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
2.编写布局文件
<TextViewandroid:id="@+id/tv1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!" /><TextViewandroid:id="@+id/tv2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!" />
3. 编写主 Activity 类中的 onCreate 函数
package com.cockroach.hook_object;import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.TextView;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);TextView tv1 = (TextView) findViewById(R.id.tv1);TextView tv2 = (TextView) findViewById(R.id.tv2);TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);tv1.setText("imei:" + tm.getDeviceId());tv2.setText("imsi:" + tm.getSubscriberId());}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
4. 完成安装,运行效果如下:
这就是我们的等会要hook的apk程序,下面编写xposed插件
2.建立一个无 activity 的工程, 编写 Xposed 插件
1. 设置清单文件
在 application 标签中增加模块说明信息
<application
android:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme"><meta-data android:name="xposedmodule" android:value="true"></meta-data><meta-data android:name="xposeddescription" android:value="qq迅雷提速神器"></meta-data><meta-data android:name="xposedminversion" android:value="54"></meta-data></application>
2. 导入 xposed 库文件
将 xposed 库文件 XposedBridgeApi-XX.jar, 放入 app/lib 文件夹下
需要注意的是: 修改 Scope 为 Provided
3. 创建一个类, Main, 实现 xposed 中的接口 IXposedHookLoadPackage 并重写方法handleLoadPackage
package com.cockroach.xposedhookdemo;import android.telephony.TelephonyManager;import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodReplacement;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;/*** Created by 18459 on 2016/6/27.*/
public class Main implements IXposedHookLoadPackage{@Overridepublic void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {if(!loadPackageParam.packageName.equals("com.cockroach.hook_object"))return;XposedBridge.log("Loaded app: " + loadPackageParam.packageName);XposedHelpers.findAndHookMethod(TelephonyManager.class, "getSubscriberId", new XC_MethodReplacement() {@Overrideprotected Object replaceHookedMethod(MethodHookParam methodHookParam) throws Throwable {return "Hook 成功了 哈哈!!!";}});}
}
- 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
- 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
4. 声明主入口类路径
需要在 main 文件夹下建立 assets 文件夹中新建一个 xposed_init 的文件,并在其中声明主入口类
到这里这个hook无参函数的简单demo插件就完成了,
然后我们hook有参数的函数
1.建立一个空工程,编写一个登陆界面,用自定义的一个方法
布局文件中添加
<EditText
android:id="@+id/et1"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入你的账号"/>
<EditText
android:id="@+id/et2"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入你的密码"/>
<Button
android:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="my_onClick"android:text="登陆"android:textSize="26dp"/>
MainActivity中添加
public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void my_onClick(View view) {EditText editText = (EditText) findViewById(R.id.et1);EditText editText1 = (EditText) findViewById(R.id.et2);String string = editText.getText().toString();String string1 = editText1.getText().toString();if(CheckRegister(string,string1)){Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();}else {Toast.makeText(MainActivity.this,"登陆失败",Toast.LENGTH_SHORT).show();}}private boolean CheckRegister(String string, String string1) {return string.equals(string1);}
}
- 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
- 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
效果,两个输入框输入相同时 提示登陆成功,不同时 提示登陆失败
到这里测试app完成
2.接下来我们HOOK CheckRegister方法,达到不管输入什么,都提示登陆成功
这里配置和前面无参插件编写一样,只需修改MainActivity
package com.cockroach.xposedhookdemo;import android.util.Log;import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;/*** Created by 18459 on 2016/6/27.*/
public class Main implements IXposedHookLoadPackage{String packName = "com.cockroach.myapplication";String className = "com.cockroach.myapplication.MainActivity";public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {if(!loadPackageParam.packageName.equals(packName))return;XposedBridge.log("Loaded app: " + loadPackageParam.packageName);XposedHelpers.findAndHookMethod(className, loadPackageParam.classLoader, "CheckRegister", String.class, String.class, new XC_MethodHook() {@Overrideprotected void beforeHookedMethod(MethodHookParam param) throws Throwable {super.beforeHookedMethod(param);Log.d("xposedplugin", (String) param.args[0]);Log.d("xposedplugin", (String) param.args[1]);param.args[0] = "123";param.args[1] = "123";param.setResult(true);}@Overrideprotected void afterHookedMethod(MethodHookParam param) throws Throwable {super.afterHookedMethod(param);Log.d("xposedplugin", (String) param.args[0]);Log.d("xposedplugin", (String) param.args[1]);}});}
}
- 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
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 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
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
到这里hook有参函数的插件完成了
测试app有了, 插件app有了,我们如何去使用?
上面我们写了共写了两个测试app和两个插件app,我们用第一对app,来做测试
1.在root后的手机上安装xposed框架app,然后进入app,选择 框架->安装更新,此时会重启,
2.将测试apk和插件apk都安装到手机上, 当插件apk安装到手机上时,框架xposed app会检测到插件,
3.进入框架app,选择 模块,选中我们写的插件,然后重启手机
开机后我们运行测试app,看到已经hook成功了
hook之后运行测试app
如果你是新手,没看明白可以问我,也许可以帮到你。
这篇关于Hook框架xposed的简单demo的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!