码迷,mamicode.com
首页 > 数据库 > 详细

常见sql注入原理详解!

时间:2017-11-17 00:06:26      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:sql注入

1、首先我们创建一个mysqli的链接


技术分享


/**数据库配置*/

$config = [‘hostname‘=>"localhost", ‘port‘=>"3306", ‘username‘=>"root",‘password‘=>‘‘,‘db‘=>‘sql‘];


/**接收参数*/

$id = $_GET[‘id‘]?$_GET[‘id‘]:"";


if(empty($id)){

echo "article is not def"

}


/**链接数据库*/

$mysqli = new \mysqli($config[‘hostname‘],$config[‘username‘],$config[‘password‘],$config[‘db‘]);


/**设置编码*/

$mysqli->set_charset("utf8");


url数字注入结果测试

我们访问url:http://localhost/mysql/index.php?id=1


结果展示:

array (size=2)  ‘article_id‘ => string ‘1‘ (length=1)  ‘title‘ => string ‘思梦php编写:PHP操作Redis详解案例‘ (length=44)


(1)/当我们在在url上稍作修改时:

http://localhost/mysql/index.php?id=1‘   //加一个单引号

这样你的sql语句就会报错


(2)我们再次修改url的时候

http://localhost/mysql/index.php?id=-1 or 1=1  //后面参数修改成这样


结果展示了所有的文章列表

D:\wamp\www\mysql\index.php:11:array (size=2)  ‘article_id‘ => string ‘1‘ (length=1)  ‘title‘ =>string ‘思梦php编写:PHP操作Redis详解案例‘ (length=44)

D:\wamp\www\mysql\index.php:11:array (size=2)  ‘article_id‘ => string ‘2‘ (length=1)  ‘title‘ =>string ‘Mysql存储过程从0开始(上)‘ (length=36)

D:\wamp\www\mysql\index.php:11:array (size=2)  ‘article_id‘ => string ‘3‘ (length=1)  ‘title‘ =>string ‘思梦php编写:PHP排序的几种方法‘ (length=42).............


技术分享


2、表单注入,主要利用sql语句的注释


$username = $_POST[‘username‘]?$_POST[‘username‘]:"";

$password = $_POST[‘password‘]?$_POST[‘password‘]:"";

$sql = "select * from tb_member where account=‘$username‘AND password=‘$pass‘";

$res = $mysqli->query($sql);

$row = $res->fetch_assoc();

if($row){

echo "登录成功!";

}else{

echo "账号密码错误!";

}

正常输入


技术分享

打印:登录成功!

我们简单修改一下


技术分享

打印:登录成功!


技术分享


原理:首先使用单引号结束sql语句,然后加#注释后面的sql语句


技术分享

同理另一种方式为

技术分享

打印:登录成功!

原理:首先使用单引号结束sql语句,然后加(-- )注释后面的sql语句


技术分享


技术分享


本文出自 “13274080” 博客,请务必保留此出处http://13284080.blog.51cto.com/13274080/1982615

常见sql注入原理详解!

标签:sql注入

原文地址:http://13284080.blog.51cto.com/13274080/1982615

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!