Android hook检测(安全方向)

什么是hook

所谓hook技术,就是通过一段代码(反射、代理)侵入到App启动过程中,在原本执行的代码前插入其它的功能。比如:通过hook技术,上传登陆页面的账号密码等。

干货

对于主流hook框架(Xposed、Cydia Substrate),通常有以下三种方式来检测一个App是否被hook:

  • 安装目录中是否存在hook工具
  • 存储中是否存在hook安装文件
  • 运行栈中是否存在hook相关类

1.查找设备安装目录中是否存在hook工具

private static boolean findHookAppName(Context context) {  
        PackageManager packageManager = context.getPackageManager();  
        List<ApplicationInfo> 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<String> libraries = new HashSet<String>();  
            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/51612641
https://blog.csdn.net/yin1031468524/article/details/63254757

  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值