我的服务器端 PHP 在 reCaptcha 之后仍然被黑(数百封垃圾邮件)

my server side PHP still got hacked after reCaptcha (hundreds-spam-emails)

我之前在 Whosebug 上的问题是关于有人每隔几个小时就向我发送数百封垃圾邮件。现在,我在服务器端修复了脚本,但第二天早上我仍然收到 30 封电子邮件之类的东西,我的托管公司给了我 FTP 的新密码,并将我的索引文件移动到备份地图(网站离线) ,他们说这是因为下面的可疑脚本而被黑客入侵。他们说 "This often happens via a leaked script in your website, a script that is " 过时了”。这是什么意思?他们在电子邮件中说这个脚本文件有问题。这是不可能破解的,因为我在服务器端使用了 reCaptcha,有什么东西吗不见了?

<?php

if(isset($_POST['g-recaptcha-response'])){
      $captcha=$_POST['g-recaptcha-response'];


      }


/* OUTCOMMENTED CODE BELOW DOESN'T LET FORM SEND IF EVERYTHING IS CHECKED???? 


    if(!$captcha){
          echo '<h2>Check captcha .</h2>';
          exit;
        }*/



    $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=(SECRETKEY)&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
            if($response.success==false)
            {
                echo '<span id="status" style="font-size:1vmax;color:red;">ReCaptcha ERROR</span>';
            }else
            {

        if( isset($_POST['n']) && isset($_POST['e']) && isset($_POST['mn']) && 

isset($_POST['m']) ){
        $n = $_POST['n']; // HINT: use preg_replace() to filter the data
        $e = $_POST['e'];
        $mn = $_POST['mn'];
        $m = nl2br($_POST['m']);
        $to = "gesternl@gester.nl"; 
        $from = $e;
        $subject = 'Contact Formulier-eng';
        $message = '<b>Naam:</b> '.$n.' <br><b>Email:</b> '.$e.' <br><b>Mobiel-nummer:</b> '.$mn.' <p>'.$m.'</p>';
        $headers = "Van: $from\n";
        $headers .= 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        if( mail($to, $subject, $message, $headers) ){
            echo "success";
        } else {
            echo "The server failed to send a message. Please try again later. Thank you!";
        }
    }
        }
    ?>

我刚刚再次上传它,看看现在会发生什么。有人可以帮我使这个文件对黑客安全吗?在上一个问题中没有人真正提供帮助,只是在没有代码的情况下给出了建议(而且我很笨)。

(第 8 行周围注释掉的代码不起作用,我不明白,有人知道为什么有人可以侵入它吗?)

是的,HTML 中用于 recaptcha 的代码与 public 密钥

链接良好

一方面,您没有清理用户输入。您应该立即修复它,因为它是一个安全漏洞。

  1. 您必须像@kevin Cai 所说的那样清理用户输入
  2. 你有一行错误:if($response.success==false)

    $response=file_get_contents("......");
    
    $result = json_decode($response);
    
    if($result->success==false){
    
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=(SECRETKEY)&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false)

这段代码是一段不幸的废话,它已经出现在很多(糟糕的)教程中。它不提供任何保护——条件始终为假,因为 $response.success 被解释为将常量 success 连接到 reCaptcha API 返回的 API 响应。这将导致 CAPTCHA 始终被视为有效,无论用户的输入如何。

使用 Google reCaptcha 库验证来自 reCaptcha API 的响应。它位于:https://github.com/google/recaptcha