https://chatgpt.com/share/684e364e-dd08-8002-b363-a3582babee48
https://chatgpt.com/share/684e630f-998c-8002-849b-fcb31d1eec86
sudo apt install fail2ban # Debian/Ubuntu
sudo systemctl enable –now fail2ban
新增 jail
/etc/fail2ban/jail.local
[DEFAULT]
封鎖時間、偵測視窗、失敗次數
bantime = 1h ; 封 1 小時(可寫 86400 或 1d)
findtime = 10m ; 10 分鐘內
maxretry = 5 ; 失敗 5 次就封
ignoreip = 127.0.0.1/8 192.0.2.10 ; 白名單 (例: 你的固定 IP)
[sshd] ; 啟用預設的 sshd filter
enabled = true
port = ssh ; 或 22,2222 等自訂 Port
logpath = %(sshd_log)s ; Debian=/var/log/auth.log, RHEL=/var/log/secure
backend = systemd ; 系統用 systemd journal 時可啟用
[apache-badbots]
enabled = true
port = http,https
filter = apache-badbots
logpath = /var/log/apache2/access.log
maxretry = 1
bantime = 86400 # 封 1 天
sudo systemctl restart fail2ban
看全局
sudo fail2ban-client status
看 sshd jail 詳細資訊
sudo fail2ban-client status sshd
全域白名單(所有 jail 皆生效)
[DEFAULT]
ignoreip = 127.0.0.1/8 192.0.2.10 203.0.113.0/24 2001:db8::/32
自定義一份適合您格式的 filter
建立 /etc/fail2ban/filter.d/apache-custombots.conf
並加入:
[Definition]
failregex = ^ .“(GET|POST|HEAD)..env.HTTP.” [45]\d{2} .+ “.” ^ .“(GET|POST|HEAD)..git.HTTP.” [45]\d{2} .+ “.“
^ .“(GET|POST|HEAD).” [45]\d{2} .+ “.ZmEu.“
^ .“(GET|POST|HEAD).” [45]\d{2} .+ “.Hello World.“
^ .“(GET|POST|HEAD).” [45]\d{2} .+ “.Keydrop.“
ignoreregex =
然後在 jail.local
加入:
[apache-custombots]
enabled = true
port = http,https
logpath = /var/log/apache2/access.log
filter = apache-custombots
maxretry = 1
findtime = 600
bantime = 1h
再 reload:
sudo fail2ban-client reload
sudo fail2ban-client status apache-custombots
查看 iptables
sudo iptables -L