![Postfix의 자동 블랙리스트](https://linux55.com/image/169236/Postfix%EC%9D%98%20%EC%9E%90%EB%8F%99%20%EB%B8%94%EB%9E%99%EB%A6%AC%EC%8A%A4%ED%8A%B8.png)
남용을 성공적으로 제한하는 Postfix 지시문이 많이 있습니다.하지만: 제한 사항이 구현되면 학대자는 간단히 다시 연결하여 동일한 테스트를 다시 실패하는 전체 프로세스를 거칠 수 있습니다. 클라이언트가 연결되기 전에 적용되는 제한에 대한 영구 메모리는 없습니다.
이 루프를 단락시키고 리소스가 많이 소요되는 명령을 실행한 후 블록으로 직접 이동할 수 있는 솔루션이 있습니까? 이상적으로는 IPv4/IPv6 주소 블랙리스트를 지원하고 싶습니다.
답변1
고쳐 쓰다:
저는 솔루션을 대대적으로 업데이트하고 IPv6 지원과 하우스키핑을 추가하여 가해자가 서버 액세스를 중단한 후 IP를 정리하는 "롤링" 차단 목록을 구현했습니다.
소개하다:
남용자를 막을 수 있는 Postfix 지시문은 많지만 이러한 지시문의 문제점은 이전 결정에 대한 지속적인 기억이 없다는 것입니다.따라서 어뷰징자는 끝없이 재접속할 수 있으며 동일한 테스트를 통해 트래픽이 다시 실패해야 합니다. 따라서 궁극적인 목표는 이러한 끝없는 남용의 순환을 끊고 첫 번째 리소스 집약적 테스트에 실패한 후 문제가 되는 IP를 금지하는 것입니다.
나는 가지고있다오픈소스 솔루션나는 많은 도메인에 대한 많은 계정을 호스팅하는 (다소) 사용량이 많은 메일 서버를 운영하고 있습니다. 즉, RegEx를 사용하여 /var/log/maillog
IPv4 및 IPv6 주소에 대한 남용 패턴을 캡처하는 스크립트를 트리거하는 SystemD 서비스 및 타이머를 생성합니다. 지금까지 테스트를 통해 남용을 크게 줄이는 데 성공했습니다.
의 샘플 출력 /etc/postfix/access
. 개별 IPv6 주소는 포함되지 않습니다.
213.230.115.33 REJECT
213.230.75.114 REJECT
185.66.252.78 REJECT
162.243.133.39 REJECT
104.168.159.38 REJECT
78.128.113.109 REJECT
77.40.3.227 REJECT
77.40.3.101 REJECT
61.163.192.88 REJECT
37.0.20.10 REJECT
26.189.237.221 REJECT
[2001:da8:5066:66:eda:41ff:fe1d:b27] REJECT
그런데 IPv6를 사용하여 이 문제를 처리하는 것이 약간 번거로웠지만 마침내 해냈습니다 ;-)
블랙리스트 전략:
2.5개월 이내에 약 1200개 이상의 IP를 블랙리스트에 올렸습니다.가해자 IP를 지속적으로 기록하는 이전 버전의 저장소를 사용합니다. 이 목록을 정기적으로 정리하는 메커니즘이 없으면 확장성 문제가 발생할 수 있습니다. 나의 새로운 접근 방식은 롤링 차단 목록을 만드는 것입니다. 즉, 메일 로그를 지속적으로 크롤링하여 새로운 학대자 IP를 찾고 메일 로그에 더 이상 표시되지 않는 IP를 제거하는 것입니다.내가 생각할 수 있는 다른 방법은 임의의 시간에 IP를 삭제하는 것입니다.
따라서 로그 교체 후, 어뷰징자가 새 메일 로그에 나타나지 않으면 이후에 어뷰징 시도를 하는 경우에만 블랙리스트에 포함되지 않습니다. IP는 순환 전 메일 로그의 수명 동안만 블랙리스트에 추가됩니다.
암호
내 테스트에 따르면 이 저장소는 말한 대로 작동한다고 말할 수 있습니다. 하지만 어떤 부분은 더 우아할 수 있다는 점에는 의심의 여지가 없습니다. 다음은 비즈니스 수행 및 Peer Review 제공을 위한 주요 스크립트입니다.
아이디어/제안 사항이 있으면 알려주시기 바랍니다. 아니면 나에게 끌어오기 요청을 보내는 것이 더 좋습니다(개선 사항을 테스트한 후에만 요청하십시오). 스크립트는 테스트하기 쉽습니다.그것은 당신을 위해 모든 것을 수행하며 모든 코드는 잘 설명되어 있습니다.- 그리고 긴장을 풀기도 쉽습니다.
참고하세요: 이 스크립트는 "access" 맵을 읽는 지시문이 있는 경우에만 작동합니다 main.cf
.또한 Postfix의 제한적 지시어는 왼쪽에서 오른쪽으로 적용됩니다., 따라서 "액세스" 맵을 읽는 모든 지침은 더 비용이 많이 드는 테스트를 실행하는 지침의 왼쪽에 있어야 합니다.
일부 테스트만 수행하려는 경우 저장소에 모든 항목을 제거하기 위한 스크립트가 포함되어 있습니다.
cat <<'EOF'> /etc/postfix/access-autoBlacklisting.sh
#!/bin/bash
#
# Author/Developer: Terrence Houlahan Linux Engineer F1Linux.com
# Linkedin: https://www.linkedin.com/in/terrencehoulahan
# License: GPL 3.0
# Source: https://github.com/f1linux/postfix-autoBlacklisting.git
# Version: 03.20.00
# OPERATION:
# ----------
# This script PREPENDS offending IP addresses from "/var/log/maillog" to "/etc/postfix/access"
# This ensures restrictive access rules applied before permissive grants.
# Consult README.md file for more granular detail about this script and its operation.
# Stop timer from executing Blacklisting script: Existing blacklist in /etc/postfix/access is enforce enforced while the new blacklist rebuilds
systemctl stop Postfix-AutoBlacklisting.timer
# Purge blacklist: Blacklist recreated each script execution capturing both previous offending IPs as well as newest ones present in logs
sed -i '/REJECT$/d' /etc/postfix/access
# Purge the scratch file:
> /etc/postfix/access-blacklist
### Scrape log for different forms of abuse using different tests to identify abuse IPs and squirt each to same central file:
# Enable/Disable any of below tests according to your requirements. Adding your own is easy if you use my tests which isolate offending IPs as templates.
# TEST 1: Blacklist Zombie hosts from endlessly squirting spam: These are identified by no PTR record being set for them.
# This test will catch both new zombies as well as those already RBLed which should serve to stop them constantly being endlessly checked against the RBL
# IPv4 Test:
# Below commented test was found to not be 100 perecent as accurate as the one using the awk form. Have not investigated why however.
#grep "does not resolve to address" /var/log/maillog | grep -Eo "([0-9]{1,3}[\.]){3}[0-9]{1,3}" | sort -u >> /etc/postfix/access-blacklist
grep "does not resolve to address" /var/log/maillog | awk 'match($0, /([0-9]{1,3}[\.]){3}[0-9]{1,3}/) {print substr($0, RSTART, RLENGTH)}' | sort -u >> /etc/postfix/access-blacklist
# IPv6 Test:
grep "does not resolve to address" /var/log/maillog | grep -Eo "2[0-9a-fA-F]{3}:(([0-9a-fA-F]{1,4}[:]{1,2}){1,6}[0-9a-fA-F]{1,4})" | sort -u | awk '{print "["$1"]"}' >> /etc/postfix/access-blacklist
# TEST 2: Block spammers guessing account names where they know our domain:
# WARNING: this could potentially cause a block where an unintentional misspelling of an mail account name occured.
# Uncomment only if you are OK with accepting such a risk:
# IPv4 Test:
#grep "Recipient address rejected: User unknown in virtual mailbox table" /var/log/maillog | sed -rn 's/.*\[(([0-9]{,3}.){4})\].*/\1/gp' >> /etc/postfix/access-blacklist
# IPv6 Test:
#grep "Recipient address rejected: User unknown in virtual mailbox table" /var/log/maillog | grep -Eo "2[0-9a-fA-F]{3}:(([0-9a-fA-F]{1,4}[:]{1,2}){1,6}[0-9a-fA-F]{1,4})" | sort -u | awk '{print "["$1"]"}' >> /etc/postfix/access-blacklist
# Populate an array with sorted and depuplicated list of offending IPs scraped from maillog using foregoing tests:
readarray arrayIPblacklist < <( cat /etc/postfix/access-blacklist | sort -u -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n | sed '/^$/d' )
# If "access" is a new empty file then the subsequent "sed" will fail. Any new file will have a zero size so the '-s' test will not equal 'true'.
# So we use negation to test "true" and echo a blank space to file. The subsequent "sed" will now execute.
# If "access" file already has whitelist entry then the 'if' statement does nothing and "sed" which follows executes as expected for a non-empty file:
if [ ! -s /etc/postfix/access ]; then echo "" > /etc/postfix/access; fi
for i in "${arrayIPblacklist[@]}"; do
# Write list of IPS from array to TOP of "access" file to enforce restrictions BEFORE processing whitelisted "OK" addresses:
sed -i "1i $i" /etc/postfix/access
# Append " REJECT" (with a space prepended in front of it) after each of the IPs added to to the "access" file:
sed -i '1s/$/ REJECT/' /etc/postfix/access
done
# Rebuild the /etc/postfix/access Berkeley DB:
postmap /etc/postfix/access
systemctl reload postfix.service
# After cycle completes and IPs written to /etc/postfix/acces we wipe array which repopulates anew upon next script execution:
unset arrayIPblacklist
systemctl start Postfix-AutoBlacklisting.timer
EOF
결론적으로:
이전에 차단된 IP의 메모리 내 지속성을 유지하여 비용이 많이 드는 테스트의 끝없는 반복을 방지하는 시스템을 찾았다고 생각합니다. 완벽합니까? 여전히 몇 가지 개선이 필요할 수 있지만 지금까지 테스트해 본 결과 결과는 유망해 보입니다.
스패머들에 의해 끝없이 남용되는 메일 서버에 지쳤다면, 가능한 한 빨리 메일 서버를 종료할 수 있는 빠르고 쉬운 해결책이 있습니다.