1
.Email spam protection
2.1 SpamAssassin, spamass-milter, and postfix settings
①SpamAssassin・spamass-milter Install
1
# dnf -y install spamassassin spamass-milter-postfix
➁SpamAssassin start
# systemctl start spamassassin
➂SpamAssassin Settings
# vi /etc/mail/spamassassin/v310.pre
Per Line 24 : Remove # at the beginning of the line
loadplugin Mail::SpamAssassin::Plugin::DCC
④SpamAssassin configuration file modernization script
# cd /var/www/system
# vi spamassassin-update.sh
Describe the following information
#!/bin/bash
cd /etc/mail/spamassassin
wget -q https://github.com/kittyfreak/spamassassin_user_prefs/archive/refs/heads/main.zip
[ $? -ne 0 ] && exit
unzip main.zip >/dev/null 2>&1
[ $? -ne 0 ] && exit
rm -f main.zip
mv spamassassin_user_prefs-main/user_prefs .
rm -rf spamassassin_user_prefs-main
diff user_prefs user_prefs.org > /dev/null 2>&1
if [ $? -ne 0 ]; then
cp user_prefs local.cf
echo "report_safe 0" >> local.cf
echo "rewrite_header Subject ***SPAM***" >> local.cf
# SpamAssassin restart
if [ -f /etc/rc.d/init.d/spamassassin ]; then
/etc/rc.d/init.d/spamassassin restart > /dev/null
else
systemctl restart spamassassin > /dev/null
fi
fi
cp user_prefs user_prefs.org
Grant execute permission to the spamassassin-update script and run it
# /var/www/system/spamassassin-update.sh
Confirm that the SpamAssassin configuration file (local.cf) has been created in the /etc/mail/spamassassin directory with the date of the day
# ls -l /etc/mail/spamassassin
total 1524
drwxr-xr-x 2 root root 40 Oct 31 14:21 channel.d
-rw-r--r-- 1 root root 985 Apr 19 2022 init.pre
-rw-r--r-- 1 root root 500636 Oct 31 14:31 local.cf
drwx------ 2 root root 6 Apr 19 2022 sa-update-keys
-rw-r--r-- 1 root root 62 Apr 19 2022 spamassassin-default.rc
-rwxr-xr-x 1 root root 35 Apr 19 2022 spamassassin-helper.sh
-rw-r--r-- 1 root root 55 Apr 19 2022 spamassassin-spamc.rc
-rw-r--r-- 1 root root 500588 Apr 30 2023 user_prefs
-rw-r--r-- 1 root root 500588 Oct 31 14:31 user_prefs.org
-rw-r--r-- 1 root root 2523 Oct 31 14:30 v310.pre
-rw-r--r-- 1 root root 1194 Apr 19 2022 v312.pre
-rw-r--r-- 1 root root 2416 Apr 19 2022 v320.pre
-rw-r--r-- 1 root root 1237 Apr 19 2022 v330.pre
-rw-r--r-- 1 root root 1020 Apr 19 2022 v340.pre
-rw-r--r-- 1 root root 1114 Apr 19 2022 v341.pre
-rw-r--r-- 1 root root 1313 Apr 19 2022 v342.pre
-rw-r--r-- 1 root root 1264 Apr 19 2022 v343.pre
Set up cron to automatically run a script daily that updates the SpamAssassin configuration file
# crontab -e
0 2 * * * /var/www/system/spamassassin-update.sh > /dev/null 2>&1
⑤spamass-milter startup and automatic startup settings
# systemctl start spamass-milter
# systemctl enable spamass-milter
Created symlink /etc/systemd/system/multi-user.target.wants/spamass-milter.service → /usr/lib/systemd/system/spamass-milter.service.
⑥Postfix and spamass-milter integration settings
# vi /etc/postfix/main.cf
Add the following
milter_default_action = tempfail
smtpd_milters = unix:/var/run/clamav-milter/clamav-milter.socket,unix:/run/spamass-milter/postfix/sock ※既にclamav-milterが定義されている場合
non_smtpd_milters = unix:/var/run/clamav-milter/clamav-milter.socket,unix:/run/spamass-milter/postfix/sock ※既にclamav-milterが定義されている場合
⑦Restart postfix
1
# systemctl restart postfix
⑧Procmail Settings
Create procmail configuration file
# vi /etc/postfix/main.cf
Per Line 469 : Add
#mailbox_command = /some/where/procmail
#mailbox_command = /some/where/procmail -a "$EXTENSION"
mailbox_command = /usr/bin/procmail
Postfix Settings Reflected
1
# systemctl restart postfix
2.2 Spam mail sorting settings
Mail marked as spam in the mail header by SpamAssassin will be delivered to a spam-only mailbox, while other mail will be delivered to a normal mailbox.
①Create a dedicated spam mailbox
For existing users, add a spam-only mailbox to their mailboxes.
For new users, a spam-only mailbox will be automatically created when a user is added.
①Created script to create spam-only mailboxes
# vi spamfolder-create
#!/bin/bash
for user in `ls /home`
do
id -u $user > /dev/null 2>&1
if [ $? -eq 0 ] && [ ! -d /home/$user/Maildir/.Spam/new ]; then
mkdir -p /home/$user/Maildir/.Spam/new
mkdir -p /home/$user/Maildir/.Spam/cur
mkdir -p /home/$user/Maildir/.Spam/tmp
chmod -R 700 /home/$user/Maildir/.Spam
chown -R $user. /home/$user/Maildir/.Spam
echo $user
fi
done
Run script to create spam-only mailboxes
# bash spamfolder-create
huong
linux
Measures against new users
Automatic spam-only mailbox creation when adding new users
# mkdir -p /etc/skel/Maildir/.Spam/{new,cur,tmp}
# chmod -R 700 /etc/skel/Maildir/.Spam
➁spam mail sorting
Emails identified as spam by SpamAssassin are delivered to a spam-only mailbox.
# vi /etc/procmailrc
Add the following to the end
:0
*^X-Spam-Flag: YES
$HOME/Maildir/.Spam/
➂spamass-milter confirmation
When you send a blank email to yourself, the following message is appended to the header of the received email
X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on Lepard
LLL