Helpviewer/VirusTotalApi.py

Mon, 11 Apr 2011 20:06:15 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 11 Apr 2011 20:06:15 +0200
changeset 979
0ae0c8852d31
parent 978
11f8adbcac97
child 992
566e87428fc8
permissions
-rw-r--r--

Refined the VirusTotal code.

978
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2011 Detlev Offenbach <detlev@die-offenbachs.de>
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing the <a href="http://www.virustotal.com">VirusTotal</a> API class.
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 import json
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11
979
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
12 from PyQt4.QtCore import QObject, QUrl, QByteArray, pyqtSignal
978
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 from PyQt4.QtNetwork import QNetworkRequest, QNetworkReply, QNetworkAccessManager
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 import Helpviewer.HelpWindow
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 import Preferences
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 class VirusTotalAPI(QObject):
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 """
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 Class implementing the <a href="http://www.virustotal.com">VirusTotal</a> API.
979
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
22
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
23 @signal checkServiceKeyFinished(bool, str) emitted after the service key check
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
24 has been performed. It gives a flag indicating validity (boolean) and
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
25 an error message in case of a network error (string).
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
26 @signal submitUrlError(str) emitted with the error string, if the URL scan
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
27 submission returned an error.
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
28 @signal urlScanReport(str) emitted with the URL of the URL scan report page
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
29 @signal fileScanReport(str) emitted with the URL of the file scan report page
978
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 """
979
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
31 checkServiceKeyFinished = pyqtSignal(bool, str)
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
32 submitUrlError = pyqtSignal(str)
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
33 urlScanReport = pyqtSignal(str)
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
34 fileScanReport = pyqtSignal(str)
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
35
978
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 TestServiceKeyScanID = \
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 "4feed2c2e352f105f6188efd1d5a558f24aee6971bdf96d5fdb19c197d6d3fad"
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 ServiceResult_RequestLimitReached = -2
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 ServiceResult_InvalidServiceKey = -1
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 ServiceResult_ItemNotPresent = 0
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 ServiceResult_ItemPresent = 1
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 GetFileReportPattern = "{0}://www.virustotal.com/api/get_file_report.json"
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 ScanUrlPattern = "{0}://www.virustotal.com/api/scan_url.json"
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 GetUrlReportPattern = "{0}://www.virustotal.com/api/get_url_report.json"
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 ReportUrlScanPagePattern = "http://www.virustotal.com/url-scan/report.html?id={0}"
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 ReportFileScanPagePattern = "http://www.virustotal.com/file-scan/report.html?id={0}"
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 SearchUrl = "http://www.virustotal.com/search.html"
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 def __init__(self, parent=None):
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 """
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 Constructor
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 @param parent reference to the parent object (QObject)
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 """
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 QObject.__init__(self, parent)
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60
979
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
61 self.__replies = []
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
62
978
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 self.__loadSettings()
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 def __loadSettings(self):
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 """
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 Private method to load the settings.
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 """
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 if Preferences.getHelp("VirusTotalSecure"):
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 protocol = "https"
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 else:
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 protocol = "http"
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 self.GetFileReportUrl = self.GetFileReportPattern.format(protocol)
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 self.ScanUrlUrl = self.ScanUrlPattern.format(protocol)
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 self.GetUrlReportUrl = self.GetUrlReportPattern.format(protocol)
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 self.errorMessages = {
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 -2: self.trUtf8("Request limit has been reached."),
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 -1: self.trUtf8("Invalid key given."),
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 0: self.trUtf8("Requested item is not present.")
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 }
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82
979
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
83 def preferencesChanged(self):
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
84 """
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
85 Public slot to handle a change of preferences.
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
86 """
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
87 self.__loadSettings()
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
88
978
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 def checkServiceKeyValidity(self, key, protocol=""):
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 """
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 Public method to check the validity of the given service key.
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 @param key service key (string)
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 @param protocol protocol used to access VirusTotal (string)
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 @return flag indicating validity (boolean) and an error message in
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 case of a network error (string)
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 """
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 if protocol == "":
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 urlStr = self.GetFileReportUrl
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 else:
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 urlStr = self.GetFileReportPattern.format(protocol)
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 request = QNetworkRequest(QUrl(urlStr))
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 request.setHeader(QNetworkRequest.ContentTypeHeader,
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 "application/x-www-form-urlencoded")
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 params = QByteArray("key={0}&resource={1}".format(
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 key, self.TestServiceKeyScanID))
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 nam = Helpviewer.HelpWindow.HelpWindow.networkAccessManager()
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 reply = nam.post(request, params)
979
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
110 reply.finished.connect(self.__checkServiceKeyValidityFinished)
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
111 self.__replies.append(reply)
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
112
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
113 def __checkServiceKeyValidityFinished(self):
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
114 """
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
115 Private slot to determine the result of the service key validity check.
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
116 """
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
117 res = False
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
118 msg = ""
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
119
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
120 reply = self.sender()
978
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 if reply.error() == QNetworkReply.NoError:
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 result = json.loads(str(reply.readAll(), "utf-8"))
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 if result["result"] != self.ServiceResult_InvalidServiceKey:
979
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
124 res = True
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
125 else:
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
126 msg = reply.errorString()
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
127 self.__replies.remove(reply)
978
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128
979
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
129 self.checkServiceKeyFinished.emit(res, msg)
978
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 def submitUrl(self, url):
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 """
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 Public method to submit an URL to be scanned.
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 @param url url to be scanned (QUrl)
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 @return flag indicating success (boolean) and the scan ID (string)
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137 """
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 request = QNetworkRequest(QUrl(self.ScanUrlUrl))
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 request.setHeader(QNetworkRequest.ContentTypeHeader,
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 "application/x-www-form-urlencoded")
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 params = QByteArray(
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 "key={0}&url=".format(Preferences.getHelp("VirusTotalServiceKey")))\
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 .append(QUrl.toPercentEncoding(url.toString()))
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 nam = Helpviewer.HelpWindow.HelpWindow.networkAccessManager()
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 reply = nam.post(request, params)
979
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
147 reply.finished.connect(self.__submitUrlFinished)
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
148 self.__replies.append(reply)
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
149
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
150 def __submitUrlFinished(self):
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
151 """
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
152 Private slot to determine the result of the URL scan submission.
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
153 """
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
154 reply = self.sender()
978
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 if reply.error() == QNetworkReply.NoError:
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 result = json.loads(str(reply.readAll(), "utf-8"))
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 if result["result"] == self.ServiceResult_ItemPresent:
979
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
158 self.urlScanReport.emit(
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
159 self.ReportUrlScanPagePattern.format(result["scan_id"]))
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
160 self.__getFileScanReportUrl(result["scan_id"])
978
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 else:
979
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
162 self.submitUrlError.emit(self.errorMessages[result["result"]])
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
163 else:
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
164 self.submitUrlError.emit(reply.errorString())
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
165 self.__replies.remove(reply)
978
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166
979
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
167 def __getFileScanReportUrl(self, scanId):
978
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 """
979
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
169 Private method to get the report URL for a file scan.
978
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 @param scanId ID of the scan to get the report URL for (string)
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 @return file scan report URL (string)
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 """
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 request = QNetworkRequest(QUrl(self.GetUrlReportUrl))
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 request.setHeader(QNetworkRequest.ContentTypeHeader,
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 "application/x-www-form-urlencoded")
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 params = QByteArray("key={0}&resource={1}".format(
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 Preferences.getHelp("VirusTotalServiceKey"), scanId))
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 nam = Helpviewer.HelpWindow.HelpWindow.networkAccessManager()
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 reply = nam.post(request, params)
979
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
182 reply.finished.connect(self.__getFileScanReportUrlFinished)
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
183 self.__replies.append(reply)
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
184
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
185 def __getFileScanReportUrlFinished(self):
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
186 """
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
187 Private slot to determine the result of the file scan report URL request.
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
188 """
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
189 reply = self.sender()
978
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 if reply.error() == QNetworkReply.NoError:
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191 result = json.loads(str(reply.readAll(), "utf-8"))
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192 if "file-report" in result:
979
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
193 self.fileScanReport.emit(
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
194 self.ReportFileScanPagePattern.format(result["file-report"]))
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
195 self.__replies.remove(reply)
978
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 @classmethod
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 def getSearchRequestData(cls, term):
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 """
979
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
200 Class method to assemble the search request data structure.
978
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201
979
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
202 @param term search term (string)
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
203 @return tuple of network request object, operation and parameters
0ae0c8852d31 Refined the VirusTotal code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 978
diff changeset
204 (QNetworkRequest, QNetworkAccessManager.Operation, QByteArray)
978
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 """
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 request = QNetworkRequest(QUrl(cls.SearchUrl))
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 request.setHeader(QNetworkRequest.ContentTypeHeader,
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 "application/x-www-form-urlencoded")
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 op = QNetworkAccessManager.PostOperation
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 params = QByteArray("chain=").append(QUrl.toPercentEncoding(term))
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211
11f8adbcac97 Added an interface to VirusTotal to the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 return (request, op, params)

eric ide

mercurial