basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    Docker ports bypass the input chain

    A firewall rule in input doesn’t touch containers with published ports: after DNAT their traffic goes through forward. For a ban to cover the whole server, put the rule in both input and forward.

    How a packet reaches a container

    NIC → prerouting ─┬─ destination is the host itself ──→ input   → sshd, host services,
                      │                                              containers with network_mode: host
                      └─ destination is not the host ─────→ forward → containers with published ports

    When a container publishes a port (ports: - 443:443), Docker does DNAT in prerouting: it rewrites the destination to the container’s internal address, 172.18.0.x. To the kernel the packet is no longer addressed to the host, so it goes to forward. It never reaches input, and no rule there will stop it.

    input and forward are hooks: points on a packet’s path through the kernel where netfilter hands it to the rules; the others are prerouting, output and postrouting. An nftables chain is attached to one hook (type filter hook input) and doesn’t see packets from the others.

    DNAT only changes the destination. The source stays the client’s real address, so forward can block by it just like input does.

    The table of a bouncer that covers the host and the containers

    Blocking chains get a priority ahead of Docker’s rules (filter - 10) and an accept policy: they only drop addresses from the sets and pass everything else on.

    nft list table ip crowdsec
    table ip crowdsec {
        set crowdsec-blacklists-crowdsec { type ipv4_addr; flags timeout; ... }
        set crowdsec-blacklists-CAPI     { type ipv4_addr; flags timeout; ... }

    Sets with a timeout: when a ban expires, the address drops out of the set by itself. Community blocklists and subscriptions come as separate sets, each with its own hit counter.

        chain crowdsec-chain-input {
            type filter hook input priority filter - 10; policy accept;
            ip saddr @crowdsec-blacklists-crowdsec drop
            ip saddr @crowdsec-blacklists-CAPI drop
        }

    input — SSH and everything the host itself listens on.

        chain crowdsec-chain-forward {
            type filter hook forward priority filter - 10; policy accept;
            ip saddr @crowdsec-blacklists-crowdsec drop
            ip saddr @crowdsec-blacklists-CAPI drop
        }
    }
    Key part

    forward — container published ports. Without this chain the ban never reaches sites in Docker.

    table ip6 crowdsec6 { ... the same for IPv6 ... }

    IPv6 has its own table and its own sets.

    What to block with: bouncer, DOCKER-USER or proxy

    How you block What’s covered When it fits
    firewall bouncer on input and forward the whole server: SSH, host services, every container almost always. The config that ships with the bouncer already lists both hooks: nftables_hooks with input and forward. Without that line, the bouncer uses input only
    input only SSH and host services, not containers when something else covers the containers, such as a bouncer in the reverse proxy
    your own iptables rule in DOCKER-USER containers when containers need hand-written iptables rules: Docker leaves this chain alone
    bouncer in the proxy (Traefik plugin and the like) HTTP through the proxy only sites behind a CDN: the firewall sees CDN addresses there, not visitors (CrowdSec with Traefik behind Cloudflare)

    If it doesn’t work

    The address is banned, but the site in a container still opens for it

    Cause
    The ban sits only on input: Docker did DNAT, and the packet to the container went to forward, past that chain.
    Fix
    Give the bouncer a forward hook, or add your own rule to DOCKER-USER.

    ufw deny 8080, but the container port is still open from outside

    Cause
    ufw works in input, while a published port goes through forward.
    Fix
    If only the host needs the service, publish it on localhost — ports: - "127.0.0.1:8080:8080" — and the port doesn’t exist from outside at all.

    A container with network_mode: host gets blocked, the others don’t

    Cause
    It has no DNAT: its ports are the host’s ports, and its traffic goes through input.
    Fix
    Nothing: that’s expected.

    The firewall doesn’t ban visitors of a site behind Cloudflare

    Cause
    Packets come from Cloudflare addresses, not from the visitor. Banning those cuts off everyone who comes through the CDN.
    Fix
    Put a bouncer into the reverse proxy: it reads X-Forwarded-For and bans the real address (CrowdSec with Traefik behind Cloudflare).

    The ban works over IPv4 but not over IPv6

    Cause
    There is no table for ip6, or the containers listen on IPv6 separately.
    Fix
    Check nft list table ip6 crowdsec6.

    How to check

    First, check that both hooks exist:

    nft list table ip crowdsec | grep hook
    # type filter hook input priority filter - 10; policy accept;
    # type filter hook forward priority filter - 10; policy accept;

    Then test it for real, from your phone on mobile data — it has a different address, which ifconfig.me shows. Ban it for 2 minutes: if you get the address wrong, the ban lifts itself.

    docker exec crowdsec cscli decisions add --ip <IP> --duration 2m --reason test
    # the site in the container (no CDN proxy) doesn't open at all — a timeout, not an error;
    # SSH from this address doesn't connect either
    docker exec crowdsec cscli decisions delete --ip <IP>