CSE 127: Computer Security: SQL Injection
CSE 127: Computer Security: SQL Injection
SQL Injection
Vector Li
very hack
http://127.0.0.1:8080/view.php?
user=%27%20or%20%27%27%20=%20%27
encodes
if (isset($_GET["user"])) {
$user = ($_GET["user"]);
}
$exists = true;
?>
From Someones view.php:
<?php
if (isset($_GET["user"])) {
$user = ($_GET["user"]);
}
$exists = true;
?>
http://127.0.0.1:8080/view.php?
user=%27%20or%20%27%27%20=%20%27
encodes
results in query
if (isset($_GET["user"])) {
$user = ($_GET["user"]);
}
$exists = true;
?> untrusted user input
inserted directly into
query that is sent to
the database
From Someones login.php:
<?php
else {
$query = "SELECT username FROM chattrdb.users WHERE
username='$username' AND password='$password'";
$result = pg_query($conn, $query);
if (!$row = pg_fetch_row($result))
{
session_unset();
?>
<?php
} else {
$_SESSION[username] = $username;
header(Location: view.php?user=$username);
}
?>
$username = $_SESSION['username'];
$timeStamp = date("Y-m-d H:i:s");
$userMessage = $_POST['TEXT'];
$result = pg_query($insertQuery);
?>
HW2: view.php?user=
HW2: login.php
From Someones view.php:
<?php
if (isset($_GET["user"])) {
$user = ($_GET["user"]);
}
$exists = true;
?>
Attacker
gains ability to submit SQL directly to
backend database on behalf of application database
user
Web Application Architecture
User Domains
Operating system
Database
Application
Attacker
gains direct access to database with application
database user privilege
Mitigation
Preferred way
Sanitizing User Input
Escape special characters
E.g. change ' to '' ('' treated as single ' inside quote)
Invisible to user
Escaping Problems
$conn = pg_connect (
"host=$db_host dbname=$db_name
user=$db_user password=$db_pass")
The Bigger Problem
Best
case: attacker gains union of all application users
access privileges to data