Iptables IPv4 i IPv6 - kilka adresów ip serwera - przyk

Konfiguracja serwerów, usług, itp.
kamil2234
Posty: 46
Rejestracja: 21 września 2011, 17:20

Iptables IPv4 i IPv6 - kilka adresów ip serwera - przykładowa konfiguracja - Help

Post autor: kamil2234 »

Cześć - szukam jakiegoś przykładowego pliku konfiguracyjnego Iptables z opisem reguł. Czy będzie ktoś tak dobry i wrzuci działający na Debianie skrypt ?

Mam dwa adresy IP serwera i w tym także w wersji ipv6. Serwer ma tylko jeden interfejs eth0 (Internet) i pętlę zwrotną.

Zależy mi na configu, który będzie blokować skanowanie portów etc. Z góry dziękuję za pomoc.
Awatar użytkownika
Menel
Member
Posty: 1117
Rejestracja: 24 sierpnia 2013, 19:58
Lokalizacja: doktor informoparalityki

Post autor: Menel »

kamil2234
Posty: 46
Rejestracja: 21 września 2011, 17:20

Post autor: kamil2234 »

Dzięki przypada się do nauki :)

Znalazłem taki config, ale niestety nie wszystkie reguły są tu przeze mnie zrozumiałe.

Kod: Zaznacz cały

#!/bin/sh
# set -e

### BEGIN INIT INFO
# Provides:          firewall
# Required-Start:    $network $syslog
# Required-Stop:     $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start iptables based firewall
# Description:       Start IPv4 and IPv6 firewall
### END INIT INFO

# Version:      @(#)firewall  1.0.1  2006-01-22  tehpeh@gmx.net  First version IPv4 only
#                             1.2.0  2011-05-28  tehpeh@gmx.net  Added IPv6 support
#

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=firewall



iptables4=/sbin/iptables
iptables6=/sbin/ip6tables
pub_if=eth0                                 # public interface, e.g. eth0
pub_ipv4=188.xxx.xxx.xxx                   # public IPv4 address
pub_ipv6=2001:xxxx:x:xxxx::                 # public IPv6 address


test -x $iptables4 || exit 0
test -x $iptables6 || exit 0

firewall_flush()
{
    # flush rules
    $iptables4 -F
    $iptables6 -F
    $iptables4 -F -t mangle
    $iptables6 -F -t mangle
    $iptables4 -X -t mangle
    $iptables6 -X -t mangle
    $iptables4 -F -t nat
    $iptables4 -X -t nat
    $iptables4 -X
    $iptables6 -X
}


firewall_default()
{
    default=$1

    # apply default rules
    $iptables4 -P INPUT   $default
    $iptables6 -P INPUT   $default
    $iptables4 -P FORWARD $default
    $iptables6 -P FORWARD $default
    $iptables4 -P OUTPUT  $default
    $iptables6 -P OUTPUT  $default
}


firewall_start()
{
    # default policy
    firewall_default DROP

    # accept everything from loopback
    $iptables4 -A INPUT  -i lo -j ACCEPT
    $iptables6 -A INPUT  -i lo -j ACCEPT
    $iptables4 -A OUTPUT -o lo -j ACCEPT
    $iptables6 -A OUTPUT -o lo -j ACCEPT

    # drop Bad Guys 
    $iptables4 -A INPUT -m recent --update --seconds 60 -j DROP
    $iptables6 -A INPUT -m recent --update --seconds 60 -j DROP

    # drop spoofed packets (i.e. packets with local source addresses coming from outside etc.), mark as Bad Guy
    $iptables4 -A INPUT  -i $pub_if -s $pub_ipv4 -m recent --set -j DROP
    $iptables6 -A INPUT  -i $pub_if -s $pub_ipv6 -m recent --set -j DROP

    # accept ICMP packets (ping et.al.)
    $iptables4 -A INPUT  -p icmp   -j ACCEPT
    $iptables6 -A INPUT  -p icmpv6 -j ACCEPT

    # internet (established and out)
    $iptables4 -A OUTPUT -o $pub_if -j ACCEPT
    $iptables6 -A OUTPUT -o $pub_if -j ACCEPT
    $iptables4 -A INPUT  -i $pub_if -m state --state ESTABLISHED,RELATED -j ACCEPT
    $iptables6 -A INPUT  -i $pub_if -m state --state ESTABLISHED,RELATED -j ACCEPT

    # allow public services
    $iptables4 -A INPUT -i $pub_if -p tcp -d $pub_ipv4 -m multiport --dports 25,80,143,443,993,10000 -j ACCEPT
    $iptables6 -A INPUT -i $pub_if -p tcp -d $pub_ipv6 -m multiport --dports 25,80,143,443,993,10000 -j ACCEPT

    # accept ssh connections (max 2/minute from the same IP address)
    $iptables4 -A INPUT -p tcp --dport 22 -m recent --update --seconds 300 --hitcount 10 --name SSH -j DROP
    $iptables6 -A INPUT -p tcp --dport 22 -m recent --update --seconds 300 --hitcount 10 --name SSH -j DROP
    $iptables4 -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --set --name SSH -j ACCEPT
    $iptables6 -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --set --name SSH -j ACCEPT

    # reject everything else in INPUT
    $iptables4 -A INPUT -j REJECT
    $iptables6 -A INPUT -j REJECT
}

firewall_stop()
{
    # flush rules
    firewall_flush

    # default policy
    firewall_default ACCEPT
}

case "$1" in
    start)
        echo -n "Starting $NAME: "
        firewall_start
        echo "OK."
        ;;
    stop)
        echo -n "Stopping $NAME: "
        firewall_stop
        echo "OK."
        ;;
    restart|reload)
        echo -n "Restarting $NAME: "
        firewall_stop
        sleep 1
        firewall_start
        echo "OK."
        ;;
    *)
        echo "Usage: /etc/init.d/$NAME {start|stop|restart|reload}" >&2
        exit 1
        ;;
esac

exit 0
Rozumiem, że ta reguła blokuje komunikacja przychodzącą - pytanie kiedy reguła zaczyna blokować i jakie porty ?:

Kod: Zaznacz cały

# drop Bad Guys 
    $iptables4 -A INPUT -m recent --update --seconds 60 -j DROP
    $iptables6 -A INPUT -m recent --update --seconds 60 -j DROP
Awatar użytkownika
pawkrol
Moderator
Posty: 939
Rejestracja: 03 kwietnia 2011, 10:25

Post autor: pawkrol »

Tu jest bardzo fajny artykuł i iptables ( Jak dla mnie jeden z lepszych). Po nim w końcu zrozumiałem "wędrówkę" pakietu przez łańcuchy. Iptables
ODPOWIEDZ