ORDER BY SQL Injection
Based on a CTF challenge scenario
This was an intresting and good challenge from the latest Nahamcon CTF
, The reason i choose to explain it is because it also appeared on HTB Cyber Apocalypse . It can be categorized under Blind SQL Injection with conditional responses , so let's dive in .
Code Analysis
We can see that there is a code within the challenge [ Flask Web Application ], I will not dispay all of the code but the only important snippets .
First Snippet :
We can see that it accepts POST
method and 2 post parameteres which are search
and order
. The order
parameter is executed within a SQL query , but we realize that it isn't validated to any source of injection which makes it a potentiall point of injection .
Second Snippet :
We can see that there is two tables which are metals
and flag
, the flag
table has a column called flag
, the metal
table has 3 columns atomic_number
, symbol
& name
.
Web Application Analysis
If we choose to order by or to sort by symbol
:
And if we choose to sort by atomic_number
:
So let's try to manipulate the order
parameter, if we injected the following query :
What would it do ?
First the query will check for the condition ( 1=1 => True ) and will sort depending on the condition , Since it is true so it will order by symbol as we can see :
If we changes the condition to false then it will order by atomic_number :
Nice , so now we got the key which we will solve the challenge upon. We wil give the query a condiition for the flag characters if it is true then we will see the Ac
symbol if it is fase we wil see the Li
symbol .
Exploitation
In Sqlite
there is a function called substr()
which we will use to exploit the SQL Injection .
We know that the first character of the flag is"f" so we can test for it :
We now expect to see the Ac
character :
We are on the right path !
Final Output
Last updated