SQL Injection
SQL Injection
SQL INJECTION
http://localhost/security/Example13-SQLInjection-simple.php?
account_id=1234
SQL INJECTION
http://localhost/security/Example14-SQLInjection-simple.php?
account_id=1234
How?
SQL INJECTION
$account_id = $_GET['account_id'];
if (!empty($account_id)) {
try {
$conn = getDatabaseConnection();
if ($result->num_rows > 0) {
echo '<br><br><table>';
echo '<td style="width: 100px; height: 22px">' . "<b>Account id</b>" . '</td>';
echo '<td style="width: 150px; height: 22px">' . "<b>User</b>" . '</td>';
echo '<td style="width: 100px; height: 22px">' . "<b>Balance</b>" . '</td>';
while($row = $result->fetch_assoc()) {
echo '<tr>';
echo '<td style="width: 100px; height: 18px">' . $row['account_id'] . '</td>';
echo '<td style="width: 150px; height: 18px">' . $row['user_name'] . '</td>';
echo '<td style="width: 100px; height: 18px">' . $row['account_balance'] . '</td>';
echo '</tr>';
}
echo '</table>';
} else {
echo "<br><br>No results match your search:-(";
}
mysqli_close($conn);
} catch (Exception $e) {
echo 'Error! ' + $e->getCode();
}
}
SQL INJECTION
$account_id = $_GET['account_id'];
if (!empty($account_id)) {
try {
$conn = getDatabaseConnection();
$account_id = $_GET['account_id'];
if (!empty($account_id)) {
try {
$conn = getDatabaseConnection();
“SELECT *
FROM BackAccounts
WHERE AccountId = “ . $_GET[‘account_id’]
password’ OR 1=1 —
SQL INJECTION
password’ OR 1=1 —
Thanks to http://
www.unixwiz.net/techtips/sql-
injection.html for this example
SQL INJECTION
Anatomy of an attack
We enter:
jan@loonycorn.com’
input data sanitized or not SQL INJECTION
SELECT * FROM Users Anatomy of an attack
WHERE email = ‘<user input email>’
jan@loonycorn.com’
email
user_id
password
name
SQL INJECTION
Anatomy of an attack
1. In-band SQLi
2. Blind SQLi
3. Out-of-band SQLi
SQL INJECTION
In-band SQLi
This is a kind of SQL attack when
an attacker is able to use the same
communication channel to both
launch the attack
gather the results
SQL INJECTION
In-band SQLi
1. In-band SQLi
Error based SQLi
Union based SQLi
2. Blind SQLi
3. Out-of-band SQLi
SQL INJECTION
Blind SQLi
In such an attack no data is
transferred along with the web
application
1. In-band SQLi
Error based SQLi
Union based SQLi
2. Blind SQLi
Boolean based SQLi
Time based SQLi
3. Out-of-band SQLi
SQL INJECTION
Out-of-band SQLi
1. In-band SQLi
Error based SQLi
Union based SQLi
2. Blind SQLi
Boolean based SQLi
Time based SQLi
3. Out-of-band SQLi
SQL INJECTION
Mitigation
SQL INJECTION
Mitigation
1. Parameterized statements
2. Stored procedures
3. Escaping user input
4. Least privilege
5. Whitelist validation
SQL INJECTION
Parameterized statements
It differentiates clearly
bet ween code (the actual query)
and data (user input)
SQL INJECTION
Example14-SQLInjection-parameterizedQueries.php
SQL INJECTION
Parameterized statements
$conn = getDatabaseConnection();
echo '<br><br><table>';
echo '<td style="width: 100px; height: 22px">' . "<b>Account id</b>" . '</td>';
echo '<td style="width: 150px; height: 22px">' . "<b>User</b>" . '</td>';
echo '<td style="width: 100px; height: 22px">' . "<b>Balance</b>" . '</td>';
while($stmt->fetch()) {
echo '<tr>';
echo '<td style="width: 100px; height: 18px">' . $account_id . '</td>';
echo '<td style="width: 150px; height: 18px">' . $user_name . '</td>';
echo '<td style="width: 100px; height: 18px">' . $account_balance . '</td>';
echo '</tr>';
}
echo '</table>';
SQL INJECTION
Parameterized statements
$conn = getDatabaseConnection();
1. Parameterized statements
2. Stored procedures
3. Escaping user input
4. Least privilege
5. Whitelist validation
SQL INJECTION
Stored procedures
1. Parameterized statements
2. Stored procedures
3. Escaping user input
4. Least privilege
5. Whitelist validation
SQL INJECTION
Escaping user input
1. Parameterized statements
2. Stored procedures
3. Escaping user input
4. Least privilege
5. Whitelist validation
SQL INJECTION
Least privilege
Every database account in the
database should only be given
sufficient privilege to perform the
tasks required
1. Parameterized statements
2. Stored procedures
3. Escaping user input
4. Least privilege
5. Whitelist validation
SQL INJECTION
Whitelist validation
1. Parameterized statements
2. Stored procedures
3. Escaping user input
4. Least privilege
5. Whitelist validation