GLADIATOR13, за основу бери:
#!/bin/bash
# Тут в принципе может и не надо этого всего но не помеха
# вдруг какой модуль не подгружен или форвардинг не включен
echo "1" > /proc/sys/net/ipv4/ip_forward
echo "1" > /proc/sys/net/ipv4/ip_dynaddr
modprobe iptable_nat
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
# Объявление переменных
export IPT="iptables"
# Интерфейс который смотрит в интернет
export WAN=ppp0
# Локальная сеть
export LAN=eth1
export LAN_IP_RANGE=192.168.0.0/24
# Очистка всех цепочек iptables
$IPT -F
$IPT -F -t nat
$IPT -F -t mangle
$IPT -X
$IPT -t nat -X
$IPT -t mangle -X
# Закрываем изначально ВСЁ (т.е. изначально все что не разрешено - запрещено):
$IPT -P INPUT DROP
$IPT -P OUTPUT DROP
$IPT -P FORWARD DROP
# разрешаем локальный траффик для loopback и внутренней сети
$IPT -A INPUT -i lo -j ACCEPT
$IPT -A INPUT -i $LAN -j ACCEPT
$IPT -A OUTPUT -o lo -j ACCEPT
$IPT -A OUTPUT -o $LAN -j ACCEPT
# Состояние ESTABLISHED говорит о том, что это не первый пакет в соединении.
# Пропускать все уже инициированные соединения, а также дочерние от них
$IPT -A INPUT -p all -m state --state ESTABLISHED,RELATED -j ACCEPT
# Пропускать новые, а так же уже инициированные и их дочерние соединения
$IPT -A OUTPUT -p all -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
# Разрешить форвардинг для новых, а так же уже инициированных
# и их дочерних соединений
$IPT -A FORWARD -p all -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
# Включаем фрагментацию пакетов. Необходимо из за разных значений MTU
$IPT -I FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
# Отбрасывать все пакеты, которые не могут быть идентифицированы
# и поэтому не могут иметь определенного статуса.
$IPT -A INPUT -m state --state INVALID -j DROP
$IPT -A FORWARD -m state --state INVALID -j DROP
# Приводит к связыванию системных ресурсов, так что реальный
# обмен данными становится не возможным.
$IPT -A INPUT -p tcp ! --syn -m state --state NEW -j DROP
$IPT -A OUTPUT -p tcp ! --syn -m state --state NEW -j DROP
# Разрешаем доступ из внутренней сети наружу
$IPT -A FORWARD -i $LAN -o $WAN -j ACCEPT
# Запрещаем доступ снаружи во внутреннюю сеть
$IPT -A FORWARD -i $WAN -o $LAN -j REJECT
# Маскарадинг
$IPT -t nat -A POSTROUTING -o $WAN -s $LAN_IP_RANGE -j MASQUERADE
# Далее дано как пример открытие портов извне:
# **********************************************************************
# Открываем порт для ssh
$IPT -A INPUT -i $WAN -p tcp --dport 22 -j ACCEPT
# Открытие портов для торрентов (такие же указать в torrent-клиенте)
$IPT -A INPUT -i $WAN -p tcp -m multiport --ports 49152:65535 -j ACCEPT
# Открытие 443 порта
$IPT -A INPUT -p tcp --dport 443 -j ACCEPT
# Открываем 80 порт для веб сайтов
$IPT -A INPUT -i $WAN -p tcp --dport 80 -j ACCEPT
$IPT -A INPUT -i $WAN -p udp --dport 80 -j ACCEPT
# Открытие портов для игровых серверов
$IPT -A INPUT -i $WAN -p tcp --dport 27010:27030 -j ACCEPT
$IPT -A INPUT -i $WAN -p udp --dport 27010:27030 -j ACCEPT
# Открытие порта для голосового сервера Team Speak
$IPT -A INPUT -i $WAN -p tcp --dport 9987 -j ACCEPT
$IPT -A INPUT -i $WAN -p udp --dport 9987 -j ACCEPT
# **********************************************************************
# Вывод информации о состоянии таблиц.
route -n
$IPT -L
$IPT -L -v -n
$IPT -L -v -n -t nat
Вот ещё примерчик интересный:
#!/sbin/runscript
IPTABLES=/sbin/iptables
IPTABLESSAVE=/sbin/iptables-save
IPTABLESRESTORE=/sbin/iptables-restore
FIREWALL=/etc/firewall.rules
DNS1=195.5.39.5
DNS2=195.5.39.9
#inside
IIP=10.19.2.10
IINTERFACE=eth0
LOCAL_NETWORK=10.19.2.0/24
#outside
OIP=172.16.0.9
OINTERFACE=eth1
opts="$ showstatus panic save restore showoptions rules"
depend() {
need net procparam
}
rules() {
stop
ebegin "Setting internal rules"
einfo "Setting default rule to drop"
$IPTABLES -P FORWARD DROP
$IPTABLES -P INPUT DROP
$IPTABLES -P OUTPUT DROP
#default rule
einfo "Creating states chain"
$IPTABLES -N allowed-connection
$IPTABLES -F allowed-connection
$IPTABLES -A allowed-connection -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A allowed-connection -i $IINTERFACE -m limit -j LOG --log-prefix "Bad packet from $:"
$IPTABLES -A allowed-connection -j DROP
#ICMP traffic
einfo "Creating icmp chain"
$IPTABLES -N icmp_allowed
$IPTABLES -F icmp_allowed
# $IPTABLES -A icmp_allowed -m state --state NEW -p icmp --icmp-type time-exceeded -j ACCEPT
# $IPTABLES -A icmp_allowed -m state --state NEW -p icmp --icmp-type destination-unreachable -j ACCEPT
$IPTABLES -A icmp_allowed -p icmp --icmp-type 4 -j ACCEPT
$IPTABLES -A icmp_allowed -p icmp --icmp-type 12 -j ACCEPT
$IPTABLES -A icmp_allowed -p icmp --icmp-type 3 -j ACCEPT
$IPTABLES -A icmp_allowed -p icmp --icmp-type 11 -j ACCEPT
$IPTABLES -A icmp_allowed -p icmp --icmp-type 8 -j ACCEPT
$IPTABLES -A icmp_allowed -p icmp --icmp-type 0 -j ACCEPT
$IPTABLES -A icmp_allowed -p icmp -j LOG --log-prefix "Bad ICMP traffic:"
$IPTABLES -A icmp_allowed -p icmp -j DROP
#Incoming traffic
einfo "Creating incoming ssh traffic chain"
$IPTABLES -N allow-ssh-traffic-in
$IPTABLES -F allow-ssh-traffic-in
#Flood protection
$IPTABLES -A allow-ssh-traffic-in -m limit --limit 1/second -p tcp --tcp-flags ALL RST --dport ssh -j ACCEPT
$IPTABLES -A allow-ssh-traffic-in -m limit --limit 1/second -p tcp --tcp-flags ALL FIN --dport ssh -j ACCEPT
$IPTABLES -A allow-ssh-traffic-in -m limit --limit 1/second -p tcp --tcp-flags ALL SYN --dport ssh -j ACCEPT
$IPTABLES -A allow-ssh-traffic-in -m state --state RELATED,ESTABLISHED -p tcp --dport ssh -j ACCEPT
einfo "Creating incoming http traffic chain"
$IPTABLES -N allow-www-traffic-in
$IPTABLES -F allow-www-traffic-in
#Flood protection
$IPTABLES -A allow-www-traffic-in -m limit --limit 1/second -p tcp --tcp-flags ALL RST --dport www -j ACCEPT
$IPTABLES -A allow-www-traffic-in -m limit --limit 1/second -p tcp --tcp-flags ALL FIN --dport www -j ACCEPT
$IPTABLES -A allow-www-traffic-in -m limit --limit 1/second -p tcp --tcp-flags ALL SYN --dport www -j ACCEPT
$IPTABLES -A allow-www-traffic-in -m state --state RELATED,ESTABLISHED -p tcp --dport www -j ACCEPT
einfo "Creating incoming ftp traffic chain"
$IPTABLES -N allow-ftp-traffic-in
$IPTABLES -F allow-ftp-traffic-in
#Flood protection
$IPTABLES -A allow-ftp-traffic-in -m limit --limit 5/second -p tcp --tcp-flags ALL RST -s 10.19.2.0/24 --sport ftp -j ACCEPT
$IPTABLES -A allow-ftp-traffic-in -m limit --limit 5/second -p tcp --tcp-flags ALL FIN -s 10.19.2.0/24 --sport ftp -j ACCEPT
$IPTABLES -A allow-ftp-traffic-in -m limit --limit 5/second -p tcp --tcp-flags ALL SYN -s 10.19.2.0/24 --sport ftp -j ACCEPT
$IPTABLES -A allow-ftp-traffic-in -m state --state RELATED,ESTABLISHED -p tcp -s 10.19.2.0/24 --sport ftp -j ACCEPT
$IPTABLES -A allow-ftp-traffic-in -m limit --limit 5/second -p tcp --tcp-flags ALL RST -s 10.19.2.0/24 --sport 1024:65365 -j ACCEPT
$IPTABLES -A allow-ftp-traffic-in -m limit --limit 5/second -p tcp --tcp-flags ALL FIN -s 10.19.2.0/24 --sport 1024:65365 -j ACCEPT
$IPTABLES -A allow-ftp-traffic-in -m limit --limit 5/second -p tcp --tcp-flags ALL SYN -s 10.19.2.0/24 --sport 1024:65365 -j ACCEPT
$IPTABLES -A allow-ftp-traffic-in -m state --state RELATED,ESTABLISHED -p tcp -s 10.19.2.0/24 --sport 1024:65365 -j ACCEPT
$IPTABLES -A allow-ftp-traffic-in -m limit --limit 5/second -p tcp --tcp-flags ALL RST -s 172.16.0.0/24 --sport ftp -j ACCEPT
$IPTABLES -A allow-ftp-traffic-in -m limit --limit 5/second -p tcp --tcp-flags ALL FIN -s 172.16.0.0/24 --sport ftp -j ACCEPT
$IPTABLES -A allow-ftp-traffic-in -m limit --limit 5/second -p tcp --tcp-flags ALL SYN -s 172.16.0.0/24 --sport ftp -j ACCEPT
$IPTABLES -A allow-ftp-traffic-in -m state --state RELATED,ESTABLISHED -p tcp -s 172.16.0.0/24 --sport ftp -j ACCEPT
$IPTABLES -A allow-ftp-traffic-in -m limit --limit 5/second -p tcp --tcp-flags ALL RST -s 172.16.0.0/24 --sport 1024:65365 -j ACCEPT
$IPTABLES -A allow-ftp-traffic-in -m limit --limit 5/second -p tcp --tcp-flags ALL FIN -s 172.16.0.0/24 --sport 1024:65365 -j ACCEPT
$IPTABLES -A allow-ftp-traffic-in -m limit --limit 5/second -p tcp --tcp-flags ALL SYN -s 172.16.0.0/24 --sport 1024:65365 -j ACCEPT
$IPTABLES -A allow-ftp-traffic-in -m state --state RELATED,ESTABLISHED -p tcp -s 172.16.0.0/24 --sport 1024:65365 -j ACCEPT
#Music
$IPTABLES -A allow-ftp-traffic-in -m limit --limit 1/second -p tcp --tcp-flags ALL RST --dport 8020 -j ACCEPT
$IPTABLES -A allow-ftp-traffic-in -m limit --limit 1/second -p tcp --tcp-flags ALL FIN --dport 8020 -j ACCEPT
$IPTABLES -A allow-ftp-traffic-in -m limit --limit 1/second -p tcp --tcp-flags ALL SYN --dport 8020 -j ACCEPT
$IPTABLES -A allow-ftp-traffic-in -m state --state RELATED,ESTABLISHED -p tcp -m tcp --dport 8020 -j ACCEPT
einfo "Creating incoming smb traffic chain"
$IPTABLES -N allow-smb-traffic-in
$IPTABLES -F allow-smb-traffic-in
$IPTABLES -A allow-smb-traffic-in -p tcp --tcp-flags ALL SYN -s 10.19.20.0/24 --dport netbios-ssn -j ACCEPT
$IPTABLES -A allow-smb-traffic-in -m state --state RELATED,ESTABLISHED -p tcp -s 10.19.20.0/24 --dport netbios-ssn -j ACCEPT
$IPTABLES -A allow-smb-traffic-in -p udp -s 10.19.20.0/24 --dport netbios-ns -j ACCEPT
$IPTABLES -A allow-smb-traffic-in -p udp -s 10.19.20.0/24 --dport netbios-dgm -j ACCEPT
$IPTABLES -A allow-smb-traffic-in -p udp -s 10.19.20.0/24 --dport 445 -j ACCEPT
einfo "Creating incoming ntp traffic chain"
$IPTABLES -N allow-time-traffic-in
$IPTABLES -F allow-time-traffic-in
$IPTABLES -A allow-time-traffic-in -p udp --dport ntp -j ACCEPT
#Outgoing traffic
einfo "Creating outgoing ssh traffic chain"
$IPTABLES -N allow-ssh-traffic-out
$IPTABLES -F allow-ssh-traffic-out
$IPTABLES -A allow-ssh-traffic-out -p tcp --dport ssh -j ACCEPT
einfo "Creating outgoing dns traffic chain"
$IPTABLES -N allow-dns-traffic-out
$IPTABLES -F allow-dns-traffic-out
$IPTABLES -A allow-dns-traffic-out -p udp -d $DNS1 --dport domain -j ACCEPT
$IPTABLES -A allow-dns-traffic-out -p udp -d $DNS2 --dport domain -j ACCEPT
einfo "Creating outgoing http/https traffic chain"
$IPTABLES -N allow-www-traffic-out
$IPTABLES -F allow-www-traffic-out
$IPTABLES -A allow-www-traffic-out -p tcp --dport www -j ACCEPT
$IPTABLES -A allow-www-traffic-out -p tcp --dport https -j ACCEPT
einfo "Creating outgoing mail traffic chain"
$IPTABLES -N allow-mail-traffic-out
$IPTABLES -F allow-mail-traffic-out
$IPTABLES -A allow-mail-traffic-out -p tcp --dport smtp -j ACCEPT
$IPTABLES -A allow-mail-traffic-out -p tcp --dport pop3 -j ACCEPT
$IPTABLES -A allow-mail-traffic-out -p tcp --dport imap2 -j ACCEPT
einfo "Creating outgoing ftp traffic chain"
$IPTABLES -N allow-ftp-traffic-out
$IPTABLES -F allow-ftp-traffic-out
$IPTABLES -A allow-ftp-traffic-out -p tcp --dport 1024:65365 -j ACCEPT
$IPTABLES -A allow-ftp-traffic-out -p tcp --dport ftp -j ACCEPT
$IPTABLES -A allow-ftp-traffic-out -p tcp --dport ftp-data -j ACCEPT
einfo "Creating outgoing smb traffic chain"
$IPTABLES -N allow-smb-traffic-out
$IPTABLES -F allow-smb-traffic-out
$IPTABLES -A allow-smb-traffic-out -p tcp --dport netbios-ssn -j ACCEPT
$IPTABLES -A allow-smb-traffic-out -p udp --dport netbios-ns -j ACCEPT
$IPTABLES -A allow-smb-traffic-out -p udp --dport netbios-dgm -j ACCEPT
$IPTABLES -A allow-smb-traffic-out -p tcp --dport 445 -j ACCEPT
einfo "Creating outgoing krab/terminal traffic chain"
$IPTABLES -N allow-krab-term-traffic-out
$IPTABLES -F allow-krab-term-traffic-out
$IPTABLES -A allow-krab-term-traffic-out -p tcp --dport 3050 -j ACCEPT
$IPTABLES -A allow-krab-term-traffic-out -p tcp --dport 3389 -j ACCEPT
einfo "Creating outgoing ntp traffic chain"
$IPTABLES -N allow-time-traffic-out
$IPTABLES -F allow-time-traffic-out
$IPTABLES -A allow-time-traffic-out -p udp --dport ntp -j ACCEPT
$IPTABLES -A allow-time-traffic-out -p udp --dport ntp -j ACCEPT
#Catch portscanners
einfo "Creating portscan detection chain"
$IPTABLES -N check-flags
$IPTABLES -F check-flags
$IPTABLES -A check-flags -p tcp --tcp-flags ALL FIN,URG,PSH -m limit --limit 5/minute -j LOG --log-level alert --log-prefix "NMAP-XMAS:"
$IPTABLES -A check-flags -p tcp --tcp-flags ALL FIN,URG,PSH -j DROP
$IPTABLES -A check-flags -p tcp --tcp-flags ALL ALL -m limit --limit 5/minute -j LOG --log-level 1 --log-prefix "XMAS:"
$IPTABLES -A check-flags -p tcp --tcp-flags ALL ALL -j DROP
$IPTABLES -A check-flags -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -m limit --limit 5/minute -j LOG --log-level 1 --log-prefix "XMAS-PSH:"
$IPTABLES -A check-flags -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP
$IPTABLES -A check-flags -p tcp --tcp-flags ALL NONE -m limit --limit 5/minute -j LOG --log-level 1 --log-prefix "NULL_SCAN:"
$IPTABLES -A check-flags -p tcp --tcp-flags ALL NONE -j DROP
$IPTABLES -A check-flags -p tcp --tcp-flags SYN,RST SYN,RST -m limit --limit 5/minute -j LOG --log-level 5 --log-prefix "SYN/RST:"
$IPTABLES -A check-flags -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
$IPTABLES -A check-flags -p tcp --tcp-flags SYN,FIN SYN,FIN -m limit --limit 5/minute -j LOG --log-level 5 --log-prefix "SYN/FIN:"
$IPTABLES -A check-flags -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
# Apply and add invalid states to the chains
einfo "Applying chains to INPUT"
$IPTABLES -A INPUT -m state --state INVALID -j DROP
$IPTABLES -A INPUT -j icmp_allowed
$IPTABLES -A INPUT -j check-flags
$IPTABLES -A INPUT -i lo -j ACCEPT
$IPTABLES -A INPUT -j allow-ssh-traffic-in
$IPTABLES -A INPUT -j allow-www-traffic-in
$IPTABLES -A INPUT -j allow-ftp-traffic-in
$IPTABLES -A INPUT -j allow-smb-traffic-in
$IPTABLES -A INPUT -j allow-time-traffic-in
$IPTABLES -A INPUT -j allowed-connection
einfo "Applying chains to FORWARD"
$IPTABLES -A FORWARD -m state --state INVALID -j DROP
$IPTABLES -A FORWARD -j icmp_allowed
$IPTABLES -A FORWARD -j check-flags
$IPTABLES -A FORWARD -o lo -j ACCEPT
# $IPTABLES -A FORWARD -j allow-ssh-traffic-in
# $IPTABLES -A FORWARD -j allow-time-traffic-in
# $IPTABLES -A FORWARD -j allow-ftp-traffic-in
# $IPTABLES -A FORWARD -j allow-www-traffic-in
# $IPTABLES -A FORWARD -j allow-www-traffic-out
$IPTABLES -A FORWARD -j allowed-connection
einfo "Applying chains to OUTPUT"
$IPTABLES -A OUTPUT -m state --state INVALID -j DROP
$IPTABLES -A OUTPUT -j icmp_allowed
$IPTABLES -A OUTPUT -j check-flags
$IPTABLES -A OUTPUT -o lo -j ACCEPT
$IPTABLES -A OUTPUT -j allow-ssh-traffic-out
$IPTABLES -A OUTPUT -j allow-dns-traffic-out
$IPTABLES -A OUTPUT -j allow-www-traffic-out
$IPTABLES -A OUTPUT -j allow-mail-traffic-out
$IPTABLES -A OUTPUT -j allow-ftp-traffic-out
$IPTABLES -A OUTPUT -j allow-smb-traffic-out
$IPTABLES -A OUTPUT -j allow-krab-term-traffic-out
$IPTABLES -A OUTPUT -j allow-time-traffic-out
$IPTABLES -A OUTPUT -j allowed-connection
#Allow client to route through via NAT (Network Address Translation)
$IPTABLES -t nat -A POSTROUTING -o $IINTERFACE -j MASQUERADE
eend $?
}
start() {
ebegin "Starting firewall"
if [ -e "$" ]; then
restore
else
einfo "$ does not exists. Using default rules."
rules
fi
eend $?
}
stop() {
ebegin "Stopping firewall"
$IPTABLES -F
$IPTABLES -t nat -F
$IPTABLES -X
$IPTABLES -P FORWARD ACCEPT
$IPTABLES -P INPUT ACCEPT
$IPTABLES -P OUTPUT ACCEPT
eend $?
}
showstatus() {
ebegin "Status"
$IPTABLES -L -n -v --line-numbers
einfo "NAT status"
$IPTABLES -L -n -v --line-numbers -t nat
eend $?
}
panic() {
ebegin "Setting panic rules"
$IPTABLES -F
$IPTABLES -X
$IPTABLES -t nat -F
$IPTABLES -P FORWARD DROP
$IPTABLES -P INPUT DROP
$IPTABLES -P OUTPUT DROP
$IPTABLES -A INPUT -i lo -j ACCEPT
$IPTABLES -A OUTPUT -o lo -j ACCEPT
eend $?
}
save() {
ebegin "Saving Firewall rules"
$IPTABLESSAVE > $FIREWALL
eend $?
}
restore() {
ebegin "Restoring Firewall rules"
$IPTABLESRESTORE < $FIREWALL
eend $?
}
restart() {
svc_stop; svc_start
}
showoptions() {
echo "Usage: $0 "
echo "start) will restore setting if exists else force rules"
echo "stop) delete all rules and set all to accept"
echo "rules) force settings of new rules"
echo "save) will store settings in $"
echo "restore) will restore settings from $"
echo "showstatus) Shows the status"
}
История показывает, что во всем новом обычно кроется какой-то подвох.
Классическая ошибка, которую совершают проектировщики
абсолютно надежных систем, - недооценка изобретательности клинических идиотов.