# sqlite_web - hxpCTF 2022

tl;dr

• Create a sqlite3 extension with rce payload.
• Abuse werkzeug tempfile to upload the extension to server.

Challenge points: 385
No. of solves: 17

## Analysis

We were given the latest version of sqlite_web , our goal is to get RCE on the server. Looking through the Dockerfile we can see the sqlite_web is run using the following command :

we have an encrypted.db loaded with all encrypted tables, and we also have a crypto extension from sqlean loaded for using crypto functions like md5,sha1, etc.

After setting up the challenge locally and reading sqlite_web source, we tried to find potential rce vectors in sqlite3 as we can execute SQL statements. We came accross this blog, which has a lot of tricks get rce using SQLite.

One thing we noticed was using ATTACH DATABASE we can write files in to filesystem, thus we thought of overwriting an existing webapp’s template file to insert a SSTI payload to get RCE but we couldn’t execute multiple queries together so it was not possible to write into files and the database was also loaded using read-only mode.

Next thing we notice was using load_extension() function we can load an arbitrary extension and execute function inside that extension. But in the documentation of the load_extension()

For security reasons, extension loading is disabled by default and must be enabled by a prior call to sqlite3_enable_load_extension().

So we cannot call load_extension() until it is enabled by calling sqlite3_enable_load_extension function. That’s where loading the crypto module of this challenge comes into play. sqlite_web calls dataset._database.load_extension(ext) while loading an extension. If we look into peewee for load_extension function we can see these lines:

So it enables the SQLite load_extension function for us. So we can call the load_extension function with an arbitrary extension we provide. We tried to load the crypto.so on the server it worked, but we want to load our own extension file.

Now the question is how can we upload a our own extension to the server? There is no file upload feature in the sqlite_web. At this point, I remembered the nigix trick from past hxp ctf, Where if the body of the request was too long it will store the body in a temp file, and we abuse that to get rce using include. We also have a similar scenario here so we checked for the same feature in werkzeug which is used by Flask.

Aaand we got what we wanted in formparser.py!!

werkzeug uses SpooledTemporaryFile from tempfile module to temporarily store uploaded file, and if the file sized is more than the specified max_size, it will create a temp file to store the data else it will store it in the memory itself.

Here in werkzeug we can see the max_size is specified as 500kb, so we just have to upload an extension file greater than 500kb to get that file on the server’s filesystem.

## Exploit

First, we create a sqlite3 extension with rce payload in it . We modified the crypto extension from sqlean itself to spawn a reverse shell.

compile the code to create an extension file and make sure to add some garbage to make it more than 500kb.

Then we upload that file to the server by sending a fileupload request to /table/import

Then we have to load that extension, we can’t directly access the tempfile created but there will be a file descriptor of that file in /proc/self/fd directory until that request is finished ,so we can bruteforce the fd number and load the extension.
And one more thing,at first we thought we can’t load file without .so extension but later one of our team members figured out that it is not necessary if the file an actual so file. Then we saw that the sqlite was calling the init function of the extension using the file name, then we figured out that we can give the init function as the second parameter to the load_extension function.

So the sql final query will be like this:

After figuring out every part of the solution,we didn’t have enough time to write a brute force script, but we had enough players to spam request to each fd manually to get it working before the ctf ends.

flag

## Closing thoughts

We had a lot of fun solving the challenge and learn’t a lot from it, Thanks hxp for such a nice challenge and ctf.