forked from jbremer/vtdl
/
vtdl.py
executable file
·67 lines (51 loc) · 1.44 KB
/
vtdl.py
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
#!/usr/bin/env python
# Simple Python script for searching & downloading samples from VirusTotal.
import gevent.monkey
import gevent.queue
gevent.monkey.patch_all()
import click
import os.path
import requests
VT_SEARCH = "https://www.virustotal.com/vtapi/v2/file/search"
VT_DOWNLOAD = "https://www.virustotal.com/vtapi/v2/file/download"
apikey = open(os.path.expanduser("~/.vtdl"), "rb").read().strip()
queue = gevent.queue.Queue()
@click.group()
def vtdl():
pass
@vtdl.command()
@click.argument("query")
@click.option("-c", "--count", default=100)
def search(query, count):
data = {
"apikey": apikey,
"query": query,
}
r = requests.post(VT_SEARCH, data=data)
hashes = []
while len(hashes) < count:
hashes.extend(r.json()["hashes"])
if "offset" not in r.json():
break
data["offset"] = r.json()["offset"]
r = requests.post(VT_SEARCH, data=data)
download(hashes[:count])
def _download_helper():
while not queue.empty():
h = queue.get()
if not h:
break
r = requests.get(VT_DOWNLOAD, params={"apikey": apikey, "hash": h})
open(h, "wb").write(r.content)
@vtdl.command()
@click.argument("hashes", nargs=-1)
def download(hashes):
for h in hashes:
queue.put(h)
workers = [
gevent.spawn(_download_helper)
for _ in xrange(32)
]
gevent.joinall(workers)
if __name__ == "__main__":
vtdl()