BandaAncha.eu

  • 🔍 en 📰 artículos ⏎
  • 🔍 en 💬 foros ⏎
  • 🔍 en 👇 este 💬 foro ⏎
  • 🔍 en 👇 este 💬 tema ⏎
Regístrate Regístrate Identifícate Identifícate

Automatizar iptables

heze54

Buenas, necesito desde un fichero que contiene ips en el formato 58.147.128.0/19, automatizar la insercion para bloquearlas con iptables.

He recuperado de internet el script que adjunto abajo... la cuestion es que funciona siempre y cuando solo haya una ip, ya que si hay mas se produce este error:

./iptablesBLOCK
' specified.4.1: invalid mask `19
Try `iptables -h' or 'iptables --help' for more information

¿Que falla?

Un saludo y gracias

 #! /bin/sh -e

 if [ -f bad.txt ]

 then

 for BAD_IP in `cat bad.txt`

 do

 iptables -A INPUT -s $BAD_IP -j DROP

 done

 else

 echo "Can't read bad.txt"

 fi
BocaDePez
BocaDePez

El error no es culpa del script, sino de que la ip que le pasas no es válida para designar una red con 19 bits dedicados al identificarla.

🗨️ 16
asqwerty

Pues no. Eso es una 19. a proposito, a mi si me funciona:

# iptables -A INPUT -s 58.147.128.0/19 -j DROP

# iptables -L

...

DROP all -- 0-128.io-global.com/19 anywhere

...

¿Puede que le este escapando el "/"? ¿Habra probado hacerlo a mano?

🗨️ 14
heze54

Si, si lo metes a pelo si que funciona, pero claro... estoy hablando de un fichero que tiene cientos de ips-rangos jajaja.

Si quieres te doy un par de rangos y pruebas a automatizarlo ok?

58.147.128.0/19
117.55.192.0/20
117.104.224.0/21
119.59.80.0/21

Un saludo

🗨️ 13
asqwerty

Pues a mi el script me funciona perfectamente:

# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT icmp -- anywhere anywhere
ACCEPT all -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target prot opt source destination
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

# cat prueba.sh
#! /bin/sh -e

if [ -f bad.txt ]

then

for BAD_IP in `cat bad.txt`

do

iptables -A INPUT -s $BAD_IP -j DROP

done

else

echo "Can't read bad.txt"

fi

# ./prueba.sh

#iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT icmp -- anywhere anywhere
ACCEPT all -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
DROP all -- 0-128.io-global.com/19 anywhere

Chain FORWARD (policy ACCEPT)
target prot opt source destination
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

No entiendo porque a ti no. Mira ver los modulos

# lsmod | grep ipt
ipt_REJECT 6656 2
iptable_filter 6528 1
ip_tables 13840 1 iptable_filter
x_tables 15236 6 ipt_REJECT,ip_tables,ip6t_REJECT,xt_tcpudp,xt_state,ip6_tables

Los ip6 no te hacen falta claro

🗨️ 12
heze54
🗨️ 9
asqwerty
🗨️ 7
heze54
🗨️ 6
asqwerty
🗨️ 5
heze54
heze54
🗨️ 2
asqwerty
🗨️ 1
heze54
heze54
asqwerty
🗨️ 1
heze54