forked from InQuest/sandboxapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcuckoo.py
More file actions
240 lines (178 loc) · 6.76 KB
/
Copy pathcuckoo.py
File metadata and controls
240 lines (178 loc) · 6.76 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
from __future__ import print_function
import sys
import json
import sandboxapi
class CuckooAPI(sandboxapi.SandboxAPI):
"""Cuckoo Sandbox API wrapper"""
def __init__(self, host, port=8090, api_path='/', verify_ssl=False):
"""Initialize the interface to Cuckoo Sandbox API with host and port"""
sandboxapi.SandboxAPI.__init__(self)
self.api_url = 'http://' + host + ':' + str(port) + api_path
self.verify_ssl = verify_ssl
# assume Cuckoo is *not* available.
self.server_available = False
def analyses(self):
"""Retrieve a list of analyzed samples.
@rtype: list
@return: List of objects referencing each analyzed file.
"""
response = self._request("tasks/list")
return json.loads(response.content.decode('utf-8'))['tasks']
def analyze(self, handle, filename):
"""Submit a file for analysis.
@type handle: File handle
@param handle: Handle to file to upload for analysis.
@type filename: str
@param filename: File name.
@rtype: str
@return: Task ID as a string
"""
# multipart post files.
files = {"file": (filename, handle)}
# ensure the handle is at offset 0.
handle.seek(0)
response = self._request("tasks/create/file", method='POST', files=files)
# return task id; try v1.3 and v2.0 API response formats
try:
return str(json.loads(response.content.decode('utf-8'))["task_id"])
except KeyError:
return str(json.loads(response.content.decode('utf-8'))["task_ids"][0])
def check(self, item_id):
"""Check if an analysis is complete
@type item_id: int
@param item_id: task_id to check.
@rtype: bool
@return: Boolean indicating if a report is done or not.
"""
response = self._request("tasks/view/{id}".format(id=item_id))
if response.status_code == 404:
# probably an unknown task id
return False
try:
content = json.loads(response.content.decode('utf-8'))
status = content['task']["status"]
if status == 'completed' or status == "reported":
return True
except ValueError as e:
raise sandboxapi.SandboxError(e)
return False
def delete(self, item_id):
"""Delete the reports associated with the given item_id.
@type item_id: int
@param item_id: Report ID to delete.
@rtype: bool
@return: True on success, False otherwise.
"""
try:
response = self._request("tasks/delete/{id}".format(id=item_id))
if response.status_code == 200:
return True
except sandboxapi.SandboxError:
pass
return False
def is_available(self):
"""Determine if the Cuckoo Sandbox API servers are alive or in maintenance mode.
@rtype: bool
@return: True if service is available, False otherwise.
"""
# if the availability flag is raised, return True immediately.
# NOTE: subsequent API failures will lower this flag. we do this here
# to ensure we don't keep hitting Cuckoo with requests while
# availability is there.
if self.server_available:
return True
# otherwise, we have to check with the cloud.
else:
try:
response = self._request("cuckoo/status")
# we've got cuckoo.
if response.status_code == 200:
self.server_available = True
return True
except sandboxapi.SandboxError:
pass
self.server_available = False
return False
def queue_size(self):
"""Determine Cuckoo sandbox queue length
There isn't a built in way to do this like with Joe
@rtype: int
@return: Number of submissions in sandbox queue.
"""
response = self._request("tasks/list")
tasks = json.loads(response.content.decode('utf-8'))["tasks"]
return len([t for t in tasks if t['status'] == 'pending'])
def report(self, item_id, report_format="json"):
"""Retrieves the specified report for the analyzed item, referenced by item_id.
Available formats include: json, html, all, dropped, package_files.
@type item_id: int
@param item_id: Task ID number
@type report_format: str
@param report_format: Return format
@rtype: dict
@return: Dictionary representing the JSON parsed data or raw, for other
formats / JSON parsing failure.
"""
report_format = report_format.lower()
response = self._request("tasks/report/{id}/{format}".format(id=item_id, format=report_format))
# if response is JSON, return it as an object
if report_format == "json":
try:
return json.loads(response.content.decode('utf-8'))
except ValueError:
pass
# otherwise, return the raw content.
return response.content
def score(self, report):
"""Pass in the report from self.report(), get back an int."""
score = 0
try:
# cuckoo-modified format
score = report['malscore']
except KeyError:
# cuckoo-2.0 format
score = report.get('info', {}).get('score', 0)
return score
if __name__ == "__main__":
def usage():
msg = "%s: <host> <analyses | analyze <fh> | available | delete <id> | queue | report <id>"
print(msg % sys.argv[0])
sys.exit(1)
if len(sys.argv) == 3:
cmd = sys.argv.pop().lower()
host = sys.argv.pop().lower()
arg = None
elif len(sys.argv) == 4:
arg = sys.argv.pop()
cmd = sys.argv.pop().lower()
host = sys.argv.pop().lower()
else:
usage()
# instantiate Cuckoo Sandbox API interface.
cuckoo = CuckooAPI(host)
# process command line arguments.
if "analyses" in cmd:
for a in cuckoo.analyses():
print(a["id"], a["status"], a["tags"], a["target"])
elif "analyze" in cmd:
if arg is None:
usage()
else:
with open(arg, "rb") as handle:
print(cuckoo.analyze(handle, arg))
elif "available" in cmd:
print(cuckoo.is_available())
elif "delete" in cmd:
if arg is None:
usage()
else:
print(cuckoo.delete(arg))
elif "queue" in cmd:
print(cuckoo.queue_size())
elif "report" in cmd:
if arg is None:
usage()
else:
print(cuckoo.report(arg))
else:
usage()