|
|
|
@ -23,6 +23,7 @@ import android.content.Context;
|
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.util.Pair;
|
|
|
|
|
|
|
|
|
|
import androidx.preference.PreferenceManager;
|
|
|
|
|
|
|
|
|
@ -58,45 +59,22 @@ public class VirusTotal {
|
|
|
|
|
Bundle result = new Bundle();
|
|
|
|
|
result.putString("uri", uri);
|
|
|
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
|
String apikey = prefs.getString("vt_apikey", null);
|
|
|
|
|
if (TextUtils.isEmpty(apikey))
|
|
|
|
|
Pair<Integer, String> response = call(context, "api/v3/files/" + hash);
|
|
|
|
|
|
|
|
|
|
if (response == null)
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
URL url = new URL(URI_ENDPOINT + "api/v3/files/" + hash);
|
|
|
|
|
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
|
|
|
|
|
connection.setRequestMethod("GET");
|
|
|
|
|
connection.setReadTimeout(VT_TIMEOUT * 1000);
|
|
|
|
|
connection.setConnectTimeout(VT_TIMEOUT * 1000);
|
|
|
|
|
ConnectionHelper.setUserAgent(context, connection);
|
|
|
|
|
connection.setRequestProperty("x-apikey", apikey);
|
|
|
|
|
connection.setRequestProperty("Accept", "application/json");
|
|
|
|
|
connection.connect();
|
|
|
|
|
if (response.first != HttpsURLConnection.HTTP_OK &&
|
|
|
|
|
response.first != HttpsURLConnection.HTTP_NOT_FOUND)
|
|
|
|
|
throw new FileNotFoundException(response.second);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
int status = connection.getResponseCode();
|
|
|
|
|
if (status != HttpsURLConnection.HTTP_OK && status != HttpsURLConnection.HTTP_NOT_FOUND) {
|
|
|
|
|
String error = "Error " + status + ": " + connection.getResponseMessage();
|
|
|
|
|
try {
|
|
|
|
|
InputStream is = connection.getErrorStream();
|
|
|
|
|
if (is != null)
|
|
|
|
|
error += "\n" + Helper.readStream(is);
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
|
Log.w(ex);
|
|
|
|
|
}
|
|
|
|
|
throw new FileNotFoundException(error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (status == HttpsURLConnection.HTTP_NOT_FOUND) {
|
|
|
|
|
if (response.first == HttpsURLConnection.HTTP_NOT_FOUND) {
|
|
|
|
|
result.putInt("count", 0);
|
|
|
|
|
result.putInt("malicious", 0);
|
|
|
|
|
} else {
|
|
|
|
|
String response = Helper.readStream(connection.getInputStream());
|
|
|
|
|
Log.i("VT response=" + response);
|
|
|
|
|
|
|
|
|
|
// https://developers.virustotal.com/reference/files
|
|
|
|
|
// Example: https://gist.github.com/M66B/4ea95fdb93fb10bf4047761fcc9ec21a
|
|
|
|
|
JSONObject jroot = new JSONObject(response);
|
|
|
|
|
JSONObject jroot = new JSONObject(response.second);
|
|
|
|
|
JSONObject jdata = jroot.getJSONObject("data");
|
|
|
|
|
JSONObject jattributes = jdata.getJSONObject("attributes");
|
|
|
|
|
|
|
|
|
@ -124,10 +102,46 @@ public class VirusTotal {
|
|
|
|
|
result.putInt("malicious", malicious);
|
|
|
|
|
result.putString("label", label);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Pair<Integer, String> call(Context context, String api) throws IOException {
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
|
String apikey = prefs.getString("vt_apikey", null);
|
|
|
|
|
if (TextUtils.isEmpty(apikey))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
URL url = new URL(URI_ENDPOINT + api);
|
|
|
|
|
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
|
|
|
|
|
connection.setRequestMethod("GET");
|
|
|
|
|
connection.setReadTimeout(VT_TIMEOUT * 1000);
|
|
|
|
|
connection.setConnectTimeout(VT_TIMEOUT * 1000);
|
|
|
|
|
ConnectionHelper.setUserAgent(context, connection);
|
|
|
|
|
connection.setRequestProperty("x-apikey", apikey);
|
|
|
|
|
connection.setRequestProperty("Accept", "application/json");
|
|
|
|
|
connection.connect();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
int status = connection.getResponseCode();
|
|
|
|
|
if (status != HttpsURLConnection.HTTP_OK) {
|
|
|
|
|
String error = "Error " + status + ": " + connection.getResponseMessage();
|
|
|
|
|
try {
|
|
|
|
|
InputStream is = connection.getErrorStream();
|
|
|
|
|
if (is != null)
|
|
|
|
|
error += "\n" + Helper.readStream(is);
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
|
Log.w(ex);
|
|
|
|
|
}
|
|
|
|
|
return new Pair<>(status, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String response = Helper.readStream(connection.getInputStream());
|
|
|
|
|
Log.i("VT response=" + response);
|
|
|
|
|
return new Pair<>(status, response);
|
|
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
connection.disconnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|