メールサーバ(postfix)- Rspamd(スパムフィルタ) - 学習


 クラウディア


1. 概要
2. 学習
3. スクリプト
4. 参考サイト

1. 概要

 本ページでは、「Rspamd」に、スパムやハムを学習させる方法について、記述します。

2. 学習

 下記のコマンドで、学習させます。  学習させる「.eml」を用意しておいて。  スパムとして学習。

rspamc learn_spam < spam.eml
 ハム(正常メール)として学習。

rspamc learn_ham < ham.eml
 学習しているかどうかは、下記のコマンドで確認できます。

rspamc stat
 およそ、下記のような出力結果となります。

Results for command: stat (0.291 seconds)
Messages scanned: 54
Messages with action reject: 15, 27.78%
Messages with action soft reject: 0, 0.00%
Messages with action rewrite subject: 8, 14.81%
Messages with action add header: 5, 9.26%
Messages with action greylist: 0, 0.00%
Messages with action no action: 26, 48.15%
Messages treated as spam: 28, 51.85%
Messages treated as ham: 26, 48.15%
Messages learned: 2
Connections count: 1
Control connections count: 5
Average scan time: 1.622 sec
Pools allocated: 38
Pools freed: 13
Bytes allocated: 25751072
Memory chunks allocated: 139
Shared chunks allocated: 3
Chunks freed: 0
Oversized chunks: 5
Fuzzy hashes in storage "rspamd.com": 10914561392
Fuzzy hashes stored: 10914561392
Statfile: BAYES_SPAM type: redis; length: 0; free blocks: 0; total blocks: 0; free: 0.00%; learned: 2; users: 1; languages: 0
Statfile: BAYES_HAM type: redis; length: 0; free blocks: 0; total blocks: 0; free: 0.00%; learned: 0; users: 0; languages: 0
Total learns: 2
 これまでの、判定結果等も出力されています。  ここで、重要なのは、11行目。  ちょうど、2つのメールをスパムとして学習させた直後なのですが・・・。  「Messages learned: 2」となっています。

3. スクリプト

 「rspamc learn_spam」は、「rspamc learn_ham」も含めて、以下の形式で。

rspamc learn_spam < spam.eml
 「*.eml」のような、ワイルドカードは無効です。  なので、複数のメールをまとめて、スパムやハムとして学習させるように、下記のような仕組みを作っています。  まず、下記のようなディレクトリ構成を作成して。

.
|-- _ham
|-- _spam
`-- Makefile
 「_spam」配下に、スパムメールを置き、「_ham」配下に、ハムメールを置きます。  その上で。

Makefile
 に下記のように記述します。

all: spam ham

spam:
	find ./_spam -type f -exec rspamc learn_spam {} \;

ham:
	find ./_ham  -type f -exec rspamc learn_ham {} \;
 「root」ユーザ権限で。  下記で、スパム・ハムを学習。

make
 下記で、スパムを学習。

make spam
 下記で、ハムを学習。

make ham
 ということになります。

4. 参考サイト

 本ページは、下記のサイトおよび「ChatGPT」くんを参考にさせていただきました。
Rspamd Support | Rspamd Documentation

AbemaTV 無料体験
Amazonギフトカード