-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdockerleaks
More file actions
188 lines (54 loc) · 3.95 KB
/
Copy pathdockerleaks
File metadata and controls
188 lines (54 loc) · 3.95 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
Accidentally Making the Docker Image Public
Common Mistake:
A developer might accidentally push a private image to a public Docker Hub repo (e.g., by forgetting to use --private when pushing).
docker tag my-private-image mydockerhubuser/myrepo:latest
docker push mydockerhubuser/myrepo:latest # If repo is public, it's exposed!
Risk: If the image contains hardcoded API keys, credentials, or secrets, they become public immediately.
Extracting Layers from a Private Image
Even if an image is private, if an attacker gains access to it, they can extract sensitive information by inspecting the Docker image layers.
If secrets were added in an early layer (RUN echo "SECRET_KEY=xyz123"), it remains in the image even if it's later removed.
🛑 How to Avoid This?
Use .dockerignore to exclude sensitive files from being copied into the image.
Avoid ADD or COPY secrets into the image.
Use build-time secrets instead of hardcoding them.
Docker Hub Credentials Leaked → Private Images Can Be Pulled
Attackers can steal credentials from misconfigured CI/CD pipelines or leaked .docker/config.json files.
If an attacker gets read access to a private Docker registry, they can:
Pull the private image (docker pull mycompany/private-app).
Inspect layers for secrets (docker history or docker run --rm -it mycompany/private-app /bin/sh).
🔎 Example: Leaked Docker Login File
If the ~/.docker/config.json file is publicly readable, an attacker can extract:
How to Avoid This?
Never store credentials in plain text.
Use environment variables (DOCKER_PASSWORD, DOCKER_CONFIG) in CI/CD.
Enable 2FA (Two-Factor Authentication) for Docker Hub accounts.
Conclusion
Even if an image is private, hardcoded secrets can still leak due to: 1️⃣ Accidental public uploads.
2️⃣ Extracting secrets from Docker image layers.
3️⃣ Leaked credentials to pull private images.
4️⃣ Secrets in ENV variables inside the Dockerfile.
5️⃣ Including .env files in the image.
✅ Best Practices to Prevent Leaks
✔ Use .dockerignore to exclude secret files.
✔ Pass secrets at runtime instead of hardcoding them.
✔ Use Docker secrets or a vault like AWS Secrets Manager.
✔ Check for secrets before pushing an image:
How to Prevent This? ✔ Use a .dockerignore file to prevent secret files from being copied:
.env
config/secrets.yml
private_key.pem
Summary: Complete List of Docker Secret Leaks
# Leak Source How It Happens? Fix
1️⃣ Public Docker Image Upload Pushing an image to a public registry accidentally. Use private repos and enable 2FA on Docker Hub.
2️⃣ Extracting Layers (docker history) Old secrets remain in cached layers even if removed. Use --no-cache when building images.
3️⃣ Leaked Docker Hub Credentials .docker/config.json can expose login tokens. Store credentials securely and enable 2FA.
4️⃣ Hardcoded ENV Variables ENV SECRET=xyz stores secrets inside image history. Use Docker Secrets instead of ENV.
5️⃣ Copying Secrets into Image (COPY . /) .env files get baked into image layers. Add .env to .dockerignore.
6️⃣ Build Context Leaks Local files copied into image unintentionally. Always check docker build --progress=plain.
7️⃣ Secrets in Base Images Using a compromised public image. Use trusted base images and scan with trivy.
8️⃣ Attacker Runs docker exec Extracts secrets from env variables in running containers. Avoid ENV secrets, use Vault instead.
9️⃣ Exposing Docker Daemon (/var/run/docker.sock) Allows full access to the host system. Never mount docker.sock in untrusted containers.
🔟 Secrets in Logs (docker logs) Logs expose database passwords. Use secure logging tools, never log secrets.
Always use .dockerignore to exclude sensitive files.
API Keys: Unique identifiers used to authenticate requests associated with your project or application.
Tokens: Security tokens (like OAuth tokens) that grant access to protected resources.