-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
57 lines (50 loc) · 1.66 KB
/
run.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
import os
import re
import sys
import time
import subprocess
def popen(cmd):
return subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout
def getline(cmd, key, regex, verbose=True):
for line in popen(cmd):
if key in line:
return re.search(regex, line).group(1)
elif verbose:
print line.rstrip()
raise Exception('"%s" not found in output of %s' % (key, cmd))
def system(cmd):
subprocess.check_call(cmd, shell=True)
def clonedisk(task):
uuid = getline('VBoxManage list vms -l', 'SATA (0, 0): ',
'\{([0-9a-f-]{36})\}', verbose=False)
print 'rootdisk', uuid
vdi = 'disk-%s.vdi' % task
clone = getline('VBoxManage clonemedium disk %s %s' % (uuid, vdi),
'Clone medium created ','UUID: ([0-9a-f-]{36})')
print 'clone', clone
system('VBoxManage closemedium %s' % clone)
# run image compression in background, with idle IO priority to minimize
# the impact on the next analysis task.
system('ionice -c3 sh compress-image.sh %s &' % task)
def submit(path, timeout, route):
return getline('cuckoo submit --package exe --timeout %s -o route=%s "%s"'
% (timeout, route, path), 'added as task', 'task with ID #([0-9]+)')
def wait(task):
while True:
status = getline(
'psql cuckoo cuckoo -c "select status from tasks where id = %s" -qt'
% task, '', '(\S+)')
sys.stdout.write('\r' + status + ' ')
sys.stdout.flush()
if 'completed' in status or 'reported' in status:
sys.stdout.write('\n')
return
time.sleep(15)
if len(sys.argv) != 4:
print 'usage: python run.py /absolute/path/to/sample timeout routing'
sys.exit(1)
(path, timeout, route) = sys.argv[1:]
task = submit(path, timeout, route)
print 'task', task
wait(task)
clonedisk(task)