nftables für Anfänger

Alles rund um sicherheitsrelevante Fragen und Probleme.
MoritzJ
Beiträge: 13
Registriert: 19.08.2020 20:17:07

Re: nftables für Anfänger

Beitrag von MoritzJ » 20.08.2020 19:01:56

Danke dir wanne,

werde das mal in aller Ruhe durchlesen und versuchen es in meiner conf Datei einzubauen. :THX:

MoritzJ
Beiträge: 13
Registriert: 19.08.2020 20:17:07

Re: nftables für Anfänger

Beitrag von MoritzJ » 21.08.2020 15:22:25

@wanne
Bin da nicht ganz durchgestiegen bei deinen Anmerkungen, insbesondere
Ich ziehe reject drop for
Also ist reject "sofort" und "drop" zeitverzögert?
Bezogen auf mein unteres Beispiel müsste ich also

Code: Alles auswählen

ct state invalid drop
in

Code: Alles auswählen

ct state invalid reject
ändern um eben dem User die Fehlermeldung sofort zu schicken?



Habe jetzt im Netz etwas gefunden und ein wenig umgebaut, meiner Meinung nach schon besser als der 1. Entwurf (vor Allem der "invalid packets" Kram):

Code: Alles auswählen

#!/usr/sbin/nft -f

# Start by flushing all the rules.
flush ruleset

# Define private IP for ssh access
define privateip = {1.1.1.1}

table inet filter {
    # TCP ports to allow. (Allowed services: HTTP, HTTPS)
    set tcp_accepted {
        type inet_service; flags interval;
        elements = {
        80,443
        }
    }
    # TCP port for SSH service.
    set ssh_accepted {
        type inet_service; flags interval;
        elements = {
        721
        }
    }
    # UDP ports to allow.
    set udp_accepted {
        type inet_service; flags interval;
        elements = {
        
        }
    }
    chain input {
        # This line set what traffic the chain will handle, the priority and default policy.
        # The priority comes in when you in another table have a chain set to "hook input" and want to specify in what order they should run.
        # Use a semicolon to separate multiple commands on one row.
        type filter hook input priority 0; policy drop;

        # Limit ping requests. (Limit rules need to be put before accepting "established" connections)
        ip protocol icmp icmp type echo-request limit rate over 1/second burst 5 packets drop
        ip6 nexthdr icmpv6 icmpv6 type echo-request limit rate over 1/second burst 5 packets drop

        # Allow all incomming established and related traffic. Drop invalid traffic.
        ct state established,related accept
        ct state invalid drop

        # Allow loopback.
        iif lo accept

        # Drop all fragments.
        ip frag-off & 0x1fff != 0 counter drop

        # Force SYN checks.
        tcp flags & (fin|syn|rst|ack) != syn ct state new counter drop

        # Drop XMAS packets.
        tcp flags & (fin|syn|rst|psh|ack|urg) == fin|syn|rst|psh|ack|urg counter drop

        # Drop NULL packets.
        tcp flags & (fin|syn|rst|psh|ack|urg) == 0x0 counter drop

        # Allow certain inbound ICMP types (ping, traceroute).
        # Without the nd-* ones ipv6 will not work.
        ip protocol icmp icmp type { destination-unreachable, echo-reply, echo-request, source-quench, time-exceeded } accept      
        ip6 nexthdr icmpv6 icmpv6 type { destination-unreachable, echo-reply, echo-request, nd-neighbor-solicit,  nd-router-advert, nd-neighbor-advert, packet-too-big, parameter-problem, time-exceeded } accept

        # Allow SSH for specific IP only
        iifname $privateip tcp dport @ssh_accepted ct state new accept

        # Allow needed tcp and udp ports.
        tcp dport @tcp_accepted ct state new accept
        udp dport @udp_accepted ct state new accept
    }
    chain forward {
        type filter hook forward priority 0; policy drop;

        # Forward all established and related traffic. Drop invalid traffic.
        ct state established,related accept
        ct state invalid drop
    }
    chain output {
        type filter hook output priority 0; policy drop;

        # Allow all outgoing traffic. Drop invalid traffic.
        # ipv6 ICMP needs to be explicitly allowed here.
        ip6 nexthdr ipv6-icmp accept
        ct state new,established,related accept
        ct state invalid drop
    }
}

wanne
Moderator
Beiträge: 7462
Registriert: 24.05.2010 12:39:42

Re: nftables für Anfänger

Beitrag von wanne » 21.08.2020 15:53:30

Code: Alles auswählen

ip protocol icmp icmp type { destination-unreachable, echo-reply, echo-request, source-quench, time-exceeded } accept
Das ist genau das default verhalten. Alle anderen verwirft der Kernel IMHO eh. Sprich äquivalent zu dem:

Code: Alles auswählen

ip protocol icmp accept
Sonst sieht das gut aus.
rot: Moderator wanne spricht, default: User wanne spricht.

MoritzJ
Beiträge: 13
Registriert: 19.08.2020 20:17:07

Re: nftables für Anfänger

Beitrag von MoritzJ » 21.08.2020 23:27:02

danke für die Erklärung!

kriege jetzt beim starten 2 Fehlermeldungen:

Code: Alles auswählen

        # Allow all incomming established and related traffic. Drop invalid traffic.
        ct state established,related accept
        ct state invalid drop

        # Allow loopback.
        iif lo accept
ct state invalid drop spuckt "Error: syntax error, unexpected state, expecting helper" und
iif lo accept spuckt "Error: syntax error, unexpected iif" aus

Warum ist der Syntax falsch? Schnall ich nicht :?

mat6937
Beiträge: 2953
Registriert: 09.12.2014 10:44:00

Re: nftables für Anfänger

Beitrag von mat6937 » 22.08.2020 11:27:51

MoritzJ hat geschrieben: ↑ zum Beitrag ↑
21.08.2020 23:27:02
kriege jetzt beim starten 2 Fehlermeldungen:

Code: Alles auswählen

        # Allow all incomming established and related traffic. Drop invalid traffic.
        ct state established,related accept
        ct state invalid drop

        # Allow loopback.
        iif lo accept
ct state invalid drop spuckt "Error: syntax error, unexpected state, expecting helper" und
iif lo accept spuckt "Error: syntax error, unexpected iif" aus

Warum ist der Syntax falsch? Schnall ich nicht :?
Evtl. ist ja auch nicht die Syntax, sondern der Zustand. Versuch mal mit geänderter Reihenfolge:

Code: Alles auswählen

iif lo accept
ct state invalid drop
ct state established,related accept

MoritzJ
Beiträge: 13
Registriert: 19.08.2020 20:17:07

Re: nftables für Anfänger

Beitrag von MoritzJ » 22.08.2020 12:50:07

Habs ausprobiert, aber die Fehlermeldungen bleiben:

Code: Alles auswählen

iif lo accept
ct state invalid drop
ct state established,related accept
ergibt 2 Fehlermeldungen
Error: syntax error, unexpected state, expecting helper
(2. Zeile)
Error: syntax error, unexpected state, expecting helper
(3. Zeile)

Könnte es sein, dass ich weitere Module für den Kernel brauche oder sowas in der Art?

Nachtrag
Wenn ich die drei Zeilen generell deaktiviere dann kriege ich n Syntax Fehler weiter unten bei den TCP Flags Zeilen :?

Nachtrag 2:
Fehler gefunden und behoben. Hatte durch ein Installationsscript oben eine Variable anstatt die IP drinnen, das hat alles durcheinander gebracht :facepalm:

wanne
Moderator
Beiträge: 7462
Registriert: 24.05.2010 12:39:42

Re: nftables für Anfänger

Beitrag von wanne » 22.08.2020 13:39:18

ct state invalid drop spuckt "Error: syntax error, unexpected state, expecting helper" und
iif lo accept spuckt "Error: syntax error, unexpected iif" aus

Warum ist der Syntax falsch? Schnall ich nich
Gerade nochmal ausprobiert. Bei mir frisst er das problemlos.
rot: Moderator wanne spricht, default: User wanne spricht.

MoritzJ
Beiträge: 13
Registriert: 19.08.2020 20:17:07

Re: nftables für Anfänger

Beitrag von MoritzJ » 22.08.2020 13:44:45

wanne hat geschrieben: ↑ zum Beitrag ↑
22.08.2020 13:39:18
ct state invalid drop spuckt "Error: syntax error, unexpected state, expecting helper" und
iif lo accept spuckt "Error: syntax error, unexpected iif" aus

Warum ist der Syntax falsch? Schnall ich nich
Gerade nochmal ausprobiert. Bei mir frisst er das problemlos.
hatte meinen Beitrag editiert ... ich erstelle die conf Datei via Bash Script und hatte dort einen Syntaxfehler drinnen

anstatt

Code: Alles auswählen

define privateip = {1.1.1.1}
hatte ich

Code: Alles auswählen

define privateip = {$clientip}
in der finalen conf Datei, das hat bei mir zu Fehlern geführt. Danke für eure Hilfe!

wanne
Moderator
Beiträge: 7462
Registriert: 24.05.2010 12:39:42

Re: nftables für Anfänger

Beitrag von wanne » 22.08.2020 13:46:55

Ansonsten mir hat die erste Variante eigentlich von der Idee her sehr gut gefallen. Du hast schlicht am input gefiltert. Jetzt merkst du dir die Verbindungen die du am input zugelassen hast damit du die Antworten am output auch wieder zulassen kannst. Es mag die üblichere Variante sein ich finde sie trotzdem nicht sinnvoller. Am Ende ist die Funktionalität sehr ähnlich und du hast doppelt so viele regeln die dem System doppelt so viel Arbeit machen.
Aber grundsätzlich ist das sinnvoll.
rot: Moderator wanne spricht, default: User wanne spricht.

wanne
Moderator
Beiträge: 7462
Registriert: 24.05.2010 12:39:42

Re: nftables für Anfänger

Beitrag von wanne » 22.08.2020 13:49:14

Ah und nochmal was du hast dhcp gefiltert IPv6 autoconfiguration aber nicht. Hast du die IPv4 Adresse statisch konfiguriert aber die IPv6 nicht?
rot: Moderator wanne spricht, default: User wanne spricht.

MoritzJ
Beiträge: 13
Registriert: 19.08.2020 20:17:07

Re: nftables für Anfänger

Beitrag von MoritzJ » 22.08.2020 13:59:31

Ich habe da gar nix konfiguriert, habe nur das Minimal Image installiert und sonst nix weiter gemacht.

Antworten