Container Security - Basics To Advance - by @rohitcoder
Container Security - Basics To Advance - by @rohitcoder
Workshop
1. Introduction
2. Understanding Containers
3. Docker Basics
4. Writing a Secure Dockerfile
5. Container Vulnerabilities
6. Scanning Docker Images
7. Mitigating Container Vulnerabilities
8. Building Secure Container Images
9. Running Containers Securely
10. Conclusion and Q&A
Let’s first understand,
What are Containers?
1. Container: Lightweight, isolated unit for
packaging applications and dependencies.
2. Creation: Use Docker or Kubernetes with
container images.
3. Dockerfile or Kubernetes manifest: Define
configuration and build instructions.
4. Build: Containerization tool reads instructions
and creates reproducible image.
5. Result: Portable environment for consistent
application execution.
Containers and their benefits
● Portability ● Scalability
● Isolation ● Version Control
● Efficiency ● Security
● Consistency ● Simplified Maintenance
● Rapid Deployment ● Cloud Adoption
Docker & Virtual Machines
● In virtual machines (VMs), hardware resources
are shared among multiple instances. This
involves carefully allocating specific portions of
memory and CPU to each VM. In contrast,
containers function as lightweight processes that
share the underlying operating system.
Networks
=====
create,connect,disconn
ect,destroy
Docker Basics
https://docs.docker.com/engine/reference/commandline/system_events/
Docker Basics
How to build our own docker Images?
Now what?
Security Issues in Container Lifecycle
Over-Privilege issues,
CVEs & Known Vulns Run-time Security issues
Misconfigurations
Possible Security Issues in Dockerfiles
1. Using outdated or vulnerable base images.
2. Setting weak or easily guessable passwords.
3. Running the application as the root user.
4. Copying sensitive files or credentials into the
container.
5. Exposing unnecessary ports or services.
6. Not updating packages or dependencies
regularly.
7. Installing unnecessary packages or tools.
8. Not using a .dockerignore file to exclude
sensitive files from being copied.
9. Ignoring container size and complexity, leading
to potential attack vectors.
10. Executing privileged commands or processes
within the container.
Possible Security Issues in Container Images
gcr.io/google-containers/aggregator:201
1. Vulnerable base images. 5-11-10-34b976a
2. Insecure software installation.
3. Embedded secrets and credentials.
4. Excessive privileges.
5. Unnecessary software and packages.
6. Unpatched software. rapid7/container-image
7. Insecure default settings. -scanner
8. Image sprawl.
9. Unsecured image repositories.
10. Incomplete or misconfigured image build process.
public.ecr.aws/datadog/agent:7.
45.1-rc.2-jmx
Possible Security Issues in Running Containers
1. Container Breakout
2. Privilege Escalation
3. Inadequate Isolation
4. Unsecured Container Interfaces
5. Inadequate Resource Limits
6. Container Image Vulnerabilities
7. Unprotected Sensitive Data
8. Insecure APIs and Orchestration Systems
9. Container Image Integrity
10. Insider Threats
Possible Security Issues in Running Containers
1. Container Breakout
2. Privilege Escalation
3. Inadequate Isolation
4. Unsecured Container Interfaces
5. Inadequate Resource Limits
6. Container Image Vulnerabilities
7. Unprotected Sensitive Data
8. Insecure APIs and Orchestration Systems
9. Container Image Integrity
10. Insider Threats
Hacking time!
Let’s hack a real container, and get
access to all the source code!
1. Run this Command - docker run -d -p 3000:3000
rohitcoder/node-app:vuln
2. List all running containers - docker ps
3. Now pick one Process ID and use it to get into that
docker shell - docker exec -it 51fe99fe2780 bash
4. Now Explore this container, as you explore a normal
disk!
Full root
access!
Source Code
Disclosure
How to fix Privilege Escalation?
RUN groupadd -r guests && useradd -r -g guests rohit
Full root
access!
Source Code
Disclosure
Still not a proper fix! What if attacker knows the root
password?
/usr/sbin/nologin is specifically designed to replace a shell and produces output complaining you can't
log-in
Now, no one can login as root, Secured!
Full root
access!
Running container in privilege mode vs restricted
mode Full root
access!
This gives full access to the host system / linux kernel, bypassing almost all security checks and
implementation enforced by docker. (It removes isolation capabilities)
What if, i Still want to use --privileged flag, but
i’m afraid of getting hacked? Full root
access!
Inter-Container Communication
Let’s run a container and see it’s network details
https://github.com/returntocorp/semgrep-rul
es/tree/develop/dockerfile
Let’s Install semgrep
Shift left - Security in SDLC (SAST) Continuous image scanning Use Trusted Image Providers
Misconfigurations in Kubernetes
Updating base image and installed
deployment / applications
Runtime Container Events logging softwares regularly
Q&A Time