v1 Instance Metadata Service protections bypass
The Google Cloud instance metadata service required a special header to protect against SSRF, but this header check could be bypassed by adding an extra slash in the URL or by sending an HTTP/1.0 request. An attacker who can make requests to the metadata service could then access sensitive instance metadata, such as service account credentials.
Published on lf.lc
The Instance Metadata Service has two mitigations to help protect applications that are vulnerable to SSRF. Namely, the Metadata-Flavor: Google header is required and requests with the X-Forwarded-For header are ignored.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
The target here is the Google Cloud instance metadata service, reachable from inside a GCP instance at the link-local address [redacted]. Applications running on an instance can query it to obtain information about the instance and, more importantly, service account credentials. Because of that, the metadata service is a classic target for server-side request forgery (SSRF): if an attacker can make an application issue a request to the metadata address, they may be able to read credentials that the application itself is entitled to.
To reduce that risk, the metadata service has two mitigations aimed at applications vulnerable to SSRF:
- Requests must carry the
Metadata-Flavor: Googleheader. - Requests that include an
X-Forwarded-Forheader are ignored.
The research described here found three ways to bypass the first of those protections. The second is not bypassed in this report; it is mentioned only as the other existing mitigation.
Setup
The work came out of a penetration test for a client using GCP. The client's application had a specific SSRF vulnerability that allowed requests to an arbitrary host with a partially controlled URL, of the form http://xxx:xx/xxx/yyy, where the x parts are attacker-controlled and the y parts are hardcoded. That partial control is the precondition that makes the third bypass below interesting; the first two bypasses can be demonstrated directly against the metadata service from an instance.
The demonstrations use curl and nc against [redacted].
Finding it
The starting point is the Metadata-Flavor: Google requirement. If that header can be dropped while the request still reaches the metadata content, then any SSRF primitive that cannot set custom headers, which is the common case, becomes enough to read metadata. The researcher found that the metadata service's handling of certain URL sequences lets a request through without the header, and that the same sequences interact with partially controlled URLs in a useful way for an attacker.
Exploitation
Step 1: extra slash bypasses the header requirement
Including an extra / in the URL bypasses the Metadata-Flavor: Google requirement:
curl 'http://[redacted]/computeMetadata//v1/instance/name'
The path contains // after computeMetadata instead of the usual single /. The request bypasses the Metadata-Flavor: Google requirement without the header being supplied.
Step 2: HTTP/1.0 request dumps all metadata
Sending an HTTP/1.0 request also bypasses the Metadata-Flavor: Google requirement, and, as the researcher puts it, strangely dumps all metadata content:
printf "GET / HTTP/1.0\r\n\r\n" | nc [redacted] 80
This sends a bare GET / with HTTP/1.0 and no headers at all over a raw TCP connection to port 80. The researcher notes that this strangely dumps all metadata content. The researcher notes that they have not found a scenario where this would come up in practice, but it is another example of bypassing the Metadata-Flavor requirement.
Step 3: semicolon truncates the path
When an attacker does not fully control a URL, a ; in the URL causes all content following the semicolon to be ignored:
curl -H 'Metadata-Flavor: Google' 'http://[redacted]/computeMetadata/v1/instance/name;extra/content'
Here the header is supplied, so this demonstrates the semicolon behaviour on its own: everything after the semicolon is ignored.
Step 4: combining the bypasses against the client's SSRF
The client's SSRF allowed a request to an arbitrary host with a partially controlled URL, where the attacker controls the beginning and the application appends hardcoded data at the end. The following URL bypassed both the Metadata-Flavor requirement and, specific to that vulnerability, the hardcoded data at the end of the URL:
http://[redacted]/computeMetadata//v1/instance/name;yyy
The // handles the missing Metadata-Flavor header (as in step 1), and the ; causes the hardcoded yyy suffix to be ignored (as in step 3). The researcher attributes this to the way the metadata service handled both the // and ; sequences in URLs.
Root cause
The report describes the observed behaviour but does not explain the cause. A doubled slash after computeMetadata still resolved to the metadata endpoint without the required Metadata-Flavor header, and a semicolon caused the remainder of the path to be dropped. The HTTP/1.0 behaviour is a third example of bypassing the Metadata-Flavor requirement. The report describes the observed behaviour but does not give the implementation detail behind it.
Impact
The attack scenario in the report is an attacker exploiting an SSRF vulnerability in an application running in GCP, with service account credentials from the metadata service as the target. The outcome is compromise of those credentials when the metadata service would normally protect them. The practical reach depends on the SSRF primitive: the first two bypasses matter where an attacker can reach the metadata address but cannot set the Metadata-Flavor header, and the third matters where the URL is only partially controlled and has a hardcoded suffix. The report frames the exposure as applying in specific scenarios rather than universally.
Fix
The issue was fixed by Google. The report does not describe the change that was made. The timeline is:
| Date | Event |
|---|---|
| 2019-06-14 | Issue reported to Google VRP |
| 2019-06-17 | Issue triaged |
| 2019-06-19 | Internal bug report filed |
| 2019-07-22 | Issue fixed |
| 2019-07-23 | VRP issued reward ($5000) |
Variants to test
When probing a metadata service or a similar protected internal endpoint behind an SSRF, try the following, each with the header requirement removed where the primitive allows it:
- Insert a doubled slash in the path, for example between a fixed prefix and the rest of the path. A positive result is the endpoint returning content that should require the custom header.
- Send a minimal HTTP/1.0 request with no headers over a raw connection. A positive result is metadata content coming back without the header.
- Append a semicolon and extra path segments to a URL whose suffix is fixed by the application. A positive result is the fixed suffix being ignored and the intended path being served.
- Combine the doubled slash and the semicolon in a single URL against a partially controlled SSRF. A positive result is both the header requirement and the hardcoded suffix being bypassed at once.
What happened, in plain words
Google Cloud lets you rent computers in its data centres. Each of those rented computers is called an instance. Every instance has a small built-in helper service that the programs running on it can ask questions about itself: what is my name, what network am I on, and, most importantly, what are my login credentials for other Google Cloud services. That helper is the instance metadata service, and it lives at a special address, [redacted], that only the instance itself can reach.
Because those credentials are so valuable, the metadata service is a favourite target for attackers. The usual way in is a bug called server-side request forgery (SSRF): a website takes a URL from a user and fetches it on the server's behalf. If the attacker can make the server fetch http://[redacted]/..., the server will happily hand back its own credentials.
Google added two protections to make that harder. First, any request to the metadata service must carry a special label, an HTTP header called Metadata-Flavor: Google. Second, requests that carry an X-Forwarded-For header are ignored. The research described here found three ways around the first protection. The second protection is not bypassed in this report; it is only mentioned as the other existing safeguard.
The researcher, Anthony Weems, found these bypasses during a penetration test for a client using Google Cloud. The client's application had an SSRF bug where the attacker controlled only part of the URL: the beginning was attacker-controlled and the end was fixed by the application. That partial control is what made the third bypass interesting.
Context
The target is the Google Cloud instance metadata service, reachable from inside a Google Cloud instance at the link-local address [redacted]. A link-local address is an address that only works on the local network segment, so it is not reachable from the internet; only the instance itself can talk to it. Applications running on an instance can query the metadata service to learn about the instance and, more importantly, to obtain service account credentials. A service account is a non-human identity that a program uses to log in to Google Cloud services, and its credentials are the secret keys that prove that identity.
Because of that, the metadata service is a classic target for server-side request forgery. If an attacker can make an application issue a request to the metadata address, they may be able to read credentials that the application itself is entitled to. In other words, the attacker does not need to steal a password; they borrow the application's own access.
To reduce that risk, the metadata service has two mitigations aimed at applications vulnerable to SSRF:
- Requests must carry the
Metadata-Flavor: Googleheader. - Requests that include an
X-Forwarded-Forheader are ignored.
A header is a small piece of extra information sent alongside an HTTP request or response, like the label on a parcel that tells the postal service how to handle it. Metadata-Flavor: Google is a custom label that the metadata service checks before answering. X-Forwarded-For is a header that proxies add to record the original client's address; the metadata service ignores requests that carry it, presumably because a proxy in front of an SSRF could add it.
The research described here found three ways to bypass the first of those protections. The second is not bypassed in this report; it is mentioned only as the other existing mitigation.
Setup
The work came out of a penetration test for a client using Google Cloud. A penetration test is an authorised security assessment where a tester tries to break into a system, with permission, to find weaknesses before real attackers do. The client's application had a specific SSRF vulnerability that allowed requests to an arbitrary host with a partially controlled URL, of the form http://xxx:xx/xxx/yyy, where the x parts are attacker-controlled and the y parts are hardcoded. That partial control is the precondition that makes the third bypass below interesting; the first two bypasses can be demonstrated directly against the metadata service from an instance.
The demonstrations use curl and nc against [redacted]. curl is a command-line tool for making HTTP requests, and nc (netcat) is a tool for opening raw network connections and sending bytes by hand. Both are run from a terminal on the instance.
Finding it
The starting point is the Metadata-Flavor: Google requirement. If that header can be dropped while the request still reaches the metadata content, then any SSRF primitive that cannot set custom headers, which is the common case, becomes enough to read metadata. An SSRF primitive is the specific ability an attacker has to make the server fetch a URL; many such primitives let the attacker choose the URL but not add headers. The researcher found that the metadata service's handling of certain URL sequences lets a request through without the header, and that the same sequences interact with partially controlled URLs in a useful way for an attacker.
Exploitation
Step 1: extra slash bypasses the header requirement
Including an extra / in the URL bypasses the Metadata-Flavor: Google requirement:
curl 'http://[redacted]/computeMetadata//v1/instance/name'
Line by line: curl is the tool making the request. The single-quoted string is the URL. http:// says to use plain HTTP. [redacted] is the metadata service address. /computeMetadata//v1/instance/name is the path being requested, and the interesting part is the doubled slash // after computeMetadata instead of the usual single /. The request bypasses the Metadata-Flavor: Google requirement without the header being supplied.
Step 2: HTTP/1.0 request dumps all metadata
Sending an HTTP/1.0 request also bypasses the Metadata-Flavor: Google requirement, and, as the researcher puts it, strangely dumps all metadata content:
printf "GET / HTTP/1.0\r\n\r\n" | nc [redacted] 80
Line by line: printf prints the text between the quotes. GET / HTTP/1.0 is a minimal HTTP request line: the method GET, the path /, and the protocol version HTTP/1.0. \r\n\r\n is the carriage-return and line-feed pair that ends the request line and then ends the headers section; with nothing in between, the request has no headers at all. The | pipe sends that text into nc, which opens a raw TCP connection to [redacted] on port 80 and writes the text into it. Port 80 is the standard HTTP port. This sends a bare GET / with HTTP/1.0 and no headers at all over a raw TCP connection to port 80. The researcher notes that this strangely dumps all metadata content. The researcher notes that they have not found a scenario where this would come up in practice, but it is another example of bypassing the Metadata-Flavor requirement.
Step 3: semicolon truncates the path
When an attacker does not fully control a URL, a ; in the URL causes all content following the semicolon to be ignored:
curl -H 'Metadata-Flavor: Google' 'http://[redacted]/computeMetadata/v1/instance/name;extra/content'
Line by line: curl makes the request. -H 'Metadata-Flavor: Google' adds the required header, so this test is not about the header at all. The URL is http://[redacted]/computeMetadata/v1/instance/name;extra/content, where the path ends with name;extra/content. Here the header is supplied, so this demonstrates the semicolon behaviour on its own: everything after the semicolon is ignored.
Step 4: combining the bypasses against the client's SSRF
The client's SSRF allowed a request to an arbitrary host with a partially controlled URL, where the attacker controls the beginning and the application appends hardcoded data at the end. The following URL bypassed both the Metadata-Flavor requirement and, specific to that vulnerability, the hardcoded data at the end of the URL:
http://[redacted]/computeMetadata//v1/instance/name;yyy
Line by line: http://[redacted] is the metadata address. /computeMetadata//v1/instance/name is the path the attacker wants, with the doubled slash from step 1. ;yyy is the hardcoded suffix the application appends, which the attacker cannot remove. The // handles the missing Metadata-Flavor header (as in step 1), and the ; causes the hardcoded yyy suffix to be ignored (as in step 3). The researcher attributes this to the way the metadata service handled both the // and ; sequences in URLs.
Root cause
The report describes the observed behaviour but does not explain the cause. A doubled slash after computeMetadata still resolved to the metadata endpoint without the required Metadata-Flavor header, and a semicolon caused the remainder of the path to be dropped. The HTTP/1.0 behaviour is a third example of bypassing the Metadata-Flavor requirement. The report describes the observed behaviour but does not give the implementation detail behind it.
Impact
The attack scenario in the report is an attacker exploiting an SSRF vulnerability in an application running in Google Cloud, with service account credentials from the metadata service as the target. The outcome is compromise of those credentials when the metadata service would normally protect them. The practical reach depends on the SSRF primitive: the first two bypasses matter where an attacker can reach the metadata address but cannot set the Metadata-Flavor header, and the third matters where the URL is only partially controlled and has a hardcoded suffix. The report frames the exposure as applying in specific scenarios rather than universally.
Fix
The issue was fixed by Google. The report does not describe the change that was made. The timeline is:
| Date | Event |
|---|---|
| 2019-06-14 | Issue reported to Google VRP |
| 2019-06-17 | Issue triaged |
| 2019-06-19 | Internal bug report filed |
| 2019-07-22 | Issue fixed |
| 2019-07-23 | VRP issued reward ($5000) |
Triaged means the security team looked at the report and confirmed it was worth investigating. A VRP is a Vulnerability Reward Program, Google's bug bounty scheme that pays researchers for reporting security issues.
Variants to test
When probing a metadata service or a similar protected internal endpoint behind an SSRF, try the following, each with the header requirement removed where the primitive allows it:
- Insert a doubled slash in the path, for example between a fixed prefix and the rest of the path. A positive result is the endpoint returning content that should require the custom header.
- Send a minimal HTTP/1.0 request with no headers over a raw connection. A positive result is metadata content coming back without the header.
- Append a semicolon and extra path segments to a URL whose suffix is fixed by the application. A positive result is the fixed suffix being ignored and the intended path being served.
- Combine the doubled slash and the semicolon in a single URL against a partially controlled SSRF. A positive result is both the header requirement and the hardcoded suffix being bypassed at once.
Glossary
- Instance: a single rented virtual computer running in Google Cloud.
- Instance metadata service: a small helper service on each instance, at
[redacted], that answers questions about the instance and hands out its credentials. - Link-local address: an address that only works on the local network segment and is not reachable from the internet.
- Service account: a non-human identity that a program uses to log in to cloud services.
- Credentials: the secret keys that prove an identity, such as a service account's keys.
- SSRF (server-side request forgery): a bug where an attacker makes a server fetch a URL of the attacker's choosing.
- SSRF primitive: the specific ability an attacker has to make a server fetch a URL, including what parts of the URL they control.
- HTTP header: a small piece of extra information sent with an HTTP request or response.
Metadata-Flavor: Google: the custom header the metadata service requires before answering.X-Forwarded-For: a header proxies add to record the original client's address; the metadata service ignores requests that carry it.curl: a command-line tool for making HTTP requests.nc(netcat): a tool for opening raw network connections and sending bytes by hand.- TCP: the underlying network protocol that carries HTTP connections.
- Port 80: the standard network port for plain HTTP.
- HTTP/1.0: an older version of the HTTP protocol; a request can be sent with no headers at all.
- Path: the part of a URL after the host, such as
/computeMetadata/v1/instance/name. - Penetration test: an authorised security assessment where a tester tries to break into a system, with permission, to find weaknesses.
- Triaged: reviewed and confirmed as worth investigating by the security team.
- VRP (Vulnerability Reward Program): Google's bug bounty scheme that pays researchers for reporting security issues.