Bypass apiserver proxy filter
The Kubernetes apiserver validated a node's hostname with one DNS lookup but then used a second lookup when proxying the request, so a custom DNS server could return a safe address first and an internal address second. An attacker who could create a node and send requests through the apiserver proxy could reach cloud metadata endpoints or localhost services.
Originally disclosed on HackerOne
- Triaged in 10 days
- Fixed in 12 months
- Paid in 15 days
- Disclosed after 13 months
Summary by the researcher
TL,DR: Time-of-check (apiserver proxy filter) Time-of-use (apiserver proxy request) Race Condition.
The report
Report Submission Form
Summary:
TL,DR: Time-of-check (apiserver proxy filter) Time-of-use (apiserver proxy request) Race Condition.
When the apiserver is proxying a request to a node though one of its addresses, it performs a filter validation. If the address type is a DNS record (Hostname, ExternalDNS, InternalDNS), the apiserver performs two DNS queries, one for filter validation, another for proxying the request. If the attacker sets the hostname to a custom DNS server, that is able return different values with zero TTL, it is possible to bypass that filter.
Kubernetes Version:
1.18.0
Steps To Reproduce:
Preparation
- Create a server with port UDP/53 (DNS) exposed.
- Delegate a subdomain (e.g. fake.example.com) to the IP address of the server as a NS record.
- Copy, build and run the custom DNS server (source code attached, use
go mod initandgo mod tidy). - Test that it successfully resolves any subdomain of the delegated zone (e.g. toctou.fake.example.com).
Execution
- Create a KIND cluster (manifest attached, defaults to v1.17).
- Create a Node (manifest attached, edit to change domain).
- Start
kubectl proxy. - Execute
curl localhost:8001/api/v1/nodes/toctou:80/proxy/.
Supporting Material/References:
toctou-cluster.yml, a KIND cluster manifesttoctou-node.yml, a Node with a custom address.toctou-dns-server.go, a custom DNS server that returns different results to consecutive queries.
Impact
https://github.com/kubernetes/kubernetes/pull/71980 was merged to mitigate dangerous proxying through the apiserver. An attacker with access to create nodes and send requests to them through apiserver proxy, could access cloud metadata endpoints or localhost services. This is specially important on as a service providers like https://github.com/oneinfra/oneinfra but could affect any vendor.
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
The apiserver performed a time-of-check DNS lookup for filter validation and a separate time-of-use DNS lookup for the proxy request, allowing a custom DNS server to return different answers.
Impact
An attacker with permission to create nodes and send requests through the apiserver proxy could access cloud metadata endpoints or localhost services.
What the attacker needed
Ability to create nodes; ability to send requests through apiserver proxy; control of a DNS server for a delegated subdomain
The fix
A pull request was merged to mitigate dangerous proxying through the apiserver.
How it was found
- Set up a DNS server on UDP port 53.
- Delegate a subdomain to that server with an NS record.
- Run a custom DNS server that returns different answers to consecutive queries.
- Create a KIND cluster and a Node whose address uses the delegated hostname.
- Start kubectl proxy.
- Send a request through the apiserver proxy to the node's hostname.
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.
When Kubernetes forwards a request to a node, it first checks the node's address to make sure it is not pointing at something internal. The report shows that check and the actual forwarding used two separate DNS lookups, so a malicious DNS server could give a harmless answer to the check and an internal address to the forwarding. That let an attacker reach services that should not be reachable from outside.
What is SSRF?
The server can be made to fetch an address the attacker chooses, reaching internal services, cloud metadata or other customers' data.