English 中文(简体)
我注射的密码安全吗?
原标题:Is my code safe for injection?

我之前在使用 MySQL, 并被告知这是不安全的, 所以现在我重新编码了在 PDO 中的管理员登录板, 用户和其他论坛都说不能注射。 但是黑客仍然在进入... 我编辑了登录后的网页, 告诉黑客告诉我我在上面放了什么, 黑客告诉我...

我需要知道我的密码是否安全 他告诉我他通过SQL进入

所以我首先把他们的IP存储在会话中, 所以如果他们的IP更改了, 它会把它们记录出来( 或用户名)

if ( isset($_SESSION[ last_ip ]) == false )
{
    $_SESSION[ last_ip ] = $_SERVER[ REMOTE_ADDR ];
}
if ( $_SESSION[ last_ip ] !== $_SERVER[ REMOTE_ADDR ] )
{
    session_unset();
    session_destroy();
}

那么这是我的登录:

session_start();
include  functions/functions.php ;  
$db = mysqlconnect();
$password = md5($_POST[ mypassword ]);
$mod = 1; 
$statement = $db->prepare("SELECT * FROM users WHERE username = ? AND password = ?");
$statement->execute(array($_POST[ myusername ],$password));
$result = $statement->fetchObject()->mod;
$count = $statement->rowCount();
if ( $result == 1 ) {
    $db = mysqlconnect();
    // Register $myusername, $mypassword and redirect to file "login_success.php"
    $_SESSION[ user ] = $_POST[ myusername ] ;
    //Test if it is a shared client
    if ( !empty($_SERVER[ HTTP_CLIENT_IP ]) ) {
        $ip=$_SERVER[ HTTP_CLIENT_IP ];
        //Is it a proxy address
    } elseif ( !empty($_SERVER[ HTTP_X_FORWARDED_FOR ]) ) {
        $ip=$_SERVER[ HTTP_X_FORWARDED_FOR ];
    } else {
        $ip=$_SERVER[ REMOTE_ADDR ];
    }
    $sqll = "UPDATE users SET lastip=? WHERE username=?";
    $q = $db->prepare($sqll);
    $q->execute(array($ip,$_SESSION[ username ]));
    $_SESSION[ user ] = $_POST[ myusername ] ;
    $sqlll = "INSERT INTO user_log (username,ip) VALUES (?, ?)";
    $qq = $db->prepare($sqlll);
    $qq->execute(array($_SESSION[ username ],$ip));
    header("Location: home.php");
} else {
    echo "Wrong Username or Password";
}

密码能注射吗?

这是我家的.php页面 阻止用户观看它。

/// My conenct is here                    
$sql = "SELECT * FROM users WHERE username= $_SESSION[user] ";
$result = mysql_query($sql) or die(mysql_error());
$values = mysql_fetch_array($result);


if( isset($_SESSION[ user ]) ) {

} else {
    echo "Bye Bye";
    die;
}

if ( $values[ mod ] == 1 ) {    
    echo "welcome";  
} else {
    echo"Your account has been reported for hacking";
    die;
}
问题回答

Assuming the queries you show are the only queries you use, it s funny how the hacker uses the very code you thought would block him. Very often in the IT world the front door is triple locked but the backdoor is open. In this case it s not the backdoor, but the little window in the door that you use to see if you are not opening to a crook.

使用已准备好的发言稿和对所有查询有约束力的变量。

这是一般规则 你只是忘记了"所有查询"中的"所有"

您的声明 :

$sql = "SELECT * FROM users WHERE username= $_SESSION[user] ";

is your open window. Quite obviously your hacker spoofed the username in the session data. The simple solution is to:

  1. validate the username or any data you receive from outside ($_SESSION, $_POST, some $_SERVER and others) BEFORE you do anything with it. Certainly before you perform any database interaction.
  2. use variable binding like you did in the other queries.

在此情况下删除标签或使用 Mysql_ real_escape_string 的价值较低, 因为黑客完全有可能没有无法存储的字符 。

顺便说一句,您并不真的需要使用 PDO 来使用变量绑定。 这在 Mysql 中也是完全可能的 。

$verbLok = mysqli_connect(Iam, not, gonna, tellyou);
$result = array();
if (($stmtLok = $verbLok->prepare( "SELECT * FROM users WHERE username = ?  limit 1")))
{        
    $stmtLok->bind_param("s", $_SESSION[user]);
    $stmtLok->execute();
    $result = $stmtLok->get_result();
    $stmtLok->close();
}
mysqli_close($verbLok);

$present 将保留一系列结果行。 我不再熟悉常规查询, 但据我所知它与您的代码相似 。

有关您查询中 SQL 密码注射攻击的可能性的有趣读物,请查阅 在本页

良好的 SQL 注射预防是避开可用于终止 SQL 命令的中度评价以启动新命令的任何和所有文字。

要做到这一点,您应该避开输入中的任何引号, 并使用更好的 SQL —— 使用字段和表格名称周围的回ickticks, 使用引号来包扎任何非代码文本等 。

SELECT * FROM `users` WHERE `id` =  1 

然后以 mysql_real_escape_string 或以# 039 等手动替换 来解开所有引号, 还可以避免任何 HTML, 以确保它不会弄乱其它东西, 使用 htmlities 等 。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签