How do I protect my android app from packet capture?

Issue

I want to make my Android app secure. In some applications, if the phone has a network sniffer program installed(packet capture, ssl capture etc), the application gives an alert(Remove packet capture) and turns off. How do I add a similar security measure? What technologies should I look for?

Solution

I solved this using this:

Create a HelperUtils.java class in your utils folder and add the following code:

public class HelperUtils {
private Activity activity;

public HelperUtils(Activity activity) {
    this.activity = activity;
}

public boolean isVpnConnectionAvailable(){
    String iface = "";
    try {
        for (NetworkInterface networkInst : Collections.list(NetworkInterface.getNetworkInterfaces())){
            if (networkInst.isUp())
                iface = networkInst.getName();
            if ( iface.contains("tun") || iface.contains("ppp") || iface.contains("pptp")) {
                return true;
            }
        }


    }catch (SocketException e){
        e.printStackTrace();
    }
    return false;
}

public ApplicationInfo getRestrictApp() {
    ApplicationInfo restrictPackageInfo = null;
    final PackageManager pm = activity.getPackageManager();
    //get a list of installed apps.
    List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
    for (ApplicationInfo packageInfo : packages) {
        if (packageInfo.packageName.equals("com.guoshi.httpcanary") ||
                packageInfo.packageName.equals("app.greyshirts.sslcapture") ||
                packageInfo.packageName.equals("com.guoshi.httpcanary.premium") ||
                packageInfo.packageName.equals("com.minhui.networkcapture.pro") ||
                packageInfo.packageName.equals("com.minhui.networkcapture") ||
                packageInfo.packageName.equals("com.egorovandreyrm.pcapremote") ||
                packageInfo.packageName.equals("com.packagesniffer.frtparlak") ||
                packageInfo.packageName.equals("jp.co.taosoftware.android.packetcapture") ||
                packageInfo.packageName.equals("com.emanuelef.remote_capture") ||
                packageInfo.packageName.equals("com.minhui.wifianalyzer") ||
                packageInfo.packageName.equals("com.evbadroid.proxymon") ||
                packageInfo.packageName.equals("com.evbadroid.wicapdemo") ||
                packageInfo.packageName.equals("com.evbadroid.wicap") ||
                packageInfo.packageName.equals("com.luckypatchers.luckypatcherinstaller") ||
                packageInfo.packageName.equals("ru.UbLBBRLf.jSziIaUjL")) {
            //restrictPackageName = packageInfo.packageName;
            //restrictPackageName = packageInfo.loadLabel(activity.getPackageManager()).toString();
            restrictPackageInfo = packageInfo;
        }
    }

    return restrictPackageInfo;
}

public boolean isForeground( String myPackage){
    ActivityManager manager = (ActivityManager) activity.getSystemService(ACTIVITY_SERVICE);
    List< ActivityManager.RunningTaskInfo > runningTaskInfo = manager.getRunningTasks(1);

    ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
    Log.e("123456", "Background Apps: " + componentInfo.getPackageName());
    return componentInfo != null && componentInfo.getPackageName().equals(myPackage);
}

public boolean isAppRunning(Context context, String packageName){
    ActivityManager activityManager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();
    if (processInfos != null){
        for (ActivityManager.RunningAppProcessInfo processInfo : processInfos){
            if (processInfo.processName.equals(packageName)){
                return true;
            }
        }
    }
    return false;
}

public void showWarningDialog(Activity context, String title, String message) {
    new AlertDialog.Builder(context)
            .setTitle(title)
            .setMessage(message)
            .setIcon(R.drawable.ic_warning)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    System.exit(0);
                    context.finish();
                }
            })
            .setCancelable(false)
            .show();
}

}

After this go to ur MainActivity.Java class and add the following code:

private boolean vpnStatus;
   
//check vpn connection
                    helperUtils = new HelperUtils(MainActivity.this);
                    vpnStatus = helperUtils.isVpnConnectionAvailable();
                    if (vpnStatus) {
                        helperUtils.showWarningDialog(MainActivity.this, getString(R.string.vpn_detected), getString(R.string.close_vpn));
                        return;
                    }

Add the following code in your sting.xml file:

    <string name="vpn_detected">VPN Detected!!</string> 
<string name="close_vpn">Close VPN and re-open the app to continue.</string>

This is it, now your app will show " Vpn Detected " everytime someone try to access it using a vpn or ssl capture app. Good luck.

Answered By – Anees

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published