Android安全常见风险处理解决方案(附代码)

版权声明:转载请@我原创地址 https://blog.csdn.net/weixin_39706415/article/details/83896057

 

一、资源文件保护

https://blog.csdn.net/weixin_39706415/article/details/83895850

二、Activity和Service越权检测

修改export=false

三、Activity劫持检测

处理检测app进去后台的时候提示用户 也就是在onPause的时候

    /**
     * Is foreground boolean.
     *
     * @param context the context
     * @return the boolean
     */
/*判断应用是否在前台*/
    public static boolean isForeground(Context context) {
        try {
            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            assert am != null;
            List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
            if (!tasks.isEmpty()) {
                ComponentName topActivity = tasks.get(0).topActivity;
                if (topActivity.getPackageName().equals(context.getPackageName())) {
                    return true;
                }
            }
            return false;
        } catch (SecurityException e) {
            e.printStackTrace();
            return false;
        }
    }
if (!Selfutils.isForeground(this)) {
            setShow(Config.uiStyle + "应用仍在后台运行,如需退出,请先进入"+Config.uiStyle+"应用,按手机“返回键”退出。");
        }

四、Root环境检测

当进入APP的时候检测提示用户


    /**
     * Is rooted boolean.
     *
     * @return the boolean
     */
    public static boolean isRooted() {
        // nexus 5x "/su/bin/"
        String[] paths = {"/system/xbin/", "/system/bin/", "/system/sbin/", "/sbin/", "/vendor/bin/", "/su/bin/"};
        try {
            for (int i = 0; i < paths.length; i++) {
                String path = paths[i] + "su";
                if (new File(path).exists()) {
                    String execResult = exec(new String[]{"ls", "-l", path});
                    Log.d("cyb", "isRooted=" + execResult);
                    if (TextUtils.isEmpty(execResult) || execResult.indexOf("root") == execResult.lastIndexOf("root")) {
                        return false;
                    }
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    private static String exec(String[] exec) {
        String ret = "";
        ProcessBuilder processBuilder = new ProcessBuilder(exec);
        try {
            Process process = processBuilder.start();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                ret += line;
            }
            process.getInputStream().close();
            process.destroy();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }

五、网络环境检测

这个就是通过广播监听wifi发生变化提示用户

/**
  * 监控Wifi状态的广播接收器
  */
private final class WifiStateReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context c, Intent intent) {
        Bundle bundle = intent.getExtras();
        int statusInt = bundle.getInt("wifi_state");
        switch (statusInt) {
        case WifiManager.WIFI_STATE_UNKNOWN:
            break;
        case WifiManager.WIFI_STATE_ENABLING:
            break;
        case WifiManager.WIFI_STATE_ENABLED:
            LogUtil.e(tag, "wifi enable");
            if(!isWifiEnable) {
                isWifiEnable = true;
                //断网后又连上了
                isGoon = false;
                if (!Util.isServiceRun(MultiPointControlActivity.this,
                        DLNAServiceName)) {
                    LogUtil.e(tag, "start dlna service");
                }else {
                    LogUtil.e(tag, "runing .... stop dlna service");
                    stopDLNAService();
                }
                startDLNAService();
                firstPlay();
            }
            break;
        case WifiManager.WIFI_STATE_DISABLING:
            break;
        case WifiManager.WIFI_STATE_DISABLED:
            isWifiEnable = false;
            LogUtil.e(tag, "wifi disable");
            break;
        default:
            break;
        }
    }
}

private void registReceiver() {
    receiver = new WifiStateReceiver();
    IntentFilter filter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
    registerReceiver(receiver, filter);
}

六、键盘记录保护

这个可以通过禁止截屏实现或者实现软键盘 我这边实现的是禁止截屏

//设置不可以截屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);

目前我处理的就是以上这几点 目前安全还使用了360做加固处理

猜你喜欢

转载自blog.csdn.net/weixin_39706415/article/details/83896057