mysql語句的注入錯誤是什么?

mysql語句的注入式錯誤就是利用某些數(shù)據(jù)庫的外部接口將用戶數(shù)據(jù)插入到實際的SQL語言當中,從而達到入侵數(shù)據(jù)庫乃至操作系統(tǒng)的目的。攻擊者利用它來讀取、修改或者刪除數(shù)據(jù)庫內的數(shù)據(jù),獲得數(shù)據(jù)庫中用戶資料和密碼等信息,更嚴重會獲得管理員的權限。

mysql語句的注入錯誤是什么?

(推薦教程:mysql視頻教程

sql注入式錯誤(SQL injection)

SQL Injection 就是利用某些數(shù)據(jù)庫的外部接口將用戶數(shù)據(jù)插入到實際的數(shù)據(jù)庫操作語言(SQL)當中,從而達到入侵數(shù)據(jù)庫乃至操作系統(tǒng)的目的。它的產生主要是由于程序對用戶輸入的數(shù)據(jù)沒有進行嚴格的過濾,導致非法數(shù)據(jù)庫查詢語句的執(zhí)行。
《深入淺出 MySQL》

危害
攻擊者利用它來讀取、修改或者刪除數(shù)據(jù)庫內的數(shù)據(jù),獲得數(shù)據(jù)庫中用戶資料和密碼等信息,更嚴重的就是獲得管理員的權限。

例子

 //注入式錯誤     public static void test3(String name,String passward){         Connection connection = null;         Statement st = null;         ResultSet rs = null;         try {             // 加載JDBC 驅動             Class.forName("com.mysql.jdbc.Driver");             // 獲得JDBC 連接             String url = "jdbc:mysql://localhost:3306/tulun";             connection = DriverManager.getConnection(url,"root","123456");             //創(chuàng)建一個查詢語句             st = connection.createStatement();             //sql語句             String sql = "select * from student where name = '"+ name+"' and passward = '"+passward+"'";             rs = st.executeQuery(sql);              if(rs.next()){                 System.out.println("登錄成功。");             }else{                 System.out.println("登錄失敗。");             }          } catch (Exception e) {             e.printStackTrace();         }     }     public static void main(String[] args) {                 test3("wjm3' or '1 = 1","151515");      }

數(shù)據(jù)庫信息
mysql語句的注入錯誤是什么?
如上面的代碼所示,用戶名為wjm3’ or ‘1 = 1,密碼為151515,從數(shù)據(jù)庫中可以看出我們沒有這樣的用戶,本來應該顯示登錄失敗,但是結果卻是登陸成功,因為or ‘1 = 1 已經(jīng)不是用戶名里面的內容了,它現(xiàn)在為SQL 語句里面的內容,不論如何,結果都為true,等于不用輸密碼都可以登錄。這里就產生了安全問題。

解決方法

1. PrepareStatement

 //注入式錯誤     public static void test3(String name,String passward){         Connection connection = null;         PreparedStatement st = null;         ResultSet rs = null;         try {             // 加載JDBC 驅動             Class.forName("com.mysql.jdbc.Driver");             // 獲得JDBC 連接             String url = "jdbc:mysql://localhost:3306/tulun";             connection = DriverManager.getConnection(url,"root","123456");             //創(chuàng)建一個查詢語句             String sql1 =  "select * from student where name = ? and passward = ?";             st = connection.prepareStatement(sql1);            st.setString(1,name);            st.setString(2,passward);             //sql語句             //String sql = "select * from student where name = '"+ name+"' and passward = '"+passward+"'";             rs = st.executeQuery();              if(rs.next()){                 System.out.println("登錄成功。");             }else{                 System.out.println("登錄失敗。");             }          } catch (Exception e) {             e.printStackTrace();         }finally{             try {                 connection.close();                 st.close();                 rs.close();             } catch (SQLException e) {                 e.printStackTrace();             }         }     }      public static void main(String[] args) {         test3("wjm3' or '1 = 1","151515");     }

上面這個代碼不管name 參數(shù)是什么,它都只是name 參數(shù),不會作為sql語句的一部分來執(zhí)行,一般來說推薦這個方法,比較安全。
2.自己定義函數(shù)進行校驗

  • 整理數(shù)據(jù)使之變得有效
  • 拒絕已知的非法輸入
  • 只接受已知的合法輸入

所以如果想要獲得最好的安全狀態(tài),目前最好的解決辦法就是對用戶提交或者可能改變的數(shù)據(jù)進行簡單分類,分別應用正則表達式來對用戶提供的輸入數(shù)據(jù)進行嚴格的檢測和驗證。
其實只需要過濾非法的符號組合就可以阻止已知形式的攻擊,并且如果發(fā)現(xiàn)更新的攻擊符號組合,也可以將這些符號組合增添進來,繼續(xù)防范新的攻擊。特別是空格符號和其產生相同作用的分隔關鍵字的符號,例如“/**/”,如果能成功過濾這種符號,那么有很多注入攻擊將不能發(fā)生,并且同時也要過濾它們的十六進制表示“%XX”。

以上就是

? 版權聲明
THE END
喜歡就支持一下吧
點贊9 分享