Firewall pour station : IpChains

IpChains (firewall à filtrage de paquet)

IpChains est l'interface utilisateur permettant d'administrer les règles de Firewall implémentées au niveau d'un noyau Linux.


Quelques rappels :


Scripts présentés :

Les trois scripts présentés ont les caractéristiques suivantes :


Ils pourraient par exemple être utilisés sur la distribution Debian Potato ou sur une distribution Debian Woody avec l'interface IpChains.

Exemples de logs :

May 4 13:21:04 HOST kernel: Packet log: LogDrop DENY eth0 PROTO=17 10.1.1.43:137 10.1.1.1:137 L=78 S=0x00 I=931 F=0x0000 T=128 SYN (#1)
May 4 13:21:27 HOST kernel: Packet log: LogDrop DENY eth0 PROTO=6 10.1.1.42:2579 10.1.1.1:22 L=60 S=0x00 I=943 F=0x4000 T=64 SYN (#1)

custom_net.sh :

#!/bin/sh

### NETWORK CUSTOMIZATION

echo "0" > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
echo "60" > /proc/sys/net/ipv4/ip_default_ttl
echo "0" > /proc/sys/net/ipv4/ip_forward

ZERO_FLAGS="accept_redirects accept_source_route forwarding proxy_arp send_redirects";
ONE_FLAGS="rp_filter log_martians";
CHEMIN="/proc/sys/net/ipv4/conf";

for repert in `ls "$CHEMIN"` ; do
  for fichier in `echo "$ZERO_FLAGS"`; do
    if [ -e "$CHEMIN/$repert/$fichier" ]
      then echo "0" > "$CHEMIN/$repert/$fichier"; fi
  done
  for fichier in `echo "$ONE_FLAGS"`; do
    if [ -e "$CHEMIN/$repert/$fichier" ]
      then echo "1" > "$CHEMIN/$repert/$fichier"; fi
  done
done

Ipchains_rules_down.sh :

#!/bin/sh

IPCH=/sbin/ipchains # IpChains binary

### CHECK KERNEL VERSION AND BINARY PRESENCE
if [ ! -f $IPCH ] && [ ! -x $IPCH ] ; then exit 0 ; fi
CHECK=`$IPCH -L -n 2>&1 > /dev/null || echo "bad"`
if [ "$CHECK" ]
  then
    echo "$0 : Not with this kernel"
    exit 0
fi

### VARIABLES

DEFAULT_POL="input output forward" # Default policies

### BEGIN

# Flush and remove all chains then default the policies to ACCEPT
$IPCH -F
$IPCH -X
for i in $DEFAULT_POL
  do
    $IPCH -P $i ACCEPT
done

echo "$0 done"

Ipchains_rules_up.sh :

#!/bin/sh

IPCH=/sbin/ipchains # IpChains binary

### CHECK KERNEL VERSION AND BINARY PRESENCE

if [ ! -f $IPCH ] && [ ! -x $IPCH ] ; then exit 0 ; fi
CHECK=`$IPCH -L -n 2>&1 > /dev/null || echo "bad"`
if [ "$CHECK" ]
then
echo "$0 : Not with this kernel"
exit 0
fi

### Set OUR value to the printk variable
echo "6 4 1 7" > /proc/sys/kernel/printk

### NETWORK CUSTOMIZATION

test -f /home/system/fw/custom_net.sh && test -x /home/system/fw/custom_net.sh && /home/system/fw/custom_net.sh

### VARIABLES

# Addresses
LOCAL_IP=`ifconfig eth0 | awk 'BEGIN { FS=":" ; RS=" " } /addr:/ { print $2 }'` # Get local Eth0 IP Address
BROADCAST_IP=`ifconfig eth0 | awk 'BEGIN { FS=":" ; RS=" " } /Bcast:/ { print $2 }'` # Get local Eth0 Broadcast IP Address

ADM_IP="10.1.1.77"

DNS_IP="10.1.1.10 10.1.1.11"
PROXY_IP="10.1.1.20 10.1.1.21"
MAIL_IP="10.1.1.30 10.1.1.31"

# Uncomment here and in the script to allow that kind of functionality
#NTP_IP="@IPS_OF_NTP_SERVERS"
#ICMP_IP="@IPS_OF_ALLOWED_ICMP_REQUESTER_HOSTS"

# Policies
DEFAULT_POL="input output forward"
LOG_ACCEPT="LogAcc"
LOG_DROP="LogDrop"

# Various
RPORTS=":1024"
NRPORTS="1024:"

### BEGIN

# Flush and remove all chains then default the policies to DROP
$IPCH -F
$IPCH -X
for i in $DEFAULT_POL
  do
    $IPCH -P $i DENY
done

### Create and set personnal chains

# Log and deny chain
$IPCH -N $LOG_DROP # Create a new one
$IPCH -A $LOG_DROP -j DENY -l # Log and deny

# Log and accept chain
$IPCH -N $LOG_ACCEPT # Create a new one
$IPCH -A $LOG_ACCEPT -j ACCEPT -l # Log and accept

### LOOPBACK AND REMOTE MANAGEMENT

# Allow whatever on loopback
$IPCH -A output -i lo -j ACCEPT
$IPCH -A input -i lo -j ACCEPT

# Allow SSH remote management and log connections
for i in $ADM_IP
  do
    $IPCH -A input -p tcp -s $ADM_IP $NRPORTS -d $LOCAL_IP 22 -y -j $LOG_ACCEPT
    $IPCH -A input -p tcp -s $ADM_IP $NRPORTS -d $LOCAL_IP 22 -j ACCEPT
    $IPCH -A output -p tcp -s $LOCAL_IP 22 -d $ADM_IP $NRPORTS -j ACCEPT
done

### ALLOW THESE TCP CONNECTIONS

# Allow SSH anywhere and log Syn Scan profit port
$IPCH -A output -p tcp --sport $NRPORTS --dport 22 -j ACCEPT
$IPCH -A input -p tcp --sport 22 -y -j $LOG_DROP
$IPCH -A input -p tcp --sport 22 --dport $NRPORTS -j ACCEPT

# Allow HTTP/HTTPS to HTTP proxy servers and log Syn Scan profit port
for i in $PROXY_IP
  do
    $IPCH -A output -p tcp --sport $NRPORTS -d $i 8080 -j ACCEPT
    $IPCH -A input -p tcp -s $i 8080 -y -j $LOG_DROP
    $IPCH -A input -p tcp -s $i 8080 --dport $NRPORTS -j ACCEPT
done

# Allow SMTP/POP to MailServers and log Syn Scan profit port
for i in $MAIL_IP
  do
    for p in 25 110
      do
        $IPCH -A output -p tcp --sport $NRPORTS -d $i $p -j ACCEPT
        $IPCH -A input -p tcp -s $i $p -y -j $LOG_DROP
        $IPCH -A input -p tcp -s $i $p --dport $NRPORTS -j ACCEPT
    done
done

### ALLOW THESE UDP CONNECTIONS

# Allow DNS Protocol to DNS Servers
for i in $DNS_IP
  do
    $IPCH -A output -p udp --sport $NRPORTS -d $i 53 -j ACCEPT
    $IPCH -A input -p udp -s $i 53 --dport $NRPORTS -j ACCEPT
done

### Uncomment if you want to use communications to NTP servers.
### => Also uncomment and set NTP_IP at the beginning of the script.
## Allow NTP Protocol to NTP Servers
# for i in $NTP_IP
# do
# $IPCH -A output -p udp --sport $NRPORTS -d $i 123 -j ACCEPT
# $IPCH -A input -p udp -s $i 123 --dport $NRPORTS -j ACCEPT
# done

### ALLOW THESE ICMP CONNECTIONS

### Uncomment if you want certain hosts to send us icmp requests
### => Also uncomment and set ICMP_IP at the beginning of the script
## Allow some host's icmp requests
#for i in $ICMP_IP
# do
# $IPCH -A input -p icmp --icmp-type echo-request -s $i -j ACCEPT
# $IPCH -A input -p icmp --icmp-type destination-unreachable -s $i -j ACCEPT
# $IPCH -A input -p icmp --icmp-type time-exceeded -s $i -j ACCEPT
# $IPCH -A output -p icmp --icmp-type echo-reply -d $i -j ACCEPT
#done

### Uncomment if you allow this station to send certain kind of icmp requests
## Allow some icmp requests to be sent
# $IPCH -A output -p icmp --icmp-type echo-request -j ACCEPT
# $IPCH -A input -p icmp --icmp-type echo-reply -j ACCEPT
# $IPCH -A input -p icmp --icmp-type destination-unreachable -j ACCEPT
# $IPCH -A input -p icmp --icmp-type time-exceeded -j ACCEPT

Simon Castro
Maj le 4 Mai 2003