Introduction to SQL Injection: Hands-On |
1. Reset the Database Before Using It |
2. SQL Database StructureThe database namedsqlol contains the two tables shown below.
|
3. SQL SELECT QueriesSQL uses easily-understood commands like SELECT, UPDATE, and DELETE. Try the queries below to see how SELECT works.
|
SELECT Queries to Try | |
SELECT * FROM sqlol.users | Get all fields from the table "sql.users" |
SELECT * FROM sqlol.ssn | Get all fields from the table "sql.ssn" |
SELECT name FROM sqlol.ssn | Get field "name" from the table "sql.ssn" |
SELECT ssn as name FROM sqlol.ssn | Get field "ssn" from the table "sql.ssn" and change its field name to "name" |
SELECT * FROM sqlol.ssn WHERE name='Herp Derper' | Get all fields from the table "sql.ssn" with the "name" field equal to "Herp Derper" |
SELECT * FROM sqlol.ssn WHERE name='Fred' | Get all fields from the table "sql.ssn" with the "name" field equal to "Fred" |
SELECT * FROM sqlol.ssn WHERE name='Fred' OR 'a'='a' | Get all fields from the table "sql.ssn" (the condition is always true) |
SELECT username FROM sqlol.users UNION SELECT ssn AS username FROM sqlol.ssn | Combine data from two tables |
SELECT "Literal text" into outfile '/var/www/html/test1.htm' | Put literal text into a file (you'll need to change the filename to something that hasn't been used yet) ty @faisal_hfr |
4. Search for UsernamesWebsites don't usually let you type in complete SQL queries, but only fields like usernames and passwords.Attackers can sneak SQL commands in by using special characters like apostrophes. Try the usernames below in this form to see how it works.
Performs This Query:
|
Usernames to TryFind User
Detect Vulnerability
Find Database Names
Find Tables in sqlol Database
Find Columns within ssl Table
Dump Names and SSNs
Upload a PHP Shell
|
5. Safer Search with Input ValidationThe simplest defense is to encode special characters.This stops many common SQL injection attacks with a single line of code. Try the usernames above in this form:
Performs This Query:
Uses mysql_real_escape_string to Encode Special Characters |
6. ChallengesUse the form in item 4 above to inject usernames that do the things below. For hints, see the "Sources" at the bottom of the page.Challenge 1: Display names for administrators only, as shown below:
Challenge 2: Create a file on my server with your name as a filename, as shown below.
Challenge 3: Display the /etc/passwd file in a browser, as shown below:
Challenge 4: Put your name into these two files:
/var/www/html/SQLchal/updatenow
Challenge 5: Use a different server, without the option to use raw SELECT queries. Before starting, click the button below to reset the database:
Now use this name search form: Put your name into these two files on that server:
Within a minute, your name will appear on
the
Winners page
as shown below:
|
7. Blocking ApostrophesThis form deletes apostrophes from the name before using it in a SQL query. However, the numerical field is exploitable.Try the inputs below in this form to see how it works.
Performs This Query:
|
Values to TryFind User
Detect Filtering
Detect Vulnerability
Test for 1 Column Returned
Test for 2 Columns Returned
Find Database Names
Find Tables in sqlol Database
Find Columns within ssn Table
Dump Names and SSNs
Upload a PHP Shell
|
8. Blocking SELECTThis form uses this code to remove SELECT:
Try the inputs
below in this form to see how it works.
Performs This Query:
|
Values to TryFind User
Detect Filtering
Detect Vulnerability
Test for 2 Columns Returned: FAILS
Test for 2 Columns Returned: SUCCEEDS
Test for 2 Columns Returned: SUCCEEDS
|
More ProjectsSQL Injection with SQLolExploiting SQLi with Havij and Input Filtering Fixing MySQL with Parameterized Queries
|
Thanks to@Faisal_HFR for using SELECT "Literal text" into outfile, a much better way to get onto the winers page than I used.@bcrook88 for injecting JavaScript onto the winners page twice, so I added input filtering and more strict apparmor rules to MySQL to stop it. @bcrook88 for uploading a PHP shell that let him execute arbitrary bash commands, inspiring challenge 6. SourcesBased on SQLol from SpiderLabs.SQL Injection Modify / Insert Table Values Hackproofing MySQL (from 2004) Hackproofing MySQL (alternative download link) |