13
13
14
14
"""Functions to retrieve binaries from Unified Binary Store"""
15
15
16
- from cbapi . psc . threathunter . models import Binary , Downloads
16
+ from cbc_sdk . enterprise_edr import Binary , Downloads
17
17
import logging
18
18
import copy
19
19
@@ -25,7 +25,7 @@ class RedownloadHashes:
25
25
Values and function to redownload any hashes that experienced an error during the initial download attempt.
26
26
27
27
Args:
28
- cbth (cbapi.CbThreatHunterAPI ): Carbon Black ThreatHunter object.
28
+ cbc_api (cbc_sdk.CBCloudAPI ): Carbon Black Cloud API object.
29
29
shas (List[str]): hashes to be redownloaded.
30
30
expiration_seconds (int): Desired timeout for AWS links to binaries.
31
31
@@ -40,9 +40,9 @@ class RedownloadHashes:
40
40
41
41
RETRY_LIMIT = 5
42
42
43
- def __init__ (self , cbth , shas , expiration_seconds ):
43
+ def __init__ (self , cbc_api , shas , expiration_seconds ):
44
44
"""Redownload Hashes constructor"""
45
- self .cb = cbth
45
+ self .cbc_api = cbc_api
46
46
self .shas = shas
47
47
self .expiration_seconds = expiration_seconds
48
48
self .found = []
@@ -55,8 +55,8 @@ def redownload(self):
55
55
"sha256" : self .shas ,
56
56
"expiration_seconds" : self .expiration_seconds ,
57
57
}
58
- url = self .urlobject .format (self .cb .credentials .org_key )
59
- download = self .cb .post_object (url , body ).json ()
58
+ url = self .urlobject .format (self .cbc_api .credentials .org_key )
59
+ download = self .cbc_api .post_object (url , body ).json ()
60
60
self .attempt_num += 1
61
61
# save any hashes found on the first retry
62
62
if download ["found" ]:
@@ -67,7 +67,7 @@ def redownload(self):
67
67
68
68
while download ["error" ] and self .attempt_num < self .RETRY_LIMIT :
69
69
body ["sha256" ] = copy .deepcopy (download ["error" ])
70
- download = self .cb .post_object (url , body ).json ()
70
+ download = self .cbc_api .post_object (url , body ).json ()
71
71
72
72
if download ["found" ]:
73
73
self .found .extend (copy .deepcopy (download ["found" ]))
@@ -85,12 +85,12 @@ def redownload(self):
85
85
f"the Unified Binary Store: { self .not_found } " )
86
86
87
87
88
- def _download_hashes (cbth , hashes , expiration_seconds ):
88
+ def _download_hashes (cbc_api , hashes , expiration_seconds ):
89
89
"""
90
90
Download hashes from Unified Binary Store.
91
91
92
92
Args:
93
- cbth (cbapi.CbThreatHunterAPI ): Carbon Black ThreatHunter object.
93
+ cbc_api (cbc_sdk.CBCloudAPI ): Carbon Black Cloud API object.
94
94
hashes (List[str]): hashes to be downloaded from Unified Binary Store.
95
95
expiration_seconds (int): Desired timeout for AWS links to binaries.
96
96
@@ -101,19 +101,19 @@ def _download_hashes(cbth, hashes, expiration_seconds):
101
101
"""
102
102
try :
103
103
log .debug ("Downloading hashes from Unified Binary Store" )
104
- downloads = Downloads (cbth , hashes , expiration_seconds )
104
+ downloads = Downloads (cbc_api , hashes , expiration_seconds )
105
105
return downloads
106
106
except Exception as err :
107
107
log .error (f"Error downloading hashes from Unified Binary Store: { err } " )
108
108
return None
109
109
110
110
111
- def _download_binary_metadata (cbth , found_binary ):
111
+ def _download_binary_metadata (cbc_api , found_binary ):
112
112
"""
113
113
Retrieve metadata for a binary found in the Unified Binary Store.
114
114
115
115
Args:
116
- cbth (cbapi.CbThreatHunterAPI ): Carbon BlackThreatHunter object.
116
+ cbc_api (cbc_sdk.CBCloudAPI ): Carbon Black Cloud API object.
117
117
found_binary (Dict): Dictionary with "sha256" and "url" values.
118
118
119
119
Returns:
@@ -125,7 +125,7 @@ def _download_binary_metadata(cbth, found_binary):
125
125
try :
126
126
log .debug ("Downloading metadata information" )
127
127
binary_metadata = {"url" : found_binary ["url" ]}
128
- th_binary = cbth .select (Binary , found_binary ["sha256" ])
128
+ th_binary = cbc_api .select (Binary , found_binary ["sha256" ])
129
129
if isinstance (th_binary , Binary ):
130
130
binary_metadata .update (th_binary ._info )
131
131
return binary_metadata
@@ -137,13 +137,13 @@ def _download_binary_metadata(cbth, found_binary):
137
137
return {}
138
138
139
139
140
- def _validate_download (cbth , download , expiration_seconds ):
140
+ def _validate_download (cbc_api , download , expiration_seconds ):
141
141
"""
142
142
Verifies the presence of Downloads.FoundItem. Retries downloading if there are errors during download.
143
143
144
144
Args:
145
- cbth (CbThreatHunterAPI ): Carbon BlackThreatHunter object.
146
- download (ThreatHunter .Downloads): May contain found, not_found, and error attributes.
145
+ cbc_api (cbc_sdk.CBCloudAPI ): Carbon Black Cloud API object.
146
+ download (cbc_sdk.enterprise_edr .Downloads): May contain found, not_found, and error attributes.
147
147
expiration_seconds (int): Desired timeout for AWS links to binaries.
148
148
149
149
Returns:
@@ -167,7 +167,7 @@ def _validate_download(cbth, download, expiration_seconds):
167
167
log .warning (f"{ len (download .error )} hashes experienced an error while"
168
168
f" downloading: { download .error } . Retrying download." )
169
169
170
- redownload = RedownloadHashes (cbth , [download .error ], expiration_seconds )
170
+ redownload = RedownloadHashes (cbc_api , [download .error ], expiration_seconds )
171
171
172
172
redownload .redownload ()
173
173
@@ -176,12 +176,12 @@ def _validate_download(cbth, download, expiration_seconds):
176
176
return download_found , redownload
177
177
178
178
179
- def download_hashes (cbth , hashes , expiration_seconds = 3600 ):
179
+ def download_hashes (cbc_api , hashes , expiration_seconds = 3600 ):
180
180
"""
181
181
Initiates download of hashes
182
182
183
183
Args:
184
- cbth (cbapi.CbThreatHunterAPI ): Carbon BlackThreatHunter object.
184
+ cbc_api (cbc_sdk.CBCloudAPI ): Carbon Black Cloud API object.
185
185
hashes (List[str]): hashes to be downloaded from Unified Binary Store.
186
186
expiration_seconds (int, optional): Desired timeout for AWS links to binaries.
187
187
@@ -190,15 +190,15 @@ def download_hashes(cbth, hashes, expiration_seconds=3600):
190
190
Empty list if an error occurred during download.
191
191
192
192
Examples:
193
- >>> download_hashes(cbth , ["0995f71c34f613207bc39ed4fcc1bbbee396a543fa1739656f7ddf70419309fc"])
193
+ >>> download_hashes(cbc_api , ["0995f71c34f613207bc39ed4fcc1bbbee396a543fa1739656f7ddf70419309fc"])
194
194
195
195
"""
196
196
if not hashes :
197
197
log .error ("No hashes supplied to download_hashes." )
198
198
return list ()
199
- download = _download_hashes (cbth , hashes , expiration_seconds )
199
+ download = _download_hashes (cbc_api , hashes , expiration_seconds )
200
200
201
- checked_download , retried_download = _validate_download (cbth , download , expiration_seconds )
201
+ checked_download , retried_download = _validate_download (cbc_api , download , expiration_seconds )
202
202
203
203
if not checked_download :
204
204
log .error ("Unable to retrieve binaries from the Unified Binary Store." )
@@ -213,12 +213,12 @@ def download_hashes(cbth, hashes, expiration_seconds=3600):
213
213
return found_hashes
214
214
215
215
216
- def get_metadata (cbth , binary ):
216
+ def get_metadata (cbc_api , binary ):
217
217
"""
218
218
Initiates download of binary metadata from Unified Binary Store.
219
219
220
220
Args:
221
- cbth (cbapi.CbThreatHunterAPI ): Carbon Black ThreatHunter object.
221
+ cbc_api (cbc_sdk.CBCloudAPI ): Carbon Black Cloud API object.
222
222
binary (Dict): Dictionary with "sha256" and "url" values.
223
223
224
224
Returns:
@@ -230,7 +230,7 @@ def get_metadata(cbth, binary):
230
230
return {}
231
231
else :
232
232
try :
233
- return _download_binary_metadata (cbth , binary )
233
+ return _download_binary_metadata (cbc_api , binary )
234
234
except Exception as err :
235
235
log .error (f"Failed to download metadata: { err } " )
236
236
return {}
0 commit comments