การตั้งค่า Fail2Ban ให้แจ้งเตือนผ่าน Email เมื่อตรวจพบการโจมตีหรือมี IP ถูกแบน สามารถทำได้ตามขั้นตอนด้านล่างนี้ครับ
1. ติดตั้ง sendmail หรือ mailutils
เพื่อให้ระบบสามารถส่งอีเมลได้:
sudo apt install sendmail -y
หรือถ้าคุณใช้ mailutils:
sudo apt install mailutils -y
เลือกใช้ตัวใดตัวหนึ่งครับ ส่วนใหญ่ sendmail จะเพียงพอสำหรับ Fail2Ban
2. ตั้งค่า Email ใน jail.local
เปิดแก้ไข :
sudo nano /etc/fail2ban/jail.local
จากนั้นให้เพิ่ม/แก้ไขในส่วน [DEFAULT] :
[DEFAULT]
# Email Address ที่จะได้รับการแจ้งเตือน
destemail = [email protected]
# Email Address ที่จะใช้เป็นผู้ส่ง
sender = [email protected]
# ชื่อ hostname
mta = sendmail
# Subject email
mailsubject = [Fail2Ban] แจ้งเตือน: IP ถูกแบนบน %(hostname)s
# การกระทำเมื่อพบเหตุการณ์
action = %(action_mwl)s
ความหมายของ action_mwl:
-
action_ = ใช้ default ban
-
action_m = ban + ส่ง email รายงานสั้น ๆ
-
action_mw = ban + ส่ง email พร้อม whois
-
action_mwl = ban + ส่ง email + whois + log รายละเอียด
3. ทดสอบระบบส่งเมล
ลองส่งเมลด้วยคำสั่งนี้ :
echo "ทดสอบแจ้งเตือนจาก Fail2Ban" | mail -s "ทดสอบ Fail2Ban" [email protected]
ถ้าได้รับเมล แสดงว่าระบบส่งเมลทำงานได้แล้วครับ
4. รีสตาร์ท Fail2Ban
sudo systemctl restart fail2ban
5. ทดสอบการแจ้งเตือนจริง
ลองใช้เครื่องอื่น SSH มายังเซิร์ฟเวอร์ แล้วใส่รหัสผิด 5 ครั้ง รอดูว่า:
ถ้าคุณต้องการให้ Fail2Ban ส่งอีเมลผ่าน Gmail หรือ Microsoft 365 (Outlook SMTP) หรือ SMTP อื่นๆ — จำเป็นต้องใช้ SMTP Client เช่น msmtp หรือ ssmtp เพื่อเชื่อมต่อกับ SMTP Server ของผู้ให้บริการเหล่านี้ครับ
ตัวอย่างนี้จะใช้ msmtp (ง่ายและเหมาะกับ Fail2Ban)
ขั้นตอนการตั้งค่า Fail2Ban ส่งเมลผ่าน Gmail / Microsoft 365
1. ติดตั้ง msmtp และ mailutils
ลองส่งเมลด้วยคำสั่งนี้ :
sudo apt install msmtp msmtp-mta mailutils -y
2. สร้างไฟล์ config สำหรับ msmtp
sudo nano /etc/msmtprc
ตัวอย่าง config สำหรับ Gmail :
defaults
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile /var/log/msmtp.log
account gmail
host smtp.gmail.com
port 587
from [email protected]
user [email protected]
password your-app-password
account default : gmail
ตัวอย่าง config สำหรับ Microsoft 365 (Outlook) :
defaults
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile /var/log/msmtp.log
account outlook
host smtp.office365.com
port 587
from [email protected]
user [email protected]
password your-app-password
account default : outlook
แนะนำ: ให้ใช้ App Password แทนรหัสจริง (ถ้าเปิด 2FA)
Gmail: ต้องเปิด "Allow less secure apps" หรือใช้ App Password
Microsoft: ต้องสร้าง App Password ผ่าน Security settings
3. ตั้ง permission ให้ปลอดภัย
sudo chmod 600 /etc/msmtprc
4. ตั้งค่า Fail2Ban ให้ใช้ mail (ซึ่งจะใช้ msmtp เบื้องหลัง)
เปิดไฟล์ jail.local:
sudo nano /etc/fail2ban/jail.local
ตั้งค่าดังนี้ใน [DEFAULT]:
[DEFAULT]
destemail = [email protected]
sender = [email protected]
mta = mail
action = %(action_mwl)s
5. ทดสอบส่งอีเมล
sudo nano /etc/fail2ban/jail.local
ถ้าได้รับอีเมล แสดงว่าพร้อมใช้งานแล้วครับ
6. รีสตาร์ท Fail2Ban
sudo systemctl restart fail2ban
สรุป
Gmail smtp.gmail.com 587 ต้องใช้ App Password
Microsoft 365 smtp.office365.com 587 ต้องใช้ App Password