If you enable PMG's webUI access from the public internet, you may notice alot of authentication failure in /var/log/daemon.log.
Setup fail2ban to block/ban suspicious authentication failure attempts.
IMPORTANT: This setup was intended on a new setup of PMG 7.x. If you upgrade from previous version, double check the setup is using nftables or iptables or any old lingering fail2ban configuration.
1. Install fail2ban
2. Edit /etc/fail2ban/jail.d/defaults-debian.conf with below settings to enable fail2ban to use nftables instead of iptables as debian have shipped with nftables by default since version 10. Do this step if you want to use the default nftables instead of iptables, or else just ignore this step and install iptables on your PMG 7.x.
3. Create /etc/fail2ban/jail.d/pmg-web-auth.conf with below settings. Actually only port 8006 is needed. https/http is optional.
4. Create /etc/fail2ban/filter.d/pmg-web-auth.conf with below settings.
5. Run below command to test the filter.
6. Optional to enable recidive to ban recurrent IP. Add below settings to /etc/fail2ban/jail.d/defaults-debian.conf or create new /etc/fail2ban/jail.d/recidive.conf.
7. Restart fail2ban services with the new settings.
8. Run fail2ban-client to check the jail status.
9. Check nftables rule status.
10. To enable email notification, add below settings to /etc/fail2ban/jail.d/defaults-debian.conf or create new /etc/fail2ban/jail.d/mail-notification.conf. But there is a bug to use bsd-mailx's mail program with fail2ban, so you may need to use mailutils or manually patch the codes in /etc/fail2ban/action.d/mail-*.conf.
My suggestion is just create /etc/fail2ban/action.d/bsd-mailx-*.conf from /etc/fail2ban/action.d/mail-*.conf with the code changes.
https://pve.proxmox.com/wiki/Fail2ban
Setup fail2ban to block/ban suspicious authentication failure attempts.
IMPORTANT: This setup was intended on a new setup of PMG 7.x. If you upgrade from previous version, double check the setup is using nftables or iptables or any old lingering fail2ban configuration.
Code:
Sep 13 11:39:00 pmg pmgdaemon[32409]: authentication failure; rhost=::ffff:183.253.84.171 user=root@pam msg=auth failed: Authentication failure
1. Install fail2ban
Code:
apt-get install fail2ban
2. Edit /etc/fail2ban/jail.d/defaults-debian.conf with below settings to enable fail2ban to use nftables instead of iptables as debian have shipped with nftables by default since version 10. Do this step if you want to use the default nftables instead of iptables, or else just ignore this step and install iptables on your PMG 7.x.
Code:
[DEFAULT]
banaction = nftables-multiport
banaction_allports = nftables-allports
3. Create /etc/fail2ban/jail.d/pmg-web-auth.conf with below settings. Actually only port 8006 is needed. https/http is optional.
Code:
[pmg-web-auth]
enabled = true
port = https,http,8006
filter = pmg-web-auth
logpath = /var/log/daemon.log
maxretry = 2
# 1 hour
bantime = 3600
4. Create /etc/fail2ban/filter.d/pmg-web-auth.conf with below settings.
Code:
[Definition]
failregex = pmgdaemon\[.*authentication failure; rhost=<HOST> user=.* msg=.*
ignoreregex =
5. Run below command to test the filter.
Code:
root@pmg:~# fail2ban-regex /var/log/daemon.log /etc/fail2ban/filter.d/pmg-web-auth.conf
Running tests
=============
Use failregex filter file : pmg-web-auth, basedir: /etc/fail2ban
Use log file : /var/log/daemon.log
Use encoding : UTF-8
Results
=======
Failregex: 289 total
|- #) [# of hits] regular expression
| 1) [289] pmgdaemon\[.*authentication failure; rhost=<HOST> user=.* msg=.*
`-
Ignoreregex: 0 total
Date template hits:
|- [# of hits] date format
| [1904] {^LN-BEG}(?:DAY )?MON Day %k:Minute:Second(?:\.Microseconds)?(?: ExYear)?
`-
Lines: 1904 lines, 0 ignored, 289 matched, 1615 missed
[processed in 0.07 sec]
Missed line(s): too many to print. Use --print-all-missed to print all 1615 lines
root@pmg:~#
6. Optional to enable recidive to ban recurrent IP. Add below settings to /etc/fail2ban/jail.d/defaults-debian.conf or create new /etc/fail2ban/jail.d/recidive.conf.
Code:
[recidive]
enabled = true
maxretry = 3
7. Restart fail2ban services with the new settings.
Code:
systemctl restart fail2ban
8. Run fail2ban-client to check the jail status.
Code:
root@pmg:~# fail2ban-client status pmg-web-auth
Status for the jail: pmg-web-auth
|- Filter
| |- Currently failed: 6
| |- Total failed: 187
| `- File list: /var/log/daemon.log
`- Actions
|- Currently banned: 0
|- Total banned: 19
`- Banned IP list:
root@pmg:~#
root@pmg:~# fail2ban-client status recidive
Status for the jail: recidive
|- Filter
| |- Currently failed: 2
| |- Total failed: 19
| `- File list: /var/log/fail2ban.log
`- Actions
|- Currently banned: 67
|- Total banned: 67
`- Banned IP list: 103.247.23.241 110.137.36.141 110.169.33.171 114.34.143.126 116.4.97.240 118.70.169.219 122.116.169.87 122.116.226.223 124.90.112.180 125.70.238.161 128.201.97.48 138.201.30.76 139.91.58.2 14.154.30.97 148.251.128.232 168.197.154.149 168.90.211.32 170.78.40.35 170.78.96.97 170.83.16.221 175.138.106.77 175.44.42.19 177.156.170.10 177.170.20.12 177.35.242.45 178.217.216.117 179.159.214.35 183.253.70.11 185.230.76.44 186.23.236.4 187.189.5.102 188.124.228.14 189.39.103.150 189.85.150.143 193.33.88.181 196.75.2.149 200.112.210.165 201.139.170.153 202.138.244.37 209.141.53.116 210.240.163.93 222.69.254.18 223.70.201.178 223.73.88.191 31.27.213.108 36.66.190.197 36.91.14.2 45.179.177.254 45.181.230.54 5.185.17.11 58.32.32.133 71.199.58.165 77.37.250.109 81.192.169.51 82.202.176.41 82.202.176.42 82.202.176.43 83.110.17.121 87.255.198.188 87.5.193.144 87.5.6.99 87.92.54.24 14.154.31.171 179.176.73.149 183.253.70.154 196.75.21.130 81.248.68.120
root@pmg:~#
9. Check nftables rule status.
Code:
root@pmg:~# nft list table inet f2b-table
table inet f2b-table {
set addr-set-recidive {
type ipv4_addr
elements = { 5.185.17.11, 14.154.30.97,
14.154.31.171, 31.27.213.108,
36.66.190.197, 36.91.14.2,
45.179.177.254, 45.181.230.54,
58.32.32.133, 71.199.58.165,
77.37.250.109, 81.192.169.51,
81.248.68.120, 82.202.176.41,
82.202.176.42, 82.202.176.43,
83.110.17.121, 87.5.6.99,
87.5.193.144, 87.92.54.24,
87.255.198.188, 103.247.23.241,
110.137.36.141, 110.169.33.171,
114.34.143.126, 116.4.97.240,
118.70.169.219, 122.116.169.87,
122.116.226.223, 124.90.112.180,
125.70.238.161, 128.201.97.48,
138.201.30.76, 139.91.58.2,
148.251.128.232, 168.90.211.32,
168.197.154.149, 170.78.40.35,
170.78.96.97, 170.83.16.221,
175.44.42.19, 175.138.106.77,
177.35.242.45, 177.156.170.10,
177.170.20.12, 178.217.216.117,
179.159.214.35, 179.176.73.149,
183.253.70.11, 183.253.70.154,
185.230.76.44, 186.23.236.4,
187.189.5.102, 188.124.228.14,
189.39.103.150, 189.85.150.143,
193.33.88.181, 196.75.2.149,
196.75.21.130, 200.112.210.165,
201.139.170.153, 202.138.244.37,
209.141.53.116, 210.240.163.93,
222.69.254.18, 223.70.201.178,
223.73.88.191 }
}
set addr-set-pmg-web-auth {
type ipv4_addr
}
chain f2b-chain {
type filter hook input priority filter - 1; policy accept;
meta l4proto { tcp } ip saddr @addr-set-recidive reject
tcp dport { 80, 443, 8006 } ip saddr @addr-set-pmg-web-auth reject
}
}
root@pmg:~#
10. To enable email notification, add below settings to /etc/fail2ban/jail.d/defaults-debian.conf or create new /etc/fail2ban/jail.d/mail-notification.conf. But there is a bug to use bsd-mailx's mail program with fail2ban, so you may need to use mailutils or manually patch the codes in /etc/fail2ban/action.d/mail-*.conf.
My suggestion is just create /etc/fail2ban/action.d/bsd-mailx-*.conf from /etc/fail2ban/action.d/mail-*.conf with the code changes.
Code:
[DEFAULT]
mta = mail # use default mail program
#mta = bsd-mailx # use default mail program
destemail = root@localhost # Destination email address
sender = root@<fq-hostname> # Sender email address
https://pve.proxmox.com/wiki/Fail2ban
Last edited: