FormMail で reCAPTCHA を使用する

重要: reCAPTCHA API のバージョン 1.0 のサポートは終了しました。バージョン 2.0 にアップグレードしてください。詳細

ここでは、reCAPTCHA Perl モジュールを使用せずに、FormMail スクリプトに reCAPTCHA を追加する方法について説明します。その方法がわかっている場合は、代わりに reCAPTCHA Perl モジュールを使用することもできます。

クライアント サイド(キャプチャ画像の表示方法)

HTML ページの <form> 要素内に、次のコードを追加する必要があります。

  <script type="text/javascript"
    src="http://www.google.com/recaptcha/api/challenge?k=your_public_key">
  </script>
  <noscript>
    <iframe src="http://www.google.com/recaptcha/api/noscript?k=your_public_key"
        height="300" width="500" frameborder="0"></iframe>

    <textarea name="recaptcha_challenge_field" rows="3" cols="40">
    </textarea>
    <input type="hidden" name="recaptcha_response_field"
        value="manual_challenge">
  </noscript>

言うまでもなく、要するに、your_public_key の 2 つのインスタンスをアカウント作成プロセス中に受け取った公開鍵に置き換える必要があります。秘密鍵を誤って使用しないように注意してください。

これにより、基本的には POST リクエストを介して formmail.cgi(または FormMail.pl)に渡される 2 つのパラメータが追加されます。

  • recaptcha_challenge_field: 公開鍵を介して作成されたチャレンジです。
  • recaptcha_response_field: 上記のチャレンジに対してユーザーが送信したレスポンスです。
  • サーバーサイド(ユーザーが正しい回答を入力しているかどうかをテストする方法)

    次に、formmail.cgi(または FormMail.pl)を変更して 2 つのパラメータを処理し、reCAPTCHA サーバーからのチャレンジを検証する必要があります。この時点で、FormMail.pl のバックアップ コピーを作成しておくことをおすすめします。次のコードで、「+」は FormMail スクリプトに行を追加する必要があることを意味し、「-」は行を FormMail スクリプトから削除する必要があることを意味します。いずれの場合も、FormMail スクリプトで隣接する行を表示することで、行を追加または削除する必要がある場所が示されています。

    まず、FormMail に次の行を追加して、LWP::UserAgent モジュールを使用するように Perl に指示する必要があります。

     # ACCESS CONTROL FIX: Peter D. Thompson Yezek                                #
     #                     http://www.securityfocus.com/archive/1/62033           #
     ##############################################################################
     +use LWP::UserAgent;
     +
    

    (このためには、Perl 環境にモジュール LWP::UserAgent が必要です。Perl のほとんどのインストールには、すでにこのモジュールがあります。モジュールがインストールされていない場合は、 Perl モジュールのインストールに関する基本的な手順をご覧ください)。

    次に、以下で定義されているキャプチャ チェック機能を呼び出すコードを追加します。

     # Check Required Fields
     &check_required;
    
     +# Check the captcha challenge and response.
     +&check_captcha;
     +
     # Send E-Mail
     &send_mail;
    
     # Return HTML Page or Redirect User
     &return_html;
    

    次に、CAPTCHA レスポンスを検証し、レスポンスがチャレンジと一致しない場合はエラーを生成します。

     +##############################################################################
     +# Check the CAPTCHA response via the reCAPTCHA service.
     +sub check_captcha {
     +
     +      my $ua = LWP::UserAgent->new();
     +      my $result=$ua->post(
     +      'https://www.google.com/recaptcha/api/verify',
     +      {
     +          privatekey => 'your_private_key',
     +          remoteip   => $ENV{'REMOTE_ADDR'},
     +          challenge  => $Form{'recaptcha_challenge_field'},
     +          response   => $Form{'recaptcha_response_field'}
     +      });
     +
     +      if ( $result->is_success && $result->content =~ /^true/) {
     +              return;
     +      } else {
     +              &error('captcha_failed');
     +      }
     +}
     +
     # NOTE rev1.91: This function is no longer intended to stop abuse, that      #
     #    functionality is now embedded in the checks made on @recipients and the #
     #    recipient form field.                                                   #
    

    最後に、チェックが失敗した場合にエラー メッセージを出力する機能を作成します。

             if ($Config{'missing_fields_redirect'}) {
                 print "Location: " . &clean_html($Config{'missing_fields_redirect'}) . "\n\n";
             }
     +    }
     +    elsif ($error eq 'captcha_failed') {
     +            print <<"(END ERROR HTML)";
     +Content-type: text/html
     +
     +<html>
     + <head>
     +  <title>Error: Captcha Check Failed</title>
     + </head>
     + <body bgcolor=#FFFFFF text=#000000>
     + <center>
     +  <table border=0 width=600 bgcolor=#9C9C9C>
     +    <tr><th><font size=+2>Error: Captcha Check Failed</font></th></tr%gt;
     +   </table>
     +  <table border=0 width=600 bgcolor=#CFCFCF>
     +    <tr><td>The Captcha response of the form you submitted did not match the challenge.
     +     Please check the form and make sure that your response matches the challenge in the captcha image.
     +     You can use the browser back button to return to the form.
     +     </center%gt;
     +    </td></tr>
     +   </table>
     +  </center>
     + </body>
     +</html>
     +(END ERROR HTML)
     +    }
             else {
                  foreach $missing_field (@error_fields) {
                      $missing_field_list .= "<li>" . &clean_html($missing_field) . "\n";
     .
     .
     .
      </html>
     (END ERROR HTML)
             }
     -    }
     -
         exit;
     }
    

    これで、reCAPTCHA がサイトで機能するようになりました。

    関連情報

  • デザインのカスタマイズ
  • ヒントとガイドライン
  • トラブルシューティング