Vulnerability scanning AWS ECS running Images
Get the fixable vulnerabilities of you running ECS tasks
When you have quite a few applications running on AWS ECS you should want to fix possible vulnerabilities that your docker images have. There are quite a few options to get the vulnerabilities. We will not look into all the options that are there in this post. What we do look at is getting only the vulnerabilities for running images. If you scan your docker repository it might contain images that you don’t run anymore but still have in the registry. We will also only look at only vulnerabilities that we can fix, because if you won’t fix it in the packages yourself you are unable to do anything with the vulnerabilities anyways.
- Prerequisite
- List ECS clusters
- List the running tasks in a cluster
- Get all the docker images run for the tasks
- Remove images you don’t want to scan
- Scan the images
- Filter the images that have no fixable vulnerabilities
- Write the output to a files
Prerequisite
- Install Trivy
- Have a working AWS CLI, Python and boto3 installed
create a python file for the script we are going to create with the following code:
|
|
List ECS clusters
First we need to get all the running tasks so we can later get all the task running on the clusters.
|
|
Update the main with
cluster_arns = get_ecs_cluster_arns()
List the running tasks in a cluster
After that we can get all the tasks that run in a cluster and do this for all the clusters.
|
|
Update the main with
cluster_task_arns = {}
for arn in cluster_arns:
cluster_task_arns.update( get_task_arns(arn))
Get all the docker images run for the tasks
For all the tasks can we now get the docker images. Since some can be duplicate we can use a set to prevent duplicates.
|
|
Update the main with
docker_images = get_docker_images(cluster_task_arns)
Remove images you don’t want to scan
Maybe there are some images you don’t want to scan. YOu can remove the before starting the scanning.
|
|
Update the main with
remove_images_to_not_scan(docker_images)
Scan the images
Finally we scan the docker images. Since Trivy give a lot of information back we keep only the lines we want to keep.
|
|
Update the main with
docker_image_vulnerabilities = scan_docker_images(docker_images)
Filter the images that have no fixable vulnerabilities
If there is nothing to fix we don’t want to see the images in our list.
|
|
Update the main with
only_vulnerable_images=remove_not_vulnerable_and_fixable_images(docker_image_vulnerabilities)
Write the output to a files
To keep our results for whatever we want to do with it we write the output to a json file. Add this to the main function to complete the script.
with open('my_vulnerabilities.json', 'w') as outfile:
json.dump(only_vulnerable_images, outfile, )
Run this script on any AWS account you want to get the vulnerabilities that you want to fix.