Postfixの自動ブラックリスト

Postfixの自動ブラックリスト

悪用をうまく制限するPostfixディレクティブがたくさんあります。しかし、: 制限が実装されると、虐待者は単に再接続し、同じテストを再度失敗するプロセス全体を通過できます。クライアントが接続される前に適用される制限の永続メモリはありません。

このループを短絡してリソースが多いコマンドを実行した後、ブロックに直接移動できるソリューションはありますか?理想的にはIPv4/IPv6アドレスブラックリストをサポートしたいと思います。

ベストアンサー1

修正する:

私はソリューションを大幅に更新し、IPv6のサポートとハウスキーピングを追加し、加害者がサーバーへのアクセスを停止した後にIPをクリーンアップする「ローリング」ブロックリストを実装しました。

紹介する:

乱用者を防ぐことができるPostfixディレクティブはたくさんありますが、これらのディレクティブの問題は、以前の決定に関する永続的な記憶がないことです。そのため、アビュージンガーは無限に再接続でき、同じテストでトラフィックが再び失敗する必要があります。したがって、最終的な目標は、これらの無限の乱用の循環を停止し、最初のリソース集約的なテストに失敗した後に問題になるIPを禁止することです。

私は持っていますオープンソースソリューション私は多くのドメインのための多くのアカウントをホストする(多少)使用量の多いメールサーバーを運営しています。つまり、RegEx を使用して、/var/log/maillogIPv4 アドレスと IPv6 アドレスの乱用パターンをキャプチャするスクリプトをトリガーする SystemD サービスとタイマーを生成します。これまでのテストで、乱用を大幅に減らすことに成功しました。

のサンプル出力/etc/postfix/access。個々のIPv6アドレスは含まれません。

213.230.115.33 REJECT
213.230.75.114 REJECT
185.66.252.78 REJECT
162.243.133.39 REJECT
104.168.159.38 REJECT
78.128.113.109 REJECT
77.40.3.227 REJECT
77.40.3.101 REJECT
61.163.192.88 REJECT
37.0.20.10 REJECT
26.189.237.221 REJECT
[2001:da8:5066:66:eda:41ff:fe1d:b27] REJECT

ところで、IPv6を使用してこの問題を処理するのは少し面倒でしたが、ついにやりました;-)

ブラックリスト戦略:

2.5ヶ月以内に約1200以上のIPをブラックリストに載せました。加害者IPを継続的に記録する古いバージョンのリポジトリを使用します。このリストを定期的にクリーンアップするメカニズムがないと、スケーラビリティの問題が発生する可能性があります。私の新しいアプローチは、ローリングブロックリストを作成することです。つまり、メールログを常にクロールして新しい虐待者IPを見つけ、メールログに表示されなくなったIPを削除することです。私が考えることができる別の方法は、任意の時間にIPを削除することです。

したがって、ログを交換した後、アビューイングが新しいメールログに表示されない場合は、後でアビュー化しようとした場合にのみブラックリストに含まれません。 IP は、循環前のメールログの存続期間中のみブラックリストに追加されます。

パスワード

私のテストによれば、このリポジトリは言うように動作すると言うことができます。しかし、いくつかの部分はよりエレガントであるという点に疑いの余地はありません。以下は、ビジネスを実行し、Peer Reviewを提供するための主なスクリプトです。

アイデア/提案があれば教えてください。または、私にプルリクエストを送信する方が良いです(改善をテストした後にのみリクエストしてください)。スクリプトはテストが簡単です。それはあなたのためにすべてを行い、すべてのコードはよく説明されています。- そしてリラックスするのも簡単です。

参考にしてください:このスクリプトは、「アクセス」マップを読み取るディレクティブがある場合にのみ機能しますmain.cfまた、Postfix の制限ディレクティブは左から右に適用されます。したがって、「アクセス」マップを読むすべてのガイドラインは、より高価なテストを実行するガイドラインの左側にある必要があります。

一部のテストのみを実行する場合は、リポジトリにすべてのエントリを削除するためのスクリプトが含まれています。

cat <<'EOF'> /etc/postfix/access-autoBlacklisting.sh
#!/bin/bash
#
# Author/Developer: Terrence Houlahan Linux Engineer F1Linux.com
# Linkedin: https://www.linkedin.com/in/terrencehoulahan
# License: GPL 3.0
# Source:  https://github.com/f1linux/postfix-autoBlacklisting.git
# Version: 03.20.00

# OPERATION:
# ----------
# This script PREPENDS offending IP addresses from "/var/log/maillog" to "/etc/postfix/access"
# This ensures restrictive access rules applied before permissive grants.
# Consult README.md file for more granular detail about this script and its operation.

# Stop timer from executing Blacklisting script: Existing blacklist in /etc/postfix/access is enforce enforced while the new blacklist rebuilds
systemctl stop Postfix-AutoBlacklisting.timer

# Purge blacklist: Blacklist recreated each script execution capturing both previous offending IPs as well as newest ones present in logs
sed -i '/REJECT$/d' /etc/postfix/access

# Purge the scratch file:
> /etc/postfix/access-blacklist

### Scrape log for different forms of abuse using different tests to identify abuse IPs and squirt each to same central file:
# Enable/Disable any of below tests according to your requirements. Adding your own is easy if you use my tests which isolate offending IPs as templates.

# TEST 1: Blacklist Zombie hosts from endlessly squirting spam: These are identified by no PTR record being set for them.
# This test will catch both new zombies as well as those already RBLed which should serve to stop them constantly being endlessly checked against the RBL
# IPv4 Test:
# Below commented test was found to not be 100 perecent as accurate as the one using the awk form. Have not investigated why however.
#grep "does not resolve to address" /var/log/maillog | grep -Eo "([0-9]{1,3}[\.]){3}[0-9]{1,3}" | sort -u >> /etc/postfix/access-blacklist
grep "does not resolve to address" /var/log/maillog | awk 'match($0, /([0-9]{1,3}[\.]){3}[0-9]{1,3}/) {print substr($0, RSTART, RLENGTH)}' | sort -u >> /etc/postfix/access-blacklist
# IPv6 Test:
grep "does not resolve to address" /var/log/maillog | grep -Eo "2[0-9a-fA-F]{3}:(([0-9a-fA-F]{1,4}[:]{1,2}){1,6}[0-9a-fA-F]{1,4})" | sort -u | awk '{print "["$1"]"}' >> /etc/postfix/access-blacklist


# TEST 2: Block spammers guessing account names where they know our domain:
# WARNING: this could potentially cause a block where an unintentional misspelling of an mail account name occured.
# Uncomment only if you are OK with accepting such a risk:
# IPv4 Test:
#grep "Recipient address rejected: User unknown in virtual mailbox table" /var/log/maillog | sed -rn 's/.*\[(([0-9]{,3}.){4})\].*/\1/gp' >> /etc/postfix/access-blacklist
# IPv6 Test:
#grep "Recipient address rejected: User unknown in virtual mailbox table" /var/log/maillog | grep -Eo "2[0-9a-fA-F]{3}:(([0-9a-fA-F]{1,4}[:]{1,2}){1,6}[0-9a-fA-F]{1,4})" | sort -u | awk '{print "["$1"]"}' >> /etc/postfix/access-blacklist


# Populate an array with sorted and depuplicated list of offending IPs scraped from maillog using foregoing tests:
readarray arrayIPblacklist < <( cat /etc/postfix/access-blacklist | sort -u -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n | sed '/^$/d' )

# If "access" is a new empty file then the subsequent "sed" will fail. Any new file will have a zero size so the '-s' test will not equal 'true'.
# So we use negation to test "true" and echo a blank space to file. The subsequent "sed" will now execute.
# If "access" file already has whitelist entry then the 'if' statement does nothing and "sed" which follows executes as expected for a non-empty file:
if [ ! -s /etc/postfix/access ]; then echo "" > /etc/postfix/access; fi

for i in "${arrayIPblacklist[@]}"; do
        # Write list of IPS from array to TOP of "access" file to enforce restrictions BEFORE processing whitelisted "OK" addresses:
        sed -i "1i $i" /etc/postfix/access
        # Append " REJECT" (with a space prepended in front of it) after each of the IPs added to to the "access" file:
        sed -i '1s/$/ REJECT/' /etc/postfix/access
done


# Rebuild the /etc/postfix/access Berkeley DB:
postmap /etc/postfix/access

systemctl reload postfix.service

# After cycle completes and IPs written to /etc/postfix/acces we wipe array which repopulates anew upon next script execution:
unset arrayIPblacklist

systemctl start Postfix-AutoBlacklisting.timer

EOF

結論として:

以前にブロックされたIPのメモリ内持続性を維持し、高価なテストの無限の繰り返しを防ぐシステムを見つけたと思います。完璧ですか?それでもいくつかの改善が必要になるかもしれませんが、これまでテストした結果は有望です。

スパマーによって無限に乱用されたメールサーバーに疲れた場合は、できるだけ早くメールサーバーをシャットダウンするための迅速で簡単な解決策があります。

https://github.com/f1linux/postfix-autoブラックリストの作成

おすすめ記事