閱讀本文之前 請先閱讀 我之前寫的文章 《Android安全之使用root權限繞過檢測機制,強行自動允許應用的懸浮窗/應用后臺彈出界面等權限》
了解前篇文章里面的如下幾點知識點,作為本篇的基礎:
1.在linux下如何直接運行dex (java代碼) 讓java代碼全程擁有root權限去運行一些系統api
2.匯入framework.jar, 以方便進行快速的免反射開發 (可選, 但是這樣的話你得自己寫反射和介面代理)
首先
通過在linux下執行這段dex代碼可以越權獲取到topActivity
//獲取頂部activity
public ComponentName getTopActivity(){
ComponentName topActivity = null;
for (ActivityManager.StackInfo allStackInfo : ActivityTaskManager.getService().getAllStackInfos()) {
if (allStackInfo.visible && allStackInfo.displayId == Display.DEFAULT_DISPLAY) {
topActivity =allStackInfo.topActivity;
}
}
return topActivity;
}
然后
while(true){
String act = getTopActivity();
System.out.println("topActivity=" + act );
}
哈哈哈哈哈…
騙你的, 要是這么死回圈獲取就不會有這篇文章了…
正片開始
把以下代碼 打包成jar 用dx命令打包成一個dex 運行即可 (教程在前面說的文章里)
public static void main(String[] args) throws Exception {
Looper.prepareMainLooper();
registerTaskStackChangedListener(new TaskStackListener() {
@Override
public void onTaskStackChanged() throws RemoteException {
super.onTaskStackChanged();
//獲取頂部activity
ComponentName topActivity = null;
for (ActivityManager.StackInfo allStackInfo : ActivityTaskManager.getService().getAllStackInfos()) {
if (allStackInfo.visible && allStackInfo.displayId == Display.DEFAULT_DISPLAY) {
topActivity =allStackInfo.topActivity;
}
}
sendText(""+topActivity);
}
});
Looper.loop();
}
//注冊堆疊改變監聽
private static void registerTaskStackChangedListener(TaskStackListener listener) throws Exception {
if (mTaskStackListener != null) {
ActivityTaskManager.getService().unregisterTaskStackListener(mTaskStackListener);
}
mTaskStackListener = (ITaskStackListener) listener;
ActivityTaskManager.getService().registerTaskStackListener(mTaskStackListener);
}
//利用發廣播的方式 實作 通訊"回呼"
private static void sendText(String text) {
String action = "com.dddd.zzzz.TEST";
try {
Intent intent = new Intent(action);
intent.putExtra("text", text);
getContext().sendBroadcast(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
原理:
利用root權限執行 ActivityTaskManager.getService()注冊一個activity堆疊變化監聽,避免了死回圈獲取, 減少耗電量, 取出當前的topActivity后 通過自己創建的背景關系 通過廣播發送出去, 如然后你只需用廣播接收器去接收資料即可
接收資料
這沒啥好說的 就是接收linux里的dex app行程發過來的廣播
registerReceiver(object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
LaihuaLogger.i("onReceive MX: ${intent?.action} ${intent?.getStringExtra("text")}")
}
}, IntentFilter("com.dddd.zzzz.TEST"))
本期知識點
1.linux下自己創建自己的背景關系 解決了之前文章《Android安全之使用root權限繞過檢測機制,強行自動允許應用的懸浮窗/應用后臺彈出界面等權限》中 到最后無法拿到context去呼叫一些需要context 的api 時的抓狂…
//TODO 此處為了演示 沒做背景關系快取, 會導致每次都會生成新的背景關系
private static Context getContext() throws Exception {
Class<?> ActivityThread = Class.forName("android.app.ActivityThread");
Method systemMain = ActivityThread.getDeclaredMethod("systemMain");
Object object = systemMain.invoke(null);
Class<?> ContextImpl = Class.forName("android.app.ContextImpl");
Method createSystemContext = ContextImpl.getDeclaredMethod("createSystemContext", ActivityThread);
createSystemContext.setAccessible(true);
return (Context) createSystemContext.invoke(null, object);
}
2.創建Handler運行環境, 某些系統api需要基于Handler環境進行運作
Looper.prepareMainLooper();
//你的代碼
xxxxxxx
Looper.loop();
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/341915.html
標籤:其他
上一篇:關于青年大學習提醒方法的探討
下一篇:java版Spring Cloud+Spring Boot+mybatis+uniapp b2b2c 多商戶入駐商城 直播商城 直播帶貨商城首頁介紹
