douyan1244 2012-03-30 08:19
浏览 67
已采纳

如何在PHP和MySQL中找到任何潜在的安全问题

Since I'm not an Php programmer I have to ask you, I have to challenge to investigate some source which is currently under attack. Its some local news site with custom cms inside. Its phg mysql under shared linux hosting env.

admin is registered on entering credentials in login form and those credentials are checked in db like this:

<?php
    session_start();
        include 'db.php';

    $connection = mysql_connect($dbHost, $dbUser, $dbPass) or die(mysql_error());
    mysql_select_db($dbName, $connection) or die(mysql_error());

        $queryString = "SELECT * FROM `Admins` WHERE `username` = '$user_name' AND password='$password'";
        $safeSelect = mysql_real_escape_string($queryString);

        $query = "SELECT * FROM `Admins`
            WHERE `username` = '$user_name' AND password='$password'";
    $result = mysql_query($query, $connection) or die('error making query');
    $affected_rows = mysql_num_rows($result);

        if($affected_rows == 1) {
        //add the user to our session variables
    $_SESSION['username'] = $user_name;
    header("Location: http://www.mysite.com/admin/index.php");
        exit;
        //print 'allowed';
        }
    else {
        print 'access is not allowed !!!';
    }
?>

Auth.php

<?php
    session_start();
    include 'db.php';
    if (empty($_SESSION['username'])) {
        die('to access these page you have to be registered user.
        <a href="/admin/login.php">log in</a>');
    }
?>

This session var is used on whole administration area to recognize registered user. Admin user edits and creates new content like this edit.php

<?php
session_start();
include '/admin/db.php';
include '/admin/auth.php';
  ini_set("display_errors", 1);
    error_reporting(E_ALL);
    $dbcnx = mysql_connect('localhost', $dbUser, $dbPass);
    mysql_select_db($dbName);
    if (isset($_POST['submit'])):
        // content will be updated with these
        $id = $_POST['id'];
        $cats = $_POST['cats'];
        $newstext = $_POST['newstext'];

$sql = "UPDATE `News` SET
             `NewsText`='$newstext',
             `AID`='$aid',
         `imgID`='$imgID'
             WHERE `ID`='$id'";
 if (mysql_query($sql)) {
            echo('<p><b>content is succ. updated</b></p>');
        } else {
            die('<p>Error occured when updating content: ' .
                    mysql_error() . '</p>');
        }
else: // Allow user to edit content using ID=$id

            /* $aid = $_GET['aid']; */
             if (isset($_GET['id'])) {
                if (is_numeric($_GET['id']) == FALSE) {
                    echo "<h1>Page is not found</h1>";
                    session_destroy();
                return;
             }
                $id = $_GET['id'];
            }

$row = @mysql_query("SELECT `NewsText`, `Title`, `AID`, `imgID` FROM `News` WHERE `ID`='$id'");
            if (!$row) {
                die('<p>Db error: ' .
                        mysql_error() . '</p>');
            }
$row = mysql_fetch_array($row);
            $newstext = $row['NewsText'];
            $text = $row ['Title'];
            $authid = $row ['AID'];
            $imgID = $row ['imgID'];
            $newstext = htmlspecialchars($newstext);

//ommitting html form
?>

Basically I want to ask is there some security issue here.

Found solution here http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/

  • 写回答

4条回答 默认 最新

  • dongyilu3143 2012-03-30 08:24
    关注

    Straight off the bat it looks like there is a SQL injection issue going on here. POST requests are being put straight into an SQL query which allows someone with a specially crafted POST request to execute any query pretty much on the server...

    It might be worth looking at this...

    How can I prevent SQL injection in PHP?

    Hope that helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染