Many tweaks

This commit is contained in:
Tad 2017-12-16 15:02:10 -05:00
parent f4f8f7d12e
commit cf1e90b5ec
13 changed files with 74 additions and 70 deletions

View file

@ -6,8 +6,9 @@ android {
applicationId "us.spotco.malwarescanner" applicationId "us.spotco.malwarescanner"
minSdkVersion 21 minSdkVersion 21
targetSdkVersion 26 targetSdkVersion 26
versionCode 11 versionCode 12
versionName "1.5" } versionName "1.6"
}
buildTypes { buildTypes {
release { release {
minifyEnabled false minifyEnabled false

View file

@ -30,8 +30,7 @@
android:name=".MalwareScannerService" android:name=".MalwareScannerService"
android:label="Theia Realtime Malware Scanner" android:label="Theia Realtime Malware Scanner"
android:enabled="true" android:enabled="true"
android:exported="false"> android:exported="false"/>
</service>
<receiver <receiver
android:name=".BootReceiver" android:name=".BootReceiver"
@ -39,9 +38,8 @@
android:enabled="true" android:enabled="true"
android:exported="true"> android:exported="true">
<intent-filter> <intent-filter>
<category android:name="android.intent.category.DEFAULT"/> <action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.BOOT_COMPLETED"/> <action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
</intent-filter> </intent-filter>
</receiver> </receiver>

View file

@ -8,7 +8,7 @@ import android.content.SharedPreferences;
public class BootReceiver extends BroadcastReceiver { public class BootReceiver extends BroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public final void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
SharedPreferences prefs = context.getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE); SharedPreferences prefs = context.getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE);
boolean autostart = prefs.getBoolean("autostart", false); boolean autostart = prefs.getBoolean("autostart", false);

View file

@ -13,16 +13,16 @@ import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
public class Database { class Database {
private static TextView log = null; private static TextView log = null;
private static File databasePath = null; private static File databasePath = null;
public static ArrayList<SignatureDatabase> signatureDatabases = new ArrayList<>(); public final static ArrayList<SignatureDatabase> signatureDatabases = new ArrayList<>();
public static HashMap<String, String> signaturesMD5 = new HashMap<>(); public final static HashMap<String, String> signaturesMD5 = new HashMap<>();
public static HashMap<String, String> signaturesSHA1 = new HashMap<>(); public final static HashMap<String, String> signaturesSHA1 = new HashMap<>();
public static HashMap<String, String> signaturesSHA256 = new HashMap<>(); public final static HashMap<String, String> signaturesSHA256 = new HashMap<>();
public Database(Context context, TextView log) { public Database(Context context, TextView log) {
Database.log = log; Database.log = log;
@ -158,7 +158,7 @@ public class Database {
} }
@Override @Override
protected void onProgressUpdate(String... progress) { protected final void onProgressUpdate(String... progress) {
log.append(progress[0] + "\n"); log.append(progress[0] + "\n");
} }
} }

View file

@ -29,6 +29,8 @@ import java.util.Set;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
private SharedPreferences prefs = null; private SharedPreferences prefs = null;
private MalwareScanner malwareScanner = null;
private boolean scanSystem = false; private boolean scanSystem = false;
private boolean scanApps = true; private boolean scanApps = true;
private boolean scanInternal = true; private boolean scanInternal = true;
@ -37,7 +39,7 @@ public class MainActivity extends AppCompatActivity {
private static final int REQUEST_PERMISSION_EXTERNAL_STORAGE = 0; private static final int REQUEST_PERMISSION_EXTERNAL_STORAGE = 0;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected final void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar); Toolbar toolbar = findViewById(R.id.toolbar);
@ -53,7 +55,7 @@ public class MainActivity extends AppCompatActivity {
logView.append("\nDisclaimer: This is an extremely basic signature scanner\n\n"); logView.append("\nDisclaimer: This is an extremely basic signature scanner\n\n");
final Database database = new Database(this, logView); final Database database = new Database(this, logView);
final MalwareScanner malwareScanner = new MalwareScanner(this, this, true); malwareScanner = new MalwareScanner(this, this, true);
prefs = getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE); prefs = getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE);
@ -62,22 +64,7 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
if (!malwareScanner.getStatus().equals(AsyncTask.Status.RUNNING)) { if (!malwareScanner.getStatus().equals(AsyncTask.Status.RUNNING)) {
Set<File> filesToScan = new HashSet<>(); startScanner();
if (scanSystem) {
filesToScan.addAll(Utils.getFilesRecursive(Environment.getRootDirectory()));
}
if (scanApps) {
for (ApplicationInfo packageInfo : getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA)) {
filesToScan.add(new File(packageInfo.sourceDir));
}
}
if (scanInternal) {
filesToScan.addAll(Utils.getFilesRecursive(Environment.getExternalStorageDirectory()));
}
if (scanExternal) {
filesToScan.addAll(Utils.getFilesRecursive(new File("/storage")));
}
malwareScanner.execute(filesToScan);
} else { } else {
malwareScanner.cancel(true); malwareScanner.cancel(true);
} }
@ -87,8 +74,28 @@ public class MainActivity extends AppCompatActivity {
requestPermissions(); requestPermissions();
} }
private void startScanner() {
malwareScanner = new MalwareScanner(this, this, true);
Set<File> filesToScan = new HashSet<>();
if (scanSystem) {
filesToScan.addAll(Utils.getFilesRecursive(Environment.getRootDirectory()));
}
if (scanApps) {
for (ApplicationInfo packageInfo : getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA)) {
filesToScan.add(new File(packageInfo.sourceDir));
}
}
if (scanInternal) {
filesToScan.addAll(Utils.getFilesRecursive(Environment.getExternalStorageDirectory()));
}
if (scanExternal) {
filesToScan.addAll(Utils.getFilesRecursive(new File("/storage")));
}
malwareScanner.execute(filesToScan);
}
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public final boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu); getMenuInflater().inflate(R.menu.menu_main, menu);
menu.findItem(R.id.toggleRealtime).setChecked(isServiceRunning(MalwareScannerService.class)); menu.findItem(R.id.toggleRealtime).setChecked(isServiceRunning(MalwareScannerService.class));
return true; return true;
@ -101,7 +108,7 @@ public class MainActivity extends AppCompatActivity {
} }
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public final boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.mnuUpdateDatabase: case R.id.mnuUpdateDatabase:
Database.updateDatabase(Database.signatureDatabases); Database.updateDatabase(Database.signatureDatabases);

View file

@ -8,6 +8,7 @@ import android.os.AsyncTask;
import android.os.Environment; import android.os.Environment;
import android.os.SystemClock; import android.os.SystemClock;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.TextView; import android.widget.TextView;
import java.io.File; import java.io.File;
@ -20,16 +21,16 @@ import java.util.Map;
import java.util.Random; import java.util.Random;
import java.util.Set; import java.util.Set;
public class MalwareScanner extends AsyncTask<Set<File>, Object, String> { class MalwareScanner extends AsyncTask<Set<File>, Object, String> {
private Context context = null; private Context context = null;
private TextView logOutput = null; private TextView logOutput = null;
private boolean userFacing = false; private boolean userFacing = false;
private NotificationManager notificationManager = null; private NotificationManager notificationManager = null;
private long scanTime = 0; private long scanTime = 0;
private HashMap<String, File> fileHashesMD5 = new HashMap<>(); private final HashMap<String, File> fileHashesMD5 = new HashMap<>();
private HashMap<String, File> fileHashesSHA1 = new HashMap<>(); private final HashMap<String, File> fileHashesSHA1 = new HashMap<>();
private HashMap<String, File> fileHashesSHA256 = new HashMap<>(); private final HashMap<String, File> fileHashesSHA256 = new HashMap<>();
public MalwareScanner(Activity activity, Context context, boolean userFacing) { public MalwareScanner(Activity activity, Context context, boolean userFacing) {
this.context = context; this.context = context;
@ -58,13 +59,13 @@ public class MalwareScanner extends AsyncTask<Set<File>, Object, String> {
} }
@Override @Override
protected void onPreExecute() { protected final void onPreExecute() {
scanTime = SystemClock.elapsedRealtime(); scanTime = SystemClock.elapsedRealtime();
logResult("Starting scan...", true); logResult("Starting scan...", true);
} }
@Override @Override
protected String doInBackground(Set<File>[] filesToScan) { protected final String doInBackground(Set<File>[] filesToScan) {
//Pre //Pre
fileHashesMD5.clear(); fileHashesMD5.clear();
fileHashesSHA1.clear(); fileHashesSHA1.clear();
@ -92,13 +93,13 @@ public class MalwareScanner extends AsyncTask<Set<File>, Object, String> {
fileHashesSHA1.clear(); fileHashesSHA1.clear();
fileHashesSHA256.clear(); fileHashesSHA256.clear();
System.gc(); System.gc();
//Log.d("Thiea", "Scan completed in " + (SystemClock.elapsedRealtime() - scanTime) + " ms!"); Log.d("Thiea", "Scan completed in " + (SystemClock.elapsedRealtime() - scanTime) + " ms!");
publishProgress("Scan completed in " + ((SystemClock.elapsedRealtime() - scanTime) / 1000) + " seconds!\n\n\n\n", true); publishProgress("Scan completed in " + ((SystemClock.elapsedRealtime() - scanTime) / 1000) + " seconds!\n\n\n\n", true);
return null; return null;
} }
@Override @Override
protected void onProgressUpdate(Object... objects) { protected final void onProgressUpdate(Object... objects) {
logResult((String) objects[0], (boolean) objects[1]); logResult((String) objects[0], (boolean) objects[1]);
} }

View file

@ -20,12 +20,12 @@ public class MalwareScannerService extends Service {
private ArrayList<MalwareMonitor> malwareMonitors = null; private ArrayList<MalwareMonitor> malwareMonitors = null;
@Override @Override
public IBinder onBind(Intent intent) { public final IBinder onBind(Intent intent) {
return null; return null;
} }
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public final int onStartCommand(Intent intent, int flags, int startId) {
malwareMonitors = new ArrayList<>(); malwareMonitors = new ArrayList<>();
malwareMonitors.add(new MalwareMonitor(Environment.getExternalStorageDirectory().toString())); malwareMonitors.add(new MalwareMonitor(Environment.getExternalStorageDirectory().toString()));
malwareMonitors.add(new MalwareMonitor(Environment.getExternalStorageDirectory() + "/Documents")); malwareMonitors.add(new MalwareMonitor(Environment.getExternalStorageDirectory() + "/Documents"));
@ -40,7 +40,7 @@ public class MalwareScannerService extends Service {
} }
@Override @Override
public void onDestroy() { public final void onDestroy() {
for (MalwareMonitor malwareMonitor : malwareMonitors) { for (MalwareMonitor malwareMonitor : malwareMonitors) {
malwareMonitor.stopWatching(); malwareMonitor.stopWatching();
} }
@ -48,8 +48,6 @@ public class MalwareScannerService extends Service {
} }
private void setForeground() { private void setForeground() {
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = Notification notification =
new NotificationCompat.Builder(this) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification) .setSmallIcon(R.drawable.ic_notification)
@ -62,7 +60,7 @@ public class MalwareScannerService extends Service {
startForeground(-1, notification); startForeground(-1, notification);
} }
private class MalwareMonitor extends FileObserver { private final class MalwareMonitor extends FileObserver {
private String rootPath = null; private String rootPath = null;
@ -75,7 +73,7 @@ public class MalwareScannerService extends Service {
} }
@Override @Override
public void onEvent(int eventID, String path) { public final void onEvent(int eventID, String path) {
switch (eventID) { switch (eventID) {
case FileObserver.MOVED_TO: case FileObserver.MOVED_TO:
case FileObserver.CLOSE_WRITE: case FileObserver.CLOSE_WRITE:

View file

@ -1,6 +1,6 @@
package us.spotco.malwarescanner; package us.spotco.malwarescanner;
public class SignatureDatabase { class SignatureDatabase {
private String url = null; private String url = null;
private String name = null; private String name = null;
@ -10,11 +10,11 @@ public class SignatureDatabase {
this.name = url.replaceAll("https://spotco.us/Theia/", ""); this.name = url.replaceAll("https://spotco.us/Theia/", "");
} }
public String getUrl() { public final String getUrl() {
return url; return url;
} }
public String getName() { public final String getName() {
return name; return name;
} }

View file

@ -4,9 +4,9 @@ import java.io.File;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
public class Utils { class Utils {
private static final int MAX_FILE_SIZE = (1000 * 1000) * 50; //50MB private final static int MAX_FILE_SIZE = (1000 * 1000) * 50; //50MB
public static Set<File> getFilesRecursive(File root) { public static Set<File> getFilesRecursive(File root) {
Set<File> filesAll = new HashSet<>(); Set<File> filesAll = new HashSet<>();

View file

@ -1,15 +1,15 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txtLogOutput"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent">
android:text=""
android:scrollbars="vertical" <TextView
android:maxLines="5000" android:id="@+id/txtLogOutput"
android:gravity="bottom" /> android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:scrollbars="vertical"
android:maxLines="500"
android:gravity="bottom" />
</LinearLayout> </LinearLayout>

View file

@ -1,5 +1,4 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
tools:context="us.spotco.malwarescanner.MainActivity"> tools:context="us.spotco.malwarescanner.MainActivity">

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android"> <adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_launcher_background"/> <background android:drawable="@color/ic_launcher_background" />
<foreground android:drawable="@mipmap/ic_launcher_foreground"/> <foreground android:drawable="@mipmap/ic_launcher_foreground" />
</adaptive-icon> </adaptive-icon>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android"> <adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_launcher_background"/> <background android:drawable="@color/ic_launcher_background" />
<foreground android:drawable="@mipmap/ic_launcher_foreground"/> <foreground android:drawable="@mipmap/ic_launcher_foreground" />
</adaptive-icon> </adaptive-icon>