1 | """The module describes the VirusTotalAPIIPAddresses class |
||
2 | |||
3 | Author: Evgeny Drobotun (c) 2019 |
||
4 | License: MIT (https://github.com/drobotun/virustotalapi3/blob/master/LICENSE) |
||
5 | |||
6 | More information: https://virustotalapi3.readthedocs.io/en/latest/ip_class.html |
||
7 | """ |
||
8 | |||
9 | import errno |
||
10 | import requests |
||
11 | |||
12 | from .vtapi3base import VirusTotalAPI |
||
13 | from .vtapi3error import VirusTotalAPIError |
||
14 | |||
15 | View Code Duplication | class VirusTotalAPIIPAddresses(VirusTotalAPI): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
16 | """The retrieving information about any IP addresses from the VirusTotal database methods are |
||
17 | defined in the class. |
||
18 | |||
19 | Methods: |
||
20 | get_report(): Retrieve information about an IP address. |
||
21 | get_comments(): Retrieve comments for an IP address. |
||
22 | put_comments(): Add a comment to an IP address. |
||
23 | get_relationship(): Retrieve objects related to an IP address. |
||
24 | get_votes(): Retrieve votes for an IP address. |
||
25 | put_votes(): Add a vote for an IP address. |
||
26 | """ |
||
27 | |||
28 | def get_report(self, ip_address): |
||
29 | """Retrieve information about an IP address. |
||
30 | |||
31 | Args: |
||
32 | ip_address: IP address (str). |
||
33 | |||
34 | Return: |
||
35 | The response from the server as a byte sequence. |
||
36 | |||
37 | Exception |
||
38 | VirusTotalAPIError(Connection error): In case of server connection errors. |
||
39 | VirusTotalAPIError(Timeout error): If the response timeout from the server is exceeded. |
||
40 | """ |
||
41 | self._last_http_error = None |
||
42 | self._last_result = None |
||
43 | api_url = self.base_url + '/ip_addresses/' + ip_address |
||
44 | try: |
||
45 | response = requests.get(api_url, headers=self.headers, |
||
46 | timeout=self.timeout, proxies=self.proxies) |
||
47 | except requests.exceptions.Timeout: |
||
48 | raise VirusTotalAPIError('Timeout error', errno.ETIMEDOUT) |
||
49 | except requests.exceptions.ConnectionError: |
||
50 | raise VirusTotalAPIError('Connection error', errno.ECONNABORTED) |
||
51 | else: |
||
52 | self._last_http_error = response.status_code |
||
53 | self._last_result = response.content |
||
54 | return response.content |
||
55 | |||
56 | def get_comments(self, ip_address, limit=10, cursor='""'): |
||
57 | """Retrieve comments for an IP address. |
||
58 | |||
59 | Args: |
||
60 | ip_address: IP address (str). |
||
61 | limit: Maximum number of comments to retrieve (int). The default value is 10. |
||
62 | cursor: Continuation cursor (str). The default value is ''. |
||
63 | |||
64 | Return: |
||
65 | The response from the server as a byte sequence. |
||
66 | |||
67 | Exception |
||
68 | VirusTotalAPIError(Connection error): In case of server connection errors. |
||
69 | VirusTotalAPIError(Timeout error): If the response timeout from the server is exceeded. |
||
70 | """ |
||
71 | self._last_http_error = None |
||
72 | self._last_result = None |
||
73 | query_string = {'limit': str(limit), 'cursor': cursor} |
||
74 | api_url = self.base_url + '/ip_addresses/' + ip_address + '/comments' |
||
75 | try: |
||
76 | response = requests.get(api_url, headers=self.headers, params=query_string, |
||
77 | timeout=self.timeout, proxies=self.proxies) |
||
78 | except requests.exceptions.Timeout: |
||
79 | raise VirusTotalAPIError('Timeout error', errno.ETIMEDOUT) |
||
80 | except requests.exceptions.ConnectionError: |
||
81 | raise VirusTotalAPIError('Connection error', errno.ECONNABORTED) |
||
82 | else: |
||
83 | self._last_http_error = response.status_code |
||
84 | self._last_result = response.content |
||
85 | return response.content |
||
86 | |||
87 | def put_comments(self, ip_address, text): |
||
88 | """Add a comment to an IP address. |
||
89 | |||
90 | Args: |
||
91 | ip_address: IP address (str). |
||
92 | text: Text of the comment (str). |
||
93 | |||
94 | Return: |
||
95 | The response from the server as a byte sequence. |
||
96 | |||
97 | Exception |
||
98 | VirusTotalAPIError(Connection error): In case of server connection errors. |
||
99 | VirusTotalAPIError(Timeout error): If the response timeout from the server is exceeded. |
||
100 | """ |
||
101 | self._last_http_error = None |
||
102 | self._last_result = None |
||
103 | comments = {"data": {'type': 'comment', 'attributes': {'text': text}}} |
||
104 | api_url = self.base_url + '/ip_addresses/' + ip_address + '/comments' |
||
105 | try: |
||
106 | response = requests.post(api_url, headers=self.headers, json=comments, |
||
107 | timeout=self.timeout, proxies=self.proxies) |
||
108 | except requests.exceptions.Timeout: |
||
109 | raise VirusTotalAPIError('Timeout error', errno.ETIMEDOUT) |
||
110 | except requests.exceptions.ConnectionError: |
||
111 | raise VirusTotalAPIError('Connection error', errno.ECONNABORTED) |
||
112 | else: |
||
113 | self._last_http_error = response.status_code |
||
114 | self._last_result = response.content |
||
115 | return response.content |
||
116 | |||
117 | def get_relationship(self, ip_address, relationship='/resolutions', limit=10, cursor='""'): |
||
118 | """Retrieve objects related to an IP address. |
||
119 | |||
120 | Args: |
||
121 | ip_address: IP address (str). |
||
122 | relationship: Relationship name (str). The default value is '/resolutions'. |
||
123 | limit: Maximum number of comments to retrieve (int). The default value is 10. |
||
124 | cursor: Continuation cursor (str). The default value is ''. |
||
125 | |||
126 | Return: |
||
127 | The response from the server as a byte sequence. |
||
128 | |||
129 | Exception |
||
130 | VirusTotalAPIError(Connection error): In case of server connection errors. |
||
131 | VirusTotalAPIError(Timeout error): If the response timeout from the server is exceeded. |
||
132 | """ |
||
133 | self._last_http_error = None |
||
134 | self._last_result = None |
||
135 | query_string = {'limit': str(limit), 'cursor': cursor} |
||
136 | api_url = self.base_url + '/ip_addresses/' + ip_address + relationship |
||
137 | try: |
||
138 | response = requests.get(api_url, headers=self.headers, params=query_string, |
||
139 | timeout=self.timeout, proxies=self.proxies) |
||
140 | except requests.exceptions.Timeout: |
||
141 | raise VirusTotalAPIError('Timeout error', errno.ETIMEDOUT) |
||
142 | except requests.exceptions.ConnectionError: |
||
143 | raise VirusTotalAPIError('Connection error', errno.ECONNABORTED) |
||
144 | else: |
||
145 | self._last_http_error = response.status_code |
||
146 | self._last_result = response.content |
||
147 | return response.content |
||
148 | |||
149 | def get_votes(self, ip_address, limit=10, cursor='""'): |
||
150 | """Retrieve votes for an IP address. |
||
151 | |||
152 | Args: |
||
153 | domain: Domain name (str). |
||
154 | limit: Maximum number of comments to retrieve (int). The default value is 10. |
||
155 | cursor: Continuation cursor (str). The default value is ''. |
||
156 | |||
157 | Return: |
||
158 | The response from the server as a byte sequence. |
||
159 | |||
160 | Exception |
||
161 | VirusTotalAPIError(Connection error): In case of server connection errors. |
||
162 | VirusTotalAPIError(Timeout error): If the response timeout from the server is exceeded. |
||
163 | """ |
||
164 | self._last_http_error = None |
||
165 | self._last_result = None |
||
166 | query_string = {'limit': str(limit), 'cursor': cursor} |
||
167 | api_url = self.base_url + '/ip_addresses/' + ip_address + '/votes' |
||
168 | try: |
||
169 | response = requests.get(api_url, headers=self.headers, params=query_string, |
||
170 | timeout=self.timeout, proxies=self.proxies) |
||
171 | except requests.exceptions.Timeout: |
||
172 | raise VirusTotalAPIError('Timeout error', errno.ETIMEDOUT) |
||
173 | except requests.exceptions.ConnectionError: |
||
174 | raise VirusTotalAPIError('Connection error', errno.ECONNABORTED) |
||
175 | else: |
||
176 | self._last_http_error = response.status_code |
||
177 | self._last_result = response.content |
||
178 | return response.content |
||
179 | |||
180 | def put_votes(self, ip_address, malicious=False): |
||
181 | """Add a vote for an IP address. |
||
182 | |||
183 | Args: |
||
184 | domain: IP address (str). |
||
185 | malicious: Determines a malicious (True) or harmless (False) domain (bool). |
||
186 | |||
187 | Return: |
||
188 | The response from the server as a byte sequence. |
||
189 | |||
190 | Exception |
||
191 | VirusTotalAPIError(Connection error): In case of server connection errors. |
||
192 | VirusTotalAPIError(Timeout error): If the response timeout from the server is exceeded. |
||
193 | """ |
||
194 | self._last_http_error = None |
||
195 | self._last_result = None |
||
196 | if malicious: |
||
197 | verdict = 'malicious' |
||
198 | else: |
||
199 | verdict = 'harmless' |
||
200 | votes = {'data': {'type': 'vote', 'attributes': {'verdict': verdict}}} |
||
201 | api_url = self.base_url + '/ip_addresses/' + ip_address + '/votes' |
||
202 | try: |
||
203 | response = requests.post(api_url, headers=self.headers, json=votes, |
||
204 | timeout=self.timeout, proxies=self.proxies) |
||
205 | except requests.exceptions.Timeout: |
||
206 | raise VirusTotalAPIError('Timeout error', errno.ETIMEDOUT) |
||
207 | except requests.exceptions.ConnectionError: |
||
208 | raise VirusTotalAPIError('Connection error', errno.ECONNABORTED) |
||
209 | else: |
||
210 | self._last_http_error = response.status_code |
||
211 | self._last_result = response.content |
||
212 | return response.content |
||
213 |