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

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

Changed code so api key can be in separate file, and passed in on the command-line to the Java code

File size: 3.0 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
25 boolean printall=false;
26 if (args.length<1 || args.length>3){
27 printUsage();
28
29 }
30 else if(args.length==3){
31 if(args[0].equals("-all")){
32 apiKey = args[1];
33 inputFileName = args[2];
34 printall=true;
35 }
36 else{
37 printUsage();
38 }
39
40 }
41 else if(args.length==2){
42 apiKey = args[0];
43 inputFileName = args[1];
44 }
45 else {
46 printUsage();
47 }
48
49 try {
50
51 VirusTotalConfig.getConfigInstance().setVirusTotalAPIKey(apiKey);
52
53 VirustotalPublicV2 virusTotalRef = new VirustotalPublicV2Impl();
54
55 ScanInfo scanInformation = virusTotalRef.scanFile(new File(inputFileName));
56 int responseCode = scanInformation.getResponseCode();
57 if(responseCode!=1){
58 System.err.println("Error:Failed to preform scan.");
59 System.out.println("Verbose Msg :\t" + scanInformation.getVerboseMessage());
60 System.exit(6);
61 }
62 if (printall){
63 System.out.println("___SCAN INFORMATION___");
64 System.out.println("MD5 :\t" + scanInformation.getMd5());
65 System.out.println("Perma Link :\t" + scanInformation.getPermalink());
66 System.out.println("Resource :\t" + scanInformation.getResource());
67 System.out.println("Scan Date :\t" + scanInformation.getScanDate());
68 System.out.println("Scan Id :\t" + scanInformation.getScanId());
69 System.out.println("SHA1 :\t" + scanInformation.getSha1());
70 System.out.println("SHA256 :\t" + scanInformation.getSha256());
71 System.out.println("Verbose Msg :\t" + scanInformation.getVerboseMessage());
72 System.out.println("Response Code :\t" + scanInformation.getResponseCode());
73 System.out.println("done.");
74
75 }
76
77 else {
78 System.out.println(scanInformation.getResource());
79 }
80
81 } catch (APIKeyNotFoundException ex) {
82 System.err.println("API Key not found! " + ex.getMessage());
83 System.exit(2);
84 } catch (UnsupportedEncodingException ex) {
85 System.err.println("Unsupported Encoding Format!" + ex.getMessage());
86 System.exit(3);
87 } catch (UnauthorizedAccessException ex) {
88 System.err.println("Invalid API Key " + ex.getMessage());
89 System.exit(4);
90 } catch (Exception ex) {
91 System.err.println("Something Bad Happened! " + ex.getMessage());
92 System.exit(5);
93 }
94 }
95}
Note: See TracBrowser for help on using the repository browser.