Conf IPTABLES - réseaux et sécurité - Linux et OS Alternatifs
Marsh Posté le 24-04-2008 à 16:45:41
Je connais ce fichier et il y a beaucoup de commandes qui ne fonctionnent plus. Par exemple:
Citation : # Set the default policy of the filter to deny. |
REJECT n'existe plus.
Voici mon script où je me suis inspiré de la même source que toi:
Code :
|
J'accepte tout en local, accepte uniquement les protocoles tcp et udp sur tous les ports et rejettent le reste. J'accepte également le ping.
Marsh Posté le 24-04-2008 à 16:48:19
en fait ton pc passe par ta station linux pour se connecter au net? C'est pas plutôt un proxy qu'il te faut? avec firewall bien sur.
edit: proxy: squid et squidguard
Marsh Posté le 24-04-2008 à 16:49:32
mic_12 a écrit : en fait ton pc passe par ta station linux pour se connecter au net? C'est pas plutôt un proxy qu'il te faut? avec firewall bien sur. |
Non pas forcément.
Un proxy ca a deux utilités :
- filtrage applicatif
- cache
S'il n'a besoin que de filtrage au niveau TCP/UDP, un firewall simple suffit amplement.
Marsh Posté le 24-04-2008 à 21:31:33
Ok merci beaucoup pour ses reponses je vais regarder ca de suite.
Par contre dis moi Ogaby, ton fichier prend il en compte deux reseaux distincts sur deux cartes reseaux avec d'un cote une carte pour l'interne(10.10.0.0/24) et de l'autre une adresse publique?
Sinon pour te repondre Mic_12 mon but est ici d'etre le plus invisible possible de l'exterieur d'ou l'utilisation d'un firewall et non d'un proxy
Mais je pense qu'apres je vais jouer avec un proxy aussi
Marsh Posté le 25-04-2008 à 08:41:49
overflo4 a écrit : Ok merci beaucoup pour ses reponses je vais regarder ca de suite. |
non mais tu peux le modifier pour qu'il le fasse. Il faut faire des règles pour chaque carte.
Par exemple, une ligne de la section de ce qu'il entre (input):
iptables -A INPUT -p tcp -j ACCEPT
et tu la changes en
iptables -A INPUT -i wlan0 -p tcp -j ACCEPT
Pour ce qu'il sort (output)
iptables -A OUTPUT -o wlan0 -p tcp -j ACCEPT
Pour ce qui est de "l'invisibilité", mon script apparemment suffit selon les tests en ligne. Ils disent que mes ports sont masqués ou injoignables. Je ne sais pas ce que valent ces sites car avant de faire ce script, l'outil nmap me disait que tout était OK.
> nmap -A localhost
Mais je trouve qu'avoir une politique n'acceptant que des transmissions en tcp et udp est plus importante. L'activation des protections également.
Marsh Posté le 28-04-2008 à 19:26:16
Merci Ogaby pour toutes ces infos.
Apres un petit test, maintenant mon reseau local peut sortir sur internet donc nickel.
Une seule petite quesiton demeure cependant:mon firewall lui ne peut plus sortir sur internet
Je peux pinger les adresses externes en passant par leur adresse numeriques mais pas moyens en utilsant leur noms.
J'ai verifie que mon adresse DNS ete bonne et il n'y a pas de souci avec.
D'ailleurs je ne peux pas surfer non plus en utilisant l'adresse numerique.
Je te poste ton fichier que j'ai modifie pour mon reseau au cas ou.
Merci d'avance
# Enable IP Forwarding, if it isn't already
echo 1 > /proc/sys/net/ipv4/ip_forward
# Enable broadcast echo Protection
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
# Enable bad error message Protection
echo 1 > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
# Enable IP spoofing protection
# turn on Source Address Verification
for f in /proc/sys/net/ipv4/conf/*/rp_filter; do
echo 1 > $f
done
# Disable ICMP Redirect Acceptance
for f in /proc/sys/net/ipv4/conf/*/accept_redirects; do
echo 0 > $f
done
for f in /proc/sys/net/ipv4/conf/*/send_redirects; do
echo 0 > $f
done
# Disable Source Routed Packets
for f in /proc/sys/net/ipv4/conf/*/accept_source_route; do
echo 0 > $f
done
# Log Spoofed Packets, Source Routed Packets, Redirect Packets
for f in /proc/sys/net/ipv4/conf/*/log_martians; do
echo 1 > $f
done
# Incoming
# Deny all ports
iptables -P INPUT DROP
# Unlimited traffic on the loopback interface.
iptables -A INPUT -i lo -j ACCEPT
# Only tcp and udp protocols for all ports
iptables -A INPUT -i eth0 -p tcp -j ACCEPT
iptables -A INPUT -i eth0 -p udp -j ACCEPT
iptables -A INPUT -i eth1 -p tcp -j ACCEPT
iptables -A INPUT -i eth1 -p udp -j ACCEPT
# Accept already connections established
iptables -A INPUT -i eth0 -p udp -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -i eth0 -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -i eth1 -p udp -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -i eth1 -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
#Forward only with tcp and udp
iptables -P FORWARD DROP
iptables -A FORWARD -i eth0 -p tcp -j ACCEPT
iptables -A FORWARD -i eth0 -p udp -j ACCEPT
iptables -A FORWARD -i eth1 -p tcp -j ACCEPT
iptables -A FORWARD -i eth1 -p udp -j ACCEPT
#Requests only with tcp and udp
iptables -P OUTPUT DROP
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A OUTPUT -i eth0 -p tcp -j ACCEPT
iptables -A OUTPUT -i eth0 -p udp -j ACCEPT
iptables -A OUTPUT -i eth1 -p tcp -j ACCEPT
iptables -A OUTPUT -i eth1 -p udp -j ACCEPT
#Ping accepted
iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT
iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT
Marsh Posté le 29-04-2008 à 14:38:48
Avant de lancer ton script, as-tu réinitialiser les règles de iptables? iptables -F et iptables -X
Quelle carte est en locale et quelle carte sort?
Marsh Posté le 29-04-2008 à 16:39:42
salut,
oui j'ai reinitialise les regles iptables.
La carte externe est la eth0 et la carte locale eth1.
Marsh Posté le 21-04-2008 à 19:22:07
Bonjour a tous,
mon probleme est le suivant, j'ai voulu mettre en place un firewall linux en amont de ma connexion. Pour cela j'ai mis un debian etch et j'ai essaye un fichier iptables tres complet que j'ai trouve sur le net mais le probleme c'est qu'a partir que je lance le fichier, le firewall peut toujorus acceder au net mais le reseau local deriere la seconde carte reseau ne peu plus rien faire(10.10.0.0/24).
Je vous poste le fichier, si quelqu'un pouvait m'apporter ces lumieres
ps:j'ai juste mis W.X.Y.Z a la place de ma vrai gaetway
#!/bin/sh -x
# $Id: firewall,v 2.0 2002/08/01 13:42:22 chryjs Exp $
# File provided by www.firewall-net.com
# Parameters
# $1 : external interface name
# $2 : external interface ip address
# $3 : Gateway ip address
echo "Starting firewalling... "
# ----------------------------------------------------------------------------
# Some definitions for easy maintenance.
# EDIT THESE TO SUIT YOUR SYSTEM AND ISP.
if [ ! -z "$1" ]; then
EXTERNAL_INTERFACE=$1
GATEWAY=$2
else
EXTERNAL_INTERFACE="eth0" # Internet connected interface eth0 for example
GATEWAY="W.X.Y.Z" # Adress of your gateway
fi
LOOPBACK_INTERFACE="lo" # or your local naming convention
LOCAL_INTERFACE_1="eth1" # internal LAN interface if you have one, you can define multiple
IPADDR=`LANG= LC_ALL= ifconfig ${EXTERNAL_INTERFACE} | grep 'inet addr' |
awk -F: '{ print $2 } ' | awk '{ print $1 }'`
# your main IP address
LOCALIPADDR=`LANG= LC_ALL= ifconfig ${LOCAL_INTERFACE_1} | grep 'inet addr' |
awk -F: '{ print $2 } ' | awk '{ print $1 }'`
# your local IP address
LOCALNET_1="10.10.0.0/24" # whatever private range you use
ANYWHERE="0/0" # match any IP address
DHCP_SERVER="0/0" # address of the DHCP server 0/0 if unknown
if [ -f /etc/resolv.conf ]; then
NAMESERVER_1=`grep nameserver /etc/resolv.conf | head -1 | awk '{print $2}'`
else
NAMESERVER_1="127.0.0.1" # everyone must have at least one, 0/0 if unknown
fi
SMTP_SERVER="127.0.0.1" # Your ISP mail gateway. Your relay or yourself.
POP_SERVER="0/0" # Your ISP pop mail server.
NEWS_SERVER="any/0" # Your ISP news server
LOOPBACK="127.0.0.0/8" # reserved loopback address range
CLASS_A="10.0.0.0/8" # class A private networks
CLASS_B="172.16.0.0/12" # class B private networks
CLASS_C="192.168.0.0/16" # class C private networks
CLASS_D_MULTICAST="224.0.0.0/4" # class D multicast addresses
CLASS_E_RESERVED_NET="240.0.0.0/5" # class E reserved addresses
BROADCAST_SRC="0.0.0.0" # broadcast source address
BROADCAST_DEST="255.255.255.255" # broadcast destination address
PRIVPORTS="0:1023" # well known, privileged port range
UNPRIVPORTS="1024:65535" # unprivileged port range
# ----------------------------------------------------------------------------
NFS_PORT="2049" # (TCP/UDP) NFS
SOCKS_PORT="1080" # (TCP) Socks
OPENWINDOWS_PORT="2000" # (TCP) openwindows
# X Windows port allocation begins at 6000 and increments to 6063
# for each additional server running.
XWINDOW_PORTS="6000:6063" # (TCP) X windows
# The SSH client starts at 1023 and works down to 513 for each
# additional simultaneous connection originating from a privileged port.
# Clients can optionally be configured to use only unprivileged ports.
SSH_LOCAL_PORTS="1022:65535" # port range for local clients
SSH_REMOTE_PORTS="513:65535" # port range for remote clients
# traceroute usually uses -s 32769:65535 -d 33434:33523
TRACEROUTE_SRC_PORTS="32769:65535"
TRACEROUTE_DEST_PORTS="33434:33523"
# CVS Pserver port
CVS_PSERVER_PORT="2401"
#MYSQL server port
MYSQL_PORT="3306"
# ----------------------------------------------------------------------------
# Default policy is DENY
# Explicitly accept desired INCOMING & OUTGOING connections
# Remove all existing rules belonging to this filter
iptables -F
# iptables -F -t nat # nat module must be included else comment this oune
# Remove any existing user-defined chains.
iptables -X
# Set the default policy of the filter to deny.
iptables -P INPUT DROP
iptables -P OUTPUT REJECT
iptables -P FORWARD DROP
# ----------------------------------------------------------------------------
# Enable IP Forwarding, if it isn't already
echo 1 > /proc/sys/net/ipv4/ip_forward
# Enable broadcast echo Protection
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
# Enable bad error message Protection
echo 1 > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
# Enable IP spoofing protection
# turn on Source Address Verification
for f in /proc/sys/net/ipv4/conf/*/rp_filter; do
echo 1 > $f
done
# Disable ICMP Redirect Acceptance
for f in /proc/sys/net/ipv4/conf/*/accept_redirects; do
echo 0 > $f
done
for f in /proc/sys/net/ipv4/conf/*/send_redirects; do
echo 0 > $f
done
# Disable Source Routed Packets
for f in /proc/sys/net/ipv4/conf/*/accept_source_route; do
echo 0 > $f
done
# Log Spoofed Packets, Source Routed Packets, Redirect Packets
for f in /proc/sys/net/ipv4/conf/*/log_martians; do
echo 1 > $f
done
# ---------------------------------------------------------------------------
# NAT (masquerade) modules
# These modules are necessary to masquerade their respective services.
# uncomment if they are required (NAT if you nat your local network
# and the same for ftp)
# echo "Inserting required modules for NAT"
# /sbin/insmod ip_conntrack
# /sbin/insmod iptable_nat
# /sbin/insmod ip_nat_ftp
# /sbin/insmod ip_conntrack_ftp
echo "No modules required for NAT"
# ----------------------------------------------------------------------------
# LOOPBACK
# Unlimited traffic on the loopback interface.
iptables -A INPUT -i $LOOPBACK_INTERFACE -j ACCEPT
iptables -A OUTPUT -o $LOOPBACK_INTERFACE -j ACCEPT
# ----------------------------------------------------------------------------
# Unlimited traffic within the local network.
# All internal machines have access to the fireall machine.
# Uncomment those ones if you use a specific network card for internal
# interface
iptables -A INPUT -i $LOCAL_INTERFACE_1 -s $LOCALNET_1 -j ACCEPT
iptables -A OUTPUT -o $LOCAL_INTERFACE_1 -d $LOCALNET_1 -j ACCEPT
# Uncomment those ones if you use a virtual IP on your main network card
# for internal traffic
# iptables -A INPUT -i $EXTERNAL_INTERFACE -s $LOCALNET_1 -d $LOCALIPADDR -j ACCEPT
# iptables -A OUTPUT -o $EXTERNAL_INTERFACE -d $LOCALNET_1 -s $LOCALIPADDR -j ACCEPT
# ----------------------------------------------------------------------------
# Masquerade internal traffic.
# All internal traffic is masqueraded externally.
# The iptables functionality is under development.
# No filters are applied. Just masquerading.
iptables -A POSTROUTING -t nat -o $EXTERNAL_INTERFACE -j MASQUERADE
# ----------------------------------------------------------------------------
# SPOOFING & BAD ADDRESSES
# Refuse spoofed packets.
# Ignore blatantly illegal source addresses.
# Protect yourself from sending to bad addresses.
# Refuse incoming packets pretending to be from the external address.
iptables -A INPUT -s $IPADDR -j DROP
# Refuse incoming packets claiming to be from a Class A, B or C private network
iptables -A INPUT -s $CLASS_A -j DROP
iptables -A INPUT -s $CLASS_B -j DROP
iptables -A INPUT -s $CLASS_C -j DROP
# Refuse broadcast address SOURCE packets
iptables -A INPUT -s $BROADCAST_DEST -j DROP
iptables -A INPUT -d $BROADCAST_SRC -j DROP
# Refuse Class D multicast addresses
# Multicast is illegal as a source address.
# Multicast uses UDP.
iptables -A INPUT -s $CLASS_D_MULTICAST -j DROP
# Refuse Class E reserved IP addresses
iptables -A INPUT -s $CLASS_E_RESERVED_NET -j DROP
# Refuse special addresses defined as reserved by the IANA.
# Note: The remaining reserved addresses are not included.
# Filtering them causes problems as reserved blocks are
# being allocated more often now.
# Note: this list includes the loopback, multicast, & reserved addresses.
# 0.*.*.* - Can't be blocked for DHCP users.
# 127.*.*.* - LoopBack
# 169.254.*.* - Link Local Networks
# 192.0.2.* - TEST-NET
# 224-255.*.*.* - Classes D & E, plus unallocated.
iptables -A INPUT -s 127.0.0.0/8 -j DROP
iptables -A INPUT -s 169.254.0.0/16 -j DROP
iptables -A INPUT -s 192.0.2.0/24 -j DROP
iptables -A INPUT -s 224.0.0.0/3 -j DROP
# ----------------------------------------------------------------------------
# NOTE:
# The symbolic names used in /etc/services for the port numbers vary by
# supplier. Using them is less error prone and more meaningful, though.
# ----------------------------------------------------------------------------
# TCP UNPRIVILEGED PORTS
# Avoid ports subject to protocol & system administration problems.
# NFS: establishing a TCP connection
iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp --syn \
--destination-port $NFS_PORT -j DROP
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp --syn \
--destination-port $NFS_PORT -j REJECT
# openwindows: establishing a connection
iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp --syn \
--destination-port $OPENWINDOWS_PORT -j DROP
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp --syn \
--destination-port $OPENWINDOWS_PORT -j REJECT
# Xwindows: establishing a connection
iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp --syn \
--destination-port $XWINDOW_PORTS -j DROP
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp --syn \
--destination-port $XWINDOW_PORTS -j REJECT
# SOCKS: establishing a connection
iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp --syn \
--destination-port $SOCKS_PORT -j DROP
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp --syn \
--destination-port $SOCKS_PORT -j REJECT
# CVS: establishing a pserver connexion (use SSH instead)
iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp --syn \
--destination-port $CVS_PSERVER_PORT -j DROP
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp --syn \
--destination-port $CVS_PSERVER_PORT -j REJECT
# MYSQL: establishing a connection
iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp --syn \
--destination-port $MYSQL_PORT -j DROP
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp --syn \
--destination-port $MYSQL_PORT -j REJECT
# ----------------------------------------------------------------------------
# UDP UNPRIVILEGED PORTS
# Avoid ports subject to protocol & system administration problems.
iptables -A INPUT -i $EXTERNAL_INTERFACE -p udp \
--destination-port $NFS_PORT -j DROP
# UDP INCOMING TRACEROUTE
# traceroute usually uses -S 32769:65535 -D 33434:33523
iptables -A INPUT -i $EXTERNAL_INTERFACE -p udp \
--source-port $TRACEROUTE_SRC_PORTS \
--destination-port $TRACEROUTE_DEST_PORTS -j DROP
# DNS server (53)
# ---------------
# DNS: full server
# ----------------
# server/client to server query or response
iptables -A INPUT -i $EXTERNAL_INTERFACE -p udp \
--source-port $UNPRIVPORTS \
-d $IPADDR --destination-port 53 -j ACCEPT
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p udp \
-s $IPADDR --source-port 53 \
--destination-port $UNPRIVPORTS -j ACCEPT
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p udp \
-s $IPADDR --source-port 53 \
--destination-port 53 -j ACCEPT
iptables -A INPUT -i $EXTERNAL_INTERFACE -p udp \
--source-port 53 \
-d $IPADDR --destination-port 53 -j ACCEPT
# DNS client (53)
# ---------------
# Use these if you have external DNS server (resolver)
# it's better to have a localhost caching only server in
# this case
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p udp \
-s $IPADDR --source-port $UNPRIVPORTS \
-d $NAMESERVER_1 --destination-port 53 -j ACCEPT
iptables -A INPUT -i $EXTERNAL_INTERFACE -p udp \
-s $NAMESERVER_1 --source-port 53 \
-d $IPADDR --destination-port $UNPRIVPORTS -j ACCEPT
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp \
-s $IPADDR --source-port $UNPRIVPORTS \
-d $NAMESERVER_1 --destination-port 53 -j ACCEPT
iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp ! --syn \
-s $NAMESERVER_1 --source-port 53 \
-d $IPADDR --destination-port $UNPRIVPORTS -j ACCEPT
# ------------------------------------------------------------------
# HTTP server (80)
# ----------------
# Uncomment if you have your own HTTP server (eg apache)
iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp \
--source-port $UNPRIVPORTS \
-d $IPADDR --destination-port 80 -j ACCEPT
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp ! --syn \
-s $IPADDR --source-port 80 \
--destination-port $UNPRIVPORTS -j ACCEPT
# HTTP client (80)
# ----------------
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp \
-s $IPADDR --source-port $UNPRIVPORTS \
--destination-port 80 -j ACCEPT
iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp ! --syn \
--source-port 80 \
-d $IPADDR --destination-port $UNPRIVPORTS -j ACCEPT
iptables -A OUTPUT -o $LOCAL_INTERFACE_1 -p tcp \
-s $IPADDR --source-port $UNPRIVPORTS \
--destination-port 80 -j ACCEPT
iptables -A INPUT -i $LOCAL_INTERFACE_1 -p tcp ! --syn \
--source-port 80 \
-d $IPADDR --destination-port $UNPRIVPORTS -j ACCEPT
# ------------------------------------------------------------------
# HTTPS client (443)
# ------------------
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp \
-s $IPADDR --source-port $UNPRIVPORTS \
--destination-port 443 -j ACCEPT
iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp ! --syn \
--source-port 443 \
-d $IPADDR --destination-port $UNPRIVPORTS -j ACCEPT
iptables -A OUTPUT -o $LOCAL_INTERFACE_1 -p tcp \
-s $IPADDR --source-port $UNPRIVPORTS \
--destination-port 443 -j ACCEPT
iptables -A INPUT -i $ELOCAL_INTERFACE_1 -p tcp ! --syn \
--source-port 443 \
-d $IPADDR --destination-port $UNPRIVPORTS -j ACCEPT
# ------------------------------------------------------------------
# NNTP NEWS client (119)
# ----------------------
# Uncomment these if you use Newsgroups
#iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp \
# -s $IPADDR --source-port $UNPRIVPORTS \
# -d $NEWS_SERVER --destination-port 119 -j ACCEPT
#iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp ! --syn \
# -s $NEWS_SERVER --source-port 119 \
# -d $IPADDR --destination-port $UNPRIVPORTS -j ACCEPT
# ------------------------------------------------------------------
# POP server (110)
# ----------------
# Uncomment if you have your own pop server
#iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp \
# --source-port $UNPRIVPORTS \
# -d $IPADDR --destination-port 110 -j ACCEPT
#iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp ! --syn \
# -s $IPADDR --source-port 110 \
# --destination-port $UNPRIVPORTS -j ACCEPT
# POP client (110)
# ----------------
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp \
-s $IPADDR --source-port $UNPRIVPORTS \
--destination-port 110 -j ACCEPT
iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp ! --syn \
--source-port 110 \
-d $IPADDR --destination-port $UNPRIVPORTS -j ACCEPT
# ------------------------------------------------------------------
# SMTP server (25)
# ----------------
# Uncomment if you have your own SMTP server only
#iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp \
# --source-port $UNPRIVPORTS \
# -d $IPADDR --destination-port 25 -j ACCEPT
#iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp ! --syn \
# -s $IPADDR --source-port 25 \
# --destination-port $UNPRIVPORTS -j ACCEPT
# SMTP client (25)
# ----------------
# Generally you use your own SMTP client on linux
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp \
-s $IPADDR --source-port $UNPRIVPORTS \
-d $SMTP_SERVER --destination-port 25 -j ACCEPT
iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp ! --syn \
-s $SMTP_SERVER --source-port 25 \
-d $IPADDR --destination-port $UNPRIVPORTS -j ACCEPT
# ------------------------------------------------------------------
# SSH server (22)
# ---------------
# Uncomment if you run a ssh server on your linux box
# it is preferable to allow only from some IP
iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp \
--source-port $SSH_REMOTE_PORTS \
-d $IPADDR --destination-port 22 -j ACCEPT
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp ! --syn \
-s $IPADDR --source-port 22 \
--destination-port $SSH_REMOTE_PORTS -j ACCEPT
# SSH client (22)
# ---------------
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp \
-s $IPADDR --source-port $SSH_LOCAL_PORTS \
--destination-port 22 -j ACCEPT
iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp ! --syn \
--source-port 22 \
-d $IPADDR --destination-port $SSH_LOCAL_PORTS -j ACCEPT
# ------------------------------------------------------------------
# TELNET client (23)
# ------------------
# you shouldn't use them nore server nore client
#iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp \
# -s $IPADDR --source-port $UNPRIVPORTS \
# --destination-port 23 -j ACCEPT
#iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp ! --syn \
# --source-port 23 \
# -d $IPADDR --destination-port $UNPRIVPORTS -j ACCEPT
# ------------------------------------------------------------------
# AUTH server (113)
# -----------------
# Uncomment if you run identd
# Reject, rather than deny, the incoming auth port. (NET-3-HOWTO)
#iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp \
# --source-port $UNPRIVPORTS \
# -d $IPADDR --destination-port 113 -j REJECT
# AUTH client (113)
# -----------------
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp \
-s $IPADDR --source-port $UNPRIVPORTS \
--destination-port 113 -j ACCEPT
iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp ! --syn \
--source-port 113 \
-d $IPADDR --destination-port $UNPRIVPORTS -j ACCEPT
# ------------------------------------------------------------------
# WHOIS client (43)
# -----------------
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp \
-s $IPADDR --source-port $UNPRIVPORTS \
--destination-port 43 -j ACCEPT
iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp ! --syn \
--source-port 43 \
-d $IPADDR --destination-port $UNPRIVPORTS -j ACCEPT
# ------------------------------------------------------------------
# FINGER client (79)
# ------------------
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp \
-s $IPADDR --source-port $UNPRIVPORTS \
--destination-port 79 -j ACCEPT
iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp ! --syn \
--source-port 79 \
-d $IPADDR --destination-port $UNPRIVPORTS -j ACCEPT
# ------------------------------------------------------------------
# FTP server (21)
# ---------------
# Uncomment if you run your own FTP server (such as wu-ftpd)
# incoming request
#iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp \
# --source-port $UNPRIVPORTS \
# -d $IPADDR --destination-port 21 -j ACCEPT
#iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp ! --syn \
# -s $IPADDR --source-port 21 \
# --destination-port $UNPRIVPORTS -j ACCEPT
# PORT MODE data channel responses
#iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp \
# -s $IPADDR --source-port 20 \
# --destination-port $UNPRIVPORTS -j ACCEPT
#iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp ! --syn \
# --source-port $UNPRIVPORTS \
# -d $IPADDR --destination-port 20 -j ACCEPT
# FTP client (21)
# ---------------
# outgoing request
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp \
-s $IPADDR --source-port $UNPRIVPORTS \
--destination-port 21 -j ACCEPT
iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp ! --syn \
--source-port 21 \
-d $IPADDR --destination-port $UNPRIVPORTS -j ACCEPT
# PORT mode data channel
iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp \
--source-port 20 \
-d $IPADDR --destination-port $UNPRIVPORTS -j ACCEPT
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp ! --syn \
-s $IPADDR --source-port $UNPRIVPORTS \
--destination-port 20 -j ACCEPT
# ------------------------------------------------------------------
# IRC client (6667)
# -----------------
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp \
-s $IPADDR --source-port $UNPRIVPORTS \
--destination-port 6667 -j ACCEPT
iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp ! --syn \
--source-port 6667 \
-d $IPADDR --destination-port $UNPRIVPORTS -j ACCEPT
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp \
-s $IPADDR --source-port $UNPRIVPORTS \
--destination-port $UNPRIVPORTS -j ACCEPT
iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp \
--source-port $UNPRIVPORTS \
-d $IPADDR --destination-port $UNPRIVPORTS -j ACCEPT
# ------------------------------------------------------------------
# RealAudio / QuickTime client
# ----------------------------
# Uncomment these lines if you use RealAUdio or Quicktime
#iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp ! --syn \
# --source-port 554 \
# -d $IPADDR --destination-port $UNPRIVPORTS -j ACCEPT
#iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp \
# -s $IPADDR --source-port $UNPRIVPORTS \
# --destination-port 554 -j ACCEPT
# TCP is a more secure method: 7070:7071
#iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp ! --syn \
# --source-port 7070:7071 \
# -d $IPADDR --destination-port $UNPRIVPORTS -j ACCEPT
#iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp \
# -s $IPADDR --source-port $UNPRIVPORTS \
# --destination-port 7070:7071 -j ACCEPT
# UDP is the preferred method: 6970:6999
# For LAN machines, UDP requires the RealAudio masquerading module and
# the ipmasqadm third-party software.
#iptables -A INPUT -i $EXTERNAL_INTERFACE -p udp \
# --source-port $UNPRIVPORTS \
# -d $IPADDR --destination-port 6970:6999 -j ACCEPT
#iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p udp \
# -s $IPADDR --source-port 6970:6999 \
# --destination-port $UNPRIVPORTS -j ACCEPT
# ------------------------------------------------------------------
# ICQ client (4000)
# -----------------
# Uncomment if you use ICQ
#iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p tcp \
# -s $IPADDR --source-port $UNPRIVPORTS \
# --destination-port 2000:4000 -j ACCEPT
#iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp ! --syn \
# --source-port 2000:4000 \
# -d $IPADDR --destination-port $UNPRIVPORTS -j ACCEPT
#iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p udp \
# -s $IPADDR --source-port $UNPRIVPORTS \
# --destination-port 4000 -j ACCEPT
#iptables -A INPUT -i $EXTERNAL_INTERFACE -p udp \
# --source-port 4000 \
# -d $IPADDR --destination-port $UNPRIVPORTS -j ACCEPT
# ----------------------------------------------------------------------------
# UDP accept only on selected ports
# ---------------------------------
# DHCP client (67, 68)
# --------------------
# allow dhcp server (67) to connect to dhcp client (68)
# Note: the DHCP server is the only externel source of broadcast
# messages we should see, ever.
# Required for CABLE (sometimes also for local network)
#iptables -A INPUT -i $EXTERNAL_INTERFACE -p udp \
# -s $DHCP_SERVER --source-port 67 \
# -d $IPADDR --destination-port 68 -j ACCEPT
#iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p udp \
# -s $IPADDR --source-port 68 \
# -d $DHCP_SERVER --destination-port 67 -j ACCEPT
#iptables -A INPUT -i $EXTERNAL_INTERFACE -p udp \
# -s $DHCP_SERVER --source-port 67 \
# -d $BROADCAST_DEST --destination-port 68 -j ACCEPT
#iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p udp \
# -s $BROADCAST_SRC --source-port 68 \
# -d $DHCP_SERVER --destination-port 67 -j ACCEPT
# Getting renumbered
#iptables -A INPUT -i $EXTERNAL_INTERFACE -p udp \
# -s $BROADCAST_SRC --source-port 67 \
# -d $BROADCAST_DEST --destination-port 68 -j ACCEPT
#iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p udp \
# -s $BROADCAST_SRC --source-port 68 \
# -d $BROADCAST_DEST --destination-port 67 -j ACCEPT
# As a result of the above, we're supposed to change our IP address with
# this message, which is addressed to our new address before the dhcp
# client has received the update.
#iptables -A INPUT -i $EXTERNAL_INTERFACE -p udp \
# -s $DHCP_SERVER --source-port 67 \
# --destination-port 68 -j ACCEPT
#iptables -A INPUT -i $EXTERNAL_INTERFACE -p udp \
# --source-port 67 \
# -d $IPADDR --destination-port 68 -j DROP
# ------------------------------------------------------------------
# NTP TIME clients (123)
# ----------------------
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p udp \
-s $IPADDR --source-port $UNPRIVPORTS \
-d any/0 --destination-port 123 -j ACCEPT
iptables -A INPUT -i $EXTERNAL_INTERFACE -p udp \
-s any/0 --source-port 123 \
-d $IPADDR --destination-port $UNPRIVPORTS -j ACCEPT
# ------------------------------------------------------------------
# OUTGOING TRACEROUTE
# -------------------
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p udp \
-s $IPADDR --source-port $TRACEROUTE_SRC_PORTS \
--destination-port $TRACEROUTE_DEST_PORTS -j ACCEPT
# ----------------------------------------------------------------------------
# ICMP
# To prevent denial of service attacks based on ICMP bombs, filter
# incoming Redirect (5) and outgoing Destination Unreachable (3).
# Note, however, disabling Destination Unreachable (3) is not
# advisable, as it is used to negotiate packet fragment size.
# For bi-directional ping.
# Message Types: Echo_Reply (0), Echo_Request (8)
# To prevent attacks, limit the src addresses to your ISP range.
#
# For outgoing traceroute.
# Message Types: INCOMING Dest_Unreachable (3), Time_Exceeded (11)
# default UDP base: 33434 to base+nhops-1
#
# For incoming traceroute.
# Message Types: OUTGOING Dest_Unreachable (3), Time_Exceeded (11)
# To block this, deny OUTGOING 3 and 11
# 0: echo-reply (pong)
# 3: destination-unreachable, port-unreachable, fragmentation-needed, etc.
# 4: source-quench
# 5: redirect
# 8: echo-request (ping)
# 11: time-exceeded
# 12: parameter-problem
iptables -A INPUT -i $EXTERNAL_INTERFACE -p icmp \
--icmp-type echo-reply \
-d $IPADDR -j ACCEPT
iptables -A INPUT -i $EXTERNAL_INTERFACE -p icmp \
--icmp-type destination-unreachable \
-d $IPADDR -j ACCEPT
iptables -A INPUT -i $EXTERNAL_INTERFACE -p icmp \
--icmp-type source-quench \
-d $IPADDR -j ACCEPT
iptables -A INPUT -i $EXTERNAL_INTERFACE -p icmp \
--icmp-type time-exceeded \
-d $IPADDR -j ACCEPT
iptables -A INPUT -i $EXTERNAL_INTERFACE -p icmp \
--icmp-type parameter-problem \
-d $IPADDR -j ACCEPT
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p icmp \
-s $IPADDR --icmp-type fragmentation-needed -j ACCEPT
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p icmp \
-s $IPADDR --icmp-type source-quench -j ACCEPT
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p icmp \
-s $IPADDR --icmp-type echo-request -j ACCEPT
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -p icmp \
-s $IPADDR --icmp-type parameter-problem -j ACCEPT
# ----------------------------------------------------------------------------
# Enable logging for selected denied packets
# Loading requiered module for logging
/sbin/insmod ip_LOG
iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp -j DROP
iptables -A INPUT -i $EXTERNAL_INTERFACE -p tcp -j LOG
iptables -A INPUT -i $EXTERNAL_INTERFACE -p udp \
--destination-port $PRIVPORTS -j DROP
iptables -A INPUT -i $EXTERNAL_INTERFACE -p udp \
--destination-port $PRIVPORTS -j LOG
iptables -A INPUT -i $EXTERNAL_INTERFACE -p udp \
--destination-port $UNPRIVPORTS -j DROP
iptables -A INPUT -i $EXTERNAL_INTERFACE -p udp \
--destination-port $UNPRIVPORTS -j LOG
iptables -A INPUT -i $EXTERNAL_INTERFACE -p icmp \
--icmp-type 5 -j DROP
iptables -A INPUT -i $EXTERNAL_INTERFACE -p icmp \
--icmp-type 5 -j LOG
iptables -A INPUT -i $EXTERNAL_INTERFACE -p icmp -j DROP
iptables -A INPUT -i $EXTERNAL_INTERFACE -p icmp -j LOG
#forget all other icmp
# --icmp-type 13:255 -j DROP
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -j REJECT
iptables -A OUTPUT -o $EXTERNAL_INTERFACE -j LOG
# ----------------------------------------------------------------------------
echo "done"
exit 0