prelude-sdk-beta 1406__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of prelude-sdk-beta might be problematic. Click here for more details.

File without changes
File without changes
@@ -0,0 +1,309 @@
1
+ import urllib
2
+
3
+ from prelude_sdk_beta.controllers.http_controller import HttpController
4
+ from prelude_sdk_beta.models.account import verify_credentials
5
+ from prelude_sdk_beta.models.codes import Control, EDRResponse
6
+
7
+
8
+ class BuildController(HttpController):
9
+
10
+ def __init__(self, account):
11
+ super().__init__(account)
12
+
13
+ @verify_credentials
14
+ def clone_test(self, source_test_id):
15
+ """Clone a test"""
16
+ res = self.post(
17
+ f"{self.account.hq}/build/tests",
18
+ json=dict(source_test_id=source_test_id),
19
+ headers=self.account.headers,
20
+ timeout=10,
21
+ )
22
+ return res.json()
23
+
24
+ @verify_credentials
25
+ def create_test(self, name, unit, technique=None, test_id=None):
26
+ """Create or update a test"""
27
+ body = dict(name=name, unit=unit)
28
+ if technique:
29
+ body["technique"] = technique
30
+ if test_id:
31
+ body["id"] = test_id
32
+
33
+ res = self.post(
34
+ f"{self.account.hq}/build/tests",
35
+ json=body,
36
+ headers=self.account.headers,
37
+ timeout=10,
38
+ )
39
+ return res.json()
40
+
41
+ @verify_credentials
42
+ def update_test(
43
+ self,
44
+ test_id,
45
+ name=None,
46
+ unit=None,
47
+ technique=None,
48
+ crowdstrike_expected_outcome: EDRResponse = None,
49
+ ):
50
+ """Update a test"""
51
+ body = dict()
52
+ if crowdstrike_expected_outcome:
53
+ body["expected"] = dict(crowdstrike=crowdstrike_expected_outcome.value)
54
+ if name:
55
+ body["name"] = name
56
+ if unit:
57
+ body["unit"] = unit
58
+ if technique is not None:
59
+ body["technique"] = technique
60
+
61
+ res = self.post(
62
+ f"{self.account.hq}/build/tests/{test_id}",
63
+ json=body,
64
+ headers=self.account.headers,
65
+ timeout=10,
66
+ )
67
+ return res.json()
68
+
69
+ @verify_credentials
70
+ def delete_test(self, test_id, purge):
71
+ """Delete an existing test"""
72
+ res = self.delete(
73
+ f"{self.account.hq}/build/tests/{test_id}",
74
+ json=dict(purge=purge),
75
+ headers=self.account.headers,
76
+ timeout=10,
77
+ )
78
+ return res.json()
79
+
80
+ @verify_credentials
81
+ def undelete_test(self, test_id):
82
+ """Undelete a tombstoned test"""
83
+ res = self.post(
84
+ f"{self.account.hq}/build/tests/{test_id}/undelete",
85
+ headers=self.account.headers,
86
+ timeout=10,
87
+ )
88
+ return res.json()
89
+
90
+ @verify_credentials
91
+ def upload(self, test_id, filename, data, skip_compile=False):
92
+ """Upload a test or attachment"""
93
+ if len(data) > 1000000:
94
+ raise ValueError(f"File size must be under 1MB ({filename})")
95
+
96
+ h = self.account.headers | {"Content-Type": "application/octet-stream"}
97
+ query_params = ""
98
+ if skip_compile:
99
+ query_params = "?" + urllib.parse.urlencode(dict(skip_compile=True))
100
+ res = self.post(
101
+ f"{self.account.hq}/build/tests/{test_id}/{filename}{query_params}",
102
+ data=data,
103
+ headers=h,
104
+ timeout=10,
105
+ )
106
+ return res.json()
107
+
108
+ @verify_credentials
109
+ def compile_code_string(self, code: str, source_test_id: str = None):
110
+ """Compile a code string"""
111
+ res = self.post(
112
+ f"{self.account.hq}/build/compile",
113
+ json=dict(code=code, source_test_id=source_test_id),
114
+ headers=self.account.headers,
115
+ timeout=10,
116
+ )
117
+ return res.json()
118
+
119
+ @verify_credentials
120
+ def get_compile_status(self, job_id):
121
+ res = self.get(
122
+ f"{self.account.hq}/build/compile/{job_id}",
123
+ headers=self.account.headers,
124
+ timeout=10,
125
+ )
126
+ return res.json()
127
+
128
+ @verify_credentials
129
+ def create_threat(
130
+ self, name, published, threat_id=None, source_id=None, source=None, tests=None
131
+ ):
132
+ """Create a threat"""
133
+ body = dict(name=name, published=published)
134
+ if threat_id:
135
+ body["id"] = threat_id
136
+ if source_id:
137
+ body["source_id"] = source_id
138
+ if source:
139
+ body["source"] = source
140
+ if tests:
141
+ body["tests"] = tests
142
+
143
+ res = self.post(
144
+ f"{self.account.hq}/build/threats",
145
+ json=body,
146
+ headers=self.account.headers,
147
+ timeout=10,
148
+ )
149
+ return res.json()
150
+
151
+ @verify_credentials
152
+ def update_threat(
153
+ self,
154
+ threat_id,
155
+ name=None,
156
+ source_id=None,
157
+ source=None,
158
+ published=None,
159
+ tests=None,
160
+ ):
161
+ """Update a threat"""
162
+ body = dict()
163
+ if name:
164
+ body["name"] = name
165
+ if source_id is not None:
166
+ body["source_id"] = source_id
167
+ if source is not None:
168
+ body["source"] = source
169
+ if published is not None:
170
+ body["published"] = published
171
+ if tests is not None:
172
+ body["tests"] = tests
173
+
174
+ res = self.post(
175
+ f"{self.account.hq}/build/threats/{threat_id}",
176
+ json=body,
177
+ headers=self.account.headers,
178
+ timeout=10,
179
+ )
180
+ return res.json()
181
+
182
+ @verify_credentials
183
+ def delete_threat(self, threat_id, purge):
184
+ """Delete an existing threat"""
185
+ res = self.delete(
186
+ f"{self.account.hq}/build/threats/{threat_id}",
187
+ json=dict(purge=purge),
188
+ headers=self.account.headers,
189
+ timeout=10,
190
+ )
191
+ return res.json()
192
+
193
+ @verify_credentials
194
+ def undelete_threat(self, threat_id):
195
+ """Undelete a tombstoned threat"""
196
+ res = self.post(
197
+ f"{self.account.hq}/build/threats/{threat_id}/undelete",
198
+ headers=self.account.headers,
199
+ timeout=10,
200
+ )
201
+ return res.json()
202
+
203
+ @verify_credentials
204
+ def create_detection(
205
+ self, rule: str, test_id: str, detection_id=None, rule_id=None
206
+ ):
207
+ """Create a detection"""
208
+ body = dict(rule=rule, test_id=test_id)
209
+ if detection_id:
210
+ body["detection_id"] = detection_id
211
+ if rule_id:
212
+ body["rule_id"] = rule_id
213
+
214
+ res = self.post(
215
+ f"{self.account.hq}/build/detections",
216
+ json=body,
217
+ headers=self.account.headers,
218
+ timeout=10,
219
+ )
220
+ return res.json()
221
+
222
+ @verify_credentials
223
+ def update_detection(self, detection_id: str, rule=None, test_id=None):
224
+ """Update a detection"""
225
+ body = dict()
226
+ if rule:
227
+ body["rule"] = rule
228
+ if test_id:
229
+ body["test_id"] = test_id
230
+
231
+ res = self.post(
232
+ f"{self.account.hq}/build/detections/{detection_id}",
233
+ json=body,
234
+ headers=self.account.headers,
235
+ timeout=10,
236
+ )
237
+ return res.json()
238
+
239
+ @verify_credentials
240
+ def delete_detection(self, detection_id: str):
241
+ """Delete an existing detection"""
242
+ res = self.delete(
243
+ f"{self.account.hq}/build/detections/{detection_id}",
244
+ headers=self.account.headers,
245
+ timeout=10,
246
+ )
247
+ return res.json()
248
+
249
+ @verify_credentials
250
+ def create_threat_hunt(
251
+ self,
252
+ control: Control,
253
+ test_id: str,
254
+ name: str,
255
+ query: str,
256
+ threat_hunt_id: str = None,
257
+ ):
258
+ """Create a threat hunt"""
259
+ body = dict(
260
+ control=control.name,
261
+ name=name,
262
+ query=query,
263
+ test_id=test_id,
264
+ )
265
+ if threat_hunt_id:
266
+ body["id"] = threat_hunt_id
267
+
268
+ res = self.post(
269
+ f"{self.account.hq}/build/threat_hunts",
270
+ json=body,
271
+ headers=self.account.headers,
272
+ timeout=10,
273
+ )
274
+ return res.json()
275
+
276
+ @verify_credentials
277
+ def update_threat_hunt(
278
+ self,
279
+ threat_hunt_id: str,
280
+ name: str = None,
281
+ query: str = None,
282
+ test_id: str = None,
283
+ ):
284
+ """Update a threat hunt"""
285
+ body = dict()
286
+ if name:
287
+ body["name"] = name
288
+ if query:
289
+ body["query"] = query
290
+ if test_id:
291
+ body["test_id"] = test_id
292
+
293
+ res = self.post(
294
+ f"{self.account.hq}/build/threat_hunts/{threat_hunt_id}",
295
+ json=body,
296
+ headers=self.account.headers,
297
+ timeout=10,
298
+ )
299
+ return res.json()
300
+
301
+ @verify_credentials
302
+ def delete_threat_hunt(self, threat_hunt_id: str):
303
+ """Delete an existing threat hunt"""
304
+ res = self.delete(
305
+ f"{self.account.hq}/build/threat_hunts/{threat_hunt_id}",
306
+ headers=self.account.headers,
307
+ timeout=10,
308
+ )
309
+ return res.json()
@@ -0,0 +1,243 @@
1
+ from prelude_sdk_beta.controllers.http_controller import HttpController
2
+ from prelude_sdk_beta.models.account import verify_credentials
3
+
4
+
5
+ class DetectController(HttpController):
6
+
7
+ def __init__(self, account):
8
+ super().__init__(account)
9
+
10
+ def register_endpoint(self, host, serial_num, reg_string, tags=None):
11
+ """Register (or re-register) an endpoint to your account"""
12
+ body = dict(id=f"{host}:{serial_num}")
13
+ if tags:
14
+ body["tags"] = tags
15
+ account, token = reg_string.split("/")
16
+
17
+ res = self._session.post(
18
+ f"{self.account.hq}/detect/endpoint",
19
+ headers=dict(account=account, token=token, _product="py-sdk"),
20
+ json=body,
21
+ timeout=10,
22
+ )
23
+ return res.text
24
+
25
+ @verify_credentials
26
+ def update_endpoint(self, endpoint_id, tags=None):
27
+ """Update an endpoint in your account"""
28
+ body = dict()
29
+ if tags is not None:
30
+ body["tags"] = tags
31
+
32
+ res = self.post(
33
+ f"{self.account.hq}/detect/endpoint/{endpoint_id}",
34
+ headers=self.account.headers,
35
+ json=body,
36
+ timeout=10,
37
+ )
38
+ return res.json()
39
+
40
+ @verify_credentials
41
+ def delete_endpoint(self, ident: str):
42
+ """Delete an endpoint from your account"""
43
+ params = dict(id=ident)
44
+ res = self.delete(
45
+ f"{self.account.hq}/detect/endpoint",
46
+ headers=self.account.headers,
47
+ json=params,
48
+ timeout=10,
49
+ )
50
+ return res.json()
51
+
52
+ @verify_credentials
53
+ def list_endpoints(self, days: int = 90):
54
+ """List all endpoints on your account"""
55
+ params = dict(days=days)
56
+ res = self.get(
57
+ f"{self.account.hq}/detect/endpoint",
58
+ headers=self.account.headers,
59
+ params=params,
60
+ timeout=10,
61
+ )
62
+ return res.json()
63
+
64
+ @verify_credentials
65
+ def describe_activity(self, filters: dict, view: str = "protected"):
66
+ """Get report for an Account"""
67
+ params = dict(view=view, **filters)
68
+ res = self.get(
69
+ f"{self.account.hq}/detect/activity",
70
+ headers=self.account.headers,
71
+ params=params,
72
+ timeout=10,
73
+ )
74
+ return res.json()
75
+
76
+ @verify_credentials
77
+ def threat_hunt_activity(self, threat_hunt_id=None, test_id=None, threat_id=None):
78
+ """Get threat hunt activity"""
79
+ filters = dict(
80
+ threat_hunt_id=threat_hunt_id, test_id=test_id, threat_id=threat_id
81
+ )
82
+ res = self.get(
83
+ f"{self.account.hq}/detect/threat_hunt_activity",
84
+ headers=self.account.headers,
85
+ params=filters,
86
+ timeout=10,
87
+ )
88
+ return res.json()
89
+
90
+ @verify_credentials
91
+ def list_tests(self, filters: dict = None):
92
+ """List all tests available to an account"""
93
+ res = self.get(
94
+ f"{self.account.hq}/detect/tests",
95
+ headers=self.account.headers,
96
+ params=filters if filters else {},
97
+ timeout=10,
98
+ )
99
+ return res.json()
100
+
101
+ @verify_credentials
102
+ def get_test(self, test_id):
103
+ """Get properties of an existing test"""
104
+ res = self.get(
105
+ f"{self.account.hq}/detect/tests/{test_id}",
106
+ headers=self.account.headers,
107
+ timeout=10,
108
+ )
109
+ return res.json()
110
+
111
+ @verify_credentials
112
+ def list_techniques(self):
113
+ """List techniques"""
114
+ res = self.get(
115
+ f"{self.account.hq}/detect/techniques",
116
+ headers=self.account.headers,
117
+ timeout=10,
118
+ )
119
+ return res.json()
120
+
121
+ @verify_credentials
122
+ def list_threats(self):
123
+ """List threats"""
124
+ res = self.get(
125
+ f"{self.account.hq}/detect/threats",
126
+ headers=self.account.headers,
127
+ params={},
128
+ timeout=10,
129
+ )
130
+ return res.json()
131
+
132
+ @verify_credentials
133
+ def get_threat(self, threat_id):
134
+ """Get properties of an existing threat"""
135
+ res = self.get(
136
+ f"{self.account.hq}/detect/threats/{threat_id}",
137
+ headers=self.account.headers,
138
+ timeout=10,
139
+ )
140
+ return res.json()
141
+
142
+ @verify_credentials
143
+ def list_detections(self):
144
+ """List detections"""
145
+ res = self.get(
146
+ f"{self.account.hq}/detect/detections",
147
+ headers=self.account.headers,
148
+ params={},
149
+ timeout=10,
150
+ )
151
+ return res.json()
152
+
153
+ @verify_credentials
154
+ def get_detection(self, detection_id):
155
+ """Get properties of an existing detection"""
156
+ res = self.get(
157
+ f"{self.account.hq}/detect/detections/{detection_id}",
158
+ headers=self.account.headers,
159
+ timeout=10,
160
+ )
161
+ return res.json()
162
+
163
+ @verify_credentials
164
+ def list_threat_hunts(self, filters: dict = None):
165
+ """List threat hunts"""
166
+ res = self.get(
167
+ f"{self.account.hq}/detect/threat_hunts",
168
+ headers=self.account.headers,
169
+ params=filters if filters else {},
170
+ timeout=10,
171
+ )
172
+ return res.json()
173
+
174
+ @verify_credentials
175
+ def get_threat_hunt(self, threat_hunt_id):
176
+ """Get properties of an existing threat hunt"""
177
+ res = self.get(
178
+ f"{self.account.hq}/detect/threat_hunts/{threat_hunt_id}",
179
+ headers=self.account.headers,
180
+ timeout=10,
181
+ )
182
+ return res.json()
183
+
184
+ @verify_credentials
185
+ def do_threat_hunt(self, threat_hunt_id):
186
+ """Run a threat hunt"""
187
+ res = self.post(
188
+ f"{self.account.hq}/detect/threat_hunts/{threat_hunt_id}",
189
+ headers=self.account.headers,
190
+ timeout=10,
191
+ )
192
+ return res.json()
193
+
194
+ @verify_credentials
195
+ def download(self, test_id, filename):
196
+ """Clone a test file or attachment"""
197
+ res = self.get(
198
+ f"{self.account.hq}/detect/tests/{test_id}/{filename}",
199
+ headers=self.account.headers,
200
+ timeout=10,
201
+ )
202
+ return res.content
203
+
204
+ @verify_credentials
205
+ def schedule(self, items: list):
206
+ """Schedule tests and threats so endpoints will start running them
207
+
208
+ Example: items=[dict(run_code='DAILY', tags='grp-1,grp2', test_id='123-123-123'),
209
+ dict(run_code='DAILY', tags='grp-1', threat_id='abc-def-ghi')]
210
+ """
211
+ res = self.post(
212
+ url=f"{self.account.hq}/detect/queue",
213
+ headers=self.account.headers,
214
+ json=dict(items=items),
215
+ timeout=10,
216
+ )
217
+ return res.json()
218
+
219
+ @verify_credentials
220
+ def unschedule(self, items: list):
221
+ """Unschedule tests and threats so endpoints will stop running them
222
+
223
+ Example: items=[dict(tags='grp-1,grp2', test_id='123-123-123'),
224
+ dict(tags='grp-1', threat_id='abc-def-ghi')]
225
+ """
226
+ res = self.delete(
227
+ f"{self.account.hq}/detect/queue",
228
+ headers=self.account.headers,
229
+ json=dict(items=items),
230
+ timeout=10,
231
+ )
232
+ return res.json()
233
+
234
+ @verify_credentials
235
+ def accept_terms(self, name, version):
236
+ """Accept terms and conditions"""
237
+ res = self.post(
238
+ f"{self.account.hq}/iam/terms",
239
+ headers=self.account.headers,
240
+ json=dict(name=name, version=version),
241
+ timeout=10,
242
+ )
243
+ return res.json()
@@ -0,0 +1,31 @@
1
+ from prelude_sdk_beta.controllers.http_controller import HttpController
2
+ from prelude_sdk_beta.models.account import verify_credentials
3
+ from prelude_sdk_beta.models.codes import SCMCategory
4
+
5
+
6
+ class ExportController(HttpController):
7
+
8
+ def __init__(self, account):
9
+ super().__init__(account)
10
+
11
+ @verify_credentials
12
+ def export_scm(
13
+ self,
14
+ export_type: SCMCategory,
15
+ filter: str = None,
16
+ orderby: str = None,
17
+ top: int = None,
18
+ ):
19
+ """Download partner data as a CSV"""
20
+ params = {
21
+ "$filter": filter,
22
+ "$orderby": orderby,
23
+ "$top": top,
24
+ }
25
+ res = self.post(
26
+ f"{self.account.hq}/export/scm/{export_type.name}",
27
+ params=params,
28
+ headers=self.account.headers,
29
+ timeout=10,
30
+ )
31
+ return res.json()
@@ -0,0 +1,40 @@
1
+ from prelude_sdk_beta.controllers.http_controller import HttpController
2
+ from prelude_sdk_beta.models.account import verify_credentials
3
+ from prelude_sdk_beta.models.codes import Control
4
+
5
+
6
+ class GenerateController(HttpController):
7
+ def __init__(self, account):
8
+ super().__init__(account)
9
+
10
+ @verify_credentials
11
+ def upload_threat_intel(self, file: str):
12
+ with open(file, "rb") as f:
13
+ body = f.read()
14
+ res = self.post(
15
+ f"{self.account.hq}/generate/threat-intel",
16
+ data=body,
17
+ headers=self.account.headers | {"Content-Type": "application/pdf"},
18
+ timeout=30,
19
+ )
20
+ return res.json()
21
+
22
+ @verify_credentials
23
+ def get_threat_intel(self, job_id: str):
24
+ res = self.get(
25
+ f"{self.account.hq}/generate/threat-intel/{job_id}",
26
+ headers=self.account.headers,
27
+ timeout=10,
28
+ )
29
+ return res.json()
30
+
31
+ @verify_credentials
32
+ def generate_from_partner_advisory(self, partner: Control, advisory_id: str):
33
+ params = dict(advisory_id=advisory_id)
34
+ res = self.post(
35
+ f"{self.account.hq}/generate/partner-advisories/{partner.name}",
36
+ headers=self.account.headers,
37
+ json=params,
38
+ timeout=30,
39
+ )
40
+ return res.json()
@@ -0,0 +1,63 @@
1
+ import os
2
+ import requests
3
+
4
+ from requests.adapters import HTTPAdapter, Retry
5
+
6
+
7
+ PRELUDE_BACKOFF_FACTOR = int(os.getenv("PRELUDE_BACKOFF_FACTOR", 30))
8
+ PRELUDE_BACKOFF_TOTAL = int(os.getenv("PRELUDE_BACKOFF_TOTAL", 0))
9
+
10
+
11
+ class HttpController(object):
12
+ def __init__(self, account):
13
+ self._session = requests.Session()
14
+ self.account = account
15
+
16
+ retry = Retry(
17
+ total=PRELUDE_BACKOFF_TOTAL,
18
+ backoff_factor=PRELUDE_BACKOFF_FACTOR,
19
+ status_forcelist=[429],
20
+ )
21
+
22
+ self._session.mount("http://", HTTPAdapter(max_retries=retry))
23
+ self._session.mount("https://", HTTPAdapter(max_retries=retry))
24
+
25
+ def get(self, url, retry=True, **kwargs):
26
+ res = self._session.get(url, **kwargs)
27
+ if res.status_code == 200:
28
+ return res
29
+ if res.status_code == 401 and retry and self.account.token_location:
30
+ self.account.refresh_tokens()
31
+ self.account.update_auth_header()
32
+ return self.get(url, retry=False, **kwargs)
33
+ raise Exception(res.text)
34
+
35
+ def post(self, url, retry=True, **kwargs):
36
+ res = self._session.post(url, **kwargs)
37
+ if res.status_code == 200:
38
+ return res
39
+ if res.status_code == 401 and retry and self.account.token_location:
40
+ self.account.refresh_tokens()
41
+ self.account.update_auth_header()
42
+ return self.post(url, retry=False, **kwargs)
43
+ raise Exception(res.text)
44
+
45
+ def delete(self, url, retry=True, **kwargs):
46
+ res = self._session.delete(url, **kwargs)
47
+ if res.status_code == 200:
48
+ return res
49
+ if res.status_code == 401 and retry and self.account.token_location:
50
+ self.account.refresh_tokens()
51
+ self.account.update_auth_header()
52
+ return self.delete(url, retry=False, **kwargs)
53
+ raise Exception(res.text)
54
+
55
+ def put(self, url, retry=True, **kwargs):
56
+ res = self._session.put(url, **kwargs)
57
+ if res.status_code == 200:
58
+ return res
59
+ if res.status_code == 401 and retry and self.account.token_location:
60
+ self.account.refresh_tokens()
61
+ self.account.update_auth_header()
62
+ return self.put(url, retry=False, **kwargs)
63
+ raise Exception(res.text)