1. 程式人生 > >Android安全常見風險處理解決方案(附程式碼)

Android安全常見風險處理解決方案(附程式碼)

一、資原始檔保護

二、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做加固處理