-
Notifications
You must be signed in to change notification settings - Fork 17
/
vtdl.py
executable file
·81 lines (63 loc) · 1.83 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/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
import time
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():
t = time.time()
while not queue.empty():
h = queue.get()
if not h:
break
if h == "wait":
time.sleep(max(0, 60 - time.time() + t))
t = time.time()
continue
if os.path.exists(h):
print "skipping..", h
continue
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, workercount=32):
for idx, h in enumerate(hashes):
if idx and idx % 500 == 0:
for _ in xrange(workercount):
queue.put("wait")
queue.put(h)
workers = [
gevent.spawn(_download_helper)
for _ in xrange(workercount)
]
gevent.joinall(workers)
if __name__ == "__main__":
vtdl()