-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscanner.py
507 lines (445 loc) · 20.2 KB
/
scanner.py
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
import base64
import glob
import json
import logging
import os
import queue
import subprocess
import sys
import threading
import time
from http.server import BaseHTTPRequestHandler
from http.server import HTTPServer
from socketserver import ThreadingMixIn
from urllib.parse import urlparse
from kubernetes import client, config
from kubernetes.client.rest import ApiException
from prometheus_client import Gauge, generate_latest, CollectorRegistry, CONTENT_TYPE_LATEST
from version import VERSION
QUEUE = queue.Queue()
VUL_LIST = dict()
VUL_POINTS = bytes()
LOG_LEVEL = os.getenv("LOG_LEVEL", "info").replace(" ", "").lower()
TRIVY_REPORT_DIR = os.getenv("TRIVY_REPORT_DIR", "/tmp/trivyreport")
SCAN_INTERVAL = os.getenv("SCAN_INTERVAL", "120")
HTTP_SERVER_PORT = os.getenv("HTTP_PORT", "8080")
TRIVY_BIN_PATH = os.getenv("TRIVY_BIN_PATH", "./trivy")
log = logging.getLogger(__name__)
log_format = '%(asctime)s - [%(levelname)s] [%(threadName)s] [%(funcName)s:%(lineno)d]- %(message)s'
log_config = {
"debug": logging.DEBUG,
"info": logging.INFO,
"error": logging.ERROR,
"critical": logging.CRITICAL,
"fatal": logging.FATAL
}
try:
logging.basicConfig(level=log_config[LOG_LEVEL], format=log_format)
except KeyError:
logging.basicConfig(level=logging.CRITICAL, format=log_format)
class DockerConfigNotFound(Exception):
pass
class KApiException(Exception):
pass
def list_all_pods():
log.debug("list all pods")
v1 = client.CoreV1Api()
return v1.list_pod_for_all_namespaces().items
def read_secret(namespace, secret):
log.debug("read secret: {}/{}".format(namespace, secret))
v1 = client.CoreV1Api()
try:
secret_obj = v1.read_namespaced_secret(secret, namespace)
except ApiException as err:
raise KApiException(err)
try:
decoded_password = base64.b64decode(secret_obj.data['.dockerconfigjson']).decode()
except KeyError:
raise DockerConfigNotFound("Not found .dockerconfigjson key")
load_auth_config = json.loads(decoded_password)
registry_addr = list(load_auth_config['auths'].keys())[0]
if ("username" not in load_auth_config['auths'][registry_addr]) and \
("password" not in load_auth_config['auths'][registry_addr]):
log.debug("username and password empty. Decoding")
auth_decoded = base64.b64decode(load_auth_config['auths'][registry_addr]["auth"]).decode()
username = auth_decoded.split(":")[0]
password = auth_decoded.split(":")[1]
auth = {"username": username,
"password": password,
"registry_url": registry_addr.replace("https://", "").replace("http://", "")
}
else:
auth = {"username": load_auth_config['auths'][registry_addr]['username'],
"password": load_auth_config['auths'][registry_addr]['password'],
"registry_url": registry_addr.replace("https://", "").replace("http://", "")
}
log.debug("end read secret")
return auth
def parse_pods(get_docker_auth=True):
log.debug("parse pods")
parsed_pod = list()
pods = list_all_pods()
for pod in pods:
a = {
pod.metadata.name: {
"namespace": pod.metadata.namespace,
"containers": [],
"init_containers": [],
"docker_password": []
}
}
for container in pod.spec.containers:
a[pod.metadata.name]['containers'].append(container.image)
parsed_pod.append(a)
if pod.spec.init_containers is not None:
for init_container in pod.spec.init_containers:
a[pod.metadata.name]['init_containers'].append(init_container.image)
if pod.spec.image_pull_secrets is not None:
for secret in pod.spec.image_pull_secrets:
try:
if get_docker_auth:
a[pod.metadata.name]['docker_password'] \
.append(read_secret(pod.metadata.namespace, secret.name))
except DockerConfigNotFound:
log.info("The Secret {} don't have .dockerconfigjson key.".format(secret.name))
except KApiException as err:
log.info("Error reading secret found on pod. The error returned by "
"kubernetes api was: {}".format(err))
except KeyError:
log.info("POD: {} | "
"Namespace: {} | "
"Invalid docker auth on secret {}".format(pod.metadata.name,
pod.metadata.namespace,
secret.name))
log.debug("end parse pods")
return parsed_pod
def unique_images():
log.debug("unique images")
pods = parse_pods()
images = dict()
for pod in pods:
for pod_id in pod.items():
for image in pod[pod_id[0]]['containers']:
if not pod[pod_id[0]]['docker_password']:
images[image] = {"docker_password": []}
else:
images[image] = {"docker_password": pod[pod_id[0]]['docker_password']}
for image in pod[pod_id[0]]['init_containers']:
if not pod[pod_id[0]]['docker_password']:
images[image] = {"docker_password": []}
else:
images[image] = {"docker_password": pod[pod_id[0]]['docker_password']}
log.debug("end unique images")
return images
def enqueue():
log.debug("enqueue")
images = unique_images()
for image in images:
log.debug("enqueue: {}".format({image: images[image]}))
QUEUE.put({image: images[image]})
def parse_scan(image):
log.debug("parse scan")
with open("{}/{}.json".format(TRIVY_REPORT_DIR, image), "r") as f:
try:
vul_list = json.loads(f.read())
except json.decoder.JSONDecodeError:
log.error("Error decoding trivy output scan: {}".format(image))
return {}
log.debug("end parse scan")
return vul_list
class Scan:
RUNNING = True
def trivy(self):
log.debug("trivy scan")
while self.RUNNING and not QUEUE.empty():
item = QUEUE.get()
image = list(item.keys())[0]
safe_image = image.replace("/", "__")
log.info("Scanning image: {}".format(image))
system_environment = os.environ.copy()
cmd_clear_cache = ["{} image --clear-cache {}".format(TRIVY_BIN_PATH, image)]
cmd = ["{} image --format=json --ignore-unfixed=true --output={}/{}.json {}".format(TRIVY_BIN_PATH,
TRIVY_REPORT_DIR,
safe_image,
image)]
log.debug("Trivy clear cache cmd: {}".format(cmd_clear_cache))
trivy_clear_cache = subprocess.Popen(cmd_clear_cache, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
shell=True, env=system_environment)
log.debug("STDOUT Clean Cache: {}".format(trivy_clear_cache.stdout.read().decode()))
log.debug("STDERR Clean Cache: {}".format(trivy_clear_cache.stderr.read().decode()))
log.debug("STATUS CODE Clean Cache {}".format(trivy_clear_cache.returncode))
trivy_clear_cache.wait()
log.debug("Image: {} password len: {}".format(image, len(item[image]['docker_password'])))
log.debug("Image {} password content: {}".format(image, item[image]['docker_password']))
if len(item[image]['docker_password']) > 0:
log.debug("Image {} has password".format(image))
if image.split('/')[0] in item[image]['docker_password'][0]['registry_url']:
log.info("Auth on registry {}".format(item[image]['docker_password'][0]['registry_url']))
system_environment["TRIVY_USERNAME"] = item[image]['docker_password'][0]['username']
system_environment["TRIVY_PASSWORD"] = item[image]['docker_password'][0]['password']
log.debug("Trivy scan cmd: {}".format(cmd))
trivy_scan = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=system_environment,
shell=True)
trivy_scan.wait()
log.debug("STDOUT: {}".format(trivy_scan.stdout.read().decode()))
log.debug("STDERR: {}".format(trivy_scan.stderr.read().decode()))
log.debug("STATUS CODE: {}".format(trivy_scan.returncode))
# log.debug("Parse scan: {}".format(parse_scan(safe_image)))
VUL_LIST[image] = parse_scan(safe_image)
# log.debug(VUL_LIST)
QUEUE.task_done()
log.debug("passou o task_done")
def convert_label_selector(label):
lbl = str()
if type(label) is not dict:
raise TypeError("Label must be a dict")
for key, value in label.items():
converted_label = "=".join([str(key), str(value)])
if lbl == '':
lbl = converted_label
else:
lbl = ",".join([lbl, converted_label])
return lbl
def get_pods_associated_with_ingress():
log.debug("get pods associated with ingress")
pods = list()
v1 = client.CoreV1Api()
net = client.NetworkingV1Api()
ingresses = net.list_ingress_for_all_namespaces()
# Poderia ser um list comprehension? Sim, mas ficaria tão dificil de ler...
for ingress in ingresses.items:
for rule in ingress.spec.rules:
if rule.http is None:
log.warning("Ingress: {} Rule: is None".format(rule.host))
continue
for path in rule.http.paths:
try:
service = v1.read_namespaced_service(name=path.backend.service.name,
namespace=ingress.metadata.namespace)
except ApiException as err:
log.error("Ingress: {}, error getting service: {}".format(rule.host, err))
continue
try:
endpoint = v1.list_namespaced_endpoints(namespace=ingress.metadata.namespace,
label_selector=convert_label_selector(
service.spec.selector))
except ApiException as err:
log.error("Ingress: {}, error getting endpoints. ".format(rule.host, err))
continue
for ep in endpoint.items:
if ep.subsets is None:
log.warning("The endpoint of service {} comes empty. Skipping verification".format(
path.backend.service.name))
continue
for subset in ep.subsets:
if subset.addresses is None:
log.error("The endpoint subset of service {} has no address. Skipping verification".format(
path.backend.service.name
))
continue
for address in subset.addresses:
if address.target_ref is None:
log.error(
"The target ref of address {} is none. Skipping verification".format(
subset.addresses
))
continue
if address.target_ref.name not in pods:
pods.append(address.target_ref.name)
log.debug("Pods associated with ingress: {}".format(pods))
return pods
def create_prom_points():
log.debug("create prom points")
registry = CollectorRegistry()
vulnerability_gauge = Gauge("pod_security_issue", "CVE found in all images associated with pod",
["PodName",
"Namespace",
"Image",
"IsPublic",
"BaseOS",
"VulnerabilityID",
"PkgName",
"InstalledVersion",
"FixedVersion",
"Severity"], registry=registry)
pods = parse_pods(get_docker_auth=False)
public_pods = get_pods_associated_with_ingress()
for pod in pods:
p = list(pod.keys())[0]
for container in pod[p]['containers']:
try:
for t in VUL_LIST[container]["Results"]:
if not ("Vulnerabilities" in t):
log.debug("Vulnerabilities keys not found. Passing to next list item")
continue
for v in t["Vulnerabilities"]:
log.info("Prom point pod: {}".format(p))
vulnerability_gauge.labels(
p,
pod[p]['namespace'],
container,
str(p in public_pods),
t["Type"],
v["VulnerabilityID"],
v["PkgName"],
v["InstalledVersion"],
v["FixedVersion"],
v["Severity"]
).set(1)
log.debug("Set Point to pod: {} with values: |"
"namespace: {} |"
"image: {} | "
"is public? {} | "
"base os: {} | "
"CVE: {} |"
"Package: {} |"
"Installed Version: {} |"
"Fixed in Version: {} |"
"Severity: {}".format(p,
pod[p]['namespace'],
container,
str(p in public_pods),
t["Type"],
v["VulnerabilityID"],
v["PkgName"],
v["InstalledVersion"],
v["FixedVersion"],
v["Severity"])
)
except TypeError:
log.info("Prom point pod: {}".format(p))
log.debug("Set Point to pod: {} with values: |"
"namespace: {}|"
"image: {} | "
"is public? {} | "
"base os: {} | "
"CVE: {} |"
"Package: {} |"
"Installed Version: {} |"
"Fixed in Version: {} |"
"Severity: {}".format(p,
pod[p]['namespace'],
container,
str(p in public_pods),
"NA",
"NA",
"NA",
"NA",
"NA",
"NA")
)
vulnerability_gauge.labels(
p,
pod[p]['namespace'],
container,
str(p in public_pods),
"NA",
"NA",
"NA",
"NA",
"NA",
"NA"
).set(0)
except KeyError:
log.warning("The container {} was not scanned. Wait until next round...".format(container))
log.debug("finished create prom points")
return generate_latest(registry)
def start_threads():
log.debug("start threads")
enqueue()
scan = Scan()
t1 = threading.Thread(target=scan.trivy)
t2 = threading.Thread(target=scan.trivy)
t1.start()
t2.start()
t1.join()
t2.join()
def main():
if 'KUBERNETES_PORT' in os.environ:
config.load_incluster_config()
log.debug("using in cluster config")
else:
log.debug("using kube config")
config.load_kube_config()
client.rest.logger.setLevel(logging.WARNING)
start_threads()
global VUL_POINTS
VUL_POINTS = create_prom_points()
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
pass
class VulnerabilityHandler(BaseHTTPRequestHandler):
def __init__(self, *args, **kwargs):
BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
def log_message(self, fmt, *args):
return
def do_GET(self):
url = urlparse(self.path)
if url.path == '/metrics':
self.send_response(200)
self.send_header('Content-Type', CONTENT_TYPE_LATEST)
self.end_headers()
self.wfile.write(VUL_POINTS)
elif url.path == '/':
self.send_response(200)
self.end_headers()
self.wfile.write(b"""<html>
<head><title>Container Runtime Vulnerability Scan</title></head>
<body>
<h1>Hi,</h1>
<p>Take a look at <code>/metrics</code> to get metrics.</p>
</body>
</html>""")
else:
self.send_response(404)
self.end_headers()
def http_server_handler(*args, **kwargs):
return VulnerabilityHandler(*args, **kwargs)
def start_http_server(port):
server = ThreadedHTTPServer(('', port), http_server_handler)
server.daemon_threads = True
threading.Thread(target=server.serve_forever, daemon=True).start()
def cleanup():
log.debug("Executing CleanUP")
global VUL_LIST
VUL_LIST = dict()
if os.path.exists(TRIVY_REPORT_DIR):
for f in glob.glob("{}/*.json".format(TRIVY_REPORT_DIR)):
log.debug("removing file: {}".format(f))
os.remove(f)
def setup():
log.debug("Executing Setup step")
if not os.path.exists(TRIVY_REPORT_DIR):
os.makedirs(TRIVY_REPORT_DIR)
if not os.path.exists(TRIVY_BIN_PATH):
raise FileNotFoundError("Trivy binary not found at: {}".format(TRIVY_BIN_PATH))
cmd_download_db = ["{} image --download-db-only".format(TRIVY_BIN_PATH)]
log.debug("Trivy Download db cmd: {}".format(cmd_download_db))
system_environment = os.environ.copy()
trivy_clear_cache = subprocess.Popen(cmd_download_db, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True,
env=system_environment)
log.debug("Trivy Download db return code: {}".format(trivy_clear_cache.returncode))
log.debug("Trivy Download db stdout {}".format(trivy_clear_cache.stdout.read().decode()))
log.debug("Trivy Download db stderr {}".format(trivy_clear_cache.stderr.read().decode()))
if __name__ == '__main__':
log.info("Starting Image Scanner version: {}".format(VERSION))
try:
setup()
except BaseException as e:
log.error(e)
sys.exit(1)
start_http_server(int(HTTP_SERVER_PORT))
while True:
try:
main()
cleanup()
log.info("Sleeping for {}s".format(SCAN_INTERVAL))
time.sleep(int(SCAN_INTERVAL))
except KeyboardInterrupt:
log.info("Bye...")
break
except BaseException as e:
log.error(e)
time.sleep(int(SCAN_INTERVAL))