モーグルとカバとパウダーの日記

モーグルやカバ(EXカービング)山スキー(BC)などがメインの日記でした。今は仕事のコンピュータ系のネタが主になっています。以前はスパム対策関連が多かったのですが最近はディープラーニング関連が多めです。

Javascriptを利用したコメントスパム対策

MT-Keystrokes というMT用プラグイン - モーグルとカバとパウダーの日記

こちらのコメントで教えていただいた、MT-KeystrokesというMT用のスパム対策プラグインについて調べてみた。


MT-Keystrokesプラグイン Movable Typeコメントスパム対策 - WEBデザイン BLOG
MT-Keystrokes

この元のアイデア
A short monograph on the theme of blog comment spam - SimonG.org
に書かれている。

1. Renamed the page that handles form submissions to stymie any bots that just assume it’s in the default location.

2. Preceded the form where you enter comments with two dummy forms - an empty one (for really stupid bots) followed by one that looks identical to the real one. Both these forms submit their info to the wrong page. They’re hidden from real people using the magic of CSS.

3. Did the same thing after the form, in reverse order, in case any bots start at the bottom of the page and work their way up.

4. Added a hidden field to the form which gets sent along with the other stuff. When a comment’s submitted, it checks that this field has been sent, and that it has the correct value. The value is based on the current date, so changes every day. To get this far, then, bots would have to parse the HTML to locate the correct form (and not be thrown off by the dummy forms surrounding it), and extract the names and values of all the fields. But - and here’s the evil part - the value of the hidden field in the html is wrong. It’s replaced by the correct value after the page has loaded by javascript.

5. Turned the Submit button into an image, which means the x-y position where it was clicked is logged. If no x-y position is given, we take that to mean it was submitted by a bot. This is flawed because a real person can tab to the Submit button and press return, submitting the form without actually clicking. This happened once and a legitimate comment was rejected, so I switched off this test but continued to monitor it. It turned out that this trap never caught any spam.

6. Logged the number of keypresses made when entering comments. Any comments where it’s less than two are rejected.

この最後の6がMT-Keystrokesのアイデアで、ソースを見るとすごく簡単。

<script language="JavaScript" type="text/javascript">
function keystrokes(form) {
    form.$FIELD_NAME.value = 1;
}
</script>
<input type="hidden" name="$FIELD_NAME" value="0" />
<input type="submit" onclick="keystrokes(this.form)" ...

コアとなる部分はここだけで、結局submitボタンを押されたときにチェック用フィールドの内容を書き換えて、その値でチェックするというもの。
だから、javascriptが動いていなければ普通のユーザも利用することが出来ない。


このようにjavascriptでスパムコメントを排除する方法を導入しているものは他にもあり
JavaScript によるコメントスパム対策 - Open MagicVox.net
では関数の結果を送ってチェックする方式と
JavaScriptを使ってスパムによるCGIの過負荷を防ぐ - Open MagicVox.net
formやtrackbackを送出する先のURL自体を書き換える方法などが提案されている。


javascript必須となるが、このような手法は非常に強力だろう。
そしてajaxの普及でjavascript必須は現在なら許されると思う。


ただ、日本の場合は携帯からの利用というのも考慮に入れないといけない。
そこで、接続元IPがdocomoezwebsoftbankの場合にはこれらのチェックをしないようにホワイトリスト化しておくべきだろう。


あと、5のsubmitボタンを画像にして、画像上のどこを押されたのか位置情報を得る、というのも手軽に出来るし、javascriptなしでいけるためいいなと思うのだけど、これってTabキーとEnterでsubmit押したときにも誤認識されずにいけるのだろうか?


(追記)

トラックバック打ってた MagicVox.net さんで、画像submitボタンをマウスで押さなかったときの実験をされていた。

Webフォームの画像ボタンをキーボードでsubmitした場合 - Open MagicVox.net

というわけで、キーで移動してsubmitしても、ちゃんと位置情報も送られるようだ。
これだとjavascript切ってあっても送られるから、より安全に利用できると思う。これはいいね。


あと、日本だと携帯から書き込まれる可能性が結構高いため、携帯からだと位置情報送られるのか確認したいところ。
もし送られないのなら、接続元IPが携帯だったら、このチェックをしないようにして回避しよう。