SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
SQL Injection (SQLi) is one of the most common web application vulnerabilities. It allows attackers to interfere with an application's SQL queries. By injecting malicious SQL code, attackers can gain unauthorized access to a database, retrieve sensitive data, modify or delete data, and execute administrative operations on the database.
Prepared Statements (or Parameterized Queries) offer a robust means to mitigate the risk of SQL injection attacks. Instead of directly incorporating user inputs into SQL statements, prepared statements allow you to define a SQL query with placeholders for user input. The database system ensures that these inputs are treated as data rather than executable code, which prevents attackers from injecting malicious SQL code.
Preparation: The SQL statement template is sent to the database server. The server initializes a SQL statement and returns a statement handle, but doesn't execute the SQL command.
Binding: You bind the user input values to the statement handle. This means you provide the actual values that replace the placeholders. These values are treated strictly as data.
Execution: Once the statement is prepared and bound, it is executed by the database server.
Without Prepared Statement (Vulnerable to SQLi):
$sql = "INSERT INTO users (username, password) VALUES ('" . $_POST['username'] . "', '" . $_POST['password'] . "')"; mysqli_query($conn, $sql);
Using Prepared Statement:
$stmt = $conn->prepare("INSERT INTO users (username, password) VALUES (?, ?)"); $stmt->bind_param("ss", $_POST['username'], $_POST['password']); $stmt->execute();
In the above example:
?
characters are placeholders in the SQL statement.bind_param
method binds the actual values to these placeholders."ss"
argument specifies the data type for each placeholder (s
stands for string in this case).In conclusion, while prepared statements offer a strong defense against SQL injection, they should be used as part of a comprehensive security strategy that involves multiple layers of protection.
Parameterized Queries to Prevent SQL Injection:
-- Example in SQL (for illustration purposes) SELECT * FROM users WHERE username = ? AND password = ?;
Secure Database Access in Java with Prepared Statements:
PreparedStatement
in Java for secure database access.String sql = "SELECT * FROM users WHERE username = ? AND password = ?"; try (PreparedStatement pstmt = connection.prepareStatement(sql)) { pstmt.setString(1, inputUsername); pstmt.setString(2, inputPassword); ResultSet rs = pstmt.executeQuery(); // Process the result set } catch (SQLException e) { e.printStackTrace(); }
How to Use PreparedStatement
in JDBC to Avoid SQL Injection:
PreparedStatement
to prevent SQL injection.String sql = "INSERT INTO users (username, password) VALUES (?, ?)"; try (PreparedStatement pstmt = connection.prepareStatement(sql)) { pstmt.setString(1, inputUsername); pstmt.setString(2, inputPassword); pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }
Preventing SQL Injection in PHP with Prepared Statements:
PDO
in PHP for prepared statements.$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ? AND password = ?"); $stmt->execute([$inputUsername, $inputPassword]); $result = $stmt->fetchAll();
Parameterized Queries in Hibernate for SQL Injection Prevention:
Query query = session.createQuery("FROM User WHERE username = :username AND password = :password"); query.setParameter("username", inputUsername); query.setParameter("password", inputPassword); List<User> users = query.list();
Securing SQL Queries in Python with Prepared Statements:
sqlite3
module.cursor.execute("SELECT * FROM users WHERE username = ? AND password = ?", (inputUsername, inputPassword)) result = cursor.fetchall()