$okay = 0; $username = $_POST['user']; $pw = $_POST['pw']; $sql = “select count(*) as ctr from users where username='”.mysql_real_escape_string($username).”' and password='”. mysql_real_escape_string($pw).”' limit 1″; $result = mysql_query($sql); while ($data = mysql_fetch_object($result)){ if ($data->ctr == 1){ //they're okay to enter the application! $okay = 1; } } if ($okay){ $_SESSION['loginokay'] = true; header(”index.php”); }else{ header(”login.php”); } ?>
使用 mysql_real_escape_string() 作为用户输入的包装器,就可以避免用户输入中的任何恶意 SQL 注入。如果用户尝试通过 SQL 注入传递畸形的密码,那么会将以下查询传递给数据库: select count(*) as ctr from users where username='foo' and password='\' or \'1\'=\'1′ limit 1″ 数据库中没有任何东西与这样的密码匹配。仅仅采用一个简单的步骤,就堵住了 Web 应用程序中的一个大漏洞。这里得出的经验是,总是应该对 SQL 查询的用户输入进行转义。 但是,还有几个安全漏洞需要堵住。下一项是操纵 GET 变量。 防止用户操纵 GET 变量 在前一节中,防止了用户使用畸形的密码进行登录。如果您很聪明,应该应用您学到的方法,确保对 SQL 语句的所有用户输入进行转义。 但 是,用户现在已经安全地登录了。用户拥有有效的密码,并不意味着他将按照规则行事 —— 他有很多机会能够造成损害。例如,应用程序可能允许用户查看特殊的内容。所有链接指向 template.php?pid=33 或 template.php?pid=321 这样的位置。URL 中问号后面的部分称为查询字符串。因为查询字符串直接放在 URL 中,所以也称为 GET 查询字符串。 在 PHP 中,如果禁用了 register_globals,那么可以用 $_GET['pid'] 访问这个字符串。在 template.php 页面中,可能会执行与清单 8 相似的操作。 清单 8. 示例 template.php
代码如下:
$pid = $_GET['pid']; //we create an object of a fictional class Page $obj = new Page; $cOntent= $obj->fetchPage($pid); //and now we have a bunch of PHP that displays the page ?>
$pid = $_GET['pid']; if (is_numeric($pid)){ //we create an object of a fictional class Page $obj = new Page; $cOntent= $obj->fetchPage($pid); //and now we have a bunch of PHP that displays the page }else{ //didn't pass the is_numeric() test, do something else! } ?>
$pid = $_GET['pid']; if (strlen($pid)){ if (!ereg(”^[0-9]+$”,$pid)){ //do something appropriate, like maybe logging them out or sending them back to home page } }else{ //empty $pid, so send them back to the home page } //we create an object of a fictional class Page, which is now //moderately protected from evil user input $obj = new Page; $cOntent= $obj->fetchPage($pid); //and now we have a bunch of PHP that displays the page ?>
class Page{ function fetchPage($pid){ $sql = “select pid,title,desc,kw,content,status from page where pid='”.mysql_real_escape_string($pid).”'”; } } ?>
$pid = $_GET['pid']; if (strlen($pid)){ if (!ereg(”^[0-9]+$”,$pid) && strlen($pid) > 5){ //do something appropriate, like maybe logging them out or sending them back to home page } } else { //empty $pid, so send them back to the home page } //we create an object of a fictional class Page, which is now //even more protected from evil user input $obj = new Page; $cOntent= $obj->fetchPage($pid); //and now we have a bunch of PHP that displays the page ?>
为 什么既提供 maxlength 属性,又在后端进行 substr() 检查?因为纵深防御总是好的。浏览器防止用户输入 PHP 或 MySQL 不能安全地处理的超长字符串(想像一下有人试图输入长达 1,000 个字符的名称),而后端 PHP 检查会确保没有人远程地或者在浏览器中操纵表单数据。 正如您看到的,这种方式与前一节中使用 strlen() 检查 GET 变量 pid 的长度相似。在这个示例中,忽略长度超过 5 位的任何输入值,但是也可以很容易地将值截短到适当的长度,如下所示: 清单 14. 改变输入的 GET 变量的长度
代码如下:
$pid = $_GET['pid']; if (strlen($pid)){ if (!ereg(”^[0-9]+$”,$pid)){ //if non numeric $pid, send them back to home page } }else{ //empty $pid, so send them back to the home page } //we have a numeric pid, but it may be too long, so let's check if (strlen($pid)>5){ $pid = substr($pid,0,5); } //we create an object of a fictional class Page, which is now //even more protected from evil user input $obj = new Page; $cOntent= $obj->fetchPage($pid); //and now we have a bunch of PHP that displays the page ?>