SQL注入及如何解決
SQL注入即是指web應用程式對用戶輸入數據的合法性沒有判斷或過濾不嚴,攻擊者可以在web應用程式中事先定義好的查詢語句的結尾上添加額外的SQL語句,在管理員不知情的情況下實現非法操作,以此來實現欺騙資料庫伺服器執行非授權的任意查詢,從而進一步得到相應的數據資訊。
1、SQL注入案例
模擬一個用戶登錄的SQL注入案例,用戶在控制台上輸入用戶名和密碼, 然後使用 Statement 字元串拼接的方式實現用戶的登錄。
1.1 資料庫中先創建用戶表及數據
-- 創建一張用戶表 CREATE TABLE `users` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `username` VARCHAR(20), `password` VARCHAR(50), PRIMARY KEY (`id`) ) ENGINE=INNODB DEFAULT CHARSET=utf8; -- 插入數據 INSERT INTO users(username,`password`) VALUES('張飛','123321'),('趙雲','qazxsw'),('諸葛亮','123Qwe'); INSERT INTO users(username,`password`) VALUES('曹操','741258'),('劉備','plmokn'),('孫權','!@#$%^'); -- 查看數據 SELECT * FROM users;
1.2 編寫一個登錄程式
package com.study.task0201; import java.sql.*; import java.util.Scanner; public class TestSQLIn { public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://127.0.0.1:3306/testdb?characterEncoding=UTF-8"; Connection conn = DriverManager.getConnection(url,"root","123456"); //System.out.println(conn); // 獲取語句執行平台對象 Statement Statement smt = conn.createStatement(); Scanner sc = new Scanner(System.in); System.out.println("請輸入用戶名:"); String userName = sc.nextLine(); System.out.println("請輸入密碼:"); String password = sc.nextLine(); String sql = "select * from users where username = '" + userName + "' and password = '" + password +"'";
//列印出SQL System.out.println(sql); ResultSet resultSet = smt.executeQuery(sql); if(resultSet.next()){ System.out.println("登錄成功!!!"); }else{ System.out.println("用戶名或密碼錯誤,請重新輸入!!!"); } resultSet.close(); smt.close(); conn.close(); } }
1.3 正常登錄
輸入正確的用戶名及密碼後提示”登錄成功”
1.4 登錄失敗
輸入用戶名或密碼錯誤時,提示「用戶名或密碼錯誤,請重新輸入」
1.5 模擬SQL注入
拼接的字元串中有or ‘1’=’1′ 為恆成立條件,因此 及時前面的用戶及密碼不存在也會取出所有記錄,因此提示”登錄成功”
1.6 SQL語法報錯
使用拼接的方式,還會出現SQL語法錯誤等報錯,例如
2. 解決方案
使用Statement方式,用戶可以通過字元串拼接,改變原本SQL真正的含義,導致存在SQL注入的風險。解決SQL注入,可以通過預處理對象PreparedStatement來代替Statement進行處理。
2.1 程式
import java.sql.*; import java.util.Scanner; public class TestSQLIn { public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://127.0.0.1:3306/testdb?characterEncoding=UTF-8"; Connection conn = DriverManager.getConnection(url,"root","123456"); //System.out.println(conn); // 獲取語句執行平台對象 Statement // Statement smt = conn.createStatement(); Scanner sc = new Scanner(System.in); System.out.println("請輸入用戶名:"); String userName = sc.nextLine(); System.out.println("請輸入密碼:"); String password = sc.nextLine(); String sql = "select * from users where username = ? and password = ? "; // System.out.println(sql); // ResultSet resultSet = smt.executeQuery(sql); PreparedStatement preparedStatement = conn.prepareStatement(sql); preparedStatement.setString(1,userName); preparedStatement.setString(2,password); ResultSet resultSet = preparedStatement.executeQuery(); if(resultSet.next()){ System.out.println("登錄成功!!!"); }else{ System.out.println("用戶名或密碼錯誤,請重新輸入!!!"); } preparedStatement.close(); resultSet.close(); // smt.close(); conn.close(); } }
2.2 正常登錄
2.3 用戶名密碼錯誤
當用戶名或密碼輸入錯誤時,會提示「用戶名或密碼錯誤,請重新輸入」
2.4 模擬SQL注入
按照之前的情況,進行SQL注入的寫法,測試後不再出現SQL注入情況。
2.5 模擬SQL語法錯誤
使用預處理類後,輸入帶有單引號或雙引號的內容也不會再出現SQL語法錯誤的報錯
3. 小結
Statement 與 PreparedStatement的主要區別如下:
- Statement用於執行靜態SQL語句,在執行時,必須指定一個事先準備好的SQL語句
- PrepareStatement是預編譯的SQL語句對象,語句中可以包含動態參數「?」,在執行時可以為「?」動態設置參數值
- PrepareStatement可以減少編譯次數提高資料庫性能