CentOS8 firewalld block country

Üdv!

A firewalld-vel (iptables) szeretnék blokkolni bizonyos országokat (pl. Kína... :))

A neten keresgéltem, de nem igazán találom a szép/korrekt megoldást.

Pl.:

https://www.clearos.com/clearfoundation/social/community/block-access-f…

https://documentation.clearos.com/content:en_us:kb_howtos_country_block…

https://linuxconfig.org/how-to-automatically-execute-shell-script-at-st…

 

Mi lenne a szép megoldás?

Hozzászólások

Szerintem a legkorrektebb megoldás, ha ipset-tel felveszed az adott ország tartományait és azt töltöd be. Nem tudom, hogy az nftables-nél hogy működik, de iptables-szel így csinálom és hibátéanul teszi a dolgát (látom, hogy írtad az iptables-t, de a ( alapértelmezetten nftables-t használ, menjünk biztosra).

firewalld.conf-ban

FirewallBackend=iptables

 

A link alapján: https://documentation.clearos.com/content:en_us:kb_howtos_country_block…

Ez mehetne a cron.monthly-ba:

#!/bin/bash

# A list of the ISO country codes can be found at http://en.wikipedia.org/wiki/ISO_3166-1
# Countries are case insensitive for this script

ISO="at be ch cy cz de dk es fr gb gr ie it lu mt nl pt eu va sm mc je gg im"
MAXELEM=131072
#MAXELEM=524288

if [ "`lsmod | grep ip_set`" = "" ]; then
	modprobe ip_set
fi

# Destroy country-list-temp in case it exists and is populated
ipset destroy -q country-list-temp

# Make sure the new lists exist
ipset create country-list nethash maxelem $MAXELEM -exist
ipset create country-list-temp nethash maxelem $MAXELEM -exist

# Load the country list
curl -s -d country=1 --data-urlencode "country_list=$ISO" -d format_template=prefix https://ip.ludost.net/cgi/process | grep -v ^# | while read -r line
do
    ipset -A -exist country-list-temp $line
done

if [ $(ipset list country-list-temp | wc -l) -le 7 ]; then
    logger -t country-list "Update failed"
    echo 'Country List Update failed' | mail -s 'Country List Update failed' somewhere@example.com
    ipset destroy -q country-list-temp
    exit
fi

# Make the temp list current
ipset swap country-list country-list-temp

# Destroy the (now old) temp list
ipset destroy -q country-list-temp

# add some exceptions
#ipset add -exist country-list 209.90.117.194
#ipset add -exist country-list 209.90.117.196
#ipset add -exist country-list 159.203.19.178

# Create save list for loading on boot
ipset save country-list > /usr/src/ipset_country-list.save
sed -i 's/create/create -exist/g' /usr/src/ipset_country-list.save
sed -i 's/add/add -exist/g' /usr/src/ipset_country-list.save

logger -t country-list "Updated"


# Load in all previously saved ipset sets
if [ "`lsmod | grep ip_set`" = "" ]; then
	modprobe ip_set
fi

for file in /usr/src/ipset_*.save ; do
	ipset restore <  $file
done

 

Ez az "ipset-block.sh" script:

#!/bin/bash
#
# this script name: ipset-block.sh
#


if [ "$FW_PROTO" != "ipv4" ]; then
    return 0
fi

for file in /usr/src/ipset_*.save ; do
	ipset restore <  $file
done

IPTABLES=/usr/sbin/iptables

# Block country addresses (exempt permitted countries)
#
# note the  > /dev/null 2>&1 is needed for some odd reason
ipset create country-list nethash -exist  > /dev/null 2>&1
$IPTABLES -I INPUT -m conntrack --ctstate NEW -m set --match-set country-list src -p tcp-j DROP

Systemd service unit:

# /etc/systemd/system/ipset-block
# /usr/lib/systemd/system/ipset-block

[Unit]
After=network.target

[Service]
ExecStart=/pathto/ipset-block.sh

[Install]
WantedBy=default.target

# -----

 

Ez jó lehet? Az ipset-block.sh a megadott országokat tiltja, ha jól írtam.

Nálam a Shorewall-ban így néz ki:

shorewall show |grep blacklist
  461 22122 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            match-set blacklist1 src
 1124 62213 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            match-set blacklist2 src
 1449 76028 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            match-set blacklist_cn src
 

Ugye alapvetően blacklistX konvencióval hozom létre az ipset-eket, szépen meg is jelenik. A shorewall show parancs egyébként egyenértékű az iptables -L -n -v paranccsal.

Annyira figyelj még, hogy az ipset nem perzisztens, ezért mindig legyen mentés az aktuális állapotról, és az a tűzfal indulása előtt töltődjön be, a service-ben erre is van lehetőség, én is hasonlóan oldottam meg.

Szerkesztve: 2020. 10. 30., p – 20:10

Elvileg firewalld-hez ez kell:

https://www.linode.com/community/questions/11143/top-tip-firewalld-and-…

 

# create blacklist

firewall-cmd --permanent --new-ipset=blacklist --type=hash:net --option=family=inet --option=hashsize=4096 --option=maxelem=200000

wget http://www.ipdeny.com/ipblocks/data/countries/all-zones.tar.gz
tar -vxzf all-zones.tar.gz

# add cn.zone to blacklist
firewall-cmd --permanent --ipset=blacklist --add-entries-from-file=./cn.zone

# add custom entry:
# firewall-cmd --permanent --ipset=blacklist --add-entry=$1
# firewall-cmd --ipset=blacklist --add-entry=$1

# redirect blacklist to drop zone
firewall-cmd --permanent --zone=drop --add-source=ipset:blacklist
firewall-cmd --reload


#!/bin/bash
#
# firewalld blacklist
#

# DIR contains the country list, files: *.zone
DIR=/root/firewalld-blacklist-country

# delete ipset
firewall-cmd --delete-ipset=blacklist --permanent

# create blacklist
firewall-cmd --permanent --new-ipset=blacklist --type=hash:net --option=family=inet --option=hashsize=4096 --option=maxelem=200000

# add *.zone to blacklist
for entry in "$DIR"/*.zone
do
  firewall-cmd --permanent --ipset=blacklist --add-entries-from-file=$entry
done


# redirect blacklist to drop zone
firewall-cmd --permanent --zone=drop --add-source=ipset:blacklist
firewall-cmd --reload

Működik a script, ha lefuttatom, akkor ipset delete és felépíti mégegyszer (elvileg). Jó ez így?

A ciklus után egy warning van:

Warning: ALREADY_ENABLED: ipset:blacklist

success

success