LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 05-02-2020, 07:09 PM   #16
ForestuX
LQ Newbie
 
Registered: Oct 2019
Location: /bin/bash
Distribution: slackware
Posts: 25

Rep: Reputation: Disabled
Iptables + ipset


Hello all.
Out of nftables (I don’t know about it), I think the best way to filter IPs by countries is using iptables with ipset.

Quote:
ipset is used to set up, maintain and inspect so called IP sets in the Linux kernel. Depending on the type of the set, an IP set may store IP(v4/v6) addresses, (TCP/UDP) port numbers, IP and MAC address pairs, IP address and port number pairs, etc.
Iptables matches and targets referring to sets create references, which protect the given sets in the kernel. A set cannot be destroyed while there is a single reference pointing to it.
(man ipset, I recommend to read it).

I read some about filtering sets of IPs, and I understood that filtering a lot of IPs only with iptables is not efficient, and it is better doing it by sets of them. Now I am blocking several countries in my little home server using this method, and I do it in that way. Note that the sets of IP contries can change, so it is recommended to implement it like a cron job.

I’m not a computer security specialist, neither a sysadmin. I am only a luser… But I coded this little script by myself (in fact, both, Duckduckgo and me did it) and it works in my server with GNU/Linux Slackware!!. So, I’m proud to shate it with you. For another distros you must, perhaps modify it. Im sure it is improvable, I accept suggestions.


Code:
#!/bin/bash

# Script para filtrado de direcciones de IP por paises
# mediante creación de bloques de ip del tipo hash:net con ipset
# y la creación de reglas con iptables.

# Requiere permisos de administrador

# En GNU/Linux Slackware copiarlo en /etc/cron.daily con permisos de ejecución (
chmod +x)
# Para otras distros revisar configuración de cron.

# URL fuente de bloques de direccciones IP: http://www.ipdeny.com
# Listado completo en http://www.ipdeny.com/ipblocks/data/countries
# Paises filtados:
#	Afganistán (af)
#	China (cn)
#	

for Pais in af cn;
do
    # Si el bloque de direcciones de ip existe, lo eliminamos
    ipset destroy -exist Bloque-Pais-$Pais > /dev/null 2>&1
    # Creamos nuevos bloques de direcciones del tipo hash:net
    ipset create Bloque-Pais-$Pais hash:net
    # Añadimos direcciones de IP a cada bloque
    for IP in $(wget -O - http://www.ipdeny.com/ipblocks/data/countries/$Pais.zo
ne)
    do
        ipset add Bloque-Pais-$Pais $IP
    done
done

# Creamos un fichero con todos los conjuntos de ips, por si el diablo.
mkdir -p /etc/iptables/ipset/
ipset save > /etc/iptables/ipset/bloques.ipset.save

# Escribimos en el log

Date=`date +20\%y\%m\%d_\%H\%M\%S`
echo "$Date Actualizadas direcciones de IP del filtro de paises." >> /var/log/cron

# Queda creada regla al final del fichero /etc/iptables/iptables.sh 
# De la siguiente manera para activar el filtro:
# Creamos una regla de filtrado para cada bloque de paises 
# creados con la tarea cron /etc/cron.daily/blacklist.paises.sh
# for Pais in af cn;
#	do
#	iptables -I INPUT -m set --match-set Bloque-Pais-$Pais src -p TCP -j DROP
#	echo "Creadas reglas DROP para el país $Pais"
# done
exit 0
I commented the script in my own language: today is too late, so I'll try to traduce it to English soon (specially the last lines, because is important to add a rule in your iptables script).

You can list the created sets of IPs by its name:

Code:
alfonso@Sherwood:~$ sudo /usr/sbin/ipset list --name
Password:
Bloque-Pais-af
Bloque-Pais-cn
ips.maliciosas  ## ← This set is created with another script
Each set of rules contains a lot of IPs. Remember I created sets of them with the type hash:net (see man ipset):

Code:
alfonso@Sherwood:~$ sudo /usr/sbin/ipset list Bloque-Pais-af | nl | tail -n 3
   114	103.144.237.0/24
   115	180.94.64.0/19
   116	103.96.233.0/24
116 lines of IP direcctions in CIDR notation: that is a lot of IPs.

Finally, if the rule is properly created in your iptables script (If you don't have any, you must), the rules can be listed easily:

Quote:
alfonso@Sherwood:~$ sudo /usr/sbin/iptables -nvL | grep set
294 16716 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 match-set ips.maliciosas src
49 2568 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 match-set Bloque-Pais-cn src
0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 match-set Bloque-Pais-af src
Happy slacking.

Last edited by ForestuX; 05-02-2020 at 07:25 PM.
 
Old 05-10-2020, 09:22 PM   #17
LinusGates
LQ Newbie
 
Registered: Apr 2020
Posts: 10

Rep: Reputation: Disabled
Quote:
Hi all, am I allowed to piggyback of this hread instead of creating a new one?

I have a similar query...

I want to block all incoming SYN packets and I want to know if my command below is valid please:

Code:
# iptables -A INPUT -p tcp --syn -j DROP
If I'm not allowed to piggy back off this thread then I'll create a new one and my sincerest apologies.

Thank you
Mods please delete.

Last edited by LinusGates; 05-11-2020 at 01:18 AM. Reason: edit
 
Old 05-11-2020, 12:46 AM   #18
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
Best to create a new thread.
 
Old 05-11-2020, 01:14 AM   #19
LinusGates
LQ Newbie
 
Registered: Apr 2020
Posts: 10

Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
Best to create a new thread.
Cheers will do.
 
  


Reply

Tags
firewall, ubuntu, ufw



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
iptables blocking all ips except US & US Amazon. Can't log dropped IPs. mcginlej Linux - Networking 3 10-08-2013 12:18 PM
Allow internal ips block external ips Jz87 Linux - Security 10 07-19-2010 09:42 PM
Apache BLOCK Country + Show Index for the Specific Country > How? skate Linux - Software 1 10-12-2009 07:08 AM
iptables: how to block IPs by country or registry mlnutt Linux - Security 4 09-14-2007 12:22 PM
Firefox usage share, country for country! EliasAlucard Linux - Software 6 05-09-2006 05:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 02:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration