Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

now takes a list of hashes and returns a list of reports - batch request... #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 :
Expand Down
24 changes: 21 additions & 3 deletions virustotal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -387,3 +404,4 @@ def analyze(resource):

if __name__ == "__main__":
main()