Bonjour, j'aimerais un peu d'aide concernant l'utilisation de l'API de VirusTotal car il doit y avoir quelque chose que j'ai mal compris.
Lien vers la documentation : https://developers.virustotal.com/re...vs-private-api
Je tente de faire une application minimaliste à qui j'adjoins des fichiers sur mon ordinateur et celle-ci doit communiquer avec l'API de virusTotal pour simplement me dire s'il a détecter une menace.
Code en cours de construction:
Lien vers la classe qui me permet de générer un POST: https://gist.github.com/mcxiaoke/8929954
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 public class test { protected String apiKey; protected String ressource; protected String urlStr; protected URL url; protected HttpURLConnection conn; protected Scanner scanner; protected JSONParser parse; protected String filePath; private void doResearch() throws IOException, ParseException { int responsecode = setupGET(urlStr); if (responsecode != 200) { System.out.println("ERROR: " + responsecode); } else { System.out.println(responsecode); String inline = ""; scanner = new Scanner(url.openStream()); //Write all the JSON data into a string using a scanner while (scanner.hasNext()) { inline += scanner.nextLine(); } //Close the scanner scanner.close(); //https://github.com/fangyidong/json-simple //Using the JSON simple library parse the string into a json object parse = new JSONParser(); JSONObject data_obj = (JSONObject) parse.parse(inline); System.out.println(inline); //The source doesn't exist in virustotal database //it have to be scaned first if ((long) data_obj.get("response_code") == 0) { if (responsecode != 200) { System.out.println("ERROR: " + responsecode); } else { Multipart m = new Multipart(); urlStr = "https://www.virustotal.com/vtapi/v2/file/scan"; String urlParameters = String.format("apikey=%s&file=%s", apiKey, filePath); String result = m.multipartRequest(urlStr, urlParameters, filePath, "xxx"); parse = new JSONParser(); //https://stackoverflow.com/questions/24636454/unexpected-token-end-of-file-at-position-0-json/25129876 JSONObject json = (JSONObject) parse.parse(result.toString()); String resource = (String) json.get("resource"); String scan_id = (String) json.get("scan_id"); urlStr = String.format("https://www.virustotal.com/vtapi/v2/file/report?apikey=%s&resource=%s&scan_id=%s", apiKey, resource, scan_id); conn.disconnect(); responsecode = setupGET(urlStr); if (responsecode != 200) { System.out.println("ERROR: " + responsecode); } else { inline = ""; scanner = new Scanner(url.openStream()); System.out.println("Mon URL:" + url); //Write all the JSON data into a string using a scanner while (scanner.hasNext()) { inline += scanner.nextLine(); } System.out.println(inline); //Close the scanner scanner.close(); //https://github.com/fangyidong/json-simple //Using the JSON simple library parse the string into a json object parse = new JSONParser(); data_obj = (JSONObject) parse.parse(inline); } } } } } public int setupGET(String myUrl) throws IOException { url = new URL(myUrl); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.connect(); return conn.getResponseCode(); } }
Il y a deux cas de figures, le premier étant que le fichier est déjà connu par les forces de renseignements et là il n'y a pas de problème.
Par contre quand je dois envoyer un fichier anonyme, là il mon programme but. Je préviens que je ne maitrise pas vraiment la question, sinon je ne serais sans doute pas là
Quand je fais appel à la fonctionnalité "report" (https://developers.virustotal.com/reference#file-report) et qu'il ne marche pas, je lance un "scan" (https://developers.virustotal.com/reference#file-scan) qui doit consister à uploader le fichier sur le site.
Ensuite, innocemment je refais un "report" auquel j'adjoins les informations de mon "scan" (resource, scan_id). Mais j'ai souvent l'erreur: "Unexpected token END OF FILE at position 0." alors que la variable 'urlStr' juste avant est textuellement bonne. Cela veut dire que mon JSON issu de mon dernier GET est vide au moment ou j'exécute.
Alors j'ai l'impression que la situation est plus complexe que ça. Il faut sans doute un peu de temps pour que le fichier soit uploader et analyser avant d'avoir un rapport de disponible.
Car quand je clic sur l'URL que je crée, j'ai souvent une page blanche qui me donne éventuellement un résultat après plusieurs rafraichissements.
J'aimerais savoir si le problème vient de là et qu'elle serait la manière pour résoudre ce problème.
edit:
Inline est vide:
Par contre si je commence directement par la requête POST, j'ai éventuellement un résultat:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 urlStr = String.format("https://www.virustotal.com/vtapi/v2/file/report?apikey=%s&resource=%s&scan_id=%s", URLEncoder.encode(apiKey, "UTF-8"), URLEncoder.encode(resource, "UTF-8"), URLEncoder.encode(scan_id, "UTF-8")); conn.disconnect(); responsecode = setupGET(urlStr); if (responsecode != 200) { System.out.println("ERROR: " + responsecode); } else { inline = ""; System.out.println("URL Report après Scan: "+url.toString()); scanner = new Scanner(url.openStream()); //Write all the JSON data into a string using a scanner while (scanner.hasNext()) { inline += scanner.nextLine(); } System.out.println("Inline "+inline);
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 Multipart m = new Multipart(); String url = "https://www.virustotal.com/vtapi/v2/file/scan"; String apiKey = "monApi"; //String filepath = "test.png"; String filepath = "intell.png"; String urlParameters = String.format("apikey=%s&file=%s", apiKey, filepath); /* {"scan_id": "scanId", "sha1": "sha1", "resource": "123", "response_code": 1, "sha256": "monSha256", "permalink": "https://www.virustotal.com/gui/file/monSha256/detection/f-scanId", "md5": "....", "verbose_msg": "Scan request successfully queued, come back later for the report"} */ System.out.println(m.multipartRequest(url, urlParameters, filepath, "xxx")); url = "https://www.virustotal.com/vtapi/v2/file/report?apikey=monApi&resource=123&scan_id=scan_id"; URL myUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection) myUrl.openConnection(); conn.setRequestMethod("GET"); conn.connect(); int responsecode = conn.getResponseCode(); if (responsecode != 200) { System.out.println("ERROR: " + responsecode); } else { System.out.println(responsecode); String inline = ""; Scanner scanner = new Scanner(myUrl.openStream()); //Write all the JSON data into a string using a scanner while (scanner.hasNext()) { inline += scanner.nextLine(); } //Close the scanner scanner.close(); //https://github.com/fangyidong/json-simple //Using the JSON simple library parse the string into a json object JSONParser parse = new JSONParser(); JSONObject data_obj = (JSONObject) parse.parse(inline); System.out.println("Inline:" +inline); /* Inline:{"scans": {"Bkav": {"detected": false, "version": "1.3.0.9899", "result": null, "update": "20210806"}, "Lionic": {"detected": false, "version": "4.2", "result": null, "update": "20210806"}, "MicroWorld-eScan": {"detected": false, "version": "14.0.409.0", "result": null, "update": "20210806"}, "FireEye": {"detected": false, "version": "32.44.1.0", "result": null, "update": "20210806"}, "CAT-QuickHeal": {"detected": false, "vers */
Partager