那些年Android黑科技①:只要活着,就有但願

 

「黑科技什麼的最喜歡了!
對,咱們就是要搞事。
來呀。誰怕誰。三年血賺,死刑不虧。(๑´ڡ`๑) 」
-- 來自暗世界android工程師java

前言:
這個世界上手機有三大系統,蘋果、 安卓、 中國安卓 。本篇強烈呼籲你們不要去作哪些違反用戶體驗的黑科技功能,研究研究玩玩就行了啦。全當增加技術,在真實的項目開發中儘可能能不用就不要用得好。道理你們都懂的。android

目錄

那些年Android黑科技①:只要活着,就有但願git

  • android應用內執行shell
  • 雙進程保活aidl版
  • 雙進程保活jni版
  • 保活JobService版

那些年Android黑科技②:欺騙的藝術github

  • hook技術
  • 欺騙系統之偷樑換柱

那些年Android黑科技③:幹大事不擇手段 shell

  • 應用卸載反饋
  • Home鍵監聽
  • 桌面添加快捷方式
  • 沒法卸載app(DevicePolicManager)
  • 無網絡權限偷偷上傳數據

android應用內執行shell

android系統自己是Linux做爲內核,咱們通常開發中使用 adb shell 命令來操做。但其實自己在應用內也是能夠執行的。強大的地方是在root的狀況下,能夠實現靜默安裝和操做一切你想在設備內作事情。其方法以下。數組

調用工具代碼:安全

/**
     * 是不是在root下執行命令
     *
     * @param commands        命令數組
     * @param isRoot          是否須要root權限執行
     */
    public static void execCmd(String[] commands, boolean isRoot) {
    //便於觀看刪除來不影響的部分代碼,完整的能夠在文中的github裏找到。
            process = Runtime.getRuntime().exec(isRoot ? "su" : "sh");
            os = new DataOutputStream(process.getOutputStream());
            for (String command : commands) {
                if (command == null) continue;
                os.write(command.getBytes());
                os.writeBytes("\n");
                os.flush();
            }
            os.writeBytes("exit\n");
            os.flush();
            result = process.waitFor();
                successMsg = new StringBuilder();
                errorMsg = new StringBuilder();
                successResult = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
                errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream(), "UTF-8"));
    }

沒有root權限的狀況下在屏幕上操做,實測可被執行的命令只有swipe和部分keyevent能夠生效,其他的能夠經過adb的方式調用成功。可是一但在應用內經過shell是不能夠的。這確保了android手機的安全。網絡

其中keyevent 返回鍵 音量鍵能夠調用 而home按鍵這種則不能夠。
若是你試圖調用dumpsys activity activities 來查看。會拋出權限的異常以下。實測中我有申請權限,但同樣沒法在應用內部調起。app

Permission Denial: can't dump ActivityManager from from pid=12414, uid=10369 without permission android.permission.DUMP0

image.png

image.pngide

使用參考:

Root狀況下靜默安裝:

String command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib pm install " +"apk路徑";
  ShellUtils.execCmd(command, ture);

代碼:https://github.com/BolexLiu/AndroidShell

雙進程保活aidl版 (android5.0如下)

原理介紹:實現的機制並不複雜,經過AIDL的方式開啓兩個服務分別在不一樣進程中啓動,而後互相守護監聽對方是否被關閉,若是有一方被斷開鏈接,另外一方測重啓服務。由於android在5.0以前銷燬進程是一個一個銷燬的,他並不能同時銷燬兩個。因此能夠作這件事。(被修改的rom除外,好比華爲4.4就不行,但三星能夠。)

1.配置服務進程。注意process屬性會獨立在另外一個進程中。

<service android:name=".Service.LocalService" />
    <service android:name=".Service.RemoteService"  android:process=".Remote"/>

2.咱們擁有兩個服務LocalService RemoteService。項目運行後第一件事,同時啓動服務。

startService(new Intent(this, LocalService.class));
        startService(new Intent(this, RemoteService.class));

3.在LocalService中綁定RemoteService並監聽對方的建立和銷燬,RemoteService中的實現也同樣。

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        Log.e(TAG, TAG + " onStartCommand");
        //  綁定遠程服務
        bindService(new Intent(this, RemoteService.class), mLocalServiceConnection, Context.BIND_IMPORTANT);
        return START_STICKY;
    }
    //鏈接遠程服務
    class localServiceConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            try {
                // 與遠程服務通訊
                MyProcessAIDL process = MyProcessAIDL.Stub.asInterface(service);
                Log.e(TAG, "鏈接" + process.getServiceName() + "服務成功");
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // RemoteException鏈接過程出現的異常,纔會回調,unbind不會回調
            // 監測,遠程服務已經死掉,則重啓遠程服務
            Log.e(TAG, "遠程服務掛掉了,遠程服務被殺死");
            // 啓動遠程服務
            startService(new Intent(LocalService.this, RemoteService.class));
            // 綁定遠程服務
            bindService(new Intent(LocalService.this, RemoteService.class), mLocalServiceConnection, Context.BIND_IMPORTANT);
        }
    }

代碼:https://github.com/BolexLiu/DoubleProcess

雙進程保活jni版 (android5.0如下)

原理介紹:這種雙進程守利用了Linux子進程在父進程被幹掉後還能運行而實現。因此咱們要作的是經過java去fork一段C的代碼。經過動態連接庫封裝起來。而後在C代碼裏不斷輪訓父進程的ppid是否存活。若是掛掉了側從新喚醒。

1.配置服務進程。注意process屬性會獨立在另外一個進程中。

<service
            android:name=".service.DaemonService"
            android:process=":daemon"></service>

2.在DaemonService裏利用靜態代碼塊調起so。

public class DaemonService extends Service{
  // 便於閱讀省略無關代碼,詳情去移步至github···
       static {
        System.loadLibrary("daemon");
    }
}

3.so中的C代碼輪訓進程判斷是否存活。

//便於閱讀省略無關代碼,詳情去移步至github···
//fork子進程,以執行輪詢任務
    pid_t pid = fork();
    LOGI("fork=%d", pid);
    if (pid < 0) {
// fork失敗了
    } else if (pid == 0) {
// 能夠一直採用一直判斷文件是否存在的方式去判斷,可是這樣效率稍低,下面使用監聽的方式,死循環,每一個一秒判斷一次,這樣太浪費資源了。
        int check = 1;
        while (check) {
            pid_t ppid = getppid();
            LOGI("pid=%d", getpid());
            LOGI("ppid=%d", ppid);
            if (ppid == 1) {
                LOGI("ppid == 1");
                if (sdkVersion >= 17) {
                    LOGI("> 17");
                    int ret = execlp("am", "am", "startservice", "--user", "0",
                                     "-n", name,
                                     (char *) NULL);
                } else {
                    execlp("am", "am", "startservice", "-n",
                           name,
                           (char *) NULL);
                    LOGI("else");
                }
                check = 0;
            } else {
            }
            sleep(1);
        }
    }

感謝CharonChui開源代碼。處應該有掌聲!

代碼:https://github.com/CharonChui/DaemonService

保活 JobService版 (android5.0++)

原理: JobService是官方推薦的方式,即便app完成被殺死的狀態下也能調用起來,本質是向系統註冊一個任務。經過getSystemService拿到系統的JobScheduler。而後經過JobInfo.Buidler進行構造。須要注意的是必定要指定被觸發的條件。好比:設備充電中、空閒狀態、鏈接wifi... 很是相似之前的廣播保護原理。可是實現不同。此次是咱們反向註冊給系統,而不是接收系統的廣播。

1.在AndroidManifest進行配置添加permission屬性

<service
            android:name=".MyJobService"
            android:permission="android.permission.BIND_JOB_SERVICE" />

2.MyJobServer繼承JobService類:

@Override
    public boolean onStartJob(JobParameters params) {
        //該方法被觸發調用 能夠作喚醒其餘服務的操做
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
      
        return true;
    }

3.在合適的地方向系統註冊

JobScheduler scheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);  
        ComponentName componentName = new ComponentName(MainActivity.this, MyJobService.class);  
        JobInfo.Builder builder = new JobInfo.Builder(++mJobId, componentName);  
         String delay = mDelayEditText.getText().toString();  
        if (delay != null && !TextUtils.isEmpty(delay)) {  
            //設置JobService執行的最小延時時間  
            builder.setMinimumLatency(Long.valueOf(delay) * 1000);  
        }  
        String deadline = mDeadlineEditText.getText().toString();  
        if (deadline != null && !TextUtils.isEmpty(deadline)) {  
            //設置JobService執行的最晚時間  
            builder.setOverrideDeadline(Long.valueOf(deadline) * 1000);  
        }  
        boolean requiresUnmetered = mWiFiConnectivityRadioButton.isChecked();  
        boolean requiresAnyConnectivity = mAnyConnectivityRadioButton.isChecked();  
        //設置執行的網絡條件  
        if (requiresUnmetered) {  
            builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED);  
        } else if (requiresAnyConnectivity) {  
            builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);  
        }  
        builder.setRequiresDeviceIdle(mRequiresIdleCheckbox.isChecked());//是否要求設備爲idle狀態  
        builder.setRequiresCharging(mRequiresChargingCheckBox.isChecked());//是否要設備爲充電狀態  
        scheduler.schedule(builder.build());

注意jobScheduler沒法兼容Android 5.0如下的設備,能夠參考下面的項目,在低版本中也可使用。

**實際測試 : **
研究了一段時間發現這個玩意,在國內的廠商定製事後的rom好多不起做用。 好比魅族 和小米上 若是把app殺死之後,這個服務也調用不起來了。可是在模擬器和aosp版本的Rom上是可行的。我測試時用的電池充電狀態來調用job服務。

代碼:https://github.com/evant/JobSchedulerCompat

第一部分就先到這裏。後續還有兩篇續集會緊接着養分跟上,若是你以爲不錯能夠關注我一波點個喜歡神馬的哈哈。

那些年Android黑科技①:只要活着,就有但願

  • android應用內執行shell
  • 雙進程保活aidl版
  • 雙進程保活jni版
  • 保活JobService版

那些年Android黑科技②:欺騙的藝術

  • hook技術
  • 欺騙系統之偷樑換柱

那些年Android黑科技③:幹大事不擇手段

  • 應用卸載反饋
  • Home鍵監聽
  • 桌面添加快捷方式
  • 沒法卸載app(DevicePolicManager)
  • 無網絡權限偷偷上傳數據

做者:香脆的大雞排 連接:http://www.jianshu.com/p/cb2deed0f2d8 來源:簡書 著做權歸做者全部。商業轉載請聯繫做者得到受權,非商業轉載請註明出處。

相關文章
相關標籤/搜索