奪命雷公狗---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語句)php

select * from users where id = 1 or 」=」
where 1=1;
$str = 「」=」」;
echo $str;mysql

//sql語句的拼裝(將用戶輸入的內容當成了sql語句格式的一部分)
// $sql = 「select username, password from bbs_user where username={$username} and password ={$password}」;sql

// pdo (預處理: 防sql注入, 將內容和sql語句分開)
// $sql = 「select username, password from bbs_user where username=? and password =?」;數據庫

 

 

舉例說明:服務器

 

  1. 畫出一個框圖
  2. 搭建咱們的php開發環境.
  3. 建立數據庫和用戶表(users)

3.1 create database spdb;測試

--建立一張用戶表spa

create table users (pdo

id int primary key auto_increment; --id開發

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

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 前面的;去掉便可.

相關文章
相關標籤/搜索