|
| 1 | +#!/usr/bin/nft -f |
| 2 | + |
| 3 | +# This file is part of https://github.com/random-archer/mkinitcpio-systemd-tool |
| 4 | + |
| 5 | +# Provides firewall when running inside initrd |
| 6 | +# see: https://wiki.archlinux.org/index.php/Nftables |
| 7 | + |
| 8 | +# file location in initramfs: |
| 9 | +# /etc/nftables.conf |
| 10 | + |
| 11 | +# file location in real-root: |
| 12 | +# /etc/mkinitcpio-systemd-tool/config/initrd-nftables.conf |
| 13 | + |
| 14 | +# note: |
| 15 | +# * more nft examples are in /usr/share/nftables/ |
| 16 | +# * make sure SSHD_PORT matches dropbear or tinysshd |
| 17 | + |
| 18 | +define SSHD_PORT = 22 |
| 19 | + |
| 20 | +table inet filter { |
| 21 | + set knockd4-allow { |
| 22 | + type ipv4_addr |
| 23 | + timeout 7d |
| 24 | + } |
| 25 | + set knockd4-step2 { |
| 26 | + type ipv4_addr |
| 27 | + timeout 5s |
| 28 | + } |
| 29 | + set knockd4-step1 { |
| 30 | + type ipv4_addr |
| 31 | + timeout 5s |
| 32 | + } |
| 33 | + set knockd6-allow { |
| 34 | + type ipv6_addr |
| 35 | + timeout 7d |
| 36 | + } |
| 37 | + set knockd6-step2 { |
| 38 | + type ipv6_addr |
| 39 | + timeout 5s |
| 40 | + } |
| 41 | + set knockd6-step1 { |
| 42 | + type ipv6_addr |
| 43 | + timeout 5s |
| 44 | + } |
| 45 | + chain input { |
| 46 | + type filter hook input priority 0; policy drop; |
| 47 | + ip6 nexthdr icmpv6 icmpv6 type echo-request limit rate 1/second accept |
| 48 | + ip6 nexthdr icmpv6 icmpv6 type echo-request counter drop |
| 49 | + ip protocol icmp icmp type echo-request limit rate 1/second accept |
| 50 | + ip protocol icmp icmp type echo-request counter drop |
| 51 | + ct state {established, related} accept |
| 52 | + ct state invalid drop |
| 53 | + tcp dport $SSHD_PORT ip saddr @knockd4-allow accept |
| 54 | + ip saddr @knockd4-step2 tcp dport $SSHD_PORT set add ip saddr @knockd4-allow |
| 55 | + ip saddr @knockd4-step1 tcp dport $SSHD_PORT set add ip saddr @knockd4-step2 |
| 56 | + tcp dport $SSHD_PORT set add ip saddr @knockd4-step1 |
| 57 | + tcp dport $SSHD_PORT ip6 saddr @knockd6-allow accept |
| 58 | + ip6 saddr @knockd6-step2 tcp dport $SSHD_PORT set add ip6 saddr @knockd6-allow |
| 59 | + ip6 saddr @knockd6-step1 tcp dport $SSHD_PORT set add ip6 saddr @knockd6-step2 |
| 60 | + tcp dport $SSHD_PORT set add ip6 saddr @knockd6-step1 |
| 61 | + ip6 nexthdr icmpv6 icmpv6 type { destination-unreachable, packet-too-big, time-exceeded, parameter-problem, echo-reply, nd-router-advert, nd-neighbor-solicit, nd-neighbor-advert } accept |
| 62 | + ip protocol icmp icmp type { destination-unreachable, router-advertisement, time-exceeded, parameter-problem } accept |
| 63 | + reject |
| 64 | + } |
| 65 | + chain forward { |
| 66 | + type filter hook forward priority 0; policy accept; |
| 67 | + accept |
| 68 | + } |
| 69 | + chain output { |
| 70 | + type filter hook output priority 0; policy accept; |
| 71 | + } |
| 72 | +} |
0 commit comments