Hypatia/app/src/main/java/us/spotco/malwarescanner/Database.java

164 lines
6.6 KiB
Java
Raw Normal View History

2017-12-14 13:31:35 -05:00
package us.spotco.malwarescanner;
2017-12-13 23:42:10 -05:00
2017-12-14 00:45:58 -05:00
import android.content.Context;
import android.os.AsyncTask;
import android.widget.TextView;
import java.io.File;
import java.io.FileNotFoundException;
2017-12-14 00:45:58 -05:00
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
2017-12-14 00:45:58 -05:00
2017-12-13 23:42:10 -05:00
public class Database {
2017-12-14 00:45:58 -05:00
private static Context context = null;
private static TextView log = null;
private static File databasePath = null;
2017-12-14 15:05:11 -05:00
public static ArrayList<SignatureDatabase> signatureDatabases = new ArrayList<SignatureDatabase>();
2017-12-14 14:56:54 -05:00
public static HashMap<String, String> signaturesMD5 = new HashMap<String, String>();
public static HashMap<String, String> signaturesSHA1 = new HashMap<String, String>();
public static HashMap<String, String> signaturesSHA256 = new HashMap<String, String>();
public Database(Context context, TextView log) {
2017-12-14 00:45:58 -05:00
this.context = context;
this.log = log;
2017-12-14 14:56:54 -05:00
this.databasePath = new File(context.getFilesDir() + "/signatures/");
this.databasePath.mkdir();
2017-12-14 15:05:11 -05:00
signatureDatabases.add(new SignatureDatabase("https://spotco.us/clamav-main.hdb", "clamav-main.hdb"));
signatureDatabases.add(new SignatureDatabase("https://spotco.us/clamav-main.hsb", "clamav-main.hsb"));
signatureDatabases.add(new SignatureDatabase("http://clamav.bofhland.org/bofhland_malware_attach.hdb", "bofhland_malware_attach.hdb"));
signatureDatabases.add(new SignatureDatabase("http://cdn.malware.expert/malware.expert.hdb", "malware.expert.hdb"));
signatureDatabases.add(new SignatureDatabase("http://cdn.rfxn.com/downloads/rfxn.hdb", "rfxn.hdb"));
2017-12-14 00:45:58 -05:00
}
public static boolean doesDatabaseExist() {
return databasePath.listFiles().length > 0;
2017-12-14 00:45:58 -05:00
}
public static void updateDatabase(ArrayList<SignatureDatabase> signatureDatabases) {
2017-12-14 15:05:11 -05:00
for (SignatureDatabase signatureDatabase : signatureDatabases) {
2017-12-14 14:56:54 -05:00
new Downloader().execute(signatureDatabase.getUrl(), databasePath + "/" + signatureDatabase.getName());
}
2017-12-14 00:45:58 -05:00
}
public static void loadDatabase(ArrayList<SignatureDatabase> signatureDatabases) {
2017-12-14 19:39:14 -05:00
signaturesMD5 = new HashMap<String, String>();
signaturesSHA1 = new HashMap<String, String>();
signaturesSHA256 = new HashMap<String, String>();
System.gc();
2017-12-14 15:05:11 -05:00
for (SignatureDatabase database : signatureDatabases) {
2017-12-14 14:56:54 -05:00
File databaseLocation = new File(databasePath + "/" + database.getName());
2017-12-14 15:05:11 -05:00
if (databaseLocation.exists()) {
if (database.getName().contains(".hdb")) {//.hdb format: md5, size, name
try {
Scanner scanner = new Scanner(databaseLocation);
2017-12-14 15:05:11 -05:00
while (scanner.hasNextLine()) {
String[] line = scanner.nextLine().split(":");
signaturesMD5.put(line[0], line[2]);
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} else if (database.getName().contains(".hsb")) {//.hsb format: sha256, size, name
try {
Scanner scanner = new Scanner(databaseLocation);
2017-12-14 15:05:11 -05:00
while (scanner.hasNextLine()) {
String[] line = scanner.nextLine().split(":");
2017-12-14 15:05:11 -05:00
if (line[0].length() == 32) {
2017-12-14 14:56:54 -05:00
signaturesSHA1.put(line[0], line[2]);
} else {
signaturesSHA256.put(line[0], line[2]);
}
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
2017-12-14 19:39:14 -05:00
System.gc();
2017-12-14 00:45:58 -05:00
}
public static String checkInDatabase(String hash) {
2017-12-14 15:05:11 -05:00
if (hash.length() == 32) {
if (signaturesMD5.containsKey(hash)) {
return signaturesMD5.get(hash);
}
2017-12-14 14:56:54 -05:00
if (signaturesSHA1.containsKey(hash)) {
return signaturesSHA1.get(hash);
}
2017-12-14 15:05:11 -05:00
} else if (hash.length() == 64) {
if (signaturesSHA256.containsKey(hash)) {
return signaturesSHA256.get(hash);
}
}
2017-12-14 00:45:58 -05:00
return null;
}
public static class Downloader extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... strings) {
String url = strings[0];
File out = new File(strings[1]);
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setConnectTimeout(45000);
connection.setReadTimeout(45000);
2017-12-14 16:39:23 -05:00
connection.addRequestProperty("User-Agent", "Theia: Open Source Android Malware Scanner - Database Updater");
2017-12-14 00:45:58 -05:00
if (out.exists()) {
connection.setIfModifiedSince(out.lastModified());
}
connection.connect();
int res = connection.getResponseCode();
if (res != 304) {
2017-12-14 15:05:11 -05:00
if (res == 200) {
2017-12-14 00:45:58 -05:00
if (out.exists()) {
out.delete();
}
FileOutputStream fileOutputStream = new FileOutputStream(out);
final byte data[] = new byte[1024];
int count;
while ((count = connection.getInputStream().read(data, 0, 1024)) != -1) {
fileOutputStream.write(data, 0, count);
}
fileOutputStream.close();
publishProgress("Successfully downloaded " + url + "\n");
} else {
publishProgress("File not downloaded " + res + "\n");
}
} else {
publishProgress("File not changed " + url + "\n");
}
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
publishProgress("Failed to download file from " + url + "\n");
}
return null;
}
@Override
protected void onProgressUpdate(String... progress) {
log.append(progress[0] + "\n");
}
@Override
protected void onPostExecute(String result) {
}
}
2017-12-13 23:42:10 -05:00
}