简体   繁体   English

表单中的SQL注入

[英]Sql injection from form

I test my site with Acunetix Web Vulnerability Scanner 8 and its say that i have 2 sql injection in my register page 我使用Acunetix Web Vulnerability Scanner 8测试了我的网站,并说我的注册页面中有2个sql注入

URL encoded POST input email was set to 1##xa7## URL encoded POST input username was set to 1##xa7## URL编码的POST输入电子邮件设置为1 ## xa7 ## URL编码的POST输入用户名设置为1 ## xa7 ##

Error message found: supplied argument is not a valid MySQL result 发现错误消息:提供的参数不是有效的MySQL结果

Php code for these two 这两个的php代码

 $username = mysql_real_escape_string(trim($_POST['username']));
 $email = mysql_real_escape_string(trim($_POST['email']));

        if ($username==!preg_match('/^[a-zA-Z0-9._]+$/', $username)){
         $error_stat = 1; 
              $message_error .= 'Error:invalid username.';
        }
         elseif (!filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL)) {
            $error_stat = 11; 
              $message_error .= 'Error:invalid email.';

          }

So how this can be vulnerable to sql injection 那么这如何容易受到sql注入的攻击

@Pekka @佩卡

 $checkusername = mysql_query("SELECT Username FROM users WHERE Username = '$username'");
    $checkemail = mysql_query("SELECT EmailAddress FROM users WHERE EmailAddress = '$email'");

    $username_exist = mysql_num_rows($checkusername);
    $email_exist = mysql_num_rows($checkemail);

And if one of them exist to show error: 并且如果其中之一显示错误:

<?php if ($error_stat > 0){ echo $message_error; }?>

If there is no errors 如果没有错误

$registerquery = mysql_query("INSERT INTO users (Username, password, EmailAddress,Activation,registered) VALUES('".$username."', '".$password."', '".$email."','".$activation."','".$date."')");

EDIT // 编辑//

So guys i make a another page with PDO and new error is: 所以伙计们,我用PDO制作了另一页,新的错误是:

SQLSTATE[HY000]: General error: 1267 Illegal mix of collations (cp1251_general_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='

This what the Acunetix show when i click on Launch the attack with HTTP Editor.And the username used by program is 1%c0%00xa7%c0a2 这是当我单击使用HTTP编辑器启动攻击时Acunetix所显示的内容。程序使用的用户名是1%c0%00xa7%c0a2

PDO PDO

By the program the username is = 1%c0%00xa7%c0a2 and error is in line 32 通过程序,用户名= 1%c0%00xa7%c0a2,错误在第32行

 line 30 $getMail = $dbh->prepare("SELECT EmailAddress FROM users WHERE username = :username");
    line 31 $getMail->bindParam(':username', $username);
    line 32 $getMail->execute();
    line 33 $rowMail = $getMail->fetch();
    line 34 $email = $rowMail['emailaddress'];

From what I can tell you should escape all fields in here: 据我所知,您应该在此处转义所有字段:

$registerquery = mysql_query("INSERT INTO users (Username, password, EmailAddress,Activation,registered) VALUES('".$username."', '".$password."', '".$email."','".$activation."','".$date."')");

You have: 你有:

$username = mysql_real_escape_string(trim($_POST['username']));
 $email = mysql_real_escape_string(trim($_POST['email']));

You should have the same for $password, $activation and $date... Not sure if you do $ password,$ activation和$ date的名称应该相同...不确定是否这样做

Anyway I use normally use this: 无论如何,我通常使用此:

function getPost($s) {
        if (array_key_exists($s, $_POST))
            return mysql_real_escape_string(htmlspecialchars($_POST[$s]));
        else return false;
    }


    function getGet($s) {
        if (array_key_exists($s, $_GET))
            return mysql_real_escape_string(htmlspecialchars($_GET[$s]));
        else return false;
    }

But it's to protect against XSS also... I though you should be safe with mysql_real_escape_string(). 但这也可以防止XSS ...虽然我应该使用mysql_real_escape_string()保持安全。

Anyway you can also use Prepared statements and forget all about this!! 无论如何,您也可以使用Prepared语句,而不必理会这一切!! http://php.net/manual/en/pdo.prepared-statements.php http://php.net/manual/zh/pdo.prepared-statements.php

EDIT Well, no valid username there, right? 编辑嗯,那里没有有效的用户名,对吗? So what about this: 那么呢:

try{
$getMail = $dbh->prepare("SELECT EmailAddress FROM users WHERE username = :username");
$getMail->bindParam(':username', $username);
$getMail->execute();
$rowMail = $getMail->fetch();
$email = $rowMail['emailaddress'];
}
catch( PDOException $Exception ) {
echo 'Authentication Failed';
}

Can't test it right now, but it should be something around this lines... 目前无法对其进行测试,但这应该是围绕这条线的……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM