夺命雷公狗---PDO NO:16 SQL注入讲解

//1. ‘ or ”=’ (不成功)
//2. ‘ or ”=” or ”='(成功)
//3. ‘ or ”=”#(可以)
//4. ‘ or 1=1#(可以)
//5. ‘ or 1=1;delete from bbs_user where qx=2;#(可以, 但是mysql_query()每次只能执行一个SQL语句)

select * from users where id = 1 or ”=”
where 1=1;
$str = “”=””;
echo $str;

//sql语句的拼装(将用户输入的内容当成了sql语句格式的一部分)
// $sql = “select username, password from bbs_user where username={$username} and password ={$password}”;

// pdo (预处理: 防sql注入, 将内容和sql语句分开)
// $sql = “select username, password from bbs_user where username=? and password =?”;

 

 

举例说明:

 

  1. 画出一个框图
  2. 搭建我们的php开发环境.
  3. 创建数据库和用户表(users)

3.1 create database spdb;

--创建一张用户表

create table users (

id int primary key auto_increment; --id

username varchar(64) unique not null, --用户名

password varchar(64) not null,--密码

email varchar(64) not null)

 

--添加两个测试用户

 

insert into users (username,password,email) values('shunping','shunping','shunping@sohu.com');

insert into users (username,password,email) values('xiaoming','xiaoming','xiaoming@sohu.com');

 

  1. 开发php页面

php项目默认应当防在 htdos目前

 

 

Login.php

 

 

LoginCl.php

 

 

ManageUsers.php

 

  1. 注意事项:

对应我们的php初学者,我们写

① $sql="select * from users where username='$username' and password='$password'";

5.1  

使用万能密码:  bb' or 1='1

使用万能用户名 xx' union select * from users/*

 

② $sql="select * from users where username=$username and password=$password";

这种写法,没有’’ ,我们的mysql数据库会把你的输入当做 数字对待

使用万能密码 

33 union select * from users;

使用万能用户名:

33 union select * from users/*

 

select * from users where username=89 union select * from users/* and password=90;

 

 

  1. 如何解决sql注入问题?

 

① 在服务器中 magic_quotes_gpc 设置on,php.ini文件中修改

$sql="select * from users where username='$username' and password='$password'"; 的万能密码和用户名就失效.

 

② 在服务器中 magic_quotes_gpc 设置on,php.ini文件中修改

$sql="select * from users where username=$username and password=$password"; 的万能密码和用户名还是可以攻击.

 

 

☞ 当我们的magic_quotes_gpc设置成on后,服务器就会对所有的 ‘ 加入 \转义

name=’lll’  当数据库执行  name=\’111\’ 高手 char()

 

  1. 我们现在使用第一种方案来防止登录用户注入

7.1 密码比对

思想首先通过用户输入的用户名去查询数据库,如果查询到这个用户对应的密码,则和用户提交的密码比对,相同则说明该用户合法反之,则说明该用户非法

 

  1. 使用pdo来解决注入

8.1 在php.ini文件中启用pdo

extension=php_pdo_mysql.dll 前面的;去掉即可.

posted @ 2015-12-09 15:17  夺命雷公狗  阅读(254)  评论(0编辑  收藏  举报