-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathexample_export.py
More file actions
371 lines (318 loc) · 10.6 KB
/
example_export.py
File metadata and controls
371 lines (318 loc) · 10.6 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# -*- coding: utf-8 -*-
"""
Example script showing how to use the LaceworkClient class.
"""
import csv
import logging
from datetime import datetime, timedelta, timezone
from dotenv import load_dotenv
from laceworksdk import LaceworkClient
logging.basicConfig(level=logging.INFO)
load_dotenv()
ISO_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
class ReferenceLookup:
def __init__(self, key, field, dict, multivalue=False):
self.key = key
self.field = field
self.dict = dict
self.multivalue = multivalue
def lookup(self, value, default=None):
# only return the first matching result or default value
dict = list(filter(lambda x: x[self.key] == value, self.dict))
rows = []
for row in dict:
# return the entire row
if self.field is None:
rows.append(row)
else:
for i in self.field.split("."):
if i in row:
row = row[i]
else:
row = default
rows.append(row)
# return all multiple values
if self.multivalue:
return rows
# return first value only
else:
return rows.pop() if len(rows) > 0 else default
class DataHandler:
def __init__(self, format, file_path="export.csv"):
if format not in ["csv", "dict"]:
raise Exception(
f"Unsupported export format, expected csv or dict found: {format}"
)
self.format = format
self.file_path = file_path
def __open(self):
if self.format == "csv":
self.header = False
self.fp = open(self.file_path, "w")
self.writer = csv.writer(self.fp, quoting=csv.QUOTE_ALL)
self.dataset = csv.reader(self.fp)
else:
self.dataset = []
def __close(self):
if self.format == "csv":
self.fp.close()
def insert(self, row):
if self.format == "csv":
if not self.header:
self.writer.writerow(row.keys())
self.header = True
self.writer.writerow(row.values())
elif self.format == "dict":
self.dataset.append(row)
def get(self):
return self.dataset
def __enter__(self):
self.__open()
return self
def __exit__(self, exc_type, exc_value, traceback):
self.__close()
def query(
client,
type,
object,
start_time=None,
end_time=None,
filters=None,
returns=None,
):
if start_time is None:
start_time = datetime.now(timezone.utc) + timedelta(days=-1)
if end_time is None:
end_time = datetime.now(timezone.utc)
if filters is None:
filters = []
# build query string
q = {
"timeFilter": {
"startTime": start_time.strftime(ISO_FORMAT),
"endTime": end_time.strftime(ISO_FORMAT),
},
"filters": filters,
"returns": returns,
}
# create reference to search object
obj = getattr(getattr(client, f"{type}"), f"{object}")
# return query result reference
return obj.search(json=q)
def lookup(key, dict, default=None):
for i in key.split("."):
if i in dict:
dict = dict[i]
else:
return default
return dict
def flatten_json(y):
out = {}
def flatten(x, name=""):
if type(x) is dict:
for a in x:
flatten(x[a], name + a + "_")
elif type(x) is list:
i = 0
for a in x:
flatten(a, name + str(i) + "_")
i += 1
else:
out[name[:-1]] = x
flatten(y)
return out
def map_fields(data, field_map=None):
if field_map is None:
# flatten json
data = flatten_json(data)
field_map = {}
for key in data.keys():
field_map[key] = key
result = {}
for field in field_map.keys():
# for reference field find the matching local key and lookup the field value
if isinstance(field_map[field], ReferenceLookup):
result[field] = field_map[field].lookup(lookup(field_map[field].key, data))
else:
result[field] = lookup(field_map[field], data)
return result
def export(format, results, field_map=None, file_path="export.csv"):
with DataHandler(format, file_path=file_path) as h:
# process results
for result in results:
for data in result["data"]:
# create the data row
try:
row = map_fields(data=data, field_map=field_map)
except Exception as e:
logging.error(f"Failed to map fields for data: {data}")
raise Exception(e)
h.insert(row)
# return
return h.get()
if __name__ == "__main__":
client = LaceworkClient()
# # scenario 1 - export a list of machines to csv
# export(
# "csv",
# query(client=client, type="entities", object="machines"),
# field_map={
# "start_time": "startTime",
# "end_time": "endTime",
# "mid": "mid",
# "tags": "machineTags",
# "hostname": "hostname",
# "public_ip": "machineTags.ExternalIp",
# },
# file_path="export_machines.csv",
# )
# # scenario 2 - export a list of containers to csv
# export(
# "csv",
# query(
# client=client,
# type="entities",
# object="containers",
# returns=[
# "startTime",
# "endTime",
# "imageId",
# "podName",
# "containerName",
# "propsContainer",
# ],
# ),
# field_map={
# "start_time": "startTime",
# "image_id": "imageId",
# "pod_name": "podName",
# "container_name": "containerName",
# "props": "propsContainer",
# },
# file_path="export_containers.csv",
# )
# # scenario 3 - export a list of host vulnerabilities and lookup machine details
# # create a machine mapping reference to be used in "join"
# machines = export(
# "dict",
# query(
# client=client,
# type="entities",
# object="machines",
# returns=["mid", "hostname", "primaryIpAddr"],
# ),
# )
# export(
# "csv",
# query(
# client=client,
# type="vulnerabilities",
# object="hosts",
# filters=[
# {"field": "status", "expression": "in", "values": ["New", "Active"]},
# {
# "field": "severity",
# "expression": "in",
# "values": ["Critical", "High", "Medium"],
# },
# {"field": "fixInfo.fix_available", "expression": "eq", "value": 1},
# ],
# returns=[
# "startTime",
# "endTime",
# "severity",
# "status",
# "vulnId",
# "mid",
# "featureKey",
# "machineTags",
# "fixInfo",
# "cveProps",
# ],
# ),
# field_map={
# "start_time": "startTime",
# "end_time": "endTime",
# "mid": "mid",
# "cve_id": "vulnId",
# # lookup hostname by mid and return first result
# "hostname": ReferenceLookup("mid", "hostname", machines),
# "package_name": "featureKey.name",
# "package_namespace": "featureKey.namespace",
# "package_active": "featureKey.package_active",
# "package_status": "fixInfo.eval_status",
# "version": "featureKey.version_installed",
# "fix_available": "fixInfo.fix_available",
# "fixed_version": "fixInfo.fixed_version",
# "severity": "severity",
# "tags": "machineTags",
# "status": "status",
# },
# file_path="export_host_vulnerabilities.csv",
# )
# scenario 4 - export a list of image vulnerabilities and lookup active container details
# create a container mapping reference to be used in "join"
containers = export(
"dict",
query(
client=client,
type="entities",
object="containers",
returns=[
"imageId",
"podName",
# "containerName",
# "propsContainer"
],
),
)
# unique image_ids
image_ids = list(set([d["imageId"] for d in containers if "imageId" in d.keys()]))
logging.info("Found {0} active imageIds".format(len(image_ids)))
logging.info("Starting export of container vulnerabilities for active images...")
export(
"csv",
query(
client=client,
type="vulnerabilities",
object="containers",
filters=[
{
"field": "severity",
"expression": "in",
"values": ["Critical", "High", "Medium"],
},
{"field": "imageId", "expression": "in", "values": image_ids},
{"field": "status", "expression": "in", "values": ["VULNERABLE"]},
{"field": "fixInfo.fix_available", "expression": "eq", "value": 1},
],
returns=[
"startTime",
"imageId",
"severity",
"status",
"vulnId",
"evalCtx",
"fixInfo",
"featureKey",
],
),
field_map={
"start_time": "startTime",
"image_id": "imageId",
"cve_id": "vulnId",
# lookup all active containers matching image id and return all properties
"containers": ReferenceLookup("imageId", None, containers, multivalue=True),
"image_registry": "evalCtx.image_info.registry",
"image_repo": "evalCtx.image_info.repo",
"image_status": "evalCtx.image_info.status",
"package_name": "featureKey.name",
"package_namespace": "featureKey.namespace",
"version": "featureKey.version",
"fix_available": "fixInfo.fix_available",
"fixed_version": "fixInfo.fixed_version",
"severity": "severity",
"status": "status",
},
file_path="export_container_vulnerabilities.csv",
)