SQL注入即是指web應(yīng)用程序?qū)τ脩糨斎霐?shù)據(jù)的合法性沒有判斷或過濾不嚴(yán),攻擊者可以在web應(yīng)用程序中事先定義好的查詢語句的結(jié)尾上添加額外的SQL語句,在管理員不知情的情況下實現(xiàn)非法操作,以此來實現(xiàn)欺騙數(shù)據(jù)庫服務(wù)器執(zhí)行非授權(quán)的任意查詢,從而進(jìn)一步得到相應(yīng)的數(shù)據(jù)信息。
模擬一個用戶登錄的SQL注入案例,用戶在控制臺上輸入用戶名和密碼, 然后使用 Statement 字符串拼接的方式實現(xiàn)用戶的登錄。
1.1 數(shù)據(jù)庫中先創(chuàng)建用戶表及數(shù)據(jù)
-- 創(chuàng)建一張用戶表 CREATE TABLE `users` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `username` VARCHAR(20), `password` VARCHAR(50), PRIMARY KEY (`id`) ) ENGINE=INNODB DEFAULT CHARSET=utf8; -- 插入數(shù)據(jù) INSERT INTO users(username,`password`) VALUES('張飛','123321'),('趙云','qazxsw'),('諸葛亮','123Qwe'); INSERT INTO users(username,`password`) VALUES('曹操','741258'),('劉備','plmokn'),('孫權(quán)','!@#$%^'); -- 查看數(shù)據(jù) 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); // 獲取語句執(zhí)行平臺對象 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語法報錯
使用拼接的方式,還會出現(xiàn)SQL語法錯誤等報錯,例如
使用Statement方式,用戶可以通過字符串拼接,改變原本SQL真正的含義,導(dǎo)致存在SQL注入的風(fēng)險。解決SQL注入,可以通過預(yù)處理對象PreparedStatement來代替Statement進(jìn)行處理。
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); // 獲取語句執(zhí)行平臺對象 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 用戶名密碼錯誤
當(dāng)用戶名或密碼輸入錯誤時,會提示“用戶名或密碼錯誤,請重新輸入”
2.4 模擬SQL注入
按照之前的情況,進(jìn)行SQL注入的寫法,測試后不再出現(xiàn)SQL注入情況。
2.5 模擬SQL語法錯誤
使用預(yù)處理類后,輸入帶有單引號或雙引號的內(nèi)容也不會再出現(xiàn)SQL語法錯誤的報錯
Statement 與 PreparedStatement的主要區(qū)別如下:
到此這篇關(guān)于SQL注入以及如何解決的文章就介紹到這了,更多相關(guān)SQL注入及解決內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:鶴崗 常德 沈陽 石嘴山 昆明 葫蘆島 招商 保定
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《實例介紹SQL注入以及如何解決》,本文關(guān)鍵詞 實例,介紹,SQL,注入,以及,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。