source: gs2-extensions/malware-checker/trunk/java/src/main/java/org/greenstone/virustotal/ScanFile.java

Last change on this file was 33716, checked in by davidb, 4 years ago

Changed to also write out resource id as file, based on inputFileName

File size: 3.5 KB
Line 
1package org.greenstone.virustotal;
2import com.kanishka.virustotal.dto.ScanInfo;
3import com.kanishka.virustotal.exception.APIKeyNotFoundException;
4import com.kanishka.virustotal.exception.UnauthorizedAccessException;
5import com.kanishka.virustotalv2.VirusTotalConfig;
6import com.kanishka.virustotalv2.VirustotalPublicV2;
7import com.kanishka.virustotalv2.VirustotalPublicV2Impl;
8import java.io.*;
9import java.util.*;
10import java.lang.*;
11import java.io.UnsupportedEncodingException;
12
13
14
15public class ScanFile {
16 public static void printUsage(){
17 System.err.println("Usage: ScanFile [-all] api-key file");
18 System.exit(1);
19 }
20
21 public static void main(String[] args) {
22 String apiKey = null;
23 String inputFileName = null;
24 String outputFileName = null;
25
26 boolean printall=false;
27 if (args.length<1 || args.length>3){
28 printUsage();
29
30 }
31 else if(args.length==3){
32 if(args[0].equals("-all")){
33 apiKey = args[1];
34 inputFileName = args[2];
35 printall=true;
36 }
37 else{
38 printUsage();
39 }
40
41 }
42 else if(args.length==2){
43 apiKey = args[0];
44 inputFileName = args[1];
45 }
46 else {
47 printUsage();
48 }
49
50 //outputFileName = ".virustotal-resource-id-" + inputFileName;
51 outputFileName = inputFileName + "-VIRUSTOTAL-RESOURCEID";
52
53 try {
54
55 VirusTotalConfig.getConfigInstance().setVirusTotalAPIKey(apiKey);
56
57 VirustotalPublicV2 virusTotalRef = new VirustotalPublicV2Impl();
58
59 ScanInfo scanInformation = virusTotalRef.scanFile(new File(inputFileName));
60 int responseCode = scanInformation.getResponseCode();
61 if(responseCode!=1){
62 System.err.println("Error:Failed to preform scan.");
63 System.out.println("Verbose Msg :\t" + scanInformation.getVerboseMessage());
64 System.exit(6);
65 }
66 if (printall) {
67 System.out.println("___SCAN INFORMATION___");
68 System.out.println("MD5 :\t" + scanInformation.getMd5());
69 System.out.println("Perma Link :\t" + scanInformation.getPermalink());
70 System.out.println("Resource :\t" + scanInformation.getResource());
71 System.out.println("Scan Date :\t" + scanInformation.getScanDate());
72 System.out.println("Scan Id :\t" + scanInformation.getScanId());
73 System.out.println("SHA1 :\t" + scanInformation.getSha1());
74 System.out.println("SHA256 :\t" + scanInformation.getSha256());
75 System.out.println("Verbose Msg :\t" + scanInformation.getVerboseMessage());
76 System.out.println("Response Code :\t" + scanInformation.getResponseCode());
77 System.out.println("done.");
78
79 }
80 else {
81 String resource_id = scanInformation.getResource();
82
83 System.out.println(resource_id);
84
85 PrintWriter writer = new PrintWriter(outputFileName, "UTF-8");
86 writer.println(resource_id);
87 writer.close();
88 }
89
90 } catch (APIKeyNotFoundException ex) {
91 System.err.println("API Key not found! " + ex.getMessage());
92 System.exit(2);
93 } catch (UnsupportedEncodingException ex) {
94 System.err.println("Unsupported Encoding Format!" + ex.getMessage());
95 System.exit(3);
96 } catch (UnauthorizedAccessException ex) {
97 System.err.println("Invalid API Key " + ex.getMessage());
98 System.exit(4);
99 } catch (IOException ex) {
100 System.err.println("IO error:" + ex.getMessage());
101 System.exit(5);
102 } catch (Exception ex) {
103 System.err.println("Something Bad Happened! " + ex.getMessage());
104 System.exit(6);
105 }
106 }
107}
Note: See TracBrowser for help on using the repository browser.