Postfix Configuration: Blocking Spam with Header Checks
Linux

Postfix Configuration: Blocking Spam with Header Checks

2024-07-19 · 8 分鐘 · Ray Lee (System Analyst)

As a mail server administrator, you might encounter situations where spam emails pass through even when they have valid SPF records. One effective way to combat this is by configuring Postfix to use header checks with regular expressions to block unwanted emails. In this blog post, we’ll walk through how to set up header checks in Postfix to block spam emails.

Understanding Postfix Header Checks

Postfix header checks allow you to filter emails based on specific patterns in the email headers. By using regular expressions, you can create rules to reject emails from known spam sources or with specific characteristics.

Setting Up Header Checks in Postfix

Step 1: Create a Header Checks File

First, you need to create a file that will contain your header check rules. You can place this file in the /etc/postfix directory. For example, let’s create a file named header_checks.

sudo nano /etc/postfix/header_checks

Step 2: Define Your Header Check Rules

In the header_checks file, you can define your rules using regular expressions. For instance, if you want to block emails from the domains caeonline.com and smtp2go.com, you can add the following rule:

/^Received: .*caeonline\.com/ REJECT
/^Received: .*smtp2go\.com/ REJECT
/^From: .*smtp2go\.com/ REJECT
/^From: .*caeonline\.com/ REJECT
# You can also filter the subject line by following the sample below.
/^Subject: .*caeonline\.com(.+)$/ REJECT
/^Subject: .*cmtp2go\.com(.+)$/ REJECT

This rule looks for the Received header containing the specified domains and rejects the email if a match is found.

Step 3: Update Postfix Configuration

Next, you need to tell Postfix to use the header checks file. Open the Postfix main configuration file /etc/postfix/main.cf and add or update the following line:

header_checks = regexp:/etc/postfix/header_checks

This line specifies that Postfix should use the header_checks file with regular expressions.

Step 4: Apply the Configuration

After updating the configuration file, you need to apply the changes by reloading or restarting Postfix. You can do this using the following command:

sudo systemctl reload postfix

Or, if you prefer, you can restart Postfix:

sudo systemctl restart postfix

Step 5: Test Your Configuration

To ensure that your configuration works as expected, you can send test emails from the blocked domains and verify that they are rejected. Check the mail logs to confirm that the emails are being blocked:

sudo tail -f /var/log/mail.log

You should see entries indicating that emails from the specified domains are being rejected.

Troubleshooting

If you encounter any issues, double-check the following:

  1. Syntax Errors: Ensure that there are no syntax errors in the header_checks file. Regular expressions must be correctly formatted.
  2. File Permissions: Verify that the header_checks file has the correct permissions and is readable by Postfix.
  3. Postfix Logs: Check the Postfix logs (/var/log/mail.log) for any error messages that can help diagnose the problem.

Conclusion

Configuring header checks in Postfix is a powerful way to filter out unwanted emails based on specific patterns in the email headers. By using regular expressions, you can effectively block spam from known sources. In this blog post, we demonstrated how to set up header checks to block emails from the domains caeonline.com and smtp2go.com. With this knowledge, you can customize your own header checks to suit your specific needs and improve the overall security and reliability of your mail server.

For more advanced configurations and additional Postfix features, consult the Postfix documentation.

Happy email filtering!

English Version

As a mail server administrator, you might encounter situations where spam emails pass through even when they have valid SPF records. One effective way to combat this is by configuring Postfix to use header checks with regular expressions to block unwanted emails. In this blog post, we’ll walk through how to set up header checks in Postfix to block spam emails.

Understanding Postfix Header Checks

Postfix header checks allow you to filter emails based on specific patterns in the email headers. By using regular expressions, you can create rules to reject emails from known spam sources or with specific characteristics.

Setting Up Header Checks in Postfix

Step 1: Create a Header Checks File

First, you need to create a file that will contain your header check rules. You can place this file in the /etc/postfix directory. For example, let’s create a file named header_checks.

sudo nano /etc/postfix/header_checks

Step 2: Define Your Header Check Rules

In the header_checks file, you can define your rules using regular expressions. For instance, if you want to block emails from the domains caeonline.com and smtp2go.com, you can add the following rule:

/^Received: .*caeonline\.com/ REJECT
/^Received: .*smtp2go\.com/ REJECT
/^From: .*smtp2go\.com/ REJECT
/^From: .*caeonline\.com/ REJECT
# You can also filter the subject line by following the sample below.
/^Subject: .*caeonline\.com(.+)$/ REJECT
/^Subject: .*cmtp2go\.com(.+)$/ REJECT

This rule looks for the Received header containing the specified domains and rejects the email if a match is found.

Step 3: Update Postfix Configuration

Next, you need to tell Postfix to use the header checks file. Open the Postfix main configuration file /etc/postfix/main.cf and add or update the following line:

header_checks = regexp:/etc/postfix/header_checks

This line specifies that Postfix should use the header_checks file with regular expressions.

Step 4: Apply the Configuration

After updating the configuration file, you need to apply the changes by reloading or restarting Postfix. You can do this using the following command:

sudo systemctl reload postfix

Or, if you prefer, you can restart Postfix:

sudo systemctl restart postfix

Step 5: Test Your Configuration

To ensure that your configuration works as expected, you can send test emails from the blocked domains and verify that they are rejected. Check the mail logs to confirm that the emails are being blocked:

sudo tail -f /var/log/mail.log

You should see entries indicating that emails from the specified domains are being rejected.

Troubleshooting

If you encounter any issues, double-check the following:

  1. Syntax Errors: Ensure that there are no syntax errors in the header_checks file. Regular expressions must be correctly formatted.
  2. File Permissions: Verify that the header_checks file has the correct permissions and is readable by Postfix.
  3. Postfix Logs: Check the Postfix logs (/var/log/mail.log) for any error messages that can help diagnose the problem.

Conclusion

Configuring header checks in Postfix is a powerful way to filter out unwanted emails based on specific patterns in the email headers. By using regular expressions, you can effectively block spam from known sources. In this blog post, we demonstrated how to set up header checks to block emails from the domains caeonline.com and smtp2go.com. With this knowledge, you can customize your own header checks to suit your specific needs and improve the overall security and reliability of your mail server.

For more advanced configurations and additional Postfix features, consult the Postfix documentation.

Happy email filtering!

日本語版

メールサーバー管理者として、有効なSPFレコードを持つスパムメールがフィルターをすり抜けてしまう状況に遭遇することがあるかもしれません。これに対処する効果的な方法の一つは、Postfixのヘッダーチェックを正規表現で設定し、不要なメールをブロックすることです。このブログ記事では、Postfixでヘッダーチェックを設定してスパムメールをブロックする方法を解説します。

Postfixヘッダーチェックの理解

Postfixのヘッダーチェックを使用すると、メールヘッダー内の特定のパターンに基づいてメールをフィルタリングできます。正規表現を使用することで、既知のスパム送信元や特定の特徴を持つメールを拒否するルールを作成できます。

Postfixでのヘッダーチェックの設定

ステップ1:ヘッダーチェックファイルの作成

まず、ヘッダーチェックルールを記述するファイルを作成する必要があります。このファイルは/etc/postfixディレクトリに配置できます。例として、header_checksというファイルを作成しましょう。

sudo nano /etc/postfix/header_checks

ステップ2:ヘッダーチェックルールの定義

header_checksファイルで、正規表現を使用してルールを定義できます。例えば、caeonline.comsmtp2go.comのドメインからのメールをブロックしたい場合、以下のルールを追加できます:

/^Received: .*caeonline\.com/ REJECT
/^Received: .*smtp2go\.com/ REJECT
/^From: .*smtp2go\.com/ REJECT
/^From: .*caeonline\.com/ REJECT
# You can also filter the subject line by following the sample below.
/^Subject: .*caeonline\.com(.+)$/ REJECT
/^Subject: .*cmtp2go\.com(.+)$/ REJECT

このルールは、指定されたドメインを含むReceivedヘッダーを検索し、一致した場合にメールを拒否します。

ステップ3:Postfix設定の更新

次に、Postfixにヘッダーチェックファイルを使用するよう指示する必要があります。Postfixのメイン設定ファイル/etc/postfix/main.cfを開き、以下の行を追加または更新します:

header_checks = regexp:/etc/postfix/header_checks

この行は、Postfixが正規表現でheader_checksファイルを使用することを指定します。

ステップ4:設定の適用

設定ファイルを更新した後、Postfixをリロードまたは再起動して変更を適用する必要があります。以下のコマンドで実行できます:

sudo systemctl reload postfix

または、Postfixを再起動することもできます:

sudo systemctl restart postfix

ステップ5:設定のテスト

設定が期待通りに動作することを確認するために、ブロック対象のドメインからテストメールを送信し、拒否されることを確認できます。メールログをチェックして、メールがブロックされていることを確認しましょう:

sudo tail -f /var/log/mail.log

指定されたドメインからのメールが拒否されていることを示すエントリが表示されるはずです。

トラブルシューティング

問題が発生した場合は、以下の項目を再確認してください:

  1. 構文エラーheader_checksファイルに構文エラーがないことを確認してください。正規表現は正しくフォーマットされている必要があります。
  2. ファイル権限header_checksファイルが正しい権限を持ち、Postfixが読み取り可能であることを確認してください。
  3. Postfixログ:問題の診断に役立つエラーメッセージがないか、Postfixログ(/var/log/mail.log)を確認してください。

まとめ

Postfixでヘッダーチェックを設定することは、メールヘッダー内の特定のパターンに基づいて不要なメールをフィルタリングする強力な方法です。正規表現を使用することで、既知の送信元からのスパムを効果的にブロックできます。このブログ記事では、caeonline.comsmtp2go.comのドメインからのメールをブロックするヘッダーチェックの設定方法を紹介しました。この知識を活用して、ご自身のニーズに合わせたヘッダーチェックをカスタマイズし、メールサーバーの全体的なセキュリティと信頼性を向上させましょう。

より高度な設定や追加のPostfix機能については、Postfixドキュメントを参照してください。

メールフィルタリングをお楽しみください!

Ray Lee (System Analyst)
作者 Ray Lee (System Analyst)

iDempeire ERP Contributor, 經濟部中小企業處財務管理顧問 李寶瑞