web 安全之sql 注入(一)

来源:互联网 发布:无懈可击知乎 编辑:程序博客网 时间:2024/05/17 04:29

什么是sql 注入:SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令。

 下面进行简单演示:

html 代码:

<!doctype html><html><header>  <title>    sql 注入  </title></header><body><form method='get' action='login.php'><table><thead>sql 注入</thead><tr><th>用户名:</th><td><input type='input' name='name'></td></tr><tr><th>密码:</th><td><input type='password' name='password'></td></tr><tr><th style="colspan:2"><input type='submit' value='提交'/></th></tr></table></form></body></html>

php 代码:

<?phpclass conn{private $zi_con = null;//建立数据库连接public function __construct(){//使用mysqli 创建数据库连接   $this->zi_con = mysqli_connect("localhost","root","root","xss");   if(!$this->zi_con){   die("cannot connnect to the service".mysql_error());   }   }//判断是否用户名和密码存在public function query_user($user,$passwd){$sql = "select * from user where name ='{$user}' and password = '{$passwd}'";echo $sql."<br />"; $result =$this->zi_con-> query($sql); if($result->num_rows){ return true; }else{ return false; }}//关闭连接public function __destruct(){mysqli_close($this->zi_con);}}$obj_data = new conn();if($obj_data->query_user($_GET['name'],$_GET['password'])){echo "welcome ".$_GET['name'];}else{echo "no exists!";}?>

好戏开始:

在用户名框内输入:

' or 1 = 1 #

如图:


 结果:



怎样进行防范? 分析方面sql 不就给我们答案了吗?

在查询之前先判断一下不就行了吗?

if(empty($user) or empty($passwd) ){echo "用户名或密码为空!";}

这是最简单的sql 注入,算是通过这个了解一下sql 注入吧