自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java最新SQL注入原因以及預(yù)防方案(易理解)

開發(fā) 后端 數(shù)據(jù)庫
在現(xiàn)有的框架中sql防注入已經(jīng)做得很好了,我們需要做的就是盡量不要使用sql拼接調(diào)用

[[374950]]

 前沿

在現(xiàn)有的框架中sql防注入已經(jīng)做得很好了,我們需要做的就是盡量不要使用sql拼接調(diào)用

java sql注入原因以及預(yù)防方案(易理解)

1. SQL注入

1.1 原理

SQL注入是通過客戶端的輸入把SQL命令注入到一個(gè)應(yīng)用的數(shù)據(jù)庫中,從而執(zhí)行惡意的SQL語句。

1.2 演示

1.2.1 案例1

有一個(gè)登錄框,需要 輸入用戶名和密碼 ,然后我們的密碼輸入 'or '123' = '123 這樣的。我們在查詢用戶名和密碼是否正確的時(shí)候,本來執(zhí)行的sql語句是:select * from user where username = '' and password = ''. 這樣的sql語句,現(xiàn)在我們輸入密碼是如上這樣的,然后我們會通過參數(shù)進(jìn)行拼接,拼接后的sql語句就是:

select * from user where username = '' and password = ' ' or '123' = '123 ';這樣的了,那么會有一個(gè)or語句,只要這兩個(gè)有一個(gè)是正確的話,就條件成立,因此 123 = 123 是成立的。因此驗(yàn)證就會被跳過。這只是一個(gè)簡單的例子,

1.2.2 案例2

密碼比如是這樣的:'; drop table user;, 這樣的話,那么sql命令就變成了:

select * from user where username = '' and password = ''; drop table user;', 那么這個(gè)時(shí)候我們會把user表直接刪除了。

1.3 防范

1.3.1 前端

前端表單進(jìn)行參數(shù)格式控制;

1.3.2 后端

  • 我們可以使用預(yù)編譯語句(PreparedStatement,這 樣的話即使我們使用sql語句偽造成參數(shù),到了服務(wù)端的時(shí)候,這個(gè)偽造sql語句的參數(shù)也只是簡單的字符,并不能起到攻擊的作用。
  • 使用正則表達(dá)式過濾傳入的參數(shù)

注意: 永遠(yuǎn)也不要把未經(jīng)檢查的用戶輸入的值直接傳給數(shù)據(jù)庫

  • java中的驗(yàn)證字符串是否包含sql的判斷
  1. package cn.javanode.thread; 
  2.  
  3. import java.util.regex.Pattern; 
  4.  
  5. /** 
  6.  * @author xgt(小光頭) 
  7.  * @version 1.0 
  8.  * @date 2021-1-8 11:48 
  9.  */ 
  10. public class CheckSqlDemo { 
  11.  
  12.     /**正則表達(dá)式**/ 
  13.     private static String reg = "(?:')|(?:--)|(/\\*(?:.|[\\n\\r])*?\\*/)|" 
  14.             + "(\\b(select|update|union|and|or|delete|insert|trancate|char|into|substr|ascii|declare|exec|count|master|into|drop|execute)\\b)"
  15.  
  16.  
  17.     private static Pattern sqlPattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE); 
  18.  
  19.     private static boolean isValid(String str) { 
  20.         if (sqlPattern.matcher(str).find()) 
  21.         { 
  22.             System.out.println("未能通過過濾器:str=" + str); 
  23.             return false
  24.         } 
  25.         return true
  26.     } 
  27.  
  28.     public static void main(String[] args) { 
  29.         System.out.println(isValid("tongji_user_add")); 
  30.     } 
  31.  

 補(bǔ)充

PreparedStatement是如何防止SQL注入的?

1. 拼接參數(shù)(sql注入)

  1. Connection connection = DriverManager.getConnection(DB_URL, USER, PASS); 
  2.         PreparedStatement preparedStatement = connection.prepareStatement(sql);         
  3.         String param = "'test' or 1=1"
  4.         String sql = "select file from file where name = " + param; // 拼接SQL參數(shù) 
  5.         ResultSet resultSet = preparedStatement.executeQuery(); 
  6.         System.out.println(resultSet.next()); 

 輸出結(jié)果為 true ,DB中執(zhí)行的SQL為

  1. -- 永真條件1=1成為了查詢條件的一部分,可以返回所有數(shù)據(jù),造成了SQL注入問題 
  2. select file from file where name = 'test' or 1=1 

 2. setString (防注入) 

  1. Connection connection = DriverManager.getConnection(DB_URL, USER, PASS); 
  2.         PreparedStatement preparedStatement = connection.prepareStatement(sql);     
  3.         preparedStatement.setString(1,account);//設(shè)置參數(shù) 
  4.         preparedStatement.setString(2,password); 
  5.         ResultSet resultSet = preparedStatement.executeQuery();//執(zhí)行查詢sql,獲取結(jié)果集 

 輸出結(jié)果為 false ,DB中執(zhí)行的SQL為 

  1. select file from file where name = '\'test\' or 1=1' 

我們可以看到輸出的 SQL是把整個(gè)參數(shù)用引號包起來,并把參數(shù)中的引號作為轉(zhuǎn)義字符,從而避免了參數(shù)也作為條件的一部分

3. 源碼分析

結(jié)論 

  • preparedStatement.setString 會判斷當(dāng)前參數(shù)的符號是否需要轉(zhuǎn)義,是的話加的轉(zhuǎn)義符
  • 如果不需要,則直接加上引號

  1. //完整代碼 
  2. public void setString(int parameterIndex, String x) throws SQLException { 
  3.        synchronized (checkClosed().getConnectionMutex()) { 
  4.            // if the passed string is nullthen set this column to null 
  5.            if (x == null) { 
  6.                setNull(parameterIndex, Types.CHAR); 
  7.            } else { 
  8.                checkClosed(); 
  9.  
  10.                int stringLength = x.length(); 
  11.  
  12.                if (this.connection.isNoBackslashEscapesSet()) { 
  13.                    // Scan for any nasty chars 
  14.                    // 判斷是否需要轉(zhuǎn)義 
  15.                    boolean needsHexEscape = isEscapeNeededForString(x, stringLength); 
  16.  
  17.                    if (!needsHexEscape) { 
  18.                        byte[] parameterAsBytes = null
  19.  
  20.                        StringBuilder quotedString = new StringBuilder(x.length() + 2); 
  21.                        quotedString.append('\''); 
  22.                        quotedString.append(x); 
  23.                        quotedString.append('\''); 
  24.  
  25.                        if (!this.isLoadDataQuery) { 
  26.                            parameterAsBytes = StringUtils.getBytes(quotedString.toString(), this.charConverter, this.charEncoding, 
  27.                                    this.connection.getServerCharset(), this.connection.parserKnowsUnicode(), getExceptionInterceptor()); 
  28.                        } else { 
  29.                            // Send with platform character encoding 
  30.                            parameterAsBytes = StringUtils.getBytes(quotedString.toString()); 
  31.                        } 
  32.  
  33.                        setInternal(parameterIndex, parameterAsBytes); 
  34.                    } else { 
  35.                        byte[] parameterAsBytes = null
  36.  
  37.                        if (!this.isLoadDataQuery) { 
  38.                            parameterAsBytes = StringUtils.getBytes(x, this.charConverter, this.charEncoding, this.connection.getServerCharset(), 
  39.                                    this.connection.parserKnowsUnicode(), getExceptionInterceptor()); 
  40.                        } else { 
  41.                            // Send with platform character encoding 
  42.                            parameterAsBytes = StringUtils.getBytes(x); 
  43.                        } 
  44.  
  45.                        setBytes(parameterIndex, parameterAsBytes); 
  46.                    } 
  47.  
  48.                    return
  49.                } 

 【編輯推薦】

 

責(zé)任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2011-10-19 10:47:56

2010-04-26 16:31:09

Oracle SQL

2009-09-23 10:43:22

2024-08-26 15:31:55

2012-11-08 17:02:58

2016-09-06 13:40:20

2009-09-17 12:49:31

2010-09-14 16:28:52

2019-01-15 09:24:07

2012-11-14 17:18:58

2010-06-30 17:56:06

2019-09-17 10:06:46

數(shù)據(jù)庫程序員網(wǎng)絡(luò)安全

2010-12-20 16:04:30

2014-10-08 10:01:06

2011-07-12 10:38:10

2013-01-10 18:00:09

2020-08-30 14:34:42

Java語言安全編碼web安全

2016-09-28 16:38:47

2017-08-10 10:23:59

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號