Nftables (Linux): Unterschied zwischen den Versionen

Aus Tutorials
Zur Navigation springen Zur Suche springen
 
(61 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
{{note|Noch in Bearbeitung}}
== Installation ==
== Installation ==


Zeile 12: Zeile 9:
<pre>
<pre>
sudo systemctl enable nftables.service
sudo systemctl enable nftables.service
sudo systemctl restart nftables.service
</pre>
</pre>


== Konfiguration ==
== Firewall ==


=== Pre-Konfiguration ===
=== Pre-Konfiguration ===
Zeile 52: Zeile 50:


;<code>policy accept;</code>
;<code>policy accept;</code>
: Legt als Standard-Regel alle Pakete durch.
: Lässt als Standard-Regel alle Pakete durch.


==== Links ====
==== Links ====
Zeile 58: Zeile 56:
[https://wiki.nftables.org/wiki-nftables/index.php/Quick_reference-nftables_in_10_minutes https://wiki.nftables.org/wiki-nftables/index.php/Quick_reference-nftables_in_10_minutes]
[https://wiki.nftables.org/wiki-nftables/index.php/Quick_reference-nftables_in_10_minutes https://wiki.nftables.org/wiki-nftables/index.php/Quick_reference-nftables_in_10_minutes]


=== Server-Firewall ===
=== Beispiele ===


<pre>
<pre>
#!/usr/sbin/nft -f
# icmp (ping)
icmp type echo-request limit rate 10/second burst 2 packets counter accept;
</pre>


flush ruleset
<pre>
# open sshd (22) for internal networks only
tcp dport { ssh } ip saddr @internal_networks_ip4 limit rate 15/minute accept;</pre>


table inet filter {
==== Links ====
  set internal_networks_ip4 {
    type ipv4_addr
    flags interval
    auto-merge
    elements = { 10.2.0.0/24, 10.0.0.0/24 }
  }


  chain input {
[https://wiki.nftables.org/wiki-nftables/index.php/Rate_limiting_matchings https://wiki.nftables.org/wiki-nftables/index.php/Rate_limiting_matchings]
    type filter hook input priority 0;


    # default input chain policy drops everything
[https://de.wikipedia.org/wiki/Liste_der_standardisierten_Ports https://de.wikipedia.org/wiki/Liste_der_standardisierten_Ports]
    policy drop;


    # established/related connections
== Routing ==
    ct state established,related accept;


    # loopback interface
Wichtig ist IP-Forwarding zu aktivieren - siehe dazu [[Netzwerk_einrichten_(Linux)#IPV4-Weiterleitung_aktivieren|Netzwerk einrichten]]
    iifname lo accept;


    # icmp (ping)
<pre>
     icmp type echo-request accept;
# firewall
table ip filter {
  # allow all packets sent by the firewall machine itself
  chain output {
     type filter hook output priority 100; policy accept;
  }


    # open sshd (22) for internal networks only
  # allow LAN to firewall, disallow WAN to firewall
    tcp dport { ssh } ip saddr @internal_networks_ip4 accept comment "accept SSH from internal networks"
  chain input {
 
     type filter hook input priority 0; policy accept;
    # open tcp ports: httpd (80)
     iifname "lan0" accept
     tcp dport { http } accept;
     iifname "wan0" drop
 
    # open udp ports: domain (53), openvpn (1194)
    udp dport { domain, openvpn } accept;
 
     meta l4proto { tcp, udp } @th,16,16 3306 accept;
 
     # mysql (3306)
#    udp dport { mysql } accept;
#    tcp dport { mysql } accept;
   }
   }


  # allow packets from LAN to WAN, and WAN to LAN if LAN initiated the connection
   chain forward {
   chain forward {
     type filter hook forward priority 0; policy drop;
     type filter hook forward priority 0; policy drop;
    iifname "lan0" oifname "wan0" accept
    iifname "wan0" oifname "lan0" ct state related,established accept
   }
   }
}


   chain output {
# NAT
     type filter hook output priority 0; policy accept;
table ip nat {
   chain prerouting {
     type nat hook prerouting priority 0; policy accept;
  }
 
  # for all packets to WAN, after routing, replace source address with primary IP of WAN interface
  chain postrouting {
    type nat hook postrouting priority 100; policy accept;
    oifname "wan0" masquerade
   }
   }
}
}
</pre>
</pre>


==== Links ====
=== Links ===


[https://de.wikipedia.org/wiki/Liste_der_standardisierten_Ports https://de.wikipedia.org/wiki/Liste_der_standardisierten_Ports]
[https://wiki.gentoo.org/wiki/Nftables/Examples https://wiki.gentoo.org/wiki/Nftables/Examples]


== Kontrolle ==
== Kontrolle ==
<pre>
sudo nft list tables
</pre>


<pre>
<pre>
sudo nft list table inet filter
sudo nft list table inet filter
</pre>
</pre>
== Logging ==
In der Rule vor ''accept'' den Befehl <code>log prefix "<prefix text>: "</code> einfügen.
Beispiel:
<pre>
tcp dport { 50000 } log prefix "New connection on port 50000: " accept;   
</pre>
== Element aus Set entfernen ==
Am Beispiel ''fail2ban'':
<pre>
sudo nft delete element ip fail2ban f2b-recidive { 222.222.222.222 }
</pre>
Alternativ:
<pre>
sudo fail2ban-client set sasl unbanip 222.222.222.222
</pre>
== Beispielkonfigurationen ==
[[nftables Mailserver|Mailserver]]
[[nftables Nameserver|Nameserver]]
[[nftables Router Hofstetten|Router Hofstetten]]
[[nftables Testserver|Testserver]]
[[nftables Webserver|Webserver]]
== Blacklisting ==
[[Blacklisting mit nftables (Linux)|Blacklisting mit nftables]]


== Links ==  
== Links ==  
[https://www.netfilter.org/projects/nftables/manpage.html https://www.netfilter.org/projects/nftables/manpage.html]


[https://wiki.nftables.org/wiki-nftables/index.php/Main_Page https://wiki.nftables.org/wiki-nftables/index.php/Main_Page]
[https://wiki.nftables.org/wiki-nftables/index.php/Main_Page https://wiki.nftables.org/wiki-nftables/index.php/Main_Page]
Zeile 130: Zeile 176:
[https://wiki.debian.org/nftables https://wiki.debian.org/nftables]
[https://wiki.debian.org/nftables https://wiki.debian.org/nftables]


[http://manpages.ubuntu.com/manpages/cosmic/man8/nft.8.html http://manpages.ubuntu.com/manpages/cosmic/man8/nft.8.html]
[https://manpages.debian.org/testing/nftables/nftables.8.en.html https://manpages.debian.org/testing/nftables/nftables.8.en.html]


[https://linuxandcaffeine.com/setup-a-simple-web-server-firewall-using-nftables/ https://linuxandcaffeine.com/setup-a-simple-web-server-firewall-using-nftables/]
[https://linuxandcaffeine.com/setup-a-simple-web-server-firewall-using-nftables/ https://linuxandcaffeine.com/setup-a-simple-web-server-firewall-using-nftables/]
{{note|To check}}
[https://wiki.meurisse.org/wiki/nftables https://wiki.meurisse.org/wiki/nftables]




Zurück zu [[Ubuntu#N (Server)|Ubuntu]]
Zurück zu [[Ubuntu#N (Server)|Ubuntu]]

Aktuelle Version vom 25. Juni 2022, 16:36 Uhr

Installation

sudo apt-get install -y nftables

Service

sudo systemctl enable nftables.service
sudo systemctl restart nftables.service

Firewall

Pre-Konfiguration

sudo vi /etc/nftables.conf
#!/usr/sbin/nft -f

flush ruleset

table inet filter {
   chain input {
      type filter hook input priority 0; policy accept;
   }

   chain forward {
      type filter hook forward priority 0; policy accept;
   }

   chain output {
      type filter hook output priority 0; policy accept;
   }
}
table inet filter
Legt eine Tabelle mit dem Namen filter für die Familiy inet an.
Mit der Familie inet lassen sich Regeln für IPv4 und IPv6 auf einmal definieren.
type filter hook input priority 0;
type legt fest, welche Art von Kette gebildet werden soll. Mögliche Werte sind filter, route oder nat.
hook legt fest, in welcher Phase sich die Pakete während der Bearbeitung befinden. Mögliche Werte sind prerouting, input, forward, output oder postrouting.
priority legt die Reihenfolge der Ketten fest bzw. legt sie zwischen Netfilter-Operationen.
policy accept;
Lässt als Standard-Regel alle Pakete durch.

Links

https://wiki.nftables.org/wiki-nftables/index.php/Quick_reference-nftables_in_10_minutes

Beispiele

# icmp (ping)
icmp type echo-request limit rate 10/second burst 2 packets counter accept;
# open sshd (22) for internal networks only
tcp dport { ssh } ip saddr @internal_networks_ip4 limit rate 15/minute accept;

Links

https://wiki.nftables.org/wiki-nftables/index.php/Rate_limiting_matchings

https://de.wikipedia.org/wiki/Liste_der_standardisierten_Ports

Routing

Wichtig ist IP-Forwarding zu aktivieren - siehe dazu Netzwerk einrichten

# firewall
table ip filter {
  # allow all packets sent by the firewall machine itself
  chain output {
    type filter hook output priority 100; policy accept;
  }

  # allow LAN to firewall, disallow WAN to firewall
  chain input {
    type filter hook input priority 0; policy accept;
    iifname "lan0" accept
    iifname "wan0" drop
  }

  # allow packets from LAN to WAN, and WAN to LAN if LAN initiated the connection
  chain forward {
    type filter hook forward priority 0; policy drop;
    iifname "lan0" oifname "wan0" accept
    iifname "wan0" oifname "lan0" ct state related,established accept
  }
}

# NAT
table ip nat {
  chain prerouting {
    type nat hook prerouting priority 0; policy accept;
  }

  # for all packets to WAN, after routing, replace source address with primary IP of WAN interface
  chain postrouting {
    type nat hook postrouting priority 100; policy accept;
    oifname "wan0" masquerade
  }
}

Links

https://wiki.gentoo.org/wiki/Nftables/Examples

Kontrolle

sudo nft list tables
sudo nft list table inet filter

Logging

In der Rule vor accept den Befehl log prefix "<prefix text>: " einfügen.

Beispiel:

tcp dport { 50000 } log prefix "New connection on port 50000: " accept;    

Element aus Set entfernen

Am Beispiel fail2ban:

sudo nft delete element ip fail2ban f2b-recidive { 222.222.222.222 }

Alternativ:

sudo fail2ban-client set sasl unbanip 222.222.222.222

Beispielkonfigurationen

Mailserver

Nameserver

Router Hofstetten

Testserver

Webserver

Blacklisting

Blacklisting mit nftables

Links

https://www.netfilter.org/projects/nftables/manpage.html

https://wiki.nftables.org/wiki-nftables/index.php/Main_Page

https://wiki.debian.org/nftables

https://manpages.debian.org/testing/nftables/nftables.8.en.html

https://linuxandcaffeine.com/setup-a-simple-web-server-firewall-using-nftables/

To check

https://wiki.meurisse.org/wiki/nftables


Zurück zu Ubuntu