hacktricks/network-services-pentesting/pentesting-web/special-http-headers.md

229 lines
12 KiB
Markdown
Raw Normal View History

2022-06-27 16:53:32 +00:00
# Special HTTP headers
2022-04-28 16:01:33 +00:00
2024-07-19 09:08:05 +00:00
{% hint style="success" %}
Learn & practice AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
Learn & practice GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
2022-04-28 16:01:33 +00:00
2024-07-19 09:08:05 +00:00
<details>
2022-04-28 16:01:33 +00:00
2024-07-19 09:08:05 +00:00
<summary>Support HackTricks</summary>
2024-02-03 12:22:53 +00:00
2024-07-19 09:08:05 +00:00
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-04-28 16:01:33 +00:00
</details>
2024-07-19 09:08:05 +00:00
{% endhint %}
2022-04-28 16:01:33 +00:00
## Wordlists & Tools
2021-09-19 15:52:48 +00:00
2024-07-18 16:03:34 +00:00
* [https://github.com/danielmiessler/SecLists/tree/master/Miscellaneous/Web/http-request-headers](https://github.com/danielmiessler/SecLists/tree/master/Miscellaneous/Web/http-request-headers)
* [https://github.com/rfc-st/humble](https://github.com/rfc-st/humble)
2021-09-19 15:52:48 +00:00
2022-06-27 16:53:32 +00:00
## Headers to Change Location
2021-09-19 15:19:00 +00:00
Rewrite **IP source**:
2022-04-05 22:24:52 +00:00
* `X-Originating-IP: 127.0.0.1`
2021-09-19 15:52:48 +00:00
* `X-Forwarded-For: 127.0.0.1`
2022-04-19 22:38:50 +00:00
* `X-Forwarded: 127.0.0.1`
2021-09-19 15:52:48 +00:00
* `Forwarded-For: 127.0.0.1`
2021-09-27 10:58:30 +00:00
* `X-Forwarded-Host: 127.0.0.1`
2022-04-05 22:24:52 +00:00
* `X-Remote-IP: 127.0.0.1`
2021-09-19 15:19:00 +00:00
* `X-Remote-Addr: 127.0.0.1`
* `X-ProxyUser-Ip: 127.0.0.1`
* `X-Original-URL: 127.0.0.1`
2021-09-19 15:52:48 +00:00
* `Client-IP: 127.0.0.1`
2021-09-27 10:58:30 +00:00
* `X-Client-IP: 127.0.0.1`
* `X-Host: 127.0.0.1`
2021-09-19 15:52:48 +00:00
* `True-Client-IP: 127.0.0.1`
* `Cluster-Client-IP: 127.0.0.1`
2021-09-20 13:06:32 +00:00
* `Via: 1.0 fred, 1.1 127.0.0.1`
* `Connection: close, X-Forwarded-For` (Check hop-by-hop headers)
2021-09-19 15:19:00 +00:00
Rewrite **location**:
* `X-Original-URL: /admin/console`
* `X-Rewrite-URL: /admin/console`
2022-06-27 16:53:32 +00:00
## Hop-by-Hop headers
2021-09-19 15:19:00 +00:00
A hop-by-hop header is a header which is designed to be processed and consumed by the proxy currently handling the request, as opposed to an end-to-end header.
* `Connection: close, X-Forwarded-For`
{% content-ref url="../../pentesting-web/abusing-hop-by-hop-headers.md" %}
[abusing-hop-by-hop-headers.md](../../pentesting-web/abusing-hop-by-hop-headers.md)
{% endcontent-ref %}
2021-09-19 15:19:00 +00:00
2022-06-27 16:53:32 +00:00
## HTTP Request Smuggling
2021-09-19 15:19:00 +00:00
* `Content-Length: 30`
* `Transfer-Encoding: chunked`
2021-11-05 20:59:42 +00:00
{% content-ref url="../../pentesting-web/http-request-smuggling/" %}
[http-request-smuggling](../../pentesting-web/http-request-smuggling/)
{% endcontent-ref %}
2021-09-19 15:19:00 +00:00
2022-06-27 16:53:32 +00:00
## Cache Headers
2021-09-19 15:52:48 +00:00
2021-09-20 13:06:32 +00:00
**Server Cache Headers**:
2021-09-19 15:52:48 +00:00
* **`X-Cache`** in the response may have the value **`miss`** when the request wasn't cached and the value **`hit`** when it is cached
* Similar behaviour in the header **`Cf-Cache-Status`**
2021-09-19 15:52:48 +00:00
* **`Cache-Control`** indicates if a resource is being cached and when will be the next time the resource will be cached again: `Cache-Control: public, max-age=1800`
2022-04-05 22:24:52 +00:00
* **`Vary`** is often used in the response to **indicate additional headers** that are treated as **part of the cache key** even if they are normally unkeyed.
2021-09-19 15:52:48 +00:00
* **`Age`** defines the times in seconds the object has been in the proxy cache.
2022-08-12 16:57:56 +00:00
* **`Server-Timing: cdn-cache; desc=HIT`** also indicates that a resource was cached
2021-09-19 15:52:48 +00:00
2024-04-10 13:29:30 +00:00
{% content-ref url="../../pentesting-web/cache-deception/" %}
[cache-deception](../../pentesting-web/cache-deception/)
{% endcontent-ref %}
2021-09-19 15:52:48 +00:00
2021-09-20 13:06:32 +00:00
**Local Cache headers**:
* `Clear-Site-Data`: Header to indicate the cache that should be removed: `Clear-Site-Data: "cache", "cookies"`
* `Expires`: Contains date/time when the response should expire: `Expires: Wed, 21 Oct 2015 07:28:00 GMT`
* `Pragma: no-cache` same as `Cache-Control: no-cache`
* `Warning`: The **`Warning`** general HTTP header contains information about possible problems with the status of the message. More than one `Warning` header may appear in a response. `Warning: 110 anderson/1.3.37 "Response is stale"`
2022-06-27 16:53:32 +00:00
## Conditionals
2021-09-19 15:19:00 +00:00
2022-04-19 22:38:50 +00:00
* Requests using these headers: **`If-Modified-Since`** and **`If-Unmodified-Since`** will be responded with data only if the response header\*\*`Last-Modified`\*\* contains a different time.
* Conditional requests using **`If-Match`** and **`If-None-Match`** use an Etag value so the web server will send the content of the response if the data (Etag) has changed. The `Etag` is taken from the HTTP response.
2021-11-30 16:46:07 +00:00
* The **Etag** value is usually **calculated based** on the **content** of the response. For example, `ETag: W/"37-eL2g8DEyqntYlaLp5XLInBWsjWI"` indicates that the `Etag` is the **Sha1** of **37 bytes**.
2021-09-19 19:55:40 +00:00
2022-06-27 16:53:32 +00:00
## Range requests
2021-09-19 19:55:40 +00:00
2021-09-20 13:06:32 +00:00
* **`Accept-Ranges`**: Indicates if the server supports range requests, and if so in which unit the range can be expressed. `Accept-Ranges: <range-unit>`
* **`Range`**: Indicates the part of a document that the server should return.
* **`If-Range`**: Creates a conditional range request that is only fulfilled if the given etag or date matches the remote resource. Used to prevent downloading two ranges from incompatible version of the resource.
* **`Content-Range`**: Indicates where in a full body message a partial message belongs.
2021-09-19 19:55:40 +00:00
2022-06-27 16:53:32 +00:00
## Message body information
2021-09-19 19:55:40 +00:00
2021-11-30 16:46:07 +00:00
* **`Content-Length`:** The size of the resource, in decimal number of bytes.
2021-09-20 13:06:32 +00:00
* **`Content-Type`**: Indicates the media type of the resource
* **`Content-Encoding`**: Used to specify the compression algorithm.
* **`Content-Language`**: Describes the human language(s) intended for the audience, so that it allows a user to differentiate according to the users' own preferred language.
2021-09-20 13:06:32 +00:00
* **`Content-Location`**: Indicates an alternate location for the returned data.
2021-09-19 19:55:40 +00:00
2022-04-19 22:38:50 +00:00
From a pentest point of view this information is usually "useless", but if the resource is **protected** by a 401 or 403 and you can find some **way** to **get** this **info**, this could be **interesting.**\
2022-04-05 22:24:52 +00:00
For example a combination of **`Range`** and **`Etag`** in a HEAD request can leak the content of the page via HEAD requests:
2021-09-19 19:55:40 +00:00
* A request with the header `Range: bytes=20-20` and with a response containing `ETag: W/"1-eoGvPlkaxxP4HqHv6T3PNhV9g3Y"` is leaking that the SHA1 of the byte 20 is `ETag: eoGvPlkaxxP4HqHv6T3PNhV9g3Y`
2022-06-27 16:53:32 +00:00
## Server Info
2021-09-20 13:06:32 +00:00
* `Server: Apache/2.4.1 (Unix)`
* `X-Powered-By: PHP/5.3.3`
2022-06-27 16:53:32 +00:00
## Controls
2021-09-20 13:06:32 +00:00
2024-02-08 21:36:15 +00:00
* **`Allow`**: This header is used to communicate the HTTP methods a resource can handle. For example, it might be specified as `Allow: GET, POST, HEAD`, indicating that the resource supports these methods.
* **`Expect`**: Utilized by the client to convey expectations that the server needs to meet for the request to be processed successfully. A common use case involves the `Expect: 100-continue` header, which signals that the client intends to send a large data payload. The client looks for a `100 (Continue)` response before proceeding with the transmission. This mechanism helps in optimizing network usage by awaiting server confirmation.
2021-09-20 13:06:32 +00:00
2022-06-27 16:53:32 +00:00
## Downloads
2021-09-20 13:06:32 +00:00
2024-02-08 21:36:15 +00:00
* The **`Content-Disposition`** header in HTTP responses directs whether a file should be displayed **inline** (within the webpage) or treated as an **attachment** (downloaded). For instance:
2024-02-08 21:36:15 +00:00
```
Content-Disposition: attachment; filename="filename.jpg"
```
2024-02-08 21:36:15 +00:00
This means the file named "filename.jpg" is intended to be downloaded and saved.
2021-09-20 13:06:32 +00:00
2022-06-27 16:53:32 +00:00
## Security Headers
2022-06-27 16:53:32 +00:00
### Content Security Policy (CSP) <a href="#csp" id="csp"></a>
2022-04-19 22:38:50 +00:00
{% content-ref url="../../pentesting-web/content-security-policy-csp-bypass/" %}
[content-security-policy-csp-bypass](../../pentesting-web/content-security-policy-csp-bypass/)
{% endcontent-ref %}
2024-02-08 21:36:15 +00:00
### **Trusted Types**
2024-02-08 21:36:15 +00:00
By enforcing Trusted Types through CSP, applications can be protected against DOM XSS attacks. Trusted Types ensure that only specifically crafted objects, compliant with established security policies, can be used in dangerous web API calls, thereby securing JavaScript code by default.
```javascript
2021-10-19 22:49:43 +00:00
// Feature detection
if (window.trustedTypes && trustedTypes.createPolicy) {
// Name and create a policy
const policy = trustedTypes.createPolicy('escapePolicy', {
2024-02-08 21:36:15 +00:00
createHTML: str => str.replace(/\</g, '&lt;').replace(/>/g, '&gt;');
2021-10-19 22:49:43 +00:00
});
}
```
```javascript
2024-02-08 21:36:15 +00:00
// Assignment of raw strings is blocked, ensuring safety.
el.innerHTML = 'some string'; // Throws an exception.
2021-10-19 22:49:43 +00:00
const escaped = policy.createHTML('<img src=x onerror=alert(1)>');
2024-02-08 21:36:15 +00:00
el.innerHTML = escaped; // Results in safe assignment.
2021-10-19 22:49:43 +00:00
```
2024-02-08 21:36:15 +00:00
### **X-Content-Type-Options**
2021-10-19 22:49:43 +00:00
2024-02-08 21:36:15 +00:00
This header prevents MIME type sniffing, a practice that could lead to XSS vulnerabilities. It ensures that browsers respect the MIME types specified by the server.
2021-10-19 22:49:43 +00:00
2024-02-08 21:36:15 +00:00
```
2021-10-19 22:49:43 +00:00
X-Content-Type-Options: nosniff
```
2024-02-08 21:36:15 +00:00
### **X-Frame-Options**
2021-10-19 22:49:43 +00:00
2024-02-08 21:36:15 +00:00
To combat clickjacking, this header restricts how documents can be embedded in `<frame>`, `<iframe>`, `<embed>`, or `<object>` tags, recommending all documents to specify their embedding permissions explicitly.
2021-10-19 22:49:43 +00:00
2024-02-08 21:36:15 +00:00
```
2021-10-19 22:49:43 +00:00
X-Frame-Options: DENY
```
2024-02-08 21:36:15 +00:00
### **Cross-Origin Resource Policy (CORP) and Cross-Origin Resource Sharing (CORS)**
2021-10-19 22:49:43 +00:00
2024-02-08 21:36:15 +00:00
CORP is crucial for specifying which resources can be loaded by websites, mitigating cross-site leaks. CORS, on the other hand, allows for a more flexible cross-origin resource sharing mechanism, relaxing the same-origin policy under certain conditions.
2021-10-19 22:49:43 +00:00
```
Cross-Origin-Resource-Policy: same-origin
Access-Control-Allow-Origin: https://example.com
Access-Control-Allow-Credentials: true
```
2024-02-08 21:36:15 +00:00
### **Cross-Origin Embedder Policy (COEP) and Cross-Origin Opener Policy (COOP)**
2021-10-19 22:49:43 +00:00
2024-02-08 21:36:15 +00:00
COEP and COOP are essential for enabling cross-origin isolation, significantly reducing the risk of Spectre-like attacks. They control the loading of cross-origin resources and the interaction with cross-origin windows, respectively.
2021-10-19 22:49:43 +00:00
2024-02-08 21:36:15 +00:00
```
2021-10-19 22:49:43 +00:00
Cross-Origin-Embedder-Policy: require-corp
2024-02-08 21:36:15 +00:00
Cross-Origin-Opener-Policy: same-origin-allow-popups
2021-10-19 22:49:43 +00:00
```
2024-02-08 21:36:15 +00:00
### **HTTP Strict Transport Security (HSTS)**
2021-10-19 22:49:43 +00:00
2024-02-08 21:36:15 +00:00
Lastly, HSTS is a security feature that forces browsers to communicate with servers only over secure HTTPS connections, thereby enhancing privacy and security.
2021-10-19 22:49:43 +00:00
2024-02-08 21:36:15 +00:00
```
2021-10-19 22:49:43 +00:00
Strict-Transport-Security: max-age=3153600
```
2024-02-08 21:36:15 +00:00
## References
2021-09-19 19:55:40 +00:00
2024-02-08 21:36:15 +00:00
* [https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition)
2021-09-19 19:55:40 +00:00
* [https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers)
2021-10-19 22:49:43 +00:00
* [https://web.dev/security-headers/](https://web.dev/security-headers/)
2024-02-08 21:36:15 +00:00
* [https://web.dev/articles/security-headers](https://web.dev/articles/security-headers)
2022-04-28 16:01:33 +00:00
2024-07-19 09:08:05 +00:00
{% hint style="success" %}
Learn & practice AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
Learn & practice GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
2022-04-28 16:01:33 +00:00
2024-07-19 09:08:05 +00:00
<details>
2022-04-28 16:01:33 +00:00
2024-07-19 09:08:05 +00:00
<summary>Support HackTricks</summary>
2024-02-03 12:22:53 +00:00
2024-07-19 09:08:05 +00:00
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-04-28 16:01:33 +00:00
</details>
2024-07-19 09:08:05 +00:00
{% endhint %}