帮助MySQL查询

时间:2009-10-09 07:48:09

标签: php mysql

我被问到一个问题,但我甚至无法开始回答,所以有人可以让我知道如何开始如何回答它,

我不是在寻找一些如何回答它的教学答案

这里是:

假设“regsister_globals”和“magic_quotes_gpc”已打开,那么这段代码有什么问题?记录可能的孔,然后修复它们以产生安全版本(有4个错误)

$p = $_GET["p"];
if ($sp == "index.php") {

     if ($_get["id"] == 345)
          $filter - addslashes($_get["id"]);

     $sql = "SELECT * FROM users WHERE id = {$filter}";
     $row - mydql_fetch_assoc(mysql_query($sql));

     echo <<< HTML
     <html>
           ...... user details .....
     </html>
HTML. 
} else 
      include ($p);

6 个答案:

答案 0 :(得分:1)

谷歌搜索“SQL注入”和“输入验证”应该让你开始。

鉴于环境,

  

“假设   “regsister_globals”   和   “magic_quotes_gpc”   打开了“

我相信这个案例的目的是教你这两种设置的风险。

代码剪切实际上有4个错误与“永远不要信任源自你的脚本之外的任何信息”有关,并结合上面提到的php指令。

(除了4个错误之外还有很多错误;还有一些“-”应为“=”,小写“_get”这应该是大写等等,但我的猜测是这些只是错别字。)

答案 1 :(得分:1)

这应该让你开始:

第1洞:register_globals应该关闭 - 这是一场安全灾难。

$p = $_GET["p"];
// Where does $sp come from?
if ($sp == "index.php") {

     // What the hell? So much wrong with these two lines
     // 1. if id == 345 you don't need to addslashes
     // 2. "-" should be "="
     // 3. addslashes should be mysql_real_escape_string
     // 4. the if() should be removed so it runs every time
     if ($_get["id"] == 345)
          $filter - addslashes($_get["id"]);

     // SQL injection
     $sql = "SELECT * FROM users WHERE id = {$filter}";
     // Again with the "-" instead of "="
     // Typo in the function name
     // No error checking
     $row - mydql_fetch_assoc(mysql_query($sql));

     // No escaping of database input - vulnerable to XSS attacks
     echo <<< HTML
     <html>
           ...... user details .....
     </html>
HTML. // Should be ; not .
} else 
{
      // I can include /etc/passwd by manipulating the URL
      include ($p);
}

答案 2 :(得分:0)

代码容易受到SQL注入攻击,因为用户数据没有被转义。使用mysql_real_escape_string

答案 3 :(得分:0)

mydql_fetch_assoc应为mysql_fetch_assoc

答案 4 :(得分:0)

试试这个:

<?php
$p = $_GET["p"];
if ($p == "index.php" && $_get["id"] == 345) {

 $filter = mysql_real_escape_string($_get["id"]);

 $sql = "SELECT * FROM users WHERE id = {$filter}";
 $row = mysql_fetch_assoc(mysql_query($sql));

?>
<html>
 ...... user details .....
</html>
<?php
}
else if (strpos($p, '../')===false && file_exists($p)) {
 include $p;
}

?>

答案 5 :(得分:0)

<?php
    $allow_includes = array(
      'some1', 'some2'
    );
    $p = $_GET["p"];
    if ($p == "index.php") {
         if ($_GET["id"] == 345) {
              $filter = mysql_real_escape_string($_GET["id"]);
         }

         $sql = "SELECT * FROM users WHERE id = '{$filter}'";
         $row = mysql_fetch_assoc(mysql_query($sql));
 ?>
    HTML. 
 <?php
    }
    elseif ( in_array($p, $allow_includes) ) {
      include ($p);
    }
    else {
      echo "Error 404";
    }
 ?>