Ban an attacker and audit for a backdoor
Task
Configure fail2ban to ban repeated SSH failures, prove it works without banning yourself, and then run the setuid-binary audit that finds the kind of backdoor this defends against.
Steps
- Before enabling anything, set
ignoreipinjail.localto include your own management network. This is the step that stops the tool locking you out, and it comes first for that reason. - Configure the sshd jail in
jail.local-- notjail.conf, which package updates overwrite -- with a lowmaxretryand a shortbantimefor testing. - Start fail2ban and confirm the jail is active with
fail2ban-client status sshd. - From a machine NOT in
ignoreip, fail SSH logins until the ban triggers. Confirm the address appears infail2ban-client status sshdand that a firewall rule now blocks it. - Confirm your own address, in
ignoreip, is never banned however many times you fail. - Now the audit half: find every setuid binary with
find / -xdev -perm -4000 -type f 2>/dev/nulland compare it against a known-good list. Plant a fake backdoor --cp /bin/bash /tmp/.hidden && chmod 4755 /tmp/.hidden-- and confirm your audit finds it. - Remove the planted binary and confirm the audit is clean again.
Verify
fail2ban-client status sshd | grep -E 'Currently banned|Total banned'
grep -q 'ignoreip' /etc/fail2ban/jail.local && echo "ignoreip set"
find / -xdev -perm -4000 -type f 2>/dev/null | grep -q '/tmp/.hidden' && echo "audit found the backdoor"
rm -f /tmp/.hidden
find / -xdev -perm -4000 -type f 2>/dev/null | grep -qv '/tmp/.hidden' && echo "clean"
Step 6 is the point: a setuid copy of bash owned by root is a working backdoor -- any user can run it and become root -- and it is exactly what the find -perm -4000 audit exists to catch. An unexpected result there is an escalation, not a note.
Notes
Order matters in both halves. ignoreip before enabling fail2ban, or the first fat-fingered password locks you out of your own defence. And a known-good baseline before the audit, or you cannot tell an unexpected setuid binary from a legitimate one.