【问题标题】:What is the best way to prevent email Injection in a mailform? [duplicate]在邮件表单中防止电子邮件注入的最佳方法是什么? [复制]
【发布时间】:2013-10-08 21:44:42
【问题描述】:

您好,我已经构建了一个电子邮件表单,我想知道它是否以安全的方式构建。
我已阅读文章How to Prevent Email Injection in Your PHP Form to Mail Scripts 并将其应用于我的脚本。现在我想知道变量 $to 和 $bcc 是否保存。

function sendmail($to,$subject,$message,$bcc=NULL){

    //Prevent Email Injection in Your PHP Form to Mail Scripts
    if ( preg_match( "/[\r\n]/", $to ) ||  preg_match( "/[,]/", $to ) || preg_match( "/[\r\n]/", $bcc ) || preg_match( "/[,]/", $bcc ) ) {

        return '<h1>Danger found: possible email Injection Hijacking</h1>';
        return false;

    }else{
        // To send HTML mail, the Content-type header must be set
        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

        // Additional headers
        $headers .= 'From: No Reply <no-reply@domain.nl>' . "\r\n";
        if(isset($bcc)) $headers .= 'Bcc: ' .$bcc."\r\n";

        // Mail it
        return mail($to, $subject, $message, $headers);
    }
}
sendmail($_REQUEST['email'],'Subjectline', 'message','admin@domain.com');

【问题讨论】:

标签: php security email code-injection


【解决方案1】:

邮件中的漏洞来自标头注入。为了防止它,您可以在标题值中查找换行符,即:

"BCC: " . $email . "
X-OtherHeader: Foo-Bar

如果$email 包含换行符,例如:

webmaster@domain.com
TO: pro@hackerz.ru

您将获得一个额外的 TO 标头,这可能是恶意的。标头注入允许攻击者从您的邮件服务器向任何人发送电子邮件,实质上将您的邮件服务器变成了垃圾邮件服务器。

从外观上看,您当前的脚本是安全的。

【讨论】:

    【解决方案2】:

    确保只有一封电子邮件替换 $str(字符串),使用:

    $str=str_replace(";","",$str);
    $str=str_replace(",","",$str);
    

    【讨论】:

    猜你喜欢
    • 2010-10-23
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 2017-06-20
    • 2017-05-12
    • 2011-06-14
    • 2018-02-21
    • 1970-01-01
    相关资源
    最近更新 更多