hacktricks/network-services-pentesting/pentesting-web/nginx.md

318 lines
17 KiB
Markdown
Raw Normal View History

2022-05-08 23:13:03 +00:00
# Nginx
2022-04-28 16:01:33 +00:00
<details>
<summary><a href="https://cloud.hacktricks.xyz/pentesting-cloud/pentesting-cloud-methodology"><strong>☁️ HackTricks Cloud ☁️</strong></a> -<a href="https://twitter.com/hacktricks_live"><strong>🐦 Twitter 🐦</strong></a> - <a href="https://www.twitch.tv/hacktricks_live/schedule"><strong>🎙️ Twitch 🎙️</strong></a> - <a href="https://www.youtube.com/@hacktricks_LIVE"><strong>🎥 Youtube 🎥</strong></a></summary>
2022-04-28 16:01:33 +00:00
* 你在一个**网络安全公司**工作吗你想在HackTricks中看到你的**公司广告**吗?或者你想获得**PEASS的最新版本或下载PDF格式的HackTricks**吗?请查看[**订阅计划**](https://github.com/sponsors/carlospolop)
* 发现我们的独家[NFT收藏品**The PEASS Family**](https://opensea.io/collection/the-peass-family)
2023-08-03 19:12:22 +00:00
* 获取[**官方PEASS和HackTricks周边产品**](https://peass.creator-spring.com)
* **加入**[**💬**](https://emojipedia.org/speech-balloon/) [**Discord群组**](https://discord.gg/hRep4RUj7f) 或者 [**Telegram群组**](https://t.me/peass) 或者**关注**我在**Twitter**上的[**🐦**](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md)[**@carlospolopm**](https://twitter.com/hacktricks\_live)**.**
2023-08-03 19:12:22 +00:00
* **通过向**[**hacktricks repo**](https://github.com/carlospolop/hacktricks) **和**[**hacktricks-cloud repo**](https://github.com/carlospolop/hacktricks-cloud) **提交PR来分享你的黑客技巧。**
2022-04-28 16:01:33 +00:00
</details>
2022-04-28 16:01:33 +00:00
<figure><img src="../../.gitbook/assets/image (1) (1) (2) (4).png" alt=""><figcaption></figcaption></figure>
2022-04-28 16:01:33 +00:00
[**DragonJAR Security Conference es un evento internacional de ciberseguridad**](https://www.dragonjarcon.org/) con más de una década que se celebrará el 7 y 8 de septiembre de 2023 en Bogotá, Colombia. Es un evento de gran contenido técnico donde se presentan las últimas investigaciones en español que atrae a hackers e investigadores de todo el mundo.\
¡Regístrate ahora en el siguiente enlace y no te pierdas esta gran conferencia!:
2022-04-28 16:01:33 +00:00
{% embed url="https://www.dragonjarcon.org/" %}
2022-04-28 16:01:33 +00:00
2023-08-03 19:12:22 +00:00
## 缺少根位置 <a href="#missing-root-location" id="missing-root-location"></a>
```
server {
2023-08-03 19:12:22 +00:00
root /etc/nginx;
2023-08-03 19:12:22 +00:00
location /hello.txt {
try_files $uri $uri/ =404;
proxy_pass http://127.0.0.1:8080/;
}
}
```
`root`指令指定了Nginx的根文件夹。在上面的示例中根文件夹是`/etc/nginx`,这意味着我们可以访问该文件夹中的文件。上面的配置没有针对`/location / {...}`的位置,只有针对`/hello.txt`的位置。因此,`root`指令将被全局设置,这意味着对`/`的请求将带您到本地路径`/etc/nginx`。
一个简单的请求,比如`GET /nginx.conf`,将显示存储在`/etc/nginx/nginx.conf`中的Nginx配置文件的内容。如果根目录设置为`/etc`,对`/nginx/nginx.conf`的`GET`请求将显示配置文件。在某些情况下可能会访问到其他配置文件、访问日志甚至是HTTP基本身份验证的加密凭据。
2023-08-03 19:12:22 +00:00
## 别名LFI配置错误 <a href="#alias-lfi-misconfiguration" id="alias-lfi-misconfiguration"></a>
在Nginx配置中查找“location”语句如果有类似以下的内容
```
2023-08-03 19:12:22 +00:00
location /imgs {
alias /path/images/;
}
```
存在LFI漏洞的原因是
```
/imgs../flag.txt
```
# Nginx
Nginx is a popular web server that is commonly used to serve static content, reverse proxy, and load balance web applications. It is known for its high performance, scalability, and ability to handle a large number of concurrent connections.
## Configuration Files
Nginx uses configuration files to define how it should handle incoming requests. The main configuration file is typically located at `/etc/nginx/nginx.conf` and includes other configuration files from the `/etc/nginx/conf.d/` directory.
## Virtual Hosts
Nginx uses virtual hosts to serve multiple websites on a single server. Each virtual host has its own configuration file, typically located in the `/etc/nginx/conf.d/` directory. Virtual hosts can be configured to listen on specific IP addresses and ports, and can also be configured with SSL/TLS certificates for secure connections.
## Reverse Proxy
Nginx can be used as a reverse proxy to forward requests to backend servers. This is commonly used to load balance traffic across multiple servers or to proxy requests to application servers running on different ports.
To configure Nginx as a reverse proxy, you can use the `proxy_pass` directive in the Nginx configuration file. This directive specifies the backend server to forward requests to.
## Load Balancing
Nginx can also be used as a load balancer to distribute incoming requests across multiple backend servers. This helps to distribute the load and improve the overall performance and availability of the web application.
Nginx supports various load balancing algorithms, such as round-robin, least connections, and IP hash. These algorithms determine how Nginx selects the backend server to forward each request to.
To configure Nginx as a load balancer, you can use the `upstream` and `server` directives in the Nginx configuration file. The `upstream` directive defines a group of backend servers, and the `server` directive specifies the IP address and port of each backend server.
## Security Considerations
When configuring Nginx, it is important to consider security best practices to protect against common web application vulnerabilities. Some important security considerations include:
- Enforcing secure communication using SSL/TLS certificates.
- Implementing access controls to restrict access to sensitive resources.
- Protecting against common web attacks, such as cross-site scripting (XSS) and SQL injection.
- Regularly updating Nginx and its modules to patch security vulnerabilities.
By following these security best practices, you can help ensure that your Nginx server is secure and protected against potential threats.
```
/path/images/../flag.txt
```
2023-08-03 19:12:22 +00:00
正确的配置应该是:
```
2023-08-03 19:12:22 +00:00
location /imgs/ {
alias /path/images/;
}
```
2023-08-03 19:12:22 +00:00
**所以如果你发现了一些Nginx服务器你应该检查这个漏洞。此外如果你发现文件/目录暴力破解的行为异常,也可能是这个漏洞。**
2023-08-03 19:12:22 +00:00
更多信息:[https://www.acunetix.com/vulnerabilities/web/path-traversal-via-misconfigured-nginx-alias/](https://www.acunetix.com/vulnerabilities/web/path-traversal-via-misconfigured-nginx-alias/)
2023-08-03 19:12:22 +00:00
Accunetix测试
```
alias../ => HTTP status code 403
alias.../ => HTTP status code 404
alias../../ => HTTP status code 403
alias../../../../../../../../../../../ => HTTP status code 400
alias../ => HTTP status code 403
```
## 不安全的路径限制 <a href="#unsafe-variable-use" id="unsafe-variable-use"></a>
查看以下页面以了解如何绕过诸如以下指令的限制:
```plaintext
location = /admin {
deny all;
}
location = /admin/ {
deny all;
}
```
2023-08-03 19:12:22 +00:00
## 不安全的变量使用 <a href="#unsafe-variable-use" id="unsafe-variable-use"></a>
一个易受攻击的 Nginx 配置示例是:
```
location / {
2023-08-03 19:12:22 +00:00
return 302 https://example.com$uri;
}
```
2023-08-03 19:12:22 +00:00
HTTP请求的换行符是\r回车和\n换行。对换行符进行URL编码会得到以下字符的表示%0d%0a。当这些字符包含在一个请求中例如`http://localhost/%0d%0aDetectify:%20clrf`发送到一个存在配置错误的服务器时,由于$uri变量包含URL解码的换行符服务器将会响应一个名为`Detectify`的新头部。
```
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.19.3
Content-Type: text/html
Content-Length: 145
Connection: keep-alive
Location: https://example.com/
Detectify: clrf
```
了解有关CRLF注入和响应拆分的风险请访问[https://blog.detectify.com/2019/06/14/http-response-splitting-exploitations-and-mitigations/](https://blog.detectify.com/2019/06/14/http-response-splitting-exploitations-and-mitigations/)。
### 任何变量
在某些情况下用户提供的数据可以被视为Nginx变量。目前尚不清楚为什么会发生这种情况但这并不罕见也不容易测试正如在这份[H1报告](https://hackerone.com/reports/370094)中所示。如果我们搜索错误消息,可以看到它在[SSI过滤模块](https://github.com/nginx/nginx/blob/2187586207e1465d289ae64cedc829719a048a39/src/http/modules/ngx\_http\_ssi\_filter\_module.c#L365)中找到从而揭示了这是由SSI引起的。
2023-08-03 19:12:22 +00:00
测试这种情况的一种方法是设置一个referer头的值
```
$ curl -H Referer: bar http://localhost/foo$http_referer | grep foobar
```
2023-08-03 19:12:22 +00:00
我们扫描了这个配置错误并发现了几个用户可以打印Nginx变量值的实例。发现的易受攻击实例数量有所减少这可能表明已经修复了这个问题。
2023-08-03 19:12:22 +00:00
## 读取原始后端响应
使用Nginx的`proxy_pass`可以拦截由后端创建的错误和HTTP头。这在隐藏内部错误消息和头部信息方面非常有用以便由Nginx处理。如果后端返回自定义错误页面Nginx会自动提供该页面。但是如果Nginx无法理解这是一个HTTP响应怎么办
如果客户端向Nginx发送无效的HTTP请求该请求将原样转发给后端后端将以其原始内容进行回复。然后Nginx无法理解无效的HTTP响应只会将其转发给客户端。想象一个像这样的uWSGI应用程序
2022-04-11 23:27:21 +00:00
```python
def application(environ, start_response):
2023-08-03 19:12:22 +00:00
start_response('500 Error', [('Content-Type',
'text/html'),('Secret-Header','secret-info')])
2023-08-03 19:12:22 +00:00
return [b"Secret info, should not be visible!"]
```
而使用以下指令在Nginx中
```
http {
2023-08-03 19:12:22 +00:00
error_page 500 /html/error.html;
proxy_intercept_errors on;
proxy_hide_header Secret-Header;
}
```
[proxy\_intercept\_errors](http://nginx.org/en/docs/http/ngx\_http\_proxy\_module.html#proxy\_intercept\_errors)会在后端返回的响应状态码大于300时提供自定义响应。在上面的uWSGI应用程序中我们将发送一个`500错误`这将被Nginx拦截。
2023-08-03 19:12:22 +00:00
[proxy\_hide\_header](http://nginx.org/en/docs/http/ngx\_http\_proxy\_module.html#proxy\_hide\_header)非常直观它将隐藏客户端中指定的任何HTTP头。
2023-08-03 19:12:22 +00:00
如果我们发送一个普通的`GET`请求Nginx将返回
```
HTTP/1.1 500 Internal Server Error
Server: nginx/1.10.3
Content-Type: text/html
Content-Length: 34
Connection: close
```
2023-08-03 19:12:22 +00:00
但是如果我们发送一个无效的HTTP请求比如
```
GET /? XTTP/1.1
Host: 127.0.0.1
Connection: close
```
2023-08-03 19:12:22 +00:00
我们将得到以下响应:
```
XTTP/1.1 500 Error
Content-Type: text/html
Secret-Header: secret-info
Secret info, should not be visible!
```
2023-08-03 19:12:22 +00:00
## merge\_slashes 设置为 off
默认情况下,[merge\_slashes](http://nginx.org/en/docs/http/ngx\_http\_core\_module.html#merge\_slashes) 指令被设置为 "on",这是一种将两个或多个正斜杠压缩为一个正斜杠的机制,因此 `///` 将变为 `/`。如果 Nginx 用作反向代理,并且被代理的应用程序容易受到本地文件包含的攻击,那么在请求中使用额外的斜杠可能会为攻击留下漏洞。这一点由 [Danny Robinson 和 Rotem Bar](https://medium.com/appsflyer/nginx-may-be-protecting-your-applications-from-traversal-attacks-without-you-even-knowing-b08f882fd43d) 详细描述。
2022-04-11 23:27:21 +00:00
2023-08-03 19:12:22 +00:00
我们发现了 33 个 Nginx 配置文件将 `merge_slashes` 设置为 "off"。
2022-04-11 23:27:21 +00:00
2023-08-03 19:12:22 +00:00
## map 指令未指定默认值
2022-04-11 23:27:21 +00:00
看起来这是一种常见情况,当 **`map` 用于某种授权控制** 时。简化的示例可能如下所示:
2022-04-11 23:27:21 +00:00
```
http {
...
2023-08-03 19:12:22 +00:00
map $uri $mappocallow {
/map-poc/private 0;
/map-poc/secret 0;
/map-poc/public 1;
}
2022-04-11 23:27:21 +00:00
...
}
```
```
server {
...
2023-08-03 19:12:22 +00:00
location /map-poc {
if ($mappocallow = 0) {return 403;}
return 200 "Hello. It is private area: $mappocallow";
}
2022-04-11 23:27:21 +00:00
...
}
```
根据[手册](https://nginx.org/en/docs/http/ngx\_http\_map\_module.html)
2022-04-11 23:27:21 +00:00
2023-08-03 19:12:22 +00:00
> 默认值\
> 如果源值与指定的变量都不匹配,则设置结果值。当未指定默认值时,结果值将为空字符串。
2022-04-11 23:27:21 +00:00
很容易忘记`default`值。因此,**恶意用户可以绕过这个"授权控制"**,只需访问`/map-poc`中不存在的情况,比如`https://targethost.com/map-poc/another-private-area`。
2022-04-11 23:27:21 +00:00
2023-08-03 19:12:22 +00:00
## Nginx DNS欺骗
2022-04-11 23:27:21 +00:00
根据这篇文章:[http://blog.zorinaq.com/nginx-resol**ver-vulns/**](http://blog.zorinaq.com/nginx-resolver-vulns/),如果你**知道Nginx使用的DNS服务器**(并且你可以拦截通信),那么**可能可以欺骗Nginx的DNS记录**,所以如果使用的是**127.0.0.1**,则无效,并且知道**它正在请求的域名**。
2022-04-11 23:27:21 +00:00
2023-08-03 19:12:22 +00:00
Nginx可以使用以下方式指定要使用的DNS服务器
2022-04-11 23:27:21 +00:00
```
resolver 8.8.8.8;
```
2023-08-03 19:12:22 +00:00
## `proxy_pass`和`internal`指令
2022-04-11 23:27:21 +00:00
**`proxy_pass`**指令可用于将请求**内部重定向到其他服务器**,无论是内部还是外部。\
2023-08-03 19:12:22 +00:00
**`internal`**指令用于明确告知Nginx该位置只能在内部访问。
2022-04-11 23:27:21 +00:00
2023-08-03 19:12:22 +00:00
使用这些指令**并不是一个漏洞,但你应该检查它们的配置方式**。
2022-06-19 13:37:58 +00:00
## proxy\_set\_header Upgrade & Connection
2023-08-03 19:12:22 +00:00
如果nginx服务器配置为传递Upgrade和Connection头则可以执行[h2c Smuggling攻击](../../pentesting-web/h2c-smuggling.md)以访问受保护/内部的端点。
2022-06-19 13:37:58 +00:00
{% hint style="danger" %}
2023-08-03 19:12:22 +00:00
此漏洞将允许攻击者与`proxy_pass`端点(在本例中为`http://backend:9999`建立直接连接nginx不会检查其内容。
2022-06-19 13:37:58 +00:00
{% endhint %}
2023-08-03 19:12:22 +00:00
以下是一个易受攻击配置的示例,用于从[此处](https://bishopfox.com/blog/h2c-smuggling-request)窃取`/flag`
2022-06-19 13:37:58 +00:00
```
server {
2023-08-03 19:12:22 +00:00
listen 443 ssl;
server_name localhost;
2022-06-19 13:37:58 +00:00
2023-08-03 19:12:22 +00:00
ssl_certificate /usr/local/nginx/conf/cert.pem;
ssl_certificate_key /usr/local/nginx/conf/privkey.pem;
2022-06-19 13:37:58 +00:00
2023-08-03 19:12:22 +00:00
location / {
proxy_pass http://backend:9999;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
}
2022-06-19 13:37:58 +00:00
2023-08-03 19:12:22 +00:00
location /flag {
deny all;
}
2022-06-19 13:37:58 +00:00
```
{% hint style="warning" %}
请注意,即使`proxy_pass`指向特定的**路径**,例如`http://backend:9999/socket.io`,连接也将与`http://backend:9999`建立,因此您可以**联系该内部端点中的任何其他路径。因此在proxy\_pass的URL中指定路径并不重要。**
2022-06-19 13:37:58 +00:00
{% endhint %}
2023-08-03 19:12:22 +00:00
## 亲自尝试
2023-08-03 19:12:22 +00:00
Detectify创建了一个GitHub存储库您可以使用Docker设置自己的易受攻击的Nginx测试服务器并尝试找到本文中讨论的一些配置错误
[https://github.com/detectify/vulnerable-nginx](https://github.com/detectify/vulnerable-nginx)
2023-08-03 19:12:22 +00:00
## 静态分析工具
2022-05-08 23:13:03 +00:00
### [GIXY](https://github.com/yandex/gixy)
2023-08-03 19:12:22 +00:00
Gixy是一个用于分析Nginx配置的工具。Gixy的主要目标是防止安全配置错误并自动检测缺陷。
2022-04-11 23:27:21 +00:00
2022-07-24 19:52:09 +00:00
### [Nginxpwner](https://github.com/stark0de/nginxpwner)
2023-08-03 19:12:22 +00:00
Nginxpwner是一个简单的工具用于查找常见的Nginx配置错误和漏洞。
2022-07-24 19:52:09 +00:00
2023-08-03 19:12:22 +00:00
## 参考资料
2022-04-11 23:27:21 +00:00
2022-05-08 23:13:03 +00:00
* [**https://blog.detectify.com/2020/11/10/common-nginx-misconfigurations/**](https://blog.detectify.com/2020/11/10/common-nginx-misconfigurations/)
* [**http://blog.zorinaq.com/nginx-resolver-vulns/**](http://blog.zorinaq.com/nginx-resolver-vulns/)
* [**https://github.com/yandex/gixy/issues/115**](https://github.com/yandex/gixy/issues/115)
2022-04-28 16:01:33 +00:00
<figure><img src="../../.gitbook/assets/image (1) (1) (2) (4).png" alt=""><figcaption></figcaption></figure>
2022-04-28 16:01:33 +00:00
[**DragonJAR Security Conference es un evento internacional de ciberseguridad**](https://www.dragonjarcon.org/) con más de una década que se celebrará el 7 y 8 de septiembre de 2023 en Bogotá, Colombia. Es un evento de gran contenido técnico donde se presentan las últimas investigaciones en español que atrae a hackers e investigadores de todo el mundo.\
¡Regístrate ahora en el siguiente enlace y no te pierdas esta gran conferencia!:
2022-04-28 16:01:33 +00:00
{% embed url="https://www.dragonjarcon.org/" %}
2022-04-28 16:01:33 +00:00
<details>
2022-04-28 16:01:33 +00:00
<summary><a href="https://cloud.hacktricks.xyz/pentesting-cloud/pentesting-cloud-methodology"><strong>☁️ HackTricks Cloud ☁️</strong></a> -<a href="https://twitter.com/hacktricks_live"><strong>🐦 Twitter 🐦</strong></a> - <a href="https://www.twitch.tv/hacktricks_live/schedule"><strong>🎙️ Twitch 🎙️</strong></a> - <a href="https://www.youtube.com/@hacktricks_LIVE"><strong>🎥 Youtube 🎥</strong></a></summary>
2022-04-28 16:01:33 +00:00
* Do you work in a **cybersecurity company**? Do you want to see your **company advertised in HackTricks**? or do you want to have access to the **latest version of the PEASS or download HackTricks in PDF**? Check the [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Discover [**The PEASS Family**](https://opensea.io/collection/the-peass-family), our collection of exclusive [**NFTs**](https://opensea.io/collection/the-peass-family)
* Get the [**official PEASS & HackTricks swag**](https://peass.creator-spring.com)
* **Join the** [**💬**](https://emojipedia.org/speech-balloon/) [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** me on **Twitter** [**🐦**](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md)[**@carlospolopm**](https://twitter.com/hacktricks\_live)**.**
* **Share your hacking tricks by submitting PRs to the** [**hacktricks repo**](https://github.com/carlospolop/hacktricks) **and** [**hacktricks-cloud repo**](https://github.com/carlospolop/hacktricks-cloud).
2022-04-28 16:01:33 +00:00
</details>