Understanding Container Security
Tags: aws, devops, security, containers
Published: 2023-07-21 06:35:16

Containerized architectures are one of the most popular technologies for developers. There are many advantages to migrating to the containers, I think the most important advantage is flexibility. You can build your Docker images and containers only once, and deploy wherever you want. In addition to this advantage, there are also lots of security challenges. Many attack surfaces surround your container environment if your container structure has lots of security misconfiguration and vulnerabilities. While being very flexible, you may encounter attackers and lots of exploit scenarios in your containers.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1eeaf07j960o86rwxfkw.png)

Container security has lots of aspects. As security professionals, these are our responsibilities in a container environment:

- Secure your codebase
- Secure your app
- Secure all libraries that you’ve used
- Secure your network/communication
- Secure your CI/CD processes

In this blog, we will talk about container security challenges and what should you do about them. Let’s start together! ✍️

**Container Security Challenges**

**1) Vulnerability Overload 🥴:** There are lots of vulnerabilities in different layers in a container image or containers in runtime. Developers are confused about the prioritization of these vulnerabilities, how to fix them without any functional errors, and who is responsible for remediating them. Prioritization is important because if you have 100 critical vulnerabilities in a container cluster, you need to know which are the most critical ones. At this point, analyzing the vulnerability scan results is an important task for you.

![Container Vulnerability Overload](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9cpt7sbb0jhb9ddrh193.png)

**2) Container security is challenging 😮:** There are a lot of layers and aspects in a container such as networking, codebase, and libraries that are used, etc. We need to know all details about all of these topics and you need to get action about them. Also, if there is a vulnerability in a container system, you need lots of information about some questions like: “What is the exact vulnerability scenario in there?”, and “How is this vulnerability affect our system?”.

![Understanding Container Security](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cc1n3spy316fr8wnzx12.png)

**3) Difficulty in Integration 🧗🏻‍♀️:** There are lots of scanning, vulnerability management, and alerting tools about container security. In your company, you need to decide what is the best tool for managing your container environment. In addition to this, you need to think about our CI/CD pipeline and at which point you need to integrate these tools in order to be protected.


![Difficulty in Integration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/by3avbs2wtnnx65lo0l7.png)

**✨ Security Points of Containerized Design ✨**
As we mentioned before, you should think of every potential attack vector at each stage of a container’s lifecycle.

![Container Lifecycle Management](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q4rah6v30miix562pr7b.png)

**1) 🤔Vulnerable Application Code:** Lots of code parts are added to a container environment in your daily work. You’re using lots of libraries, third-party codes, and our business logic codes. You need to be careful about secure code best practices when you’re writing your application code.

- You need to follow OWASP guides about secure code design.
- You need to scan your code with SAST tools like SonarQube, Veracode, etc. These tools help to understand the security of your code.
- Scanning your container images for vulnerabilities is a good approach. But this scanning is not one time job, it should be done regularly (weekly, monthly, etc.) You need to follow vulnerability reports and fix all of the vulnerabilities as soon as possible. I recommend some open-source tools that could be useful: [Trivy](https://trivy.dev/), [Docker-Bench](https://github.com/docker/docker-bench-security), [Grype](https://github.com/anchore/grype).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ag32lb7xu398xcbu7fnf.png)

**2) 🤔 Badly Configured Container Images & Containers:** If an attacker is able to modify the Dockerfile, it’s possible for them to take malicious actions such as adding malware or crypto-mining software into the image, accessing build secrets and privilege escalation, etc. These are very dangerous activities for container systems. To prevent this, we need to write our Dockerfiles with security best practices. Here are some best practices that I’ve recommended:

- You should use an image from a trusted registry. Please do not use any base images that are not verified, or built by someone that you do not know. Attackers love this way to catch you.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v6cen1v18rh8gmnbu7wo.png)

- You should use smaller base images for your infrastructure. When you use the smaller one, you have less unnecessary code and a smaller attack surface.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/olj7kl5imvcntoocx5hu.png)

- Please do not be root and run any code with root in your Dockerfile. You should always define a user with at least a privilege approach. Root users are always critical to us.

- Remote code execution scenarios are critical for containers. You should check your RUN command to prevent running the codes that you don’t have or attackers can. You should audit your RUN command logs regularly. Also, access control is critical at this point. You need to clarify and define which users in your system have the change RUN command.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/02sz1wa5v5dmapg4m2n0.png)

- For your Dockerfiles, you can also scan them. There are lots of tools that can check your Dockerfiles. They will validate if Dockerfile is compliant with Docker best practices such as not using root user, making sure a health check exists, and not exposing the SSH port. You can use [Snyk ](https://docs.snyk.io/scan-containers/scan-your-dockerfile)and [Checkov](https://www.checkov.io/).


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2w24w4xoisf5c0x6t0kj.png)


**3) 🤔Exposed Secrets:** Exposed secrets are a nightmare for us. As in every environment, we’ve mentioned “Please do not hardcode your credentials.” You should not store sensitive data in different layers in your containers. You think if you’re using the remove command everything is fine, but believe me, it’s not. Your credentials are still in layers and docker history and attackers can get these credentials by reversing your container image. To prevent exposure, you should rotate & encrypt your secrets regularly, not store them in .env files and you can use secret management tools like [Vault](https://www.hashicorp.com/resources/securing-container-secrets-vault).

**4) 🤔Image Storage Security:** We’re using AWS ECR, DockerHub as image storage. While you’re using and pushing your images, you need to be sure these are not publicly accessible. Attackers love founding publicly accessible Docker images, reversing them, and getting all your system details, credentials, etc. Also, to ensure your docker data integrity and docker content trust, your Docker images should be signed and verified on runtime. We don’t want to use images that attackers changed and pushed our docker storage area.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xqd9cu5x3wfp0vkhnmqk.png)

Access control is a critical topic there. You need to decide which users in your company should access which Docker images with which access roles. You should check your image tags and YAML files before running them in your production cluster. You should be aware of everything about your Docker images.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7u5vjmuobcpqsf71zt1d.png)

**5) 🤔Insecure Networking:** Every external attack reaches your container deployment across a network area. So you need to implement a strong secure network infrastructure for your containers. You need to implement a VPC, default deny mechanism on NACLs, security groups, and firewalls. You should restrict your container ports wherever possible. For network traffic, you always think of using Web Application Firewall (WAF) solutions. WAF helps to prevent lots of attacks such as RCE, SQL injection in addition to network layer attacks. And as you know, you need to always use TLS for your encrypted traffic. Attackers love HTTP, not HTTPS.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ywewyn1ty46ygtzn838u.png)

Thanks for reading! Stay safe in containers! 👻