메일 서버 오픈 이후 불특정 사용자의 접근이 많아지기 시작해서 아래와 같이
차단 방법을 사용해서 차단을 진행함
구성환경 - ubuntu 20.04
fail2ban 설치
# apt install fail2ban
아래와 같은 filter 생성 - /etc/fail2ban/filter.d/postfix-sasl.conf
# Fail2Ban filter for postfix authentication failures
[INCLUDES]
before = common.conf
[Definition]
#_daemon = postfix/smtpd <- comment this line, "postfix/smtps/smtpd" not detected
failregex = ^%(__prefix_line)swarning: [-._\w]+\[<HOST>\]: SASL (?:LOGIN|PLAIN|(?:CRAM|DIGEST)-MD5) authentication failed(: [ A-Za-z0-9+/]*={0,2})?\s*$
위 설정 보다는 아래 설정을 권장한다. (위 failregex filter가 os 버전 특성을 타는 것 같음, Ubuntu 22에서는 잘동작하나 Ubuntu 24에서 오류 발생함)
# Fail2Ban filter for postfix authentication failures
[Definition]
# pattern
# 2024-12-05T13:04:56.868082+09:00 homemachine postfix/smtpd[5605]: warning: unknown[80.94.95.235]: SASL LOGIN authentication failed: Connection lost to authentication server, sasl_username=(unavailable)
# 2024-12-05T13:36:44.346178+09:00 homemachine postfix/smtpd[2831]: NOQUEUE: reject: RCPT from unknown[165.154.213.133]: 554 5.7.1 <[email protected]>: Relay access denied; from=<[email protected]> to=<[email protected]> proto=ESMTP helo=<mail.encicle.com>
failregex = warning: (?:.*\[<HOST>\]\:.*) SASL (PLAIN|LOGIN) authentication failed: .*$
NOQUEUE: reject: RCPT from (?:.*\[<HOST>\]\:.*)
ignoreregex = (?: Disconnected: Logged out).
fail2ban 필터 테스트
fail2ban-regex
명령어를 통해서 filter 테스트가 가능하다.
# fail2ban-regex /var/log/mail.log /etc/fail2ban/filter.d/postfix-sasl.conf
jail 설정 파일 생성 - /etc/fail2ban/jail.local
[DEFAULT]
ignoreip = 127.0.0.0/16
[postfix-sasl]
enabled = true
port = 25,465,587
filter = postfix-sasl
logpath = /var/log/mail.log
maxretry = 5
bantime = 600
이후 fail2ban 서비스를 재시작한다.
# systemctl restart fail2ban.service
fail2ban 상태확인
# fail2ban-client status
Status
|- Number of jail: 1
`- Jail list: postfix-sasl
postfix-sasl jail에 대한 세부 상태 확인
# fail2ban-client status postfix-sasl
Status for the jail: postfix-sasl
|- Filter
| |- Currently failed: 0
| |- Total failed: 14
| `- File list: /var/log/mail.log
`- Actions
|- Currently banned: 26
|- Total banned: 26
`- Banned IP list: 173.211.232.202 2.56.57.142 2.56.57.153 2.56.57.170 212.192.241.186 212.192.241.55 212.192.241.73 212.192.246.11 212.192.246.120 212.192.246.133 212.192.246.145 212.192.246.167 212.192.246.17 212.192.246.191 212.192.246.226 212.192.246.237 212.192.246.28 212.192.246.36 212.192.246.64 212.192.246.73 212.192.246.81 212.192.246.82 212.192.246.9 5.34.205.62 136.144.41.253 37.0.10.176