ScanFileFragment.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package de.tudarmstadt.informatik.hostage.ui.fragment;
  2. import android.app.Activity;
  3. import android.app.Fragment;
  4. import android.os.Bundle;
  5. import android.os.StrictMode;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.TextView;
  10. import java.io.IOException;
  11. import java.util.Set;
  12. import de.tudarmstadt.informatik.hostage.R;
  13. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  14. import virustotalapi.ReportScan;
  15. import virustotalapi.VirusTotal;
  16. public class ScanFileFragment extends Fragment {
  17. public static String filePath;
  18. public String result;
  19. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  20. super.onCreateView(inflater, container, savedInstanceState);
  21. final Activity activity = getActivity();
  22. if (activity != null) {
  23. activity.setTitle("Scan File");
  24. }
  25. View rootView = inflater.inflate(R.layout.fragment_scan_file, container, false);
  26. // PackageManager manager = Hostage.getContext().getPackageManager();
  27. //PackageInfo info = null;
  28. /*try {
  29. info = manager.getPackageInfo(Hostage.getContext().getPackageName(), 0);
  30. } catch (PackageManager.NameNotFoundException e) {
  31. e.printStackTrace();
  32. }*/
  33. TextView scanResult = (TextView) rootView.findViewById(R.id.scanResult);
  34. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  35. StrictMode.setThreadPolicy(policy);
  36. try {
  37. scanResult.setText(scanFile());
  38. } catch (IOException e) {
  39. e.printStackTrace();
  40. }
  41. // scanfile(HelperUtils.filePath);
  42. // result=getFileScanReport();
  43. // TextView version = (TextView) rootView.findViewById(R.id.hostageVersion);
  44. /*version.setText("ver. "+versionApp);
  45. hostage.setMovementMethod(LinkMovementMethod.getInstance());
  46. version.setMovementMethod(LinkMovementMethod.getInstance());
  47. */
  48. return rootView;
  49. }
  50. // public String scanner;
  51. // public String path= HelperUtils.getFilePath();
  52. public String scanFile() throws IOException {
  53. StringBuilder sb = new StringBuilder();
  54. VirusTotal VT = new VirusTotal("111c226204f5de7228563bbca91c5860e4965fbe936307dffa8f2f2d575ff292"); // Your Virus Total API Key
  55. Set<ReportScan> Report = VT.ReportScan(HelperUtils.fileMD5); //The SHA256 file
  56. for (ReportScan report : Report) {
  57. sb.append("\nAV: " + report.getVendor() + " Detected: " + report.getDetected() + " Update: " + report.getUpdate() + " Malware Name: " + report.getMalwarename());
  58. }
  59. return sb.toString();
  60. }
  61. }
  62. /*public String scanfile(String path){
  63. String result ="";
  64. try {
  65. VirusTotalConfig.getConfigInstance().setVirusTotalAPIKey("111c226204f5de7228563bbca91c5860e4965fbe936307dffa8f2f2d575ff292");
  66. VirustotalPublicV2 virusTotalRef = new VirustotalPublicV2Impl();
  67. ScanInfo scanInformation = virusTotalRef.scanFile(new File(HelperUtils.filePath));
  68. System.out.println("___SCAN INFORMATION___");
  69. System.out.println("MD5 :\t" + scanInformation.getMd5());
  70. System.out.println("Perma Link :\t" + scanInformation.getPermalink());
  71. System.out.println("Resource :\t" + scanInformation.getResource());
  72. System.out.println("Scan Date :\t" + scanInformation.getScanDate());
  73. System.out.println("Scan Id :\t" + scanInformation.getScanId());
  74. System.out.println("SHA1 :\t" + scanInformation.getSha1());
  75. System.out.println("SHA256 :\t" + scanInformation.getSha256());
  76. System.out.println("Verbose Msg :\t" + scanInformation.getVerboseMessage());
  77. System.out.println("Response Code :\t" + scanInformation.getResponseCode());
  78. result = String.valueOf(scanInformation.getResponseCode());
  79. System.out.println("done.");
  80. } catch (APIKeyNotFoundException ex) {
  81. System.err.println("API Key not found! " + ex.getMessage());
  82. } catch (UnsupportedEncodingException ex) {
  83. System.err.println("Unsupported Encoding Format!" + ex.getMessage());
  84. } catch (UnauthorizedAccessException ex) {
  85. System.err.println("Invalid API Key " + ex.getMessage());
  86. } catch (Exception ex) {
  87. System.err.println("Something Bad Happened! " + ex.getMessage());
  88. }
  89. return result;
  90. }
  91. public String getFileScanReport() {
  92. try {
  93. VirusTotalConfig.getConfigInstance().setVirusTotalAPIKey("111c226204f5de7228563bbca91c5860e4965fbe936307dffa8f2f2d575ff292");
  94. VirustotalPublicV2 virusTotalRef = new VirustotalPublicV2Impl();
  95. String resource=(HelperUtils.fileMD5);
  96. FileScanReport report = virusTotalRef.getScanReport(resource);
  97. System.out.println("MD5 :\t" + report.getMd5());
  98. System.out.println("Perma link :\t" + report.getPermalink());
  99. System.out.println("Resourve :\t" + report.getResource());
  100. System.out.println("Scan Date :\t" + report.getScanDate());
  101. System.out.println("Scan Id :\t" + report.getScanId());
  102. System.out.println("SHA1 :\t" + report.getSha1());
  103. System.out.println("SHA256 :\t" + report.getSha256());
  104. System.out.println("Verbose Msg :\t" + report.getVerboseMessage());
  105. System.out.println("Response Code :\t" + report.getResponseCode());
  106. System.out.println("Positives :\t" + report.getPositives());
  107. System.out.println("Total :\t" + report.getTotal());
  108. HashMap<String, VirusScanInfo> scans = (HashMap<String, VirusScanInfo>) report.getScans();
  109. for (String key : scans.keySet()) {
  110. VirusScanInfo virusInfo = scans.get(key);
  111. System.out.println("Scanner : " + key);
  112. System.out.println("\t\t Result : " + virusInfo.getResult());
  113. System.out.println("\t\t Update : " + virusInfo.getUpdate());
  114. System.out.println("\t\t Version :" + virusInfo.getVersion());
  115. result = report.getPermalink();
  116. scanner = key;
  117. }
  118. } catch (APIKeyNotFoundException ex) {
  119. System.err.println("API Key not found! " + ex.getMessage());
  120. } catch (UnsupportedEncodingException ex) {
  121. System.err.println("Unsupported Encoding Format!" + ex.getMessage());
  122. } catch (UnauthorizedAccessException ex) {
  123. System.err.println("Invalid API Key " + ex.getMessage());
  124. } catch (Exception ex) {
  125. System.err.println("Something Bad Happened! " + ex.getMessage());
  126. }
  127. return result;
  128. }
  129. */