Подбор правила Iptables

Существует цепочка BAN с этим правилом:

-A BAN -m recent --name ping --rcheck --seconds 5 --hitcount 3 --rsource  -j RETURN

это будет соответствовать, когда будет 2 ping через 5 секунд, но если я добавляю, удаляют адреса из списка ping, который не соответствовал этому верхнему правилу, никогда не будет соответствовать почему?

-A BAN -m recent --name ping --remove
0
задан 4 February 2018 в 08:12

1 ответ

Так как Ваше первое правило не инициировало до третьего раза оно было пересечено, и Ваше второе правило удаляет запись в таблице полностью, первое правило будет только когда-либо наблюдать то, что оно думает, первый пакет от того IP-адреса.

Не используйте "удалить" правило, и таблицей "ping" будут управлять очень хорошо.

Править: Вот один способ использовать iptables и недавний модуль, чтобы иметь другое время запрета, чем время для становления запрещенным. Я использовал 5 ping за 10 секунд как критерии запрета, и только 120 секунд запрещают время (только, чтобы помочь проверить).

#!/bin/sh
FWVER=0.01
#
# ping_then_block Smythies 2018.02.05 Ver:0.01
#       An iptables recent module example of how to make the
#       ban time differ from the time to become banned.
#
#       See here:
#       https://askubuntu.com/questions/1002958/iptables-rule-matching
#
#       run as sudo
#

echo "Loading ping_then_block $FWVER..\n"

# The location of the iptables program
#
IPTABLES=/sbin/iptables

#Setting the EXTERNAL and INTERNAL interfaces and addresses for the network
#
# Smythies (for testing)
EXTIF="enp9s0"
EXTIP="192.168.111.104"
NETWORK="192.168.111.0/24"

UNIVERSE="0.0.0.0/0"

#Clearing any previous configuration
#
echo "  Clearing any existing rules and setting default policies.."
$IPTABLES -P INPUT ACCEPT
$IPTABLES -F INPUT
$IPTABLES -P OUTPUT ACCEPT
$IPTABLES -F OUTPUT
$IPTABLES -P FORWARD ACCEPT
$IPTABLES -F FORWARD
# Otherwise, I can not seem to delete it later on
$IPTABLES -F ping-check
$IPTABLES -F ping-ban
# Delete user defined chains
$IPTABLES -X
# Reset all IPTABLES counters
$IPTABLES -Z

#######################################################################
# USER DEFINED CHAIN SUBROUTINES:
#
# ping-ban
#
# An ICMP echo request packet source IP address needs to added to
# the bad guy list
#
# Custom tables must exist before being referenced, hence the order
# of these sub-routines.
#
$IPTABLES -N ping-ban

$IPTABLES -A ping-ban -m recent --update --hitcount 1 --seconds 120 --name PING_BAN -j DROP
$IPTABLES -A ping-ban -m recent --set --name PING_BAN
$IPTABLES -A ping-ban -j DROP

#######################################################################
# USER DEFINED CHAIN SUBROUTINES:
#
# ping-check
#
# An ICMP echo request packet has arrived and the source IP
# address is either not on the bad guy list, or is but the penalty
# period criteria has been met.
#
# Check if the IP needs to be added to the bad guy list, and
# drop it if it does.
#
# Custom tables must exist before being referenced, hence the order
# of these sub-routines.
#
$IPTABLES -N ping-check

$IPTABLES -A ping-check -m recent --update --hitcount 5 --seconds 10 --name PING_TABLE -j ping-ban
$IPTABLES -A ping-check -m recent --set --name PING_TABLE
$IPTABLES -A ping-check -j ACCEPT

#
# If you are on the bad guy list, then you are banned.
#
$IPTABLES -A INPUT -i $EXTIF -m recent --update --seconds 120 --name PING_BAN -j LOG --log-prefix "BANPING:" --log-level info
$IPTABLES -A INPUT -i $EXTIF -m recent --update --seconds 120 --name PING_BAN -j DROP

#
# All ICMP? Or just ECHO requests?
#
$IPTABLES -A INPUT -i $EXTIF -p ICMP --icmp-type echo-request -s $UNIVERSE -d $EXTIP -j ping-check

$IPTABLES -A INPUT -i $EXTIF -s $UNIVERSE -m state --state ESTABLISHED,RELATED -j ACCEPT

echo ping_then_block rule set version $FWVER done.
2
ответ дан 31 October 2019 в 02:01

Другие вопросы по тегам:

Похожие вопросы: