Skip to content

Commit

Permalink
Merge pull request #85 from Fuzion24/feature/run_test_results_from_br…
Browse files Browse the repository at this point in the history
…oadcast_receiver

Run test results from broadcast receiver and serialize to user provided file path
  • Loading branch information
Fuzion24 committed Nov 25, 2015
2 parents 8e9576d + 9a5039d commit 9dc3f4c
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 42 deletions.
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
</intent-filter>
</receiver>

<receiver android:name="fuzion24.device.vulnerability.broadcastreceiver.ScanRunnerBroadcastReceiver">
<intent-filter>
<action android:name="com.android.vts.RUN_SCAN"/>
</intent-filter>
</receiver>

</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package fuzion24.device.vulnerability.broadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

import org.json.JSONObject;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import fuzion24.device.vulnerability.test.VulnerabilityTestResult;
import fuzion24.device.vulnerability.util.DeviceInfo;
import fuzion24.device.vulnerability.vulnerabilities.VulnerabilityOrganizer;
import fuzion24.device.vulnerability.vulnerabilities.VulnerabilityResultSerialzier;
import fuzion24.device.vulnerability.vulnerabilities.VulnerabilityTest;

/**
* Created by fuzion24 on 11/25/15.
*/
public class ScanRunnerBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "ScanRunnerReceiver";
@Override
public void onReceive(final Context context, Intent intent) {

Log.d(TAG, "Received broadcast for scanrunner");
//Only allow this code to be ran on debug builds, since it accepts and writes to arbitrary file
//paths, which would allow another app to arbitrarily write anywhere in this app's context.
// http://android-developers.blogspot.com/2010/09/securing-android-lvl-applications.html
boolean isDebuggable = ( 0 != ( context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE ) );
if(!isDebuggable){
Log.d(TAG, "Not running the tests because the app is not debuggable");
return;
}

Bundle intentExtras = intent.getExtras();
if(intentExtras == null){
Log.d(TAG, "There were no extras with the broadcast. Include RESULT_PATH");
return;
}

final String writeResultPath = intentExtras.getString("RESULT_PATH");
if(writeResultPath == null || writeResultPath.equals("")){
Log.d(TAG, "Result write path is null or empty");
}

Log.d(TAG, "Results will be written to: " + writeResultPath);

new AsyncTask<Void,Void,Void>(){
@Override
protected Void doInBackground(Void... params) {
List<VulnerabilityTest> tests = VulnerabilityOrganizer.getTests(context);
List<VulnerabilityTestResult> results = new ArrayList<VulnerabilityTestResult>();
for(VulnerabilityTest vt : tests){
Log.d(TAG, "Running: " + vt.getCVEorID());
boolean vulnerable = false;
Exception x = null;
try {
vulnerable = vt.isVulnerable(context);
}catch(Exception e){
x = e;
}
results.add(new VulnerabilityTestResult(vt, vulnerable, x));
}

try {
JSONObject jobj = VulnerabilityResultSerialzier.serializeResultsToJson(results, DeviceInfo.getDeviceInfo());
FileOutputStream fos = new FileOutputStream(writeResultPath);
fos.write(jobj.toString(2).getBytes());
fos.close();
}catch(Exception e){
e.printStackTrace();
}

return null;
}
}.execute();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import fuzion24.device.vulnerability.test.VulnerabilityTestRunner;
import fuzion24.device.vulnerability.test.adapter.RecyclerAdapter;
import fuzion24.device.vulnerability.util.DeviceInfo;
import fuzion24.device.vulnerability.vulnerabilities.VulnerabilityResultSerialzier;

public class MainActivity extends AppCompatActivity {

Expand Down Expand Up @@ -139,7 +140,7 @@ public void onClick(View v) {

Intent intent = null;
try {
JSONObject json = serializeResults(testResults, devInfo);
JSONObject json = VulnerabilityResultSerialzier.serializeResultsToJson(testResults, devInfo);
if (itemId == R.id.menu_export_results) {
intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
Expand All @@ -165,47 +166,6 @@ public void onClick(View v) {
}
}

private JSONObject serializeResults(List<VulnerabilityTestResult> results, DeviceInfo devInfo) throws JSONException {
// not sure if this is too intense to do on the main thread...
JSONArray testResults = new JSONArray();
JSONObject buildInfo = new JSONObject();
JSONObject combinedResults = new JSONObject();

buildInfo.put("fingerprint", devInfo.getBuildFingerPrint());
buildInfo.put("kernelVersion", devInfo.getKernelVersion());
buildInfo.put("brand", devInfo.getBuildBrand());
buildInfo.put("manufacturer", devInfo.getBuildManufacturer());
buildInfo.put("model", devInfo.getBuildModel());
buildInfo.put("release", devInfo.getBuildRelease());
buildInfo.put("sdk", devInfo.getBuildSDK());
buildInfo.put("builddate", devInfo.getBuildDateUTC());
buildInfo.put("id", devInfo.getBuildID());
buildInfo.put("cpuABI", devInfo.getBuildCpuABI());
buildInfo.put("cpuABI2", devInfo.getBuildCpuABI2());

JSONArray supportedABIs = new JSONArray();
for(String abi : devInfo.getSupportedABIS()){
supportedABIs.put(abi);
}

buildInfo.put("supportedABIs", supportedABIs);
buildInfo.put("versionCode", BuildConfig.VERSION_CODE);
buildInfo.put("versionName", BuildConfig.VERSION_NAME);

for (VulnerabilityTestResult s : results) {
JSONObject res = new JSONObject();
res.put("name", s.getCVEorID());
res.put("isVulnerable", s.isVulnerable());
res.put("exception", s.getException());
testResults.put(res);
}

combinedResults.put("buildInfo", buildInfo);
combinedResults.put("results", testResults);

return combinedResults;
}

private void runTestsSuit() {
new VulnerabilityTestRunner(MainActivity.this, true, new ResultsCallback() {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package fuzion24.device.vulnerability.vulnerabilities;

import com.nowsecure.android.vts.BuildConfig;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import fuzion24.device.vulnerability.test.VulnerabilityTestResult;
import fuzion24.device.vulnerability.util.DeviceInfo;

/**
* Created by fuzion24 on 11/25/15.
*/
public class VulnerabilityResultSerialzier {

public static JSONObject serializeResultsToJson(List<VulnerabilityTestResult> results, DeviceInfo devInfo) throws JSONException {
// not sure if this is too intense to do on the main thread...
JSONArray testResults = new JSONArray();
JSONObject buildInfo = new JSONObject();
JSONObject combinedResults = new JSONObject();

buildInfo.put("fingerprint", devInfo.getBuildFingerPrint());
buildInfo.put("kernelVersion", devInfo.getKernelVersion());
buildInfo.put("brand", devInfo.getBuildBrand());
buildInfo.put("manufacturer", devInfo.getBuildManufacturer());
buildInfo.put("model", devInfo.getBuildModel());
buildInfo.put("release", devInfo.getBuildRelease());
buildInfo.put("sdk", devInfo.getBuildSDK());
buildInfo.put("builddate", devInfo.getBuildDateUTC());
buildInfo.put("id", devInfo.getBuildID());
buildInfo.put("cpuABI", devInfo.getBuildCpuABI());
buildInfo.put("cpuABI2", devInfo.getBuildCpuABI2());

JSONArray supportedABIs = new JSONArray();
for(String abi : devInfo.getSupportedABIS()){
supportedABIs.put(abi);
}

buildInfo.put("supportedABIs", supportedABIs);
buildInfo.put("versionCode", BuildConfig.VERSION_CODE);
buildInfo.put("versionName", BuildConfig.VERSION_NAME);

for (VulnerabilityTestResult s : results) {
JSONObject res = new JSONObject();
res.put("name", s.getCVEorID());
res.put("isVulnerable", s.isVulnerable());
res.put("exception", s.getException());
testResults.put(res);
}

combinedResults.put("buildInfo", buildInfo);
combinedResults.put("results", testResults);

return combinedResults;
}

}

0 comments on commit 9dc3f4c

Please sign in to comment.