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

讓你徹底明白SQL注入

運維 數(shù)據(jù)庫運維
SQL注入攻擊是黑客對數(shù)據(jù)庫進行攻擊常用的手段之一,隨著B/S模式應(yīng)用開發(fā)的發(fā)展,使用這種模式編寫應(yīng)用程序的程序員也越來越多。

[[356244]]

SQL注入攻擊是黑客對數(shù)據(jù)庫進行攻擊常用的手段之一,隨著B/S模式應(yīng)用開發(fā)的發(fā)展,使用這種模式編寫應(yīng)用程序的程序員也越來越多。但是由于程序員的水平及經(jīng)驗參差不齊,相當大一部分程序員在編寫代碼的時候,沒有對用戶輸入數(shù)據(jù)的合法性進行判斷,使應(yīng)用程序存在安全隱患。用戶可以提交一段數(shù)據(jù)庫查詢代碼,根據(jù)程序返回的結(jié)果,獲得某些他想獲取的數(shù)據(jù),這就是所謂的SQL Injection,即SQL注入。

一、背景假如某高校開發(fā)了一個網(wǎng)課系統(tǒng),要求學生選課后完成學習,數(shù)據(jù)庫中有一張表course,這張表存放著每個學生的選課信息及完成情況,具體設(shè)計如下:

 


 

 

 

數(shù)據(jù)如下:

 

本系統(tǒng)采用mysql做為數(shù)據(jù)庫,使用Jdbc來進行數(shù)據(jù)庫的相關(guān)操作。系統(tǒng)提供了一個功能查詢該學生的課程完成情況,代碼如下。

  1. @RestController 
  2. public class Controller { 
  3.      
  4.     @Autowired 
  5.     SqlInject sqlInject; 
  6.      
  7.     @GetMapping("list"
  8.     public List<Course> courseList(@RequestParam("studentId") String studentId){ 
  9.         List<Course> orders = sqlInject.orderList(studentId); 
  10.         return orders; 
  11.     } 
  1. @Service 
  2. public class SqlInject { 
  3.  
  4.     @Autowired 
  5.     private JdbcTemplate jdbcTemplate; 
  6.      
  7.     public List<Course> orderList(String studentId){ 
  8.         String sql = "select id,course_id,student_id,status from course where student_id = "+ studentId; 
  9.         return jdbcTemplate.query(sql,new BeanPropertyRowMapper(Course.class)); 
  10.     } 

二、注入攻擊演示**1 **. 正常情況下查詢一個學生所選課程及完成情況只需要傳入student_id,便可以查到相關(guān)數(shù)據(jù)。

 

根據(jù)響應(yīng)結(jié)果,我們很快便能寫出對應(yīng)的sql,如下:

  1. select id,course_id,student_id,status  
  2. from course  
  3. where student_id = 4 

2. 如果我們想要獲取這張表的所有數(shù)據(jù),只需要保證上面這個sql的where條件恒真就可以了。

  1. select id,course_id,student_id,status  
  2. from course  
  3. where student_id = 4 or 1 = 1  

請求接口的時候?qū)tudendId 設(shè)置為4 or 1 = 1,這樣這條sql的where條件就恒真了。sql也就等同于下面這樣

  1. select id,course_id,student_id,status  
  2. from course  

請求結(jié)果如下,我們拿到了這張表的所有數(shù)據(jù)

 

3. 查詢mysql版本號,使用union拼接sql

  1. union select 1,1,version(),1 

 

4. 查詢數(shù)據(jù)庫名

  1. union select 1,1,database(),1 

 

5. 查詢mysql當前用戶的所有庫

  1. union select 1,1, (SELECT GROUP_CONCAT(schema_name) FROM information_schema.schemata) schemaName,1 

 

看完上面這些演示后,你害怕了嗎?你所有的數(shù)據(jù)配置都完全暴露出來了,除此之外,還可以完成很多操作,更新數(shù)據(jù)、刪庫、刪表等等。

三、如何防止sql注入

1. 代碼層防止sql注入攻擊的最佳方案就是sql預編譯

  1. public List<Course> orderList(String studentId){ 
  2.     String sql = "select id,course_id,student_id,status from course where student_id = ?"
  3.     return jdbcTemplate.query(sql,new Object[]{studentId},new BeanPropertyRowMapper(Course.class)); 

這樣我們傳進來的參數(shù) 4 or 1 = 1就會被當作是一個student_id,所以就不會出現(xiàn)sql注入了。

2. 確認每種數(shù)據(jù)的類型,比如是數(shù)字,數(shù)據(jù)庫則必須使用int類型來存儲

3. 規(guī)定數(shù)據(jù)長度,能在一定程度上防止sql注入

4. 嚴格限制數(shù)據(jù)庫權(quán)限,能最大程度減少sql注入的危害

5. 避免直接響應(yīng)一些sql異常信息,sql發(fā)生異常后,自定義異常進行響應(yīng)

6. 過濾參數(shù)中含有的一些數(shù)據(jù)庫關(guān)鍵詞

  1. @Component 
  2. public class SqlInjectionFilter implements Filter { 
  3.     @Override 
  4.     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { 
  5.         HttpServletRequest req=(HttpServletRequest)servletRequest; 
  6.         HttpServletRequest res=(HttpServletRequest)servletResponse; 
  7.         //獲得所有請求參數(shù)名 
  8.         Enumeration params = req.getParameterNames(); 
  9.         String sql = ""
  10.         while (params.hasMoreElements()) { 
  11.             // 得到參數(shù)名 
  12.             String name = params.nextElement().toString(); 
  13.             // 得到參數(shù)對應(yīng)值 
  14.             String[] value = req.getParameterValues(name); 
  15.             for (int i = 0; i < value.length; i++) { 
  16.                 sql = sql + value[i]; 
  17.             } 
  18.         } 
  19.         if (sqlValidate(sql)) { 
  20.             throw new IOException("您發(fā)送請求中的參數(shù)中含有非法字符"); 
  21.         } else { 
  22.             chain.doFilter(servletRequest,servletResponse); 
  23.         } 
  24.     } 
  25.  
  26.     /** 
  27.      * 關(guān)鍵詞校驗 
  28.      * @param str 
  29.      * @return 
  30.      */ 
  31.     protected static boolean sqlValidate(String str) { 
  32.         // 統(tǒng)一轉(zhuǎn)為小寫 
  33.         str = str.toLowerCase(); 
  34.         // 過濾掉的sql關(guān)鍵字,可以手動添加 
  35.         String badStr = "'|and|exec|execute|insert|select|delete|update|count|drop|*|%|chr|mid|master|truncate|" + 
  36.                 "char|declare|sitename|net user|xp_cmdshell|;|or|-|+|,|like'|and|exec|execute|insert|create|drop|" + 
  37.                 "table|from|grant|use|group_concat|column_name|" + 
  38.                 "information_schema.columns|table_schema|union|where|select|delete|update|order|by|count|*|" + 
  39.                 "chr|mid|master|truncate|char|declare|or|;|-|--|+|,|like|//|/|%|#"
  40.         String[] badStrs = badStr.split("\\|"); 
  41.         for (int i = 0; i < badStrs.length; i++) { 
  42.             if (str.indexOf(badStrs[i]) >= 0) { 
  43.                 return true
  44.             } 
  45.         } 
  46.         return false
  47.     } 

本文轉(zhuǎn)載自微信公眾號「Java旅途」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系Java旅途公眾號。

 

責任編輯:武曉燕 來源: Java旅途
相關(guān)推薦

2019-02-25 09:20:53

2017-06-07 18:40:33

PromiseJavascript前端

2019-07-26 10:15:06

Redis數(shù)據(jù)庫

2021-01-15 07:44:21

SQL注入攻擊黑客

2024-05-16 12:24:53

2019-11-28 08:59:03

SQL注入網(wǎng)絡(luò)攻擊網(wǎng)絡(luò)安全

2022-01-16 18:55:33

MySQL事務(wù)數(shù)據(jù)庫

2023-12-27 08:16:54

Sessiontoken安全性

2014-03-27 15:57:45

Android組件Activity

2020-11-03 10:32:48

回調(diào)函數(shù)模塊

2024-01-17 08:18:14

RPAJava技術(shù)

2010-12-20 16:04:30

2022-07-27 08:31:28

SQL開發(fā)控制

2021-08-11 22:17:48

負載均衡LVS機制

2021-10-20 08:49:30

Vuexvue.js狀態(tài)管理模式

2021-11-07 23:46:32

MySQLSQL索引

2009-06-16 11:44:00

Java IO系統(tǒng)

2024-05-09 09:09:19

組合模式對象

2024-05-10 08:43:04

外觀模式接口系統(tǒng)

2024-05-13 10:45:25

中介模式面向?qū)ο?/a>數(shù)量
點贊
收藏

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