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

148 lines
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.BufferedReader;
2017-12-14 00:45:58 -05:00
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
2017-12-14 00:45:58 -05:00
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
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 TextView log = null;
private static File databasePath = null;
2017-12-14 21:29:43 -05:00
public static ArrayList<SignatureDatabase> signatureDatabases = new ArrayList<>();
2017-12-14 15:05:11 -05:00
2017-12-14 21:29:43 -05:00
public static HashMap<String, String> signaturesMD5 = new HashMap<>();
public static HashMap<String, String> signaturesSHA1 = new HashMap<>();
public static HashMap<String, String> signaturesSHA256 = new HashMap<>();
public Database(Context context, TextView log) {
2017-12-14 21:29:43 -05:00
Database.log = log;
databasePath = new File(context.getFilesDir() + "/signatures/");
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) {
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()) {
if (database.getName().contains(".hdb")) {//.hdb format: md5, size, name
try {
BufferedReader reader = new BufferedReader(new FileReader(databaseLocation));
String line;
2017-12-14 21:29:43 -05:00
while ((line = reader.readLine()) != null) {
String[] lineS = line.split(":");
signaturesMD5.put(lineS[0], lineS[2]);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
} else if (database.getName().contains(".hsb")) {//.hsb format: sha256, size, name
try {
BufferedReader reader = new BufferedReader(new FileReader(databaseLocation));
String line;
2017-12-14 21:29:43 -05:00
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 {
signaturesSHA256.put(lineS[0], lineS[2]);
2017-12-14 14:56:54 -05:00
}
}
reader.close();
} catch (Exception e) {
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
}