net.easysol.dsb.web_protector.antiphishing.AntiphishingController.java Source code

Java tutorial

Introduction

Here is the source code for net.easysol.dsb.web_protector.antiphishing.AntiphishingController.java

Source

package net.easysol.dsb.web_protector.antiphishing;

import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build.VERSION;
import android.os.SystemClock;
import android.provider.Settings.System;
import android.support.v4.app.NotificationCompat.Builder;
import android.support.v4.app.TaskStackBuilder;
import android.util.Log;
import java.util.ArrayList;
import java.util.Iterator;
import net.easysol.dsb.DSB;
import net.easysol.dsb.application.ConfApp;
import net.easysol.dsb.application.ExpressionGenerator;
import net.easysol.dsb.device_protector.analytics.DeviceSystemAnalytics;
import net.easysol.dsb.licensing.License;
import net.easysol.dsb.licensing.LicenseResponse;
import net.easysol.dsb.licensing.LicensingController;
import net.easysol.dsb.licensing.events.EventSenderController;
import net.easysol.dsb.licensing.protected_sites.ProtectedSite;
import net.easysol.dsb.web_protector.ScreenReceiver;
import net.easysol.dsb.web_protector.WebAnalyzerService;
import net.easysol.dsb.web_protector.WebProtectorPreferences;
import net.easysol.dsb.web_protector.blacklist.WebBlackListHelper;
import net.easysol.dsb.web_protector.blacklist.WebProtectorDAOContext;
import net.easysol.dsb.web_protector.browser.BlockingPage;
import net.easysol.dsb.web_protector.browser.BrowserHelper;
import net.easysol.dsb.web_protector.browser.BrowserType;
import net.easysol.dsb.web_protector.browser.URLState;
import org.apache.cordova.BuildConfig;
import org.bouncycastle.easy.asn1.x509.ReasonFlags;
import org.bouncycastle.easy.math.ec.ECFieldElement.F2m;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class AntiphishingController {
    private static /* synthetic */ int[] $SWITCH_TABLE$net$easysol$dsb$web_protector$browser$BrowserType = null;
    private static AntiphishingController INSTANCE = null;
    private static final int M_ID = 301;
    private WebPharmingController antipharmingHelper;
    private ArrayList<String> blockedPages;
    private BrowserHelper browserHelper;
    private WebProtectorDAOContext dataContext;
    private Editor editor;
    private EventSenderController eventSender;
    private ExpressionGenerator expressionGenerator;
    private LicensingController licensingController;
    private Context myContext;
    private SharedPreferences preferences;
    private ArrayList<String> trustEntries;
    private ArrayList<String> trustPages;
    private WebAnalyzerListener webAnalyzerListener;
    private WebBlackListHelper webBlackListHelper;

    /* renamed from: net.easysol.dsb.web_protector.antiphishing.AntiphishingController.1 */
    class C02601 extends AsyncTask<Void, Void, Void> {
        private final /* synthetic */ License val$license;

        C02601(License license) {
            this.val$license = license;
        }

        protected Void doInBackground(Void... params) {
            AntiphishingController.this.eventSender.sendFakeAppBrowsingEvent(this.val$license.licenseKey,
                    this.val$license.malwareApps, this.val$license.malwareSignatures);
            return null;
        }
    }

    static /* synthetic */ int[] $SWITCH_TABLE$net$easysol$dsb$web_protector$browser$BrowserType() {
        int[] iArr = $SWITCH_TABLE$net$easysol$dsb$web_protector$browser$BrowserType;
        if (iArr == null) {
            iArr = new int[BrowserType.values().length];
            try {
                iArr[BrowserType.CHROME.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                iArr[BrowserType.NATIVE.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                iArr[BrowserType.NEW_NATIVE.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
            $SWITCH_TABLE$net$easysol$dsb$web_protector$browser$BrowserType = iArr;
        }
        return iArr;
    }

    static {
        INSTANCE = null;
    }

    public static AntiphishingController getInstance(Context myContext) {
        createInstance(myContext);
        return INSTANCE;
    }

    private static void createInstance(Context myContext) {
        if (INSTANCE == null) {
            synchronized (AntiphishingController.class) {
                if (INSTANCE == null) {
                    INSTANCE = new AntiphishingController(myContext);
                }
            }
        }
    }

    private AntiphishingController(Context context) {
        this.myContext = context;
        this.dataContext = WebProtectorDAOContext.getInstance(this.myContext);
        this.preferences = this.myContext.getSharedPreferences(ConfApp.SHARED_PREFERENCES, 0);
        this.editor = this.preferences.edit();
        this.browserHelper = new BrowserHelper(this.myContext);
        this.expressionGenerator = new ExpressionGenerator();
        this.licensingController = new LicensingController(this.myContext);
        this.webBlackListHelper = new WebBlackListHelper(this.myContext, this.dataContext);
        this.antipharmingHelper = new WebPharmingController(this.myContext);
        this.eventSender = EventSenderController.getInstance(this.myContext);
        this.trustPages = new ArrayList();
        this.trustEntries = new ArrayList();
        this.blockedPages = new ArrayList();
    }

    public void userPresentBroadcataRegister(Context context) {
        ScreenReceiver userPresentReceiver = new ScreenReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.intent.action.SCREEN_OFF");
        context.registerReceiver(userPresentReceiver, filter);
    }

    public void startWebAnalyzerService() {
        if (this.webAnalyzerListener != null) {
            this.webAnalyzerListener.onWebAnalyzerOn();
        }
        if (this.preferences.getBoolean(WebProtectorPreferences.WEB_PROTECTOR_ENABLE_SP, true)) {
            this.myContext.startService(new Intent(this.myContext, WebAnalyzerService.class));
        }
    }

    public void stopWebAnalyzerService() {
        if (this.webAnalyzerListener != null) {
            this.webAnalyzerListener.onWebAnalyzerOff();
        }
        Log.i("SERVICE", "Stoping web protector");
        AlarmManager alarmManager = (AlarmManager) this.myContext
                .getSystemService(NotificationCompatApi24.CATEGORY_ALARM);
        Intent service = new Intent(this.myContext, WebAnalyzerService.class);
        PendingIntent op = PendingIntent.getService(this.myContext,
                WebProtectorPreferences.REQUEST_CODE_WEB_PROTECTOR, service, 0);
        op.cancel();
        alarmManager.cancel(op);
        this.myContext.stopService(service);
    }

    public boolean updateBrowsingBlackList(String responseLicense) {
        return validateLicense(readLicenseJSON(responseLicense));
    }

    private LicenseResponse[] readLicenseJSON(String licenseJSON) {
        LicenseResponse[] licenseResponses = null;
        try {
            JSONArray jArray = new JSONArray(licenseJSON);
            int cant = jArray.length();
            licenseResponses = new LicenseResponse[cant];
            for (int i = 0; i < cant; i++) {
                LicenseResponse license = new LicenseResponse();
                JSONObject lkJSON = jArray.getJSONObject(i);
                license.licenseKey = lkJSON.getString("licenseKey");
                license.malwareActive = lkJSON.getBoolean(License.MALWARE_ACTIVE);
                license.licenseStatus = lkJSON.getInt(License.LICENSE_STATUS);
                license.status = lkJSON.getString(License.STATUS);
                license.clientName = lkJSON.getString(License.CLIENT_NAME);
                license.protectedSites = readJSONArrayString(lkJSON.getJSONArray("protectedSites"));
                license.descriptionProtectedSites = readJSONArrayString(
                        lkJSON.getJSONArray("descriptionProtectedSites"));
                licenseResponses[i] = license;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return licenseResponses;
    }

    private ArrayList<String> readJSONArrayString(JSONArray jArray) throws JSONException {
        int cant = jArray.length();
        ArrayList<String> protectedSites = new ArrayList();
        for (int i = 0; i < cant; i++) {
            protectedSites.add(jArray.getString(i));
        }
        return protectedSites;
    }

    public boolean validateLicense(LicenseResponse[] licenses) {
        if (licenses == null) {
            return false;
        }
        this.webBlackListHelper.cleanBlacklist(licenses);
        for (LicenseResponse license : licenses) {
            if (license.licenseKey != null && license.licenseStatus == 0) {
                this.webBlackListHelper.saveBlackListVersion(license.licenseKey);
            }
        }
        return true;
    }

    public void scheduleNextWebAnalyzer(long interval) {
        if (DeviceSystemAnalytics.getInstance(this.myContext).screenIsOn()) {
            ((AlarmManager) this.myContext.getSystemService(NotificationCompatApi24.CATEGORY_ALARM)).set(3,
                    SystemClock.elapsedRealtime() + interval,
                    PendingIntent.getService(this.myContext, WebProtectorPreferences.REQUEST_CODE_WEB_PROTECTOR,
                            new Intent(this.myContext, WebAnalyzerService.class), 0));
            return;
        }
        Log.d("RECEIVER", "PowerManager Screen Off");
        stopWebAnalyzerService();
    }

    public boolean isMaliciousURL(String url) {
        if (this.webBlackListHelper.isMalwareIncident(url) || this.antipharmingHelper.isPharmingIncident(url)
                || this.webBlackListHelper.isPhishingIncident(url)) {
            return true;
        }
        return false;
    }

    public boolean phishingAnalyzer(String url, boolean subimEvents) {
        if (!this.webBlackListHelper.isPhishingIncident(url)) {
            return false;
        }
        if (subimEvents) {
            this.eventSender.sendPhishingEvent(this.webBlackListHelper.findLicensesForPhishingBlackSites(url), url);
        }
        return true;
    }

    public boolean isMalwareAppInfected(String url, boolean showNotification) {
        boolean isMalwareApp = false;
        boolean subimEvents = this.preferences.getBoolean(ConfApp.SUBMIT_EVENTS_SP, true);
        if (this.expressionGenerator.isValidURL(url)) {
            String urlPath = (String) this.expressionGenerator.getHostList().get(0);
            String urlPath1 = String.format("www.%s", new Object[] { urlPath });
            this.editor.putString(WebProtectorPreferences.URL_CACHE_MALWARE_SP, urlPath);
            this.editor.commit();
            License license = this.licensingController.licenseByURL(urlPath);
            if (license == null) {
                license = this.licensingController.licenseByURL(urlPath1);
                this.editor.putString(WebProtectorPreferences.URL_CACHE_MALWARE_SP, urlPath1);
                this.editor.commit();
                urlPath = urlPath1;
            }
            if (license != null && license.malware) {
                if (subimEvents) {
                    sendFakeAppBrowsingEvent(license);
                }
                String[] names = license.malwareApps.split(",");
                String[] packageNames = license.malwarePackages.split(",");
                for (int i = 0; i < names.length; i++) {
                    if (WebUtilsHelper.isAppInstalled(this.myContext, packageNames[i])) {
                        isMalwareApp = true;
                        if (this.preferences.getBoolean(WebProtectorPreferences.ALLOW_MALWARE_APP_NOTIFICATION,
                                true) && showNotification) {
                            createMalwareNotificaction(i + M_ID, names[i], packageNames[i]);
                        }
                    }
                }
            }
        }
        return isMalwareApp;
    }

    public boolean isRiskyConnection(String url) {
        boolean subimEvents = this.preferences.getBoolean(ConfApp.SUBMIT_EVENTS_SP, true);
        if (!this.expressionGenerator.isValidURL(url)) {
            return false;
        }
        String urlPath = (String) this.expressionGenerator.getHostList().get(0);
        String urlPath1 = String.format("www.%s", new Object[] { urlPath });
        this.editor.putString(WebProtectorPreferences.URL_CACHE_MALWARE_SP, urlPath);
        this.editor.commit();
        License license = this.licensingController.licenseByURL(urlPath);
        if (license == null) {
            license = this.licensingController.licenseByURL(urlPath1);
            this.editor.putString(WebProtectorPreferences.URL_CACHE_MALWARE_SP, urlPath1);
            this.editor.commit();
            urlPath = urlPath1;
        }
        if (license == null || license.licenseKey == null
                || DSB.sdk(this.myContext).CONNECTION_PROTECTOR_API.isSecureConnection(url)) {
            return false;
        }
        return true;
    }

    private void sendFakeAppBrowsingEvent(License license) {
        new C02601(license).execute(new Void[0]);
    }

    public boolean browsingMalwareAnalyzer(String url, boolean subimEvents) {
        if (!this.webBlackListHelper.isMalwareIncident(url)) {
            return false;
        }
        if (subimEvents) {
            this.eventSender.sendMalwareBrowsingEvent(this.webBlackListHelper.findLicensesforMalwareBlackSites(url),
                    url);
        }
        return true;
    }

    public URLState getURLState(String url) {
        String currentBrowser = this.preferences.getString(WebProtectorPreferences.BROWSER_CACHE_SP, null);
        URLState state = new URLState();
        if (!(url == null || currentBrowser == null)) {
            state.isMalware = this.webBlackListHelper.isMalwareIncident(url);
            state.isPharming = this.antipharmingHelper.isPharmingIncident(url);
            state.isPhishing = this.webBlackListHelper.isPhishingIncident(url);
            state.pharmingIP = this.antipharmingHelper.getPharmingIP(url);
            state.isMalwareApp = isMalwareAppInfected(url, false);
            if (currentBrowser != null && currentBrowser.length() > 0) {
                state.currentBrowser = BrowserType.valueOf(currentBrowser);
            }
            if (state.isMalware || state.isPharming || state.isPhishing) {
                if (this.expressionGenerator.isValidURL(url)) {
                    if (this.expressionGenerator.getPathExpressionList().get(0) == null
                            || ((String) this.expressionGenerator.getPathExpressionList().get(0)).length() <= 1) {
                        state.url = (String) this.expressionGenerator.getHostList().get(0);
                    } else {
                        state.url = String.format("%s%s",
                                new Object[] { this.expressionGenerator.getHostList().get(0),
                                        this.expressionGenerator.getPathExpressionList().get(0) });
                    }
                }
            } else if (this.expressionGenerator.isValidURL(url)) {
                state.url = (String) this.expressionGenerator.getHostList().get(0);
            }
        }
        return state;
    }

    public boolean isUserPageTrust(String url) {
        if (!this.expressionGenerator.isValidURL(url)) {
            return false;
        }
        String urlPath = (String) this.expressionGenerator.getHostList().get(0);
        String rawTrustPages = this.preferences.getString(WebProtectorPreferences.TRUST_PAGES_SP, null);
        if (rawTrustPages == null) {
            return false;
        }
        ArrayList<String> trustPages = new ArrayList();
        try {
            trustPages = readJSONArrayString(new JSONArray(rawTrustPages));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Iterator it = trustPages.iterator();
        while (it.hasNext()) {
            if (((String) it.next()).contains(urlPath)) {
                return true;
            }
        }
        return false;
    }

    public boolean isUserPageMalicius(String url) {
        if (!this.expressionGenerator.isValidURL(url)) {
            return false;
        }
        String urlPath = (String) this.expressionGenerator.getHostList().get(0);
        String rawBlockedPages = this.preferences.getString(WebProtectorPreferences.BLOCKED_PAGES_SP, null);
        if (rawBlockedPages == null) {
            return false;
        }
        ArrayList<String> blockedPages = new ArrayList();
        try {
            blockedPages = readJSONArrayString(new JSONArray(rawBlockedPages));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Iterator it = blockedPages.iterator();
        while (it.hasNext()) {
            if (((String) it.next()).contains(urlPath)) {
                return true;
            }
        }
        return false;
    }

    @SuppressLint({ "InlinedApi" })
    private void createMalwareNotificaction(int mId, String malwareAppName, String packageName) {
        String currentLanguage = this.myContext.getResources().getConfiguration().locale.getLanguage();
        String title = this.preferences.getString(WebProtectorPreferences.MALWARE_TITLE_NOTIFICATION, null);
        String message = this.preferences.getString(WebProtectorPreferences.MALWARE_MESSAGE_NOTIFICATION, null);
        int icon = this.preferences.getInt(WebProtectorPreferences.MALWARE_ICON_NOTIFICATION, 0);
        if (message == null) {
            if (currentLanguage.equalsIgnoreCase("es")) {
                message = String.format("%s - %s",
                        new Object[] { malwareAppName, WebProtectorPreferences.MALWARE_APP_BROWSING_MESSAGE_ES });
            } else {
                message = String.format("%s - %s",
                        new Object[] { malwareAppName, WebProtectorPreferences.MALWARE_APP_BROWSING_MESSAGE_EN });
            }
        }
        if (title == null) {
            if (currentLanguage.equalsIgnoreCase("es")) {
                title = WebProtectorPreferences.MALWARE_APP_BROWSING_TITLE_ES;
            } else {
                title = WebProtectorPreferences.MALWARE_APP_BROWSING_TITLE_EN;
            }
        }
        if (icon == 0) {
            icon = 17301543;
        }
        Builder mBuilder = new Builder(this.myContext);
        mBuilder.setSmallIcon(icon);
        mBuilder.setContentTitle(title);
        mBuilder.setContentText(message);
        mBuilder.setAutoCancel(true);
        long[] vibrate = new long[4];
        vibrate[1] = 100;
        vibrate[2] = 200;
        vibrate[3] = 300;
        mBuilder.setVibrate(vibrate);
        mBuilder.setSound(System.DEFAULT_NOTIFICATION_URI);
        Uri packageURI = Uri.parse("package:" + packageName);
        Intent resultIntent = new Intent();
        if (VERSION.SDK_INT < 14) {
            resultIntent = new Intent("android.intent.action.DELETE", packageURI);
        } else {
            resultIntent = new Intent("android.intent.action.UNINSTALL_PACKAGE", packageURI);
        }
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this.myContext);
        stackBuilder.addNextIntent(resultIntent);
        mBuilder.setContentIntent(stackBuilder.getPendingIntent(0, 134217728));
        ((NotificationManager) this.myContext.getSystemService("notification")).notify(mId, mBuilder.build());
    }

    public void initDSBBlockPage() {
        WebUtilsHelper.createFileWithContent(this.myContext, WebProtectorPreferences.LOADER_PAGE_EN,
                BlockingPage.loader_en, 1);
        WebUtilsHelper.createFileWithContent(this.myContext, WebProtectorPreferences.BLOCK_PAGE_EN,
                BlockingPage.english, 1);
        WebUtilsHelper.createFileWithContent(this.myContext, WebProtectorPreferences.LOADER_PAGE_ES,
                BlockingPage.loader_es, 1);
        WebUtilsHelper.createFileWithContent(this.myContext, WebProtectorPreferences.BLOCK_PAGE_ES,
                BlockingPage.spanish, 1);
    }

    public void setWebAnalyzerListener(WebAnalyzerListener webAnalyzerListener) {
        this.webAnalyzerListener = webAnalyzerListener;
    }

    public boolean pharmingAnalyzer(String url, boolean subimEvents) {
        return this.antipharmingHelper.pharmingAnalyzer(url, subimEvents);
    }

    public void loadCurrentBlockingPage() {
        switch ($SWITCH_TABLE$net$easysol$dsb$web_protector$browser$BrowserType()[BrowserType
                .valueOf(this.preferences.getString(WebProtectorPreferences.BROWSER_CACHE_SP, null)).ordinal()]) {
        case ReasonFlags.privilegeWithdrawn /*1*/:
            this.browserHelper.loadChromeBlockingPage();
        case ReasonFlags.certificateHold /*2*/:
            this.browserHelper.loadNativeBlockingPage();
        case F2m.PPB /*3*/:
            this.browserHelper.loadNewNativeBlockingPage();
        default:
        }
    }

    public void loadCurrentURL() {
        String browser = this.preferences.getString(WebProtectorPreferences.BROWSER_CACHE_SP, null);
        String url = this.preferences.getString(WebProtectorPreferences.URL_SP, null);
        if (browser != null && url != null) {
            switch ($SWITCH_TABLE$net$easysol$dsb$web_protector$browser$BrowserType()[BrowserType.valueOf(browser)
                    .ordinal()]) {
            case ReasonFlags.privilegeWithdrawn /*1*/:
                this.browserHelper.loadChromeURL(url);
            case ReasonFlags.certificateHold /*2*/:
                this.browserHelper.loadNativeURL(url);
            case F2m.PPB /*3*/:
                this.browserHelper.loadNewNativeURL(url);
            default:
            }
        }
    }

    public String getSerializeURLState() {
        String url = this.preferences.getString(WebProtectorPreferences.URL_SP, null);
        String rawState = BuildConfig.FLAVOR;
        if (url == null) {
            return rawState;
        }
        URLState state = getURLState(url);
        JSONObject jstate = new JSONObject();
        try {
            jstate.put("isMalware", state.isMalware);
            jstate.put("isMalwareApp", state.isMalwareApp);
            jstate.put("isPharming", state.isPharming);
            jstate.put("isPhishing", state.isPhishing);
            jstate.put("currentBrowser", state.currentBrowser.name());
            jstate.put("pharmingIP", state.pharmingIP);
            jstate.put(ProtectedSite.URL, state.url);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jstate.toString();
    }

    public void addTrustPages(String page) {
        removeBlockedPage(page);
        this.trustPages.add(page);
        this.editor.putString(WebProtectorPreferences.TRUST_PAGES_SP, new JSONArray(this.trustPages).toString());
        this.editor.commit();
    }

    public void removeTrustPage(String page) {
        this.trustPages.remove(page);
        this.editor.putString(WebProtectorPreferences.TRUST_PAGES_SP, new JSONArray(this.trustPages).toString());
        this.editor.commit();
    }

    public void addTrustEntry(String page) {
        if (this.trustEntries.contains(page)) {
            this.trustEntries.remove(page);
        } else {
            this.trustEntries.add(page);
        }
        this.editor.putString(WebProtectorPreferences.TRUST_ENTRY_SP, new JSONArray(this.trustEntries).toString());
        this.editor.commit();
    }

    public void removeTrustEntry(String page) {
        this.trustEntries.remove(page);
        this.editor.putString(WebProtectorPreferences.TRUST_ENTRY_SP, new JSONArray(this.trustEntries).toString());
        this.editor.commit();
    }

    public void addBlockedPages(String page) {
        removeTrustPage(page);
        this.blockedPages.add(page);
        this.editor.putString(WebProtectorPreferences.BLOCKED_PAGES_SP,
                new JSONArray(this.blockedPages).toString());
        this.editor.commit();
    }

    public void removeBlockedPage(String page) {
        this.blockedPages.remove(page);
        this.editor.putString(WebProtectorPreferences.BLOCKED_PAGES_SP, new JSONArray(this.trustPages).toString());
        this.editor.commit();
    }
}