forked from InQuest/sandboxapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
164 lines (124 loc) · 4.93 KB
/
Copy path__init__.py
File metadata and controls
164 lines (124 loc) · 4.93 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
import time
import random
import requests
__all__ = [
'cuckoo',
'fireeye',
'joe',
'vmray',
'falcon',
]
class SandboxError(Exception):
"""
Custom exception class to be raised by known errors in SandboxAPI and its
subclasses, and caught where this library is used.
"""
pass
class SandboxAPI(object):
"""Sandbox API wrapper base class"""
def __init__(self, *args, **kwargs):
"""Initialize the interface to Sandbox API"""
self.api_url = None
# assume is *not* available.
self.server_available = False
# turn SSL verify on by default
self.verify_ssl = True
def _request(self, uri, method='GET', params=None, files=None, headers=None, auth=None):
"""Robustness wrapper. Tries up to 3 times to dance with the Sandbox API.
@type uri: str
@param uri: URI to append to base_url.
@type params: dict
@param params: Optional parameters for API.
@type files: dict
@param files: Optional dictionary of files for multipart post.
@rtype: requests.response.
@return: Response object.
@raise SandboxError: If all attempts failed.
"""
# make up to three attempts to dance with the API, use a jittered
# exponential back-off delay
for i in range(3):
try:
full_url = '{b}{u}'.format(b=self.api_url, u=uri)
response = None
if method == 'POST':
response = requests.post(full_url, data=params, files=files, headers=headers,
verify=self.verify_ssl, auth=auth)
else:
response = requests.get(full_url, params=params, headers=headers,
verify=self.verify_ssl, auth=auth)
# if the status code is 503, is no longer available.
if response:
if response.status_code >= 500:
# server error
self.server_available = False
raise SandboxError("server returned {c} status code on {u}, assuming unavailable...".format(
c=response.status_code, u=response.url))
else:
return response
# 0.4, 1.6, 6.4, 25.6, ...
except requests.exceptions.RequestException:
time.sleep(random.uniform(0, 4 ** i * 100 / 1000.0))
# if we couldn't reach the API, we assume that the box is down and lower availability flag.
self.server_available = False
# raise an exception.
msg = "exceeded 3 attempts with sandbox API: {u}, p:{p}, f:{f}".format(u=full_url,
p=params, f=files)
try:
msg += "\n" + response.content.decode('utf-8')
except AttributeError:
pass
raise SandboxError(msg)
def analyses(self):
"""Retrieve a list of analyzed samples.
@rtype: list
@return: List of objects referencing each analyzed file.
"""
raise NotImplementedError
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: Item ID as a string
"""
raise NotImplementedError
def check(self, item_id):
"""Check if an analysis is complete
@type item_id: int | str
@param item_id: item_id to check.
@rtype: bool
@return: Boolean indicating if a report is done or not.
"""
raise NotImplementedError
def delete(self, item_id):
"""Delete the reports associated with the given item_id.
@type item_id: int | str
@param item_id: Report ID to delete.
@rtype: bool
@return: True on success, False otherwise.
"""
raise NotImplementedError
def is_available(self):
"""Determine if the Sandbox API servers are alive or in maintenance mode.
@rtype: bool
@return: True if service is available, False otherwise.
"""
raise NotImplementedError
def queue_size(self):
"""Determine sandbox queue length
@rtype: int
@return: Number of submissions in sandbox queue.
"""
raise NotImplementedError
def report(self, item_id, report_format="json"):
"""Retrieves the specified report for the analyzed item, referenced by item_id.
@type item_id: int | str
@param item_id: Item ID
@rtype: dict
@return: Dictionary representing the JSON parsed data or raw, for other
formats / JSON parsing failure.
"""
raise NotImplementedError