| 1 |
# Advanced Filtering |
| 2 |
|
| 3 |
## Port Knocking |
| 4 |
|
| 5 |
Port knocking is a technique where attempting to connect to port A enables |
| 6 |
access to port B from that same source IP address. This is usually used to |
| 7 |
hide SSH service. |
| 8 |
|
| 9 |
Example configuration: |
| 10 |
|
| 11 |
``` |
| 12 |
zone { |
| 13 |
localhost |
| 14 |
public |
| 15 |
} |
| 16 |
|
| 17 |
iplist { |
| 18 |
# List of IP addresses that have performed initial knock.. |
| 19 |
# It is valid for 30 seconds. |
| 20 |
@knock dynamic=yes element_timeout=30s |
| 21 |
} |
| 22 |
|
| 23 |
public-localhost { |
| 24 |
# Allow SSH if IP address is in @knock iplist. |
| 25 |
ssh saddr @knock |
| 26 |
|
| 27 |
# Add source IP address to @knock iplist if there is UDP packet to port 5042. |
| 28 |
udp 5042 iplist_update saddr @knock drop log + ":knock" |
| 29 |
|
| 30 |
# Delete IP address from @knock iplist if there is any packet to any other |
| 31 |
# port. This prevents opening SSH if port scan is received. |
| 32 |
saddr @knock iplist_delete saddr @knock continue log + ":unknock" |
| 33 |
|
| 34 |
# ...other normal rules... |
| 35 |
drop log |
| 36 |
} |
| 37 |
``` |
| 38 |
|
| 39 |
|
| 40 |
## Automatic IP Address Banning |
| 41 |
|
| 42 |
Foomuuri supports automatic IP address banning without any external programs. |
| 43 |
This is usually enough, no `fail2ban` is needed. Banning happens fully on |
| 44 |
packet path, native on nftables level. |
| 45 |
|
| 46 |
Major differences compared to `fail2ban` program: |
| 47 |
|
| 48 |
* Banning is done on connection count/rate level only, not on protocol |
| 49 |
success/failure level. This means that too many successful SSH connection |
| 50 |
could result banning. Therefore it is important to use high rate limit or |
| 51 |
list of known good hosts, or both. |
| 52 |
* Foomuuri restart and reboot will clear ban list. |
| 53 |
* There's no need for external programs or complex log file parsing. |
| 54 |
|
| 55 |
Example configuration: |
| 56 |
|
| 57 |
``` |
| 58 |
zone { |
| 59 |
localhost |
| 60 |
public |
| 61 |
} |
| 62 |
|
| 63 |
iplist { |
| 64 |
# List of known good hosts, don't ban these |
| 65 |
@good 192.168.0.0/24 foobar.fi |
| 66 |
|
| 67 |
# List containing banned IP addresses. They will be banned for 5 minutes. |
| 68 |
@banned dynamic=yes element_timeout=5m |
| 69 |
} |
| 70 |
|
| 71 |
public-localhost { |
| 72 |
# Drop all new traffic if source IP address is in @banned iplist. |
| 73 |
# Update/reset ban expire timeout. Add this as first rule. |
| 74 |
saddr @banned iplist_update saddr @banned drop log + ":banned" |
| 75 |
|
| 76 |
# Allow SSH from known good hosts and others with rate limit. |
| 77 |
# If there are more than 5/minute connections add them to @banned iplist. |
| 78 |
ssh saddr @good |
| 79 |
ssh saddr_rate "5/minute burst 5" |
| 80 |
ssh iplist_update saddr @banned drop log + ":ban-ssh" |
| 81 |
|
| 82 |
# ...other normal rules, with or without similar rate limit banning... |
| 83 |
|
| 84 |
# Instead of normal final "drop log" rule: |
| 85 |
# - Allow 2/min dropped connections without banning. |
| 86 |
# - Somebody is port scanning us. Add IP address to @banned iplist. |
| 87 |
# - Make sure @good addresses are never banned. |
| 88 |
saddr @good drop log |
| 89 |
saddr_rate "2/minute burst 10" drop log |
| 90 |
iplist_update saddr @banned drop log + ":ban-portscan" |
| 91 |
} |
| 92 |
``` |