#! /bin/bash # # ---------------------------------------------------------------------------------- # Origineel script: http://www.leovee.nl/firewall.php # Minimale modificaties door Rudy Brinkman, http://www.brinkhost.nl # # Dit is een shell script voor initialisatie van iptables. # Middels dit script wordt ip-adressen in een zwarte lijst de toegang tot de server # geweigerd en illegale ssh logins worden geblokkeerd. # # INSTALLATIE # # iptinit opslaan in /bin chmod 700 # blacklist.txt in /etc # logging gebeurt in /var/log/messages # # execute script # /bin/iptinit # # Niet vergeten! Pas configfile aan van iptables, zodat het script na een herstart # of crash van het systeem iptables weer vult. # # vi /etc/sysconfig/iptables-config # zet IPTABLES_SAVE_ON_STOP en IPTABLES_SAVE_ON_RESTART beiden op "yes" # # Dit script is getest en werkend bevonden op Linux Fedora en CentOS # ---------------------------------------------------------------------------------- # Flush chains en delete non-standaard chains iptables -F iptables -X # Instellen policies standaard chains iptables -P INPUT ACCEPT iptables -P FORWARD ACCEPT iptables -P OUTPUT ACCEPT # Aanmaken eigen chains iptables -N bad_packets iptables -N blacklist # Limiteer nieuwe incoming connecties op zelfde ip-adress (ssh) tot 5 per minuut om DDoS af # te weren en block bruto force of geautomatiseerde inlogpogingen. # # Geeft problemen op sommige servers, na reboot kwam het niet goed op en zorgde voor # foutmeldingen en inlogproblemen op twee Fedora servers (niet op CentOS). # # Uncomment onderstaande regels als je de functie wel wilt gebruiken of testen.. # # iptables -I INPUT -p tcp --dport 22 -i eth0 -m state --state NEW -m recent --set # iptables -I INPUT -p tcp --dport 22 -i eth0 -m state --state NEW -m recent --update --seconds 60 --hitcount 5 -j DROP # Instellen INPUT chain iptables -A INPUT -p tcp --syn --dport 80 -j blacklist iptables -A INPUT -p tcp --syn --dport 443 -j blacklist iptables -A INPUT -p tcp --syn --dport 21 -j blacklist iptables -A INPUT -p tcp --syn --dport 22 -j blacklist iptables -A INPUT -p tcp --syn --dport 23 -j blacklist iptables -A INPUT -j bad_packets # Vullen chain bad_packets # SYN-flood aanvallen iptables -A bad_packets -p tcp --syn -m limit --limit 1/s -j ACCEPT # Portscan aanvallen iptables -A bad_packets -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j ACCEPT # Ping-flood aanvallen met behulp van het ICMP protocol iptables -A bad_packets -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT # Heel gemeen: wel een nieuwe connectie, maar geen SYN bit gezet. iptables -A bad_packets -p tcp ! --syn -m state --state NEW -j DROP # TCP Spoofing met Sequence Number voorspelling iptables -A bad_packets -p tcp --tcp-flags SYN,ACK SYN,ACK -m state --state NEW -j REJECT --reject-with tcp-reset # Vullen chain blacklist if [ -f /etc/blacklist.txt ]; then for HOST in `cat /etc/blacklist.txt` do iptables -A blacklist -s $HOST -j LOG --log-level info iptables -A blacklist -s $HOST -j DROP done fi # Laat de inhoud van iptables zien iptables -n -L