Add support for downloading databases over Tor

This commit is contained in:
Tad 2018-04-16 01:06:57 -04:00
parent 3088cc2855
commit ceae4ae010
8 changed files with 67 additions and 21 deletions

View file

@ -6,8 +6,8 @@ android {
applicationId "us.spotco.malwarescanner"
minSdkVersion 16
targetSdkVersion 26
versionCode 32
versionName "2.6"
versionCode 33
versionName "2.7"
}
buildTypes {
debug {
@ -31,4 +31,5 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:design:26.1.0'
implementation 'info.guardianproject.netcipher:netcipher:2.0.0-alpha1'
}

View file

@ -27,6 +27,8 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
@ -34,10 +36,12 @@ import java.util.HashSet;
class Database {
private static TextView log = null;
private static SharedPreferences prefs = null;
private static File databasePath = null;
public final static HashSet<SignatureDatabase> signatureDatabases = new HashSet<>();
public final static String baseURL = "https://spotco.us/MalwareScannerSignatures/";
public final static String baseURLOnion = "https://hypatiagbf5vp3ba.onion/MalwareScannerSignatures/"; //TODO: Setup the .onion
public final static HashMap<String, String> signaturesMD5 = new HashMap<>();
public final static HashMap<String, String> signaturesSHA1 = new HashMap<>();
@ -62,7 +66,8 @@ class Database {
public static void updateDatabase(Context context, HashSet<SignatureDatabase> signatureDatabases) {
initDatabase(context);
for (SignatureDatabase signatureDatabase : signatureDatabases) {
new Downloader().execute(signatureDatabase.getUrl(), databasePath + "/" + signatureDatabase.getName());
boolean onionRouting = prefs.getBoolean("ONION_ROUTING", false);
new Downloader().execute(onionRouting, signatureDatabase.getUrl(), databasePath + "/" + signatureDatabase.getName());
}
}
@ -72,7 +77,7 @@ class Database {
signatureDatabases.clear();
SharedPreferences prefs = context.getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE);
prefs = context.getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE);
if (prefs.getBoolean("SIGNATURES_EXTENDED", false)) {
signatureDatabases.add(new SignatureDatabase(baseURL + "bofhland_malware_attach.hdb"));
signatureDatabases.add(new SignatureDatabase(baseURL + "crdfam.clamav.hdb"));
@ -150,18 +155,26 @@ class Database {
}
}
public static class Downloader extends AsyncTask<String, String, String> {
public static class Downloader extends AsyncTask<Object, String, String> {
@Override
protected String doInBackground(String... strings) {
String url = strings[0];
File out = new File(strings[1]);
protected String doInBackground(Object... objects) {
boolean onionRouting = (boolean) objects[0];
String url = (String) objects[1];
File out = new File((String) objects[2]);
publishProgress("Downloading " + url.replaceAll(baseURL, ""));
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setConnectTimeout(45000);
connection.setReadTimeout(45000);
connection.addRequestProperty("User-Agent", "Theia: Open Source Android Malware Scanner - Database Updater");
HttpURLConnection connection;
if (onionRouting) {
//url = url.replaceAll(baseURL, baseURLOnion); //TODO: Setup the .onion
Proxy orbot = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 9050));
connection = (HttpURLConnection) new URL(url).openConnection(orbot);
} else {
connection = (HttpURLConnection) new URL(url).openConnection();
}
connection.setConnectTimeout(90000);
connection.setReadTimeout(30000);
connection.addRequestProperty("User-Agent", "Hypatia");
if (out.exists()) {
connection.setIfModifiedSince(out.lastModified());
}

View file

@ -40,16 +40,21 @@ import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
import info.guardianproject.netcipher.proxy.OrbotHelper;
public class MainActivity extends AppCompatActivity {
private SharedPreferences prefs = null;
private MalwareScanner malwareScanner = null;
private TextView logView;
private boolean scanSystem = false;
private boolean scanApps = true;
private boolean scanInternal = true;
@ -66,7 +71,7 @@ public class MainActivity extends AppCompatActivity {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
TextView logView = findViewById(R.id.txtLogOutput);
logView = findViewById(R.id.txtLogOutput);
logView.setMovementMethod(new ScrollingMovementMethod());
logView.append("Copyright 2017 Spot Communications, Inc.\n");
logView.append("License: GPLv3\n");
@ -88,6 +93,8 @@ public class MainActivity extends AppCompatActivity {
}
});
OrbotHelper.get(this).init();
requestPermissions();
Utils.considerStartService(this);
@ -97,6 +104,7 @@ public class MainActivity extends AppCompatActivity {
public final boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
menu.findItem(R.id.toggleRealtime).setChecked(Utils.isServiceRunning(MalwareScannerService.class, this));
menu.findItem(R.id.toggleOnionRouting).setChecked(prefs.getBoolean("ONION_ROUTING", false));
return true;
}
@ -131,7 +139,6 @@ public class MainActivity extends AppCompatActivity {
prefs.edit().putBoolean("SIGNATURES_CLAMAV-MAIN", databaseDefaults[1]).apply();
prefs.edit().putBoolean("SIGNATURES_CLAMAV-DAILY", databaseDefaults[2]).apply();
prefs.edit().putBoolean("SIGNATURES_EXTENDED", databaseDefaults[3]).apply();
updateDatabase();
}
});
@ -142,7 +149,27 @@ public class MainActivity extends AppCompatActivity {
@Override
public final boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.toggleOnionRouting:
if (!item.isChecked()) {
if (OrbotHelper.isOrbotInstalled(this)) {
prefs.edit().putBoolean("ONION_ROUTING", !item.isChecked()).apply();
item.setChecked(true);
} else {
startActivity(OrbotHelper.getOrbotInstallIntent(this));
prefs.edit().putBoolean("ONION_ROUTING", false).apply();
item.setChecked(false);
Toast.makeText(this, R.string.lblOnionRoutingNotInstalled, Toast.LENGTH_SHORT).show();
}
} else {
prefs.edit().putBoolean("ONION_ROUTING", false).apply();
item.setChecked(false);
}
break;
case R.id.mnuUpdateDatabase:
if (prefs.getBoolean("ONION_ROUTING", false)) {
OrbotHelper.requestStartTor(this);
logView.append("Downloading over Tor, this may take a while...\n");
}
updateDatabase();
break;
case R.id.mnuSelectDatabases:

View file

@ -22,7 +22,6 @@ import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Environment;
@ -59,7 +58,7 @@ class MalwareScanner extends AsyncTask<Set<File>, Object, String> {
logOutput = activity.findViewById(R.id.txtLogOutput);
} else {
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel detectionChannel = new NotificationChannel("DETECTION", context.getString(R.string.lblNotificationMalwareDetectionTitle), NotificationManager.IMPORTANCE_HIGH);
detectionChannel.setDescription(context.getString(R.string.lblNotificationMalwareDetectionDescription));
notificationManager.createNotificationChannel(detectionChannel);
@ -82,7 +81,7 @@ class MalwareScanner extends AsyncTask<Set<File>, Object, String> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setVisibility(Notification.VISIBILITY_SECRET);
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mBuilder.setChannelId("DETECTION");
}
notificationManager.notify(new Random().nextInt(), mBuilder.build());

View file

@ -69,7 +69,7 @@ public class MalwareScannerService extends Service {
}
notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel foregroundChannel = new NotificationChannel("FOREGROUND", getString(R.string.lblNotificationRealtimeTitle), NotificationManager.IMPORTANCE_LOW);
foregroundChannel.setDescription(getString(R.string.lblNotificationRealtimeDescription));
foregroundChannel.setShowBadge(false);
@ -118,7 +118,7 @@ public class MalwareScannerService extends Service {
.setPriority(Notification.PRIORITY_MIN)
.setShowWhen(false);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
foregroundNotification.setChannelId("FOREGROUND");
}

View file

@ -92,7 +92,7 @@ class Utils {
if (autostart) {
Intent realtimeScanner = new Intent(context, MalwareScannerService.class);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(realtimeScanner);
} else {
context.startService(realtimeScanner);

View file

@ -2,6 +2,10 @@
xmlns:tools="http://schemas.android.com/tools"
tools:context="us.spotco.malwarescanner.MainActivity">
<item
android:id="@+id/toggleOnionRouting"
android:title="@string/lblOnionRoutingToggle"
android:checkable="true"/>
<item
android:id="@+id/mnuUpdateDatabase"
android:title="@string/lblUpdateDatabase" />
@ -12,7 +16,6 @@
android:id="@+id/toggleRealtime"
android:title="@string/lblRealtimeScannerToggle"
android:checkable="true" />
<item
android:id="@+id/mnuScanSystem"
android:title="@string/lblScanSystem"

View file

@ -1,5 +1,8 @@
<resources>
<string name="app_name">Hypatia</string>
<string name="lblOnionRoutingToggle">Download over Tor</string>
<string name="lblOnionRoutingNotInstalled">Orbot is not installed!</string>
<string name="lblOnionRoutingNotRunning">Orbot is not running!</string>
<string name="lblUpdateDatabase">Update databases</string>
<string name="lblSelectDatabases">Select databases</string>
<string name="lblSelectDatabasesTitle">Select databases to enable</string>