Fail2Ban + nftables

Locked
User avatar
Moritz83
Posts: 38
Joined: Sun 26. Jul 2020, 18:01

Fail2Ban + nftables

Post by Moritz83 »

Servus,

suche einen Fehler in meiner Konfiguration (Debian 10 + Keyhelp 20.2 + nftables + fail2ban) und zwar wurde meine IP gebannt (habs zum Spass mal ausprobiert)

Code: Select all

fail2ban-client status sshd
zeigt mir dies auch an aber ich kann weiterhin per SSH einloggen (was ja eigentlich nicht gehen sollte). Habe die Konfiguration aus Ollis Thread (viewtopic.php?f=16&t=8449) übernommen, allerdings noch die Datei
defaults-debian.conf
um die explizite Portangabe erweitert

Code: Select all

[sshd]
enabled = true
port = 721
Des Weiteren habe ich in der Datei
jai.conf
die banaction Zeilen wie folgt geändert

Code: Select all

banaction = nftables-multiport
banaction_allports = nftables-allports
und in meine
/etc/nftables.conf
sieht so aus (am Schluss ist der entsprechende Eintrag bzgl. Fail2Ban

Code: Select all

#!/usr/sbin/nft -f

# Start by flushing all the rules.
flush ruleset

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

table inet filter {
    # TCP ports to allow. (Allowed services: HTTP, HTTPS, SFTP)
    set tcp_accepted {
        type inet_service; flags interval;
        elements = {
        80,443,722
        }
    }
    # TCP port for SSH service.
    set ssh_accepted {
        type inet_service; flags interval;
        elements = {
        721
        }
    }
    # UDP ports to allow. (Allowed services: Teamspeak 3)
    set udp_accepted {
        type inet_service; flags interval;
        elements = {
        9987
        }
    }
    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.
        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

        # OBS! Rules with "limit" need to be put before rules accepting "established" connections.
        # Allow all incomming established and related traffic. Drop invalid traffic.
        ct state established,related accept
        ct state invalid drop

        # Allow loopback.
        # Interfaces can by set with "iif" or "iifname" (oif/oifname). If the interface can come and go use "iifname", otherwise use "iif" since it performs better.
        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).
        # With these allowed you are a good network citizen.
        # 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
        ip saddr $privateip tcp dport @ssh_accepted accept
        tcp dport @ssh_accepted drop

        # 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
    }
}

include "/etc/nftables/fail2ban.conf"
Ich sollte doch eigentlich trotz der expliziten Freigabe meiner IP für SSHD durch fail2ban gebannt werden können, oder?
Habe zum Spass auch mal den Teil mit der IP Freigabe komplett rausgenommen, bringt nix ... trotz angezeigter geblockter IP von Fail2Ban kann ich mich einloggen ... warum ist das so?
User avatar
Moritz83
Posts: 38
Joined: Sun 26. Jul 2020, 18:01

Re: Fail2Ban + nftables

Post by Moritz83 »

Nachtrag:

Im Logfile finden sich jede Menge Fehler :roll:

Code: Select all

2020-09-15 17:38:46,136 fail2ban.jail           [602]: INFO    Jail 'postfix-rbl' started
2020-09-15 17:38:46,190 fail2ban.jail           [602]: INFO    Jail 'dovecot' started
2020-09-15 17:38:46,216 fail2ban.jail           [602]: INFO    Jail 'mysqld-auth' started
2020-09-15 17:38:46,233 fail2ban.jail           [602]: INFO    Jail 'recidive' started
2020-09-15 17:38:46,312 fail2ban.actions        [602]: NOTICE  [sshd] Restore Ban 77.56.188.228
2020-09-15 17:38:46,332 fail2ban.jail           [602]: INFO    Jail 'keyhelp-phpmyadmin' started
2020-09-15 17:38:46,409 fail2ban.utils          [602]: Level 39 7f6d9ff59570 -- exec: nft add set inet fail2ban f2b-sshd \{ type ipv4_addr\; \}
nft insert rule inet fail2ban fail2ban tcp dport \{ 721 \} ip saddr @f2b-sshd drop
2020-09-15 17:38:46,410 fail2ban.utils          [602]: ERROR   7f6d9ff59570 -- stderr: 'Error: Could not process rule: No such file or directory'
2020-09-15 17:38:46,410 fail2ban.utils          [602]: ERROR   7f6d9ff59570 -- stderr: 'add set inet fail2ban f2b-sshd { type ipv4_addr; }'
2020-09-15 17:38:46,410 fail2ban.utils          [602]: ERROR   7f6d9ff59570 -- stderr: '             ^^^^^^^^'
2020-09-15 17:38:46,410 fail2ban.utils          [602]: ERROR   7f6d9ff59570 -- stderr: 'Error: Could not process rule: No such file or directory'
2020-09-15 17:38:46,410 fail2ban.utils          [602]: ERROR   7f6d9ff59570 -- stderr: 'insert rule inet fail2ban fail2ban tcp dport { 721 } ip saddr @f2b-sshd drop'
2020-09-15 17:38:46,410 fail2ban.utils          [602]: ERROR   7f6d9ff59570 -- stderr: '                 ^^^^^^^^'
2020-09-15 17:38:46,410 fail2ban.utils          [602]: ERROR   7f6d9ff59570 -- returned 1
2020-09-15 17:38:46,410 fail2ban.actions        [602]: ERROR   Failed to execute ban jail 'sshd' action 'nftables-multiport' info 'ActionInfo({'ip': '77.56.188.228', 'family': 'inet4', 'ip-rev': '228.188.56.77.', 'ip-host': '77-56-188-228.dclient.hispeed.ch', 'fid': '77.56.188.228', 'failures': 2, 'time': 1600184326.0785308, 'matches': 'Sep 15 15:32:25 jvps sshd[31608]: Invalid user 23132123 from 77.56.188.228 port 8832\nSep 15 15:57:06 jvps sshd[32673]: Invalid user 132321 from 77.56.188.228 port 7125', 'restored': 1, 'F-*': {'matches': [['', 'Sep 15 15:32:25', ' jvps sshd[31608]: Invalid user 23132123 from 77.56.188.228 port 8832'], 'Sep 15 15:57:06 jvps sshd[32673]: Invalid user 132321 from 77.56.188.228 port 7125'], 'failures': 2, 'mlfid': ' jvps sshd[31608]: ', 'ip4': '77.56.188.228', 'user': '23132123'}, 'ipmatches': '2020-09-15 15:26:39,835 fail2ban.actions        [28689]: NOTICE  [sshd] Ban 77.56.188.228\n2020-09-15 15:31:04,234 fail2ban.actions        [28689]: NOTICE  [sshd] Ban 77.56.188.228\n2020-09-15 15:32:18,542 fail2ban.actions        [28689]: NOTICE  [sshd] Ban 77.56.188.228\n2020-09-15 15:57:06,417 fail2ban.actions        [28689]: NOTICE  [sshd] Ban 77.56.188.228\nSep 15 15:32:25 jvps sshd[31608]: Invalid user 23132123 from 77.56.188.228 port 8832\nSep 15 15:57:06 jvps sshd[32673]: Invalid user 132321 from 77.56.188.228 port 7125\nSep 15 15:57:12 jvps sshd[32675]: Invalid user 21312321 from 77.56.188.228 port 7182\nSep 15 15:57:25 jvps sshd[32678]: Invalid user 312213 from 77.56.188.228 port 7278\nSep 15 16:00:30 jvps sshd[320]: Invalid user 13213 from 77.56.188.228 port 8809\nSep 15 16:00:36 jvps sshd[322]: Invalid user 123123 from 77.56.188.228 port 8862', 'ipjailmatches': 'Sep 15 15:32:25 jvps sshd[31608]: Invalid user 23132123 from 77.56.188.228 port 8832\nSep 15 15:57:06 jvps sshd[32673]: Invalid user 132321 from 77.56.188.228 port 7125\nSep 15 15:57:12 jvps sshd[32675]: Invalid user 21312321 from 77.56.188.228 port 7182\nSep 15 15:57:25 jvps sshd[32678]: Invalid user 312213 from 77.56.188.228 port 7278\nSep 15 16:00:30 jvps sshd[320]: Invalid user 13213 from 77.56.188.228 port 8809\nSep 15 16:00:36 jvps sshd[322]: Invalid user 123123 from 77.56.188.228 port 8862', 'ipfailures': 10, 'ipjailfailures': 6})': Error starting action Jail('sshd')/nftables-multiport
2020-09-15 17:38:46,480 fail2ban.actions        [602]: NOTICE  [recidive] Restore Ban 77.56.188.228
2020-09-15 17:38:46,517 fail2ban.utils          [602]: Level 39 7f6d9fd020f8 -- exec: nft add set inet fail2ban f2b-recidive \{ type ipv4_addr\; \}
nft insert rule inet fail2ban fail2ban meta l4proto tcp ip saddr @f2b-recidive drop
2020-09-15 17:38:46,517 fail2ban.utils          [602]: ERROR   7f6d9fd020f8 -- stderr: 'Error: Could not process rule: No such file or directory'
2020-09-15 17:38:46,517 fail2ban.utils          [602]: ERROR   7f6d9fd020f8 -- stderr: 'add set inet fail2ban f2b-recidive { type ipv4_addr; }'
2020-09-15 17:38:46,517 fail2ban.utils          [602]: ERROR   7f6d9fd020f8 -- stderr: '             ^^^^^^^^'
2020-09-15 17:38:46,517 fail2ban.utils          [602]: ERROR   7f6d9fd020f8 -- stderr: 'Error: Could not process rule: No such file or directory'
2020-09-15 17:38:46,517 fail2ban.utils          [602]: ERROR   7f6d9fd020f8 -- stderr: 'insert rule inet fail2ban fail2ban meta l4proto tcp ip saddr @f2b-recidive drop'
2020-09-15 17:38:46,517 fail2ban.utils          [602]: ERROR   7f6d9fd020f8 -- stderr: '                 ^^^^^^^^'
2020-09-15 17:38:46,517 fail2ban.utils          [602]: ERROR   7f6d9fd020f8 -- returned 1
2020-09-15 17:38:46,518 fail2ban.actions        [602]: ERROR   Failed to execute ban jail 'recidive' action 'nftables-allports' info 'ActionInfo({'ip': '77.56.188.228', 'family': 'inet4', 'ip-rev': '228.188.56.77.', 'ip-host': '77-56-188-228.dclient.hispeed.ch', 'fid': '77.56.188.228', 'failures': 2, 'time': 1600184326.233535, 'matches': '2020-09-15 15:26:39,835 fail2ban.actions        [28689]: NOTICE  [sshd] Ban 77.56.188.228\n2020-09-15 15:31:04,234 fail2ban.actions        [28689]: NOTICE  [sshd] Ban 77.56.188.228', 'restored': 1, 'F-*': {'matches': [['', '2020-09-15 15:26:39,835', ' fail2ban.actions        [28689]: NOTICE  [sshd] Ban 77.56.188.228'], '2020-09-15 15:31:04,234 fail2ban.actions        [28689]: NOTICE  [sshd] Ban 77.56.188.228'], 'failures': 2, 'ip4': '77.56.188.228'}, 'ipmatches': '2020-09-15 15:26:39,835 fail2ban.actions        [28689]: NOTICE  [sshd] Ban 77.56.188.228\n2020-09-15 15:31:04,234 fail2ban.actions        [28689]: NOTICE  [sshd] Ban 77.56.188.228\n2020-09-15 15:32:18,542 fail2ban.actions        [28689]: NOTICE  [sshd] Ban 77.56.188.228\n2020-09-15 15:57:06,417 fail2ban.actions        [28689]: NOTICE  [sshd] Ban 77.56.188.228\nSep 15 15:32:25 jvps sshd[31608]: Invalid user 23132123 from 77.56.188.228 port 8832\nSep 15 15:57:06 jvps sshd[32673]: Invalid user 132321 from 77.56.188.228 port 7125\nSep 15 15:57:12 jvps sshd[32675]: Invalid user 21312321 from 77.56.188.228 port 7182\nSep 15 15:57:25 jvps sshd[32678]: Invalid user 312213 from 77.56.188.228 port 7278\nSep 15 16:00:30 jvps sshd[320]: Invalid user 13213 from 77.56.188.228 port 8809\nSep 15 16:00:36 jvps sshd[322]: Invalid user 123123 from 77.56.188.228 port 8862', 'ipjailmatches': '2020-09-15 15:26:39,835 fail2ban.actions        [28689]: NOTICE  [sshd] Ban 77.56.188.228\n2020-09-15 15:31:04,234 fail2ban.actions        [28689]: NOTICE  [sshd] Ban 77.56.188.228\n2020-09-15 15:32:18,542 fail2ban.actions        [28689]: NOTICE  [sshd] Ban 77.56.188.228\n2020-09-15 15:57:06,417 fail2ban.actions        [28689]: NOTICE  [sshd] Ban 77.56.188.228', 'ipfailures': 10, 'ipjailfailures': 4})': Error starting action Jail('recidive')/nftables-allports
Was kann das sein? Es scheint als ob er die table "fail2ban" nicht findet, aber die ist doch da?
User avatar
Moritz83
Posts: 38
Joined: Sun 26. Jul 2020, 18:01

Re: Fail2Ban + nftables

Post by Moritz83 »

keine Ahnung wieso aber wenn ich nur Keyhelp installiere dann klappt es plötzlich problemlos. Keine Ahnung ob der im Eingangspost erwähnte Thread von Olli jetzt etwas "zerschiesst" oder ich einfach nur was falsches gemacht habe.
User avatar
Moritz83
Posts: 38
Joined: Sun 26. Jul 2020, 18:01

Re: Fail2Ban + nftables

Post by Moritz83 »

Ich mach hier nochmal auf, entweder bin ich zu doof oder habe etwas vergessen oder was auch immer:

Ich habe jetzt nur die Grundinstallation von Keyhelp drauf, F2B bannt auch wie gewünscht aber die fail2ban.conf (nftables) bleibt ohne Einträge trotz gebannter IPs. Hat hier jemand ein gutes Tutorial zur Hand oder kann mir erklären was ich verändern muss damit F2B mit nftables funktioniert?

Meiner Meinung nach sollten die entsprechenden IP's als Einträge entweder in nftables oder iptables zu finden sein, nach der Grundinstallation trifft aber keines der beiden zu :/
User avatar
OlliTheDarkness
Posts: 1854
Joined: Tue 14. Aug 2018, 16:41
Location: Essen (NRW)

Re: Fail2Ban + nftables

Post by OlliTheDarkness »

Also mein F2B arbeitet supi.

Viele böse IP´s hats bereits gelegt ^^
Unbenannt.png
Mit freundlichen Grüßen
OlliTheDarkness

**************************************************************
Helden leben lange, Legenden sterben nie

:!: World Hack Organization :!:
**************************************************************
Locked