当前位置:网站首页>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.");
}
}
}边栏推荐
- Customize to prevent repeated submission of annotations (using redis)
- Startups must survive
- redis的dict的扩容机制(rehash)
- Spark history server performance improvement (I) -- Application List
- Spark tuning tool -- detailed explanation of sparklens
- Application of analytic hierarchy process in college teaching evaluation system (principle + example + tool)
- VFP function to summarize all numeric columns of grid to cursor
- 现在网上炒股开户身份证信息安全吗?
- Using DBF of VFP to web salary query system
- 時創能源沖刺科創板:擬募資11億 年營收7億淨利反降36%
猜你喜欢

揭秘GaussDB(for Redis):全面对比Codis

分享7个神仙壁纸网站,让新的壁纸,给自己小小的雀跃,不陷入年年日日的重复。

云原生数据湖以存储、计算、数据管理等能力通过信通院评测认证

Golden sun education listed in the U.S.: a small cap medium cap stock with a market value of USD 360million

Capacity expansion mechanism of Dict Of redis (rehash)

Recommend a virtual machine software available for M1 computer

数据库系列:MySQL索引优化总结(综合版)

Nacos installation and use

The service layer reports an error. The XXX method invalid bound statement (not found) cannot be found

Spark history server performance improvement (I) -- Application List
随机推荐
一個硬件工程師走過的彎路
Niuke.com: host scheduling
兴业证券是国企吗?在兴业证券开户资金安全吗?
How to open an account for trading futures Shanghai nickel products online
Thirty lines of code prevent VFP forms from running repeatedly, and the function supports parameter transfer
Solution to the timeout scenario of Flink streaming computing (official live broadcast)
云原生数据湖以存储、计算、数据管理等能力通过信通院评测认证
Windows11 MySQL service is missing
Database Series: MySQL index optimization summary (comprehensive version)
Oracle Spatial creating spatial tables
Under what circumstances will Flink combine operator chains to form operator chains?
Recommend a virtual machine software available for M1 computer
黑马畅购商城---2.分布式文件存储FastDFS
Translation of meisai C topic in 2022 + sharing of ideas
Whole process of web page request
一个硬件工程师走过的弯路
Research on parallel computing architecture of meteorological early warning based on supercomputing platform
The cloud native data lake has passed the evaluation and certification of the ICT Institute with its storage, computing, data management and other capabilities
What is the development history, specific uses and structure of the chip
按钮多次点击造成结果