Intro
The hxp CTF 2022 started at 19:00 on March 10th, 2023 and went on for two whole days. In our team I was solving web challenges and I want to share some of the solutions and sploits to the tasks that we were able to solve.
As for now (March 18th, 2023), CTF page is up here and all challenge files can still be downloaded, albeit challenge servers are already down.
Valentine
We’re given a simple web service to create valentines. It has two endpoints. First allows us to save our ejs template, ensuring that every time substring
<%
occurres in a template, it is a part of<%= name %>
. Second endpoint allows us to render any saved template with any parameters that we pass to it.
What we need to do in this task is quite obvious. The only way that we can get RCE to read the flag is by SSTI in ejs engine. Unfortunately, it seems that there’s no way to bypass filter that allows only <%= name %>
templates in. So, how can we sneak in something else?
After reading source files and some quick googling we stumbled upon this article by Eslam Salem. It thoroughly described RCE via SSTI & PP in ejs template engine. It shows that we can overwrite any of the following ejs settings to change it’s behavior:
|
|
Unfortunately Eslam’s sploit already doesn’t work, but we can do something similar. After messing around with source code of ejs and express and a bit of trial and error, we’ve discovered that we can still overwrite settings by sending settings[view options][<option>]
in query string, mapped to any value that we want it to be. We’ve tested it on local node instance and were able to get RCE. But sploit, for some odd reason, still didn’t work when ran against the docker instance or remote server.
The reason was that when we ran the code agains node instance, our node server was deployed in development environment and, hence, had the NODE_ENV
variable set to development
, unlike docker and remote, which had production
mode on. That affected cache, which made it so that our settings overwrite didn’t work (probably, because of this part in ejs source code). Fortunately, we were able to overwrite cache as well.
But the challenge is not quite over yet. You see, to disable caching we needed to assign some value to cache that will make it cast to false
inside if
statement. But what can it be? If we set it to null
or false
, it will be interpreted as a string and, therefore, will keep the cache on. If we don’t specify any value, it won’t overwrite true
value that is on by default in production environment. So, what can we do? We noticed that our query is parsed with qs
, which allows us to use [cache]=
in query string to show that cache
is an empty array. Which happens to work in our case and set cache
variable to false-y expression!
So, the payload goes as follows:
|
|
Archived
This challenge features the latest version of Apache Archiva as a main (and only) service. We’re given user priveledges in that application and our goal is to get file read on the system.
This challenge looks more scary, than it actually is. First of all, let’s think: we’re given a challenge, in which we can make request for an admin instance to do something (XSS?). admin.py
script just logs in as an admin and goes to /repository/internal
path.
Obviously, the first thing that I’ve tried is injecting some XSS into that page. /repository/internal
lists all artifacts that have been uploaded to internal repository. All users are able to do that, so if we happen to find XSS there, stealing admin’s cookie would be pretty easy. And would you look at that, we’re able to create artifact with an XSS in it’s name, which would be unsafely inserted into page that admins visit and steal theirs cookie! The only thing is that with we’re unable to create payloads with some symbols (for example, we can’t use dot). So we need to be careful with our payload.
So, we’ve gained access to admin’s account, what’s next? We need to somehow gain file read priveleges. Luckily, it’s not that difficult either. We can just create new repository with, which has it’s root at the root of file system. And after navigating to /repository/<our-repo-id>
we can read any file on file system, including /flag.txt
:D!
Full sploit:
|
|
Sqlite_web
We’re given this open-source project started with read-only database. Our goal is to gain RCE
So, first of all, all that encryption stuff is not related to the task. Flag is only accessible at the file system, so decrypting database (even if we could) is not going to give us anything. After all, this is web task, not crypto.
This task loads crypto.so
from sqlean, so extension loading is enabled. That means that we can load any shared object binary from the file system into sqlite to gain remote code execution. The question now is: how do we get malicious shared object file uploaded to the server?
The answer is temporary files that werkzeug creates when handling file uploads of more than 500kb. So we can append null bytes to our shared object file, so it’s big enough. We also can “bypass” csv/json limitation by just setting file type to application/json. Then we need to randomly try for file desecriptors until we get just the correct timing to read file, that has just been temporarly stored on the file system. This timing needs to be very precise, so we used multiple threads to simultaneously upload and read files to increase odds of getting that timing.
Our sploit:
|
|