2021-07-26 12:09:41 +02:00
|
|
|
#!/usr/sbin/nft -f
|
|
|
|
#
|
|
|
|
# Initially based on: https://wiki.nftables.org/wiki-nftables/index.php/Simple_ruleset_for_a_server
|
|
|
|
#
|
|
|
|
|
|
|
|
flush ruleset
|
|
|
|
|
|
|
|
# Lists updated daily by update-spamhaus-nftables.sh
|
|
|
|
include "/etc/nftables/spamhaus-ipv4.nft"
|
|
|
|
include "/etc/nftables/spamhaus-ipv6.nft"
|
|
|
|
|
2021-07-31 20:46:50 +02:00
|
|
|
# Lists updated monthly (manually)
|
|
|
|
include "/etc/nftables/abuseipdb-ipv4.nft"
|
|
|
|
include "/etc/nftables/abuseipdb-ipv6.nft"
|
|
|
|
|
2021-07-29 09:16:00 +02:00
|
|
|
# Lists updated daily by update-abusech-nftables.sh
|
|
|
|
include "/etc/nftables/abusech-ipv4.nft"
|
|
|
|
|
2021-07-26 12:09:41 +02:00
|
|
|
# Notes:
|
|
|
|
# - tables hold chains, chains hold rules
|
|
|
|
# - inet is for both ipv4 and ipv6
|
|
|
|
table inet filter {
|
2021-07-27 21:03:23 +02:00
|
|
|
set spamhaus-ipv4 {
|
|
|
|
type ipv4_addr
|
2021-07-26 12:09:41 +02:00
|
|
|
# if the set contains prefixes we need to use the interval flag
|
|
|
|
flags interval
|
2021-07-27 21:03:23 +02:00
|
|
|
elements = $SPAMHAUS_IPV4
|
|
|
|
}
|
2021-07-26 12:09:41 +02:00
|
|
|
|
2021-07-27 21:03:23 +02:00
|
|
|
set spamhaus-ipv6 {
|
|
|
|
type ipv6_addr
|
2021-07-26 12:09:41 +02:00
|
|
|
flags interval
|
2021-07-27 21:03:23 +02:00
|
|
|
elements = $SPAMHAUS_IPV6
|
|
|
|
}
|
2021-07-26 12:09:41 +02:00
|
|
|
|
2021-07-29 09:16:00 +02:00
|
|
|
set abusech-ipv4 {
|
|
|
|
type ipv4_addr
|
|
|
|
elements = $ABUSECH_IPV4
|
|
|
|
}
|
|
|
|
|
2021-07-31 20:46:50 +02:00
|
|
|
set abuseipdb-ipv4 {
|
|
|
|
type ipv4_addr
|
|
|
|
elements = $ABUSEIPDB_IPV4
|
|
|
|
}
|
|
|
|
|
|
|
|
set abuseipdb-ipv6 {
|
|
|
|
type ipv6_addr
|
|
|
|
elements = $ABUSEIPDB_IPV6
|
|
|
|
}
|
|
|
|
|
2021-07-27 21:03:23 +02:00
|
|
|
chain input {
|
2021-07-26 12:09:41 +02:00
|
|
|
type filter hook input priority 0;
|
|
|
|
|
2021-07-30 08:37:30 +02:00
|
|
|
ct state {established, related} accept comment "Allow traffic from established and related packets"
|
2021-07-26 12:09:41 +02:00
|
|
|
|
2021-07-30 08:37:30 +02:00
|
|
|
ct state invalid counter drop comment "Early drop of invalid connections"
|
2021-07-26 12:09:41 +02:00
|
|
|
|
2021-07-30 08:37:30 +02:00
|
|
|
ip saddr @spamhaus-ipv4 counter drop comment "Early drop of incoming packets matching spamhaus-ipv4 list"
|
|
|
|
ip6 saddr @spamhaus-ipv6 counter drop comment "Early drop of incoming packets matching spamhaus-ipv6 list"
|
2021-07-26 12:09:41 +02:00
|
|
|
|
2021-07-30 08:37:30 +02:00
|
|
|
ip saddr @abusech-ipv4 counter drop comment "Early drop of packets matching abusech-ipv4 list"
|
2021-07-29 09:16:00 +02:00
|
|
|
|
2021-07-31 20:46:50 +02:00
|
|
|
ip saddr @abuseipdb-ipv4 counter drop comment "Early drop of incoming packets matching abuseipdb-ipv4 list"
|
|
|
|
ip6 saddr @abuseipdb-ipv6 counter drop comment "Early drop of incoming packets matching abuseipdb-ipv6 list"
|
|
|
|
|
2021-07-30 08:37:30 +02:00
|
|
|
iifname lo accept comment "Allow from loopback"
|
2021-07-26 12:09:41 +02:00
|
|
|
|
2021-07-30 08:37:30 +02:00
|
|
|
ip protocol icmp limit rate 4/second accept comment "Allow ICMP"
|
|
|
|
ip6 nexthdr ipv6-icmp limit rate 4/second accept comment "Allow IPv6 ICMP"
|
|
|
|
ip protocol igmp limit rate 4/second accept comment "Allow IGMP"
|
2021-07-26 12:09:41 +02:00
|
|
|
|
2021-07-27 20:22:32 +02:00
|
|
|
{# SSH rules #}
|
2021-07-30 08:37:30 +02:00
|
|
|
ip saddr 0.0.0.0/0 ct state new tcp dport 22 counter accept comment "Allow SSH"
|
|
|
|
ip6 saddr ::/0 ct state new tcp dport 22 counter accept comment "Allow SSH"
|
2021-07-27 20:22:32 +02:00
|
|
|
|
|
|
|
{# Web rules #}
|
|
|
|
{% if 'web' in group_names %}
|
2021-07-30 08:37:30 +02:00
|
|
|
ip saddr 0.0.0.0/0 ct state new tcp dport 80 counter accept comment "Allow HTTP"
|
|
|
|
ip saddr 0.0.0.0/0 ct state new tcp dport 443 counter accept comment "Allow HTTPS"
|
|
|
|
ip6 saddr ::/0 ct state new tcp dport 80 counter accept comment "Allow HTTP"
|
|
|
|
ip6 saddr ::/0 ct state new tcp dport 443 counter accept comment "Allow HTTPS"
|
2021-07-27 20:22:32 +02:00
|
|
|
{% endif %}
|
|
|
|
|
2021-09-28 06:34:25 +02:00
|
|
|
ip saddr 0.0.0.0/0 ct state new udp dport 60001-60003 counter accept comment "Allow mosh"
|
|
|
|
ip6 saddr ::/0 ct state new udp dport 60001-60003 counter accept comment "Allow mosh"
|
2021-09-05 15:23:42 +02:00
|
|
|
|
2021-07-27 20:22:32 +02:00
|
|
|
{# Extra rules #}
|
|
|
|
{% if extra_iptables_rules is defined %}
|
|
|
|
{% for rule in extra_iptables_rules %}
|
|
|
|
ip saddr {{ ghetto_ipsets[rule.acl].src }} ct state new {{ rule.protocol }} dport {{ rule.port }} counter accept
|
|
|
|
|
|
|
|
{% if ghetto_ipsets[rule.acl].ipv6src is defined %}
|
|
|
|
ip6 saddr {{ ghetto_ipsets[rule.acl].ipv6src }} ct state new {{ rule.protocol }} dport {{ rule.port }} counter accept
|
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
{% endfor %}
|
|
|
|
{% endif %}
|
2021-07-26 12:09:41 +02:00
|
|
|
|
|
|
|
# everything else
|
|
|
|
reject with icmpx type port-unreachable
|
2021-07-27 21:03:23 +02:00
|
|
|
}
|
|
|
|
chain forward {
|
|
|
|
type filter hook forward priority 0;
|
|
|
|
}
|
|
|
|
chain output {
|
|
|
|
type filter hook output priority 0;
|
2021-07-29 09:16:00 +02:00
|
|
|
|
2021-07-30 08:37:30 +02:00
|
|
|
ip daddr @spamhaus-ipv4 counter drop comment "Drop outgoing packets matching spamhaus-ipv4 list"
|
|
|
|
ip6 daddr @spamhaus-ipv6 counter drop comment "Drop outgoing packets matching spamhaus-ipv6 list"
|
|
|
|
|
|
|
|
ip daddr @abusech-ipv4 counter drop comment "Drop outgoing packets matching abusech-ipv4 list"
|
2021-07-31 20:46:50 +02:00
|
|
|
|
|
|
|
ip daddr @abuseipdb-ipv4 counter drop comment "Drop outgoing packets matching abuseipdb-ipv4 list"
|
|
|
|
ip6 daddr @abuseipdb-ipv6 counter drop comment "Drop outgoing packets matching abuseipdb-ipv6 list"
|
2021-07-27 21:03:23 +02:00
|
|
|
}
|
2021-07-26 12:09:41 +02:00
|
|
|
}
|