Android hook検出(安全方向)


ホークとは
hookテクノロジーとは,アプリケーション起動中にコード(反射,エージェント)が侵入し,本来実行していたコードの前に他の機能を挿入することである.例えば、hook技術を通じて、ログインページのアカウントパスワードなどをアップロードします.
乾物
主流のhookフレームワーク(Xposed、Cydia Substrate)では、通常、Appがhookされているかどうかを検出する3つの方法があります.
  • インストールディレクトリにhookツール
  • が存在するかどうか
  • メモリにhookインストールファイル
  • が存在するかどうか
  • 実行スタックにhook関連クラスが存在するかどうか
  • 1.デバイスインストールディレクトリにhookツールが存在するかどうかを調べる
    private static boolean findHookAppName(Context context) {  
            PackageManager packageManager = context.getPackageManager();  
            List applicationInfoList = packageManager  
                    .getInstalledApplications(PackageManager.GET_META_DATA);  
      
            for (ApplicationInfo applicationInfo : applicationInfoList) {  
                if (applicationInfo.packageName.equals("de.robv.android.xposed.installer")) {  
                    Log.wtf("HookDetection", "Xposed found on the system.");  
                    return true;  
                }  
                if (applicationInfo.packageName.equals("com.saurik.substrate")) {  
                    Log.wtf("HookDetection", "Substrate found on the system.");  
                    return true;  
                }  
            }  
            return false;  
        }  
    

    2.デバイスストレージにhookインストールファイルが存在するかどうかを検索
    private static boolean findHookAppFile() {  
            try {  
                Set libraries = new HashSet();  
                String mapsFilename = "/proc/" + android.os.Process.myPid() + "/maps";  
                BufferedReader reader = new BufferedReader(new FileReader(mapsFilename));  
                String line;  
                while ((line = reader.readLine()) != null) {  
                    if (line.endsWith(".so") || line.endsWith(".jar")) {  
                        int n = line.lastIndexOf(" ");  
                        libraries.add(line.substring(n + 1));  
                    }  
                }  
                reader.close();  
                for (String library : libraries) {  
                    if (library.contains("com.saurik.substrate")) {  
                        Log.wtf("HookDetection", "Substrate shared object found: " + library);  
                        return true;  
                    }  
                    if (library.contains("XposedBridge.jar")) {  
                        Log.wtf("HookDetection", "Xposed JAR found: " + library);  
                        return true;  
                    }  
                }  
            } catch (Exception e) {  
                Log.wtf("HookDetection", e.toString());  
            }  
            return false;  
        }  
    

    3.ルックアッププログラムが実行するスタックにhook関連クラスが存在するかどうか
    private static boolean findHookStack() {  
            try {  
                throw new Exception("findhook");  
            } catch (Exception e) {  
      
                //        
                // for(StackTraceElement stackTraceElement : e.getStackTrace()) {  
                // Log.wtf("HookDetection", stackTraceElement.getClassName() + "->"+  
                // stackTraceElement.getMethodName());  
                // }  
      
                int zygoteInitCallCount = 0;  
                for (StackTraceElement stackTraceElement : e.getStackTrace()) {  
                    if (stackTraceElement.getClassName().equals("com.android.internal.os.ZygoteInit")) {  
                        zygoteInitCallCount++;  
                        if (zygoteInitCallCount == 2) {  
                            Log.wtf("HookDetection", "Substrate is active on the device.");  
                            return true;  
                        }  
                    }  
                    if (stackTraceElement.getClassName().equals("com.saurik.substrate.MS$2")  
                            && stackTraceElement.getMethodName().equals("invoked")) {  
                        Log.wtf("HookDetection", "A method on the stack trace has been hooked using Substrate.");  
                        return true;  
                    }  
                    if (stackTraceElement.getClassName().equals("de.robv.android.xposed.XposedBridge")  
                            && stackTraceElement.getMethodName().equals("main")) {  
                        Log.wtf("HookDetection", "Xposed is active on the device.");  
                        return true;  
                    }  
                    if (stackTraceElement.getClassName().equals("de.robv.android.xposed.XposedBridge")  
                            && stackTraceElement.getMethodName().equals("handleHookedMethod")) {  
                        Log.wtf("HookDetection", "A method on the stack trace has been hooked using Xposed.");  
                        return true;  
                    }  
                }  
            }  
            return false;  
        }  
    

    4.総合判断(trueはhook)
        public static boolean isHook(Context context) {  
            if (findHookAppName(context) || findHookAppFile() || findHookStack()) {  
                return true;  
            }  
            return false;  
        } 
    

    参照先:http://blog.csdn.net/u012131859/article/details/51612641https://blog.csdn.net/yin1031468524/article/details/63254757