【问题标题】:How do people send spam through my contact form人们如何通过我的联系表发送垃圾邮件
【发布时间】:2012-09-05 20:04:53
【问题描述】:

今天有人联系我说从某个域发送了大量垃圾邮件。 这是一个相对简单的 php 网站,有一个包含姓名、电子邮件、电话和消息的联系表格。除非整个服务器已被黑客入侵,否则我无法看到该网站可用于向多个用户发送垃圾邮件的任何其他方式。

电子邮件经过验证,错误字符已从邮件正文中删除。我尝试了多种方法来尝试通过表单修改标题,但似乎无法正常工作,所以我开始认为表单是安全的。

这是表单验证:

                              $to='owner@website.com';
              
              $messageSubject='Enquiry from the website';
              $confirmationSubject='Your email to website.com';
              $confirmationBody="Thankyou for your recent email enquiry to website.com.\n\nYour email has been sent and we will get back to you as soon as possible.\n\nThe message you sent was:\n";
              $email='';
              $body='';
              $displayForm=true;
              if ($_POST){
                $email=stripslashes($_POST['email']);
                $body=stripslashes($_POST['body']);
                $name=stripslashes($_POST['name']);
                $phone=stripslashes($_POST['phone']);
                // validate e-mail address
                $valid=eregi('^([0-9a-z]+[-._+&])*[0-9a-z]+@([-0-9a-z]+[.])+[a-z]{2,6}$',$email);
                $crack=eregi("(\r|\n)(to:|from:|cc:|bcc:)",$body);
                $spam=eregi("http",$body);
                $businessBody = "Enquiry from: $name\nEmail: $email\nPhone: $phone\n\nMessage:\n$body";
                if ($email && $body && $phone && $name && $valid && !$crack & !$spam){
                    if (mail($to,$messageSubject,$businessBody,'From: '.$email."\r\n") && mail($email,$confirmationSubject,$confirmationBody.$body,'From: '.$to."\r\n")){
                        $displayForm=false;
                        echo "<div><p>Your message to us was sent successfully, and a confirmation copy has also been sent to your e-mail address.</p><p>Your message was:<br>".htmlspecialchars($body)."</p></div>";
                    }
                    else echo '<div class="emailMessage"><p>Something went wrong when the server tried to send your message. This might be due to a server error, and is probably not your fault. We apologise for any inconvenience caused. You are welcome to telephone us on 01383 625110</p></div>'; // the messages could not be sent
                }
                else if ($crack) echo '<div class="emailMessage"><p>Your message contained e-mail headers within the message body. This seems to be a cracking attempt and the message has not been sent.</p></div>'; // cracking attempt
                else if ($spam) echo '<div class="emailMessage"><p>Your message contained characters that our system has flagged as spam email and has not been sent.</p></div>'; // spam mail!
                else echo '<div class="emailMessage"><p>Your message could not be sent. You must complete all fields - name, phone number, e-mail address and a message.</p></div>'; // form not complete
                }

任何人都可以看到这种表单被滥用的方式吗?

编辑

事实证明,有人在为群发邮件构建的服务器上放置了另一个加密文件,所以它实际上并不是来自这个表单。 无论如何感谢您的回答,他们可能会帮助其他人!

【问题讨论】:

  • 您向 $eamil 发送电子邮件(从表单中填充),我可以自动将一堆地址提交到垃圾邮件。

标签: php forms validation spam


【解决方案1】:

使用像SwiftMailer 这样的正确邮件类,您的麻烦会更少(代码也更漂亮)。通常,这类表单会通过在“from”或其他标头中添加换行符来滥用,从而允许它们设置自己的恶意标头。

例如:

if (mail($to,$messageSubject,$businessBody,'From: '.$email."\r\n") && mail($email,$confirmationSubject,$confirmationBody.$body,'From: '.$to."\r\n")){

虽然您已经去除了斜线,但您并没有去除新的行。如果我将我的电子邮件设置为:

ceejayoz@example.com
Bcc: victim@example.com

victim@example.com 在电子邮件中被密件抄送。通过一些更聪明的东西 - 将其变成多部分电子邮件并添加额外的默认部分 - 他们可以完全自定义电子邮件。

【讨论】:

    【解决方案2】:

    如果 $_POST['email'] 是,则可以批量发送:

    usera@test.com,userb@test.com,userc@test.com
    

    【讨论】:

    • post 数据是 from email,'to' 是第一行代码
    【解决方案3】:

    @ceejayoz 的回答很好,虽然我看到您试图在代码中防止密件抄送和换行,但仅限于电子邮件正文,而不是电子邮件字段。这是最容易受到黑客攻击的电子邮件字段。

    这是我多年来用来保护电子邮件字段的一些代码:

     $value = "some@email.com";
        if (!preg_match("/^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i", $value)){
             $error[$i] = "Invalid email address.";
        }
            //Nuke email header injection.
            $submitted = $value;
            $value = str_replace('\\r', '\n', $value);
            $value = str_replace('\\n', '\n', $value);
            $value = str_replace('\r', '\n', $value);
            $value = str_replace(',', '\n', $value);
            $value = str_replace(';', '\n', $value);
            $value = str_replace(' ', '\n', $value);
            $value = explode('\n', $value);
            $value = $value[0];
            if (trim($value) !== trim($submitted)) {
                echo "Stop hacking me!";
            }
        }
    

    或者使用其他人的课程,正如 ceejayoz 所说。

    另外,请考虑使用验证码和其他方式,或限制每分钟提交次数,作为为代码添加多级防御的另一种方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-28
      • 2012-06-12
      • 2015-01-30
      • 2017-11-06
      • 2013-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多