diff --git a/README.md b/README.md index 64c76a6..72c8bd5 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ Instantiate the handler's class. Use the method ``get()``. Its first parameter can be : +- A list of hashes (MD5, SHA1, SHA256) - returns a list of reports - A hash (MD5, SHA1, SHA256) - A scan-id (VirusTotal's scan UID) - A file object (file, socket, StringIO) @@ -51,6 +52,9 @@ For example, # EICAR's MD5 (see Links section) report = v.get("44D88612FEA8A8F36DE82E1278ABB02F") + # Multihash - Makes a batch request so it saves on your API request limit, returns a list of reports + report_list = v.get(["100b471f6735e4a7d736c7e371289af8","0a624a3caf4fe3ccd7ef83412bc9d155","b644800a0cc59363ba7b51699f9ac20e"]) + ### Scan a file Use the method ``scan()``. Its first parameter can be : diff --git a/virustotal.py b/virustotal.py index 7a99e1d..25ced4f 100644 --- a/virustotal.py +++ b/virustotal.py @@ -116,11 +116,15 @@ def _limit_call_handler(self): @classmethod def _fileobj_to_fcontent(cls, anything, filename = None): # anything can be: + # - A list of hashes # - A MD5, SHA1, SHA256 # - A scan id # - A filepath or URL # - A file object + if isinstance(anything, list): + return ["multihash", ', '.join(p for p in anything), filename] + if isinstance(anything, basestring): # Is MD5, SHA1, SHA256? if all(i in "1234567890abcdef" for i in anything.lower()) and len(anything) in [32, 40, 64]: @@ -171,7 +175,22 @@ def get(self, anything, filename = None): data, )).read() - report = Report(req, self) + if o[0] == "multihash": + report = [] + if isinstance(req, basestring): + try: + r = json.loads(req) + if isinstance(r, dict): # multihash basically just had one hash + rep_item = Report(r, self) + report.append(rep_item) + else: + for ret_data in r: + rep_item = Report(ret_data, self) + report.append(rep_item) + except ValueError: + raise VirusTotal.ApiError() + else: + report = Report(req, self) return report @@ -220,10 +239,8 @@ def __new__(cls, r, parent): if isinstance(r, basestring): try: r = json.loads(r) - except ValueError: raise VirusTotal.ApiError() - assert isinstance(r, dict) if r["response_code"] == 0: @@ -387,3 +404,4 @@ def analyze(resource): if __name__ == "__main__": main() +