当前位置:网站首页>Hook技术
Hook技术
2022-06-25 11:49:00 【用户9854323】
参考: https://www.jianshu.com/p/4f6d20076922
使用 Java 反射实现 API Hook
通过对 Android 平台的虚拟机注入与 Java 反射的方式,来改变 Android 虚拟机调用函数的方式(ClassLoader),从而达到 Java 函数重定向的目的,这里我们将此类操作称为 Java API Hook。
下面通过 Hook View 的 OnClickListener 来说明 Hook 的使用方法。
首先进入 View 的 setOnClickListener 方法,我们看到 OnClickListener 对象被保存在了一个叫做 ListenerInfo 的内部类里,其中 mListenerInfo 是 View 的成员变量。ListeneInfo 里面保存了 View 的各种监听事件,比如 OnClickListener、OnLongClickListener、OnKeyListener 等等。
public class MainActivity extends AppCompatActivity {
TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = findViewById(R.id.text);
mTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("tag", "origin click");
}
});
hookOnClickListener(mTextView);
}
private void hookOnClickListener(View view) {
try {
// 得到 View 的 ListenerInfo 对象
Method getListenerInfo = View.class.getDeclaredMethod("getListenerInfo");
getListenerInfo.setAccessible(true);
Object listenerInfo = getListenerInfo.invoke(view);
// 得到 原始的 OnClickListener 对象
Class<?> listenerInfoClz = Class.forName("android.view.View$ListenerInfo");
Field mOnClickListener = listenerInfoClz.getDeclaredField("mOnClickListener");
mOnClickListener.setAccessible(true);
View.OnClickListener originOnClickListener = (View.OnClickListener) mOnClickListener.get(listenerInfo);
// 用自定义的 OnClickListener 替换原始的 OnClickListener
View.OnClickListener hookedOnClickListener = new HookedOnClickListener(originOnClickListener);
mOnClickListener.set(listenerInfo, hookedOnClickListener);
} catch (Exception e) {
Log.e("tag", "hook clickListener failed!", e);
}
}
public class HookedOnClickListener implements View.OnClickListener {
private View.OnClickListener origin;
HookedOnClickListener(View.OnClickListener origin) {
this.origin = origin;
}
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "hook click", Toast.LENGTH_SHORT).show();
Log.e("tag", "Before click, do what you want to to.");
if (origin != null) {
origin.onClick(v);
}
Log.e("tag", "After click, do what you want to to.");
}
}
}边栏推荐
- Cesium draw point line surface
- How to open an account for trading futures Shanghai nickel products online
- VFP develops a official account to receive coupons, and users will jump to various target pages after registration, and a set of standard processes will be sent to you
- Idea uses the fast request interface for debugging
- Tool usage summary
- Use of JSP sessionscope domain
- Flink batch key points (personal translation)
- 机器学习自学成才的十条戒律
- Gradle知识点
- Recommend a virtual machine software available for M1 computer
猜你喜欢

Niuke: rotation array

推荐一款M1电脑可用的虚拟机软件

Record the process of submitting code to openharmony once

ThingsPanel 发布物联网手机客户端(多图)

Application of analytic hierarchy process in college teaching evaluation system (principle + example + tool)

VFP develops a official account to receive coupons, and users will jump to various target pages after registration, and a set of standard processes will be sent to you

Thingspanel releases Internet of things mobile client (multiple pictures)

为什么ping不通网站 但是却可以访问该网站?

Oracle Spatial creating spatial tables

Evaluating the overall situation of each class in a university based on entropy weight method (formula explanation + simple tool introduction)
随机推荐
数据库系列:MySQL索引优化总结(综合版)
The idea of mass distribution of GIS projects
JS indexof() always returns -1
The service layer reports an error. The XXX method invalid bound statement (not found) cannot be found
Sentinel integrated Nacos data source
Xishan technology rushes to the scientific innovation board: it plans to raise 660million yuan. Guoyijun and his wife have 60% of the voting rights
Countdownlatch source code analysis
時創能源沖刺科創板:擬募資11億 年營收7億淨利反降36%
开哪家证券公司的账户是比较好,比较安全的
Why can't the form be closed? The magic of revealing VFP object references
Effective reading of literature
Two ways of redis persistence -- detailed explanation of RDB and AOF
quarkus saas动态数据源切换实现,简单完美
Using DBF of VFP to web salary query system
Cesium editing faces
Spark runs wordcount (case 2)
confluence7.4.X升级实录
Dark horse shopping mall ---8 Microservice gateway and JWT token
ThingsPanel 发布物联网手机客户端(多图)
Flink batch key points (personal translation)