minifirewall/minifirewall

381 lines
9.2 KiB
Bash
Executable File

#!/bin/sh
# minifirewall is shellscripts for easy firewalling on a standalone server
# See http://git.evolix.org/?p=evolinux/minifirewall.git;a=summary
# Copyright (c) 2007-2011 Evolix
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License.
# Script netfilter/iptables
# http://netfilter.org/
#
# Designed for Linux kernel 2.6
# http://www.kernel.org/
# Description
# script for local server
# Start or stop a mini-firewall
#
### BEGIN INIT INFO
# Provides: minfirewall
# Required-Start:
# Required-Stop:
# Should-Start: $network $syslog $named
# Should-Stop: $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop the firewall
# Description: Firewall designed by evolix.fr
### END INIT INFO
DESC="minifirewall"
NAME="minifirewall"
###
# Configuration des variables
###
# chemin iptables
IPT=/sbin/iptables
IPT6=/sbin/ip6tables
# variables TCP/IP
LOOPBACK='127.0.0.0/8'
CLASSA='10.0.0.0/8'
CLASSB='172.16.0.0/12'
CLASSC='192.168.0.0/16'
CLASSD='224.0.0.0/4'
CLASSE='240.0.0.0/5'
ALL='0.0.0.0'
BROAD='255.255.255.255'
PORTSROOT='0:1023'
PORTSUSER='1024:65535'
case "$1" in
start)
echo "Demarrage regles IPTables..."
# Stop and warn if error!
set -e
trap 'echo "ERROR in minifirewall configuration (fix it now!) or script manipulation (fix yourself)." ' INT TERM EXIT
# 1.Protections diverses
# ne pas repondre aux ping broadcast
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
# Ignorer les mauvais messages d'erreurs ICMP
echo 1 > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
# effacer la source des paquets routes
for i in /proc/sys/net/ipv4/conf/*/accept_source_route; do
echo 0 > $i
done
# activer les TCP SYN cookies evitant des attaques DoS de type TCP-SYN-FLOOD
# cf http://cr.yp.to/syncookies.html
echo 1 > /proc/sys/net/ipv4/tcp_syncookies
# desactiver les messages ICMP d'information de redirection
# potentiellement dangereux
for i in /proc/sys/net/ipv4/conf/*/accept_redirects; do
echo 0 > $i
done
for i in /proc/sys/net/ipv4/conf/*/send_redirects; do
echo 0 > $i
done
# filtrage par chemin inverse: verifie que les reponses sortent bien de l'interface d'arrivee
for i in /proc/sys/net/ipv4/conf/*/rp_filter; do
echo 1 > $i
done
# log des paquets avec adresse incoherente
for i in /proc/sys/net/ipv4/conf/*/log_martians; do
echo 1 > $i
done
# 2. Sur la machine
$IPT -N LOG_DROP
$IPT -A LOG_DROP -j LOG --log-prefix '[IPTABLES DROP] : '
$IPT -A LOG_DROP -j DROP
$IPT -N LOG_ACCEPT
$IPT -A LOG_ACCEPT -j LOG --log-prefix '[IPTABLES ACCEPT] : '
$IPT -A LOG_ACCEPT -j ACCEPT
# Configuration
configfile="/etc/firewall.rc"
if ! test -f $configfile; then
echo "$configfile does not exist" >&2
exit 1
fi
tmpfile=`mktemp`
. $configfile 2>$tmpfile >&2
if [ -s $tmpfile ]; then
echo "$configfile returns standard or error output (see below). Stopping."
cat $tmpfile
exit 1
fi
rm $tmpfile
# trusted ip addresses
$IPT -N ONLYTRUSTED
$IPT -A ONLYTRUSTED -j LOG_DROP
for x in $TRUSTEDIPS
do
$IPT -I ONLYTRUSTED -s $x -j ACCEPT
done
# privilegied ip addresses
# (trusted ip addresses *are* privilegied)
$IPT -N ONLYPRIVILEGIED
$IPT -A ONLYPRIVILEGIED -j ONLYTRUSTED
for x in $PRIVILEGIEDIPS
do
$IPT -I ONLYPRIVILEGIED -s $x -j ACCEPT
done
# chain for restrictions (blacklist ips/ranges)
$IPT -N NEEDRESTRICT
# On autorise tout sur l'interface loopback
$IPT -A INPUT -i lo -j ACCEPT
[ $IPV6 != 'off' ] && $IPT6 -A INPUT -i lo -j ACCEPT
# if OUTPUTDROP
$IPT -A OUTPUT -o lo -j ACCEPT
# on evite pas mal de paquets "martiens" effet de bord de virus
# notamment W32/Blaster qui attaquait windowsupdate.com
# et dont l'enregistrement DNS avait ete change pour 127.0.0.1
# $IPT -t NAT -I PREROUTING -s $LOOPBACK -i ! lo -j DROP
$IPT -A INPUT -s $LOOPBACK ! -i lo -j DROP
#################################################################
# Les services accessibles
#################################################################
# Les services accessibles en local ?
#$IPT -A INPUT -i $INT2 -j ACCEPT
$IPT -A INPUT -s $INTLAN -j ACCEPT
# On passe tout d'abord par la chaine de protection pour certains services
for x in $SERVICESTCP1p
do
$IPT -A INPUT -p tcp --dport $x -j NEEDRESTRICT
done
for x in $SERVICESUDP1p
do
$IPT -A INPUT -p udp --dport $x -j NEEDRESTRICT
done
# Services publics
for x in $SERVICESTCP1
do
$IPT -A INPUT -p tcp --dport $x -j ACCEPT
[ $IPV6 != 'off' ] && $IPT6 -A INPUT -p tcp --dport $x -j ACCEPT
done
for x in $SERVICESUDP1
do
$IPT -A INPUT -p udp --dport $x -j ACCEPT
[ $IPV6 != 'off' ] && $IPT6 -A INPUT -p udp --dport $x -j ACCEPT
done
# Services semi-publics
for x in $SERVICESTCP2
do
$IPT -A INPUT -p tcp --dport $x -j ONLYPRIVILEGIED
done
for x in $SERVICESUDP2
do
$IPT -A INPUT -p udp --dport $x -j ONLYPRIVILEGIED
done
# Services prives
for x in $SERVICESTCP3
do
$IPT -A INPUT -p tcp --dport $x -j ONLYTRUSTED
done
for x in $SERVICESUDP3
do
$IPT -A INPUT -p udp --dport $x -j ONLYTRUSTED
done
#################################################################
# Les services auxquels la machine peut acceder
#################################################################
# DNS
# autoriser a recevoir des reponses DNS
for x in $DNSSERVEURS
do
$IPT -A INPUT -p tcp ! --syn --sport 53 --dport $PORTSUSER -s $x -j ACCEPT
$IPT -A INPUT -p udp --sport 53 --dport $PORTSUSER -s $x -m state --state ESTABLISHED,RELATED -j ACCEPT
done
# HTTP
# autoriser a se connecter a certaines IP en http (miroirs debian par exemple)
for x in $HTTPSITES
do
$IPT -A INPUT -p tcp ! --syn --sport 80 --dport $PORTSUSER -s $x -j ACCEPT
done
# HTTPS
for x in $HTTPSSITES
do
$IPT -A INPUT -p tcp ! --syn --sport 443 --dport $PORTSUSER -s $x -j ACCEPT
done
# FTP
# autoriser a se connecter a certaines IP en ftp (miroirs debian par exemple)
for x in $FTPSITES
do
# requetes exterieures sur le canal de controle
$IPT -A INPUT -p tcp ! --syn --sport 21 --dport $PORTSUSER -s $x -j ACCEPT
# FTP port-mode sur le canal de donnees
$IPT -A INPUT -p tcp --sport 20 --dport $PORTSUSER -s $x -j ACCEPT
# FTP passive-mode sur le canal de donnees
# ATTENTION, cela active aussi les connexions sur tous les ports TCP > 1024 pour cette machine
$IPT -A INPUT -p tcp ! --syn --sport $PORTSUSER --dport $PORTSUSER -s $x -j ACCEPT
done
# autoriser a se connecter sur certaines IP par SSH
for x in $SSHOK
do
$IPT -A INPUT -p tcp ! --syn --sport 22 -s $x -j ACCEPT
done
# SMTP
for x in $SMTPOK
do
$IPT -A INPUT -p tcp ! --syn --sport 25 --dport $PORTSUSER -j ACCEPT
done
# SMTP secure
for x in $SMTPSECUREOK
do
$IPT -A INPUT -p tcp ! --syn --sport 465 --dport $PORTSUSER -j ACCEPT
$IPT -A INPUT -p tcp ! --syn --sport 587 --dport $PORTSUSER -j ACCEPT
done
# NTP
# autoriser synchronisation ntpdate
for x in $NTPOK
do
$IPT -A INPUT -p udp --sport 123 -s $x -j ACCEPT
done
# ICMP
$IPT -A INPUT -p icmp -j ACCEPT
[ $IPV6 != 'off' ] && $IPT6 -A INPUT -p icmpv6 -j ACCEPT
# politique
# par defaut rien ne rentre
$IPT -P INPUT DROP
[ $IPV6 != 'off' ] && $IPT6 -P INPUT DROP
# par defaut rien ne transite (obsolete, notamment pour les VM)
#echo 0 > /proc/sys/net/ipv4/ip_forward
#$IPT -P FORWARD DROP
#$IPT6 -P FORWARD DROP
# par defaut tout peut sortir sauf l'UDP (sinon voir OUTPUTDROP)
$IPT -P OUTPUT ACCEPT
[ $IPV6 != 'off' ] && $IPT6 -P OUTPUT ACCEPT
$IPT -A OUTPUT -p udp --match state --state ESTABLISHED,RELATED -j ACCEPT
$IPT -A OUTPUT -p udp -j DROP
[ $IPV6 != 'off' ] && $IPT6 -A OUTPUT -p udp --match state --state ESTABLISHED,RELATED -j ACCEPT
[ $IPV6 != 'off' ] && $IPT6 -A OUTPUT -p udp -j DROP
trap - INT TERM EXIT
echo "Fin du chargement des regles... "
;;
stop)
echo "On vide toutes les regles et on accepte tout..."
# On supprime toutes les regles
$IPT -F INPUT
$IPT -F OUTPUT
$IPT -F LOG_DROP
$IPT -F LOG_ACCEPT
$IPT -F ONLYTRUSTED
$IPT -F ONLYPRIVILEGIED
$IPT -F NEEDRESTRICT
$IPT -t nat -F
$IPT -t mangle -F
$IPT6 -F INPUT
$IPT6 -F OUTPUT
# On accepte tout
$IPT -P INPUT ACCEPT
$IPT -P OUTPUT ACCEPT
$IPT6 -P INPUT ACCEPT
$IPT6 -P OUTPUT ACCEPT
#$IPT -P FORWARD ACCEPT
#$IPT -t nat -P PREROUTING ACCEPT
#$IPT -t nat -P POSTROUTING ACCEPT
# On supprime les tables creees
$IPT -X LOG_DROP
$IPT -X LOG_ACCEPT
$IPT -X ONLYPRIVILEGIED
$IPT -X ONLYTRUSTED
$IPT -X NEEDRESTRICT
echo "OK"
;;
status)
$IPT -L -n -v --line-numbers
$IPT -t nat -L -n -v --line-numbers
$IPT -t mangle -L -n -v --line-numbers
$IPT6 -L -n -v --line-numbers
$IPT6 -t mangle -L -n -v --line-numbers
;;
reset)
echo "On remet les compteurs a zero..."
$IPT -Z
$IPT -t nat -Z
$IPT -t mangle -Z
$IPT6 -Z
$IPT6 -t mangle -Z
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {start|stop|restart|status|reset|squid}"
exit 1
esac
exit 0