How to block an country with NFTables and db ip

How to block an country with NFTables and db ip

It can be useful in order to reduce the surface attack by allow only one or several countries to access to your web site. I manage a site which has an exclusively public in France. The server I use is a Debian 10 with Nftables as firewall. I have searched for a long time on the web an easy and fast solution, Nftables being recent, and the most of sites deals with Iptables, IP database maxmind with the mmdb format to convert…
I will explain in this post how to configure Nftables with the geoip database DB-IP which can be updated for free every month.

configuration db geoip

First we will clone the repo from github.

git clone https://github.com/JMGuisadoG/nftables-geoip

We run the script in the directory

cd nftables-geoip
./nft_geoip.py --file-location location.csv --download

The script has generated several files with nft format as well the IP database with csv format

Configuration Nftables

Then, we will create a directory nftables in /etc to store the useful files

sudo mkdir /etc/nftables
sudo mv geoip-ipv4.nft /etc/nftables
sudo mv geoip-def-all.nft /etc/nftables

As mentioned on the github “It is not possible to use the country definitions inside an interactive nft shell“. We have to modify the configuration file of Ntables (/etc/nftables.conf )

table inet filter {

        include "/etc/nftables/geoip-def-all.nft"
        include "/etc/nftables/geoip-ipv4.nft"
        include "/etc/nftables/geoip-ipv6.nft"

        chain output {
                type filter hook output priority filter; policy accept;
        }

        chain input {
                type filter hook input priority filter; policy accept;
                meta mark set ip saddr map @geoip4
                meta mark set ip6 saddr map @geoip6
                meta mark $FR tcp dport 443 accept
                tcp dport 443 drop
        }
}

The parameter include allow to load the geoip databases. At beginning, I used the cloned directory in home but it does not work. So I create the directory nftables in /etc .
meta mark set ip : allow to mark the inbound packets with their country code.
meta mark $FR allow to apply the NTables arguments on the packets marked FR.
For the example, I allow all the packets from France with the destination port 443. You can adapt to your configuration.

Official nftables site

SEE TOO Crowdsec Installation on rpi3 with DietPi / Raspberry OS

1 COMMENT

derda

this doesnt work…

In file included from /etc/nftables.conf:8:1-33:
/etc/geoip-def-all.nft:259:20-20: Error: syntax error, unexpected ‘{‘, expecting string
map continent_code {
^
In file included from /etc/nftables.conf:8:1-33:
/etc/geoip-def-all.nft:260:2-5: Error: syntax error, unexpected type
type mark : mark
^^^^
In file included from /etc/nftables.conf:8:1-33:
/etc/geoip-def-all.nft:261:2-6: Error: syntax error, unexpected flags
flags interval
^^^^^
In file included from /etc/nftables.conf:8:1-33:
/etc/geoip-def-all.nft:262:2-9: Error: syntax error, unexpected elements
elements = {
^^^^^^^^
In file included from /etc/nftables.conf:8:1-33:
/etc/geoip-def-all.nft:263:3-3: Error: syntax error, unexpected ‘$’
$AF : $asia,
^
In file included from /etc/nftables.conf:8:1-33:
/etc/geoip-def-all.nft:264:3-3: Error: syntax error, unexpected ‘$’
$AX : $europe,
^
In file included from /etc/nftables.conf:8:1-33:
/etc/geoip-def-all.nft:265:3-3: Error: syntax error, unexpected ‘$’
$AL : $europe,
^
In file included from /etc/nftables.conf:8:1-33:
/etc/geoip-def-all.nft:266:3-3: Error: syntax error, unexpected ‘$’
$DZ : $africa,
^
In file included from /etc/nftables.conf:8:1-33:
/etc/geoip-def-all.nft:267:3-3: Error: syntax error, unexpected ‘$’
$AS : $oceania,
^
In file included from /etc/nftables.conf:8:1-33:
/etc/geoip-def-all.nft:268:3-3: Error: syntax error, unexpected ‘$’
$AD : $europe,

Leave a Reply

Your email address will not be published. Required fields are marked *