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;
|
|
|
|
|
2017-12-14 21:21:29 -05:00
|
|
|
import java.io.BufferedReader;
|
2017-12-14 00:45:58 -05:00
|
|
|
import java.io.File;
|
2017-12-14 14:18:49 -05:00
|
|
|
import java.io.FileNotFoundException;
|
2017-12-14 00:45:58 -05:00
|
|
|
import java.io.FileOutputStream;
|
2017-12-14 21:21:29 -05:00
|
|
|
import java.io.FileReader;
|
2017-12-14 00:45:58 -05:00
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
2017-12-14 14:18:49 -05:00
|
|
|
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>();
|
2017-12-14 14:18:49 -05:00
|
|
|
|
|
|
|
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"));
|
2017-12-14 21:10:51 -05:00
|
|
|
//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() {
|
2017-12-14 14:18:49 -05:00
|
|
|
return databasePath.listFiles().length > 0;
|
2017-12-14 00:45:58 -05:00
|
|
|
}
|
|
|
|
|
2017-12-14 14:18:49 -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 14:18:49 -05:00
|
|
|
}
|
2017-12-14 00:45:58 -05:00
|
|
|
}
|
|
|
|
|
2017-12-14 14:18:49 -05:00
|
|
|
public static void loadDatabase(ArrayList<SignatureDatabase> signatureDatabases) {
|
2017-12-14 21:10:51 -05:00
|
|
|
signaturesMD5.clear();
|
|
|
|
signaturesSHA1.clear();
|
|
|
|
signaturesSHA256.clear();
|
2017-12-14 19:39:14 -05:00
|
|
|
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()) {
|
2017-12-14 14:18:49 -05:00
|
|
|
if (database.getName().contains(".hdb")) {//.hdb format: md5, size, name
|
|
|
|
try {
|
2017-12-14 21:21:29 -05:00
|
|
|
BufferedReader reader = new BufferedReader(new FileReader(databaseLocation));
|
|
|
|
String line;
|
|
|
|
while((line = reader.readLine()) != null) {
|
|
|
|
String[] lineS = line.split(":");
|
|
|
|
signaturesMD5.put(lineS[0], lineS[2]);
|
2017-12-14 14:18:49 -05:00
|
|
|
}
|
2017-12-14 21:21:29 -05:00
|
|
|
reader.close();
|
|
|
|
} catch (Exception e) {
|
2017-12-14 14:18:49 -05:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
} else if (database.getName().contains(".hsb")) {//.hsb format: sha256, size, name
|
|
|
|
try {
|
2017-12-14 21:21:29 -05:00
|
|
|
BufferedReader reader = new BufferedReader(new FileReader(databaseLocation));
|
|
|
|
String line;
|
|
|
|
while((line = reader.readLine()) != null) {
|
|
|
|
String[] lineS = line.split(":");
|
|
|
|
if (lineS[0].length() == 32) {
|
|
|
|
signaturesSHA1.put(lineS[0], lineS[2]);
|
2017-12-14 14:56:54 -05:00
|
|
|
} else {
|
2017-12-14 21:21:29 -05:00
|
|
|
signaturesSHA256.put(lineS[0], lineS[2]);
|
2017-12-14 14:56:54 -05:00
|
|
|
}
|
2017-12-14 14:18:49 -05:00
|
|
|
}
|
2017-12-14 21:21:29 -05:00
|
|
|
reader.close();
|
|
|
|
} catch (Exception e) {
|
2017-12-14 14:18:49 -05:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-14 19:39:14 -05:00
|
|
|
System.gc();
|
2017-12-14 00:45:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|