As had been said already, ufw (and its underlying rules mechanism) does not see the ssh username and cannot block based on that detail.
However, a tool such as fail2ban
can and does block connection attempts based on username. It monitors the logs generated by utilities such as sshd
and blocks (bans) traffic attempts based on entries matched there.
The fail2ban
tool comes with a number of match patterns, including one for sshd
. You would define the number of attempts in a specific duration before a ban was applied to further traffic from that source, and the duration of the ban. There is also a rule that looks for repeated bans and applies an even longer ban (I have mine set at weeks for this rule). You can also choose whether to ban traffic by type or just block the originating host entirely (I use this latter approach)
Assuming Debian you may be able to use these override files directly:
File /etc/fail2ban/fail2ban.local
[Definition]
allowipv6 = no
dbpurgeage = 2462400 ; 4 week 0.5 day
File /etc/fail2ban/jail.local
[DEFAULT]
# ignoreip = 127.0.0.1/8 …
bantime = 3600 ; 1 hour
action_ap = %(banaction_allports)s[name=%(__name__)s, bantime="%(bantime)s", port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
action = %(action_ap)s
[sshd]
enabled = true
port = ssh
logpath = %(sshd_log)s
backend = %(sshd_backend)s
findtime = 7200 ; 2 hours
bantime = 86400 ; 1 day
[sshd-ddos]
port = ssh
logpath = %(sshd_log)s
backend = %(sshd_backend)s