常见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语句

 

 

posted @ 2017-11-16 21:35  思梦PHP  阅读(1707)  评论(0编辑  收藏  举报