changed it, split up in two files:
Here it is. It works fine for me (I hope so) and the helping-functions
helps you keeping the overview in your firewall-script.
In the function firewall_internet_init_connection i have some blockings
from hacking attempts of local-networks
on the internet-interface. Maybe that's what you are looking for.
I don't have functions to forward some services to oter server in our
company. infrastructure and therfore the script is growing.
Maybe it helps you to develop your firewall-script that is a little bit
more 'human-readable'.
greetings,
whylee.
the first file /etc/firewall/firewall-functions.sh:
#!/bin/sh
#
#
##############################################
# /etc/firewall/firewall-functions.sh #
# #
# Hier sind die Hilfsfunktionen, die in #
# /etc/firewall/start-firewall.sh gebraucht #
# werden. #
##############################################
IPT=/sbin/iptables
##############################################
# firewall_init_modules #
# Initialisierung der Module NAT, FTP, IRC #
##############################################
function firewall_init_modules
{
echo Firewall: Initialisierung der Module NAT, FTP, IRC
# Das NAT-Modul laden (dies zieht all die anderen mit).
modprobe iptable_nat
modprobe ip_nat_ftp
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_conntrack_irc
modprobe ip_nat_irc
}
##############################################
# firewall_reset_policy_drop #
# Loeschen aller Firewalleintraege und #
# Einrichten der Drop-Policy #
##############################################
function firewall_reset_policy_drop
{
echo Firewall: Loeschen aller Firewalleintraege
# Alle Eintraege in der filter-tabelle loeschen
$IPT --flush
# Alle chains, die nicht in der filter-tabelle sind loeschen
$IPT --delete-chain
# Alle Eintraege in der nat-Tabelle loeschen
$IPT --table nat --flush
# Alle chains, die nicht in der nat-tabelle sind loeschen
$IPT --table nat --delete-chain
echo Firewall: Erstellen der Default-Regeln
# Default-Regel ist DROP
$IPT --policy INPUT DROP
$IPT --policy OUTPUT DROP
$IPT --policy FORWARD DROP
$IPT -X
}
################################################
# firewall_enable_adsl_pptp_connection #
# Erlaubt das Pingen und die PPTP-Verbindung #
# zu einem ADSL-Modem #
# Notwendige Variablen: #
# IF_ADSL=eth1 #
# IPADR_ADSL=10.0.0.140 #
# IPADR_ADSL_MODEM=10.0.0.138 #
################################################
function firewall_enable_adsl_pptp_connection
{
echo Firewall: Regeln fuer die Verbindung zum ADSL-Modem ueber
Interface $IF_ADSL einrichten
#Ping zum ADSL-Modem raus lassen:
$IPT -A OUTPUT -o $IF_ADSL -p icmp -s $IPADR_ADSL -d $IPADR_ADSL_MODEM
-j ACCEPT
#Ping vom ADSL-Modem rein lassen:
$IPT -A INPUT -i $IF_ADSL -p icmp -s $IPADR_ADSL_MODEM -d $IPADR_ADSL
-j ACCEPT
# PPTP-Protokoll zum ADSL-Modem raus lassen:
$IPT -A OUTPUT -o $IF_ADSL -p tcp -s $IPADR_ADSL -d $IPADR_ADSL_MODEM
--dport 1723 -j ACCEPT
# PPTP-Protokoll zum ADSL-Modem raus lassen:
$IPT -A INPUT -i $IF_ADSL -p tcp -s $IPADR_ADSL_MODEM -d $IPADR_ADSL
--sport 1723 -j ACCEPT
# PPTP-Protokoll zum ADSL-Modem raus lassen:
$IPT -A OUTPUT -o $IF_ADSL -p gre -s $IPADR_ADSL -d $IPADR_ADSL_MODEM
-j ACCEPT
# PPTP-Protokoll zum ADSL-Modem raus lassen:
$IPT -A INPUT -i $IF_ADSL -p gre -s $IPADR_ADSL_MODEM -d $IPADR_ADSL
-j ACCEPT
# Alle anderen Packete werden dort nicht hinaus gelassen!
$IPT -A OUTPUT -o $IF_ADSL -j DROP
}
################################################
# firewall_loopback_init_connection #
# Richtet die Standardregeln fuer die #
# Loopbackverbindung ein. #
################################################
function firewall_loopback_init_connection
{
echo Firewall: Regeln fuer die Loopbackverbindung einrichten
$IPT -A INPUT -i lo -j ACCEPT
$IPT -A OUTPUT -o lo -j ACCEPT
}
################################################
# firewall_internet_init_connection #
# Richtet die Standardregeln fuer die #
# Internetverbindung ein. #
# Notwendige Variablen: #
# IF_WORLD=ppp0 oder eth1,... #
# IPADR_WORLD=a.b.c.d #
################################################
function firewall_internet_init_connection
{
echo Firewall: Regeln fuer die Internetverbindung ueber Interface
$IF_WORLD einrichten
#Von aussen gleich mal gefakte Absender verbieten:
$IPT -A INPUT -i $IF_WORLD -s 10.0.0.0/8 -j DROP
$IPT -A INPUT -i $IF_WORLD -s 172.16.0.0/12 -j DROP
$IPT -A INPUT -i $IF_WORLD -s 192.168.0.0/24 -j DROP
#ICMP-Protokoll (Ping) zulassen
$IPT -A INPUT -i $IF_WORLD -p icmp -j ACCEPT
#IGMP-Protokoll verbieten (Router)
$IPT -A INPUT -i $IF_WORLD -p igmp -j DROP
#Stehende Verbindungen reinlassen
$IPT -A INPUT -i $IF_WORLD -p tcp -m state --state
ESTABLISHED,RELATED -j ACCEPT
#Alles raus lassen
$IPT -A OUTPUT -o $IF_WORLD -j ACCEPT
#Stehende Verbindungen weiterleiten
$IPT -A FORWARD -i $IF_WORLD -p tcp -m state --state
ESTABLISHED,RELATED -j ACCEPT
}
################################################
# firewall_internet_allow_dns 'DNS_IP' #
# Laesst die UDP-Anworten der DNS-Server rein #
################################################
function firewall_internet_allow_dns
{
echo Firewall: Regeln fuer die DNS-Serveranworten von $1 einrichten
$IPT -A INPUT -i $IF_WORLD -p udp -d $IPADR_WORLD -s $1 --sport 53
-j ACCEPT
$IPT -A FORWARD -i $IF_WORLD -p udp -s $1 --sport 53 -j ACCEPT
}
#####################################################
# firewall_internet_allow_tcpservice 'service' #
# Erlaubt die angegebene TCP-Connection zum Server #
#####################################################
function firewall_internet_allow_tcpservice
{
echo Firewall: TCP-Service $1 am Server erlauben
$IPT -A INPUT -i $IF_WORLD -p tcp -d $IPADR_WORLD --dport $1 -j ACCEPT
}
#####################################################
# firewall_internet_allow_udpservice 'service' #
# Erlaubt die angegebene UDP-Connection zum Server #
#####################################################
function firewall_internet_allow_udpservice
{
echo Firewall: UDP-Service $1 am Server erlauben
$IPT -A INPUT -i $IF_WORLD -p udp -d $IPADR_WORLD --dport $1 -j ACCEPT
}
################################################
# firewall_nat_init 'network' #
# Initialisiert das NAT/Masquerading #
################################################
function firewall_nat_init
{
echo Firewall: NAT/MAsquerading fuer Netz $1 wird eingerichtet
$IPT -t nat -A POSTROUTING -s $1 -d \! $1 -o $IF_WORLD -j SNAT
--to-source $IPADR_WORLD
# Packet-Forwarding einschalten
echo 1 > /proc/sys/net/ipv4/ip_forward
}
#######################################################
# firewall_intranet_init 'interface' 'network' #
# Intranetzugriffe nach aussen und NAT freischalten #
#######################################################
function firewall_intranet_init
{
echo Firewall: Intranetzugriffe von Interface $1 aus Netzwerk $2
werden freigeschaltet
$IPT -A INPUT -i $1 -j ACCEPT
$IPT -A OUTPUT -o $1 -j ACCEPT
$IPT -A FORWARD -i $1 -j ACCEPT
$IPT -A FORWARD -i $IF_WORLD -m state --state ESTABLISHED,RELATED -j
ACCEPT
$IPT -t nat -A POSTROUTING -s $2 -d \! $2 -o $IF_WORLD -j SNAT
--to-source $IPADR_WORLD
# Packet-Forwarding einschalten
echo 1 > /proc/sys/net/ipv4/ip_forward
}
and the second file /etc/firewall/start-firewall.sh:
this is the file that i am usually starting with the up-command in
/etc/network/interfaces or in this case with pptp-adsl-dial-in a link in
/etc/ppp/ip-up.d
#!/bin/sh
#
#
# Tool zum erstellen der iptabels
# Author: Weilhartner Stefan
############################################
# 1. Initialisierung der Variablen #
############################################
echo Initialisierung der Variablen
IF_WORLD=ppp0
IPADR_WORLD= 1.2.3.4 <= this is the static-ip-address, that i got from
my isp
IF_ADSL=eth1
IPADR_ADSL=10.0.0.140
IPADR_ADSL_MODEM=10.0.0.138
IF_INTERNAL1=eth2
IPADR_INTERNAL1=192.168.101.254
NETADR_INTERNAL1=192.168.101.0/24
IPT=/sbin/iptables
# ip-addresses from the nameservers of my isp
IPADR_DNS1=195.34.133.21
IPADR_DNS2=195.34.133.22
##############################################
# Laden der Firewall-Hilfsfunktionen #
##############################################
. /etc/firewall/firewall-functions.sh
##############################################
# firewall_init_modules() #
# Initialisierung der Module NAT, FTP, IRC #
##############################################
firewall_init_modules
##############################################
# firewall_reset_policy_drop() #
# Loeschen aller Firewalleintraege und #
# Einrichten der Drop-Policy #
##############################################
firewall_reset_policy_drop
################################################
# firewall_enable_adsl_pptp_connection() #
# Erlaubt das Pingen und die PPTP-Verbindung #
# zu einem ADSL-Modem #
# Notwendige Variablen: #
# IF_ADSL=eth1 #
# IPADR_ADSL=10.0.0.140 #
# IPADR_ADSL_MODEM=10.0.0.138 #
################################################
firewall_enable_adsl_pptp_connection
################################################
# firewall_loopback_init_connection() #
# Richtet die Standardregeln fuer die #
# Loopbackverbindung ein. #
################################################
firewall_loopback_init_connection
################################################
# firewall_internet_init_connection() #
# Richtet die Standardregeln fuer die #
# Internetverbindung ein. #
# Notwendige Variablen: #
# IF_WORLD=ppp0 oder eth1,... #
# IPADR_WORLD=a.b.c.d #
################################################
firewall_internet_init_connection
################################################
# firewall_internet_allow_dns [DNS_IP] #
# Laesst die UDP-Anworten der DNS-Server rein #
################################################
firewall_internet_allow_dns $IPADR_DNS1
firewall_internet_allow_dns $IPADR_DNS2
firewall_internet_allow_tcpservice smtp
firewall_internet_allow_tcpservice 22
firewall_internet_allow_tcpservice 80
firewall_internet_allow_tcpservice 53
firewall_internet_allow_udpservice 53
################################################
# firewall_nat_init 'network' #
# Initialisiert das NAT/Masquerading #
################################################
#firewall_nat_init 192.168.0.0/16
#######################################################
# firewall_intranet_init 'interface' 'network' #
# Intranetzugriffe nach aussen und NAT freischalten #
#######################################################
firewall_intranet_init $IF_INTERNAL1 192.168.0.0/16
#######################################################
# firewall_logging #
#######################################################
$IPT -A INPUT -i $IF_ADSL -m limit --limit 3/minute -j LOG --log-prefix
"Illegale ADSL-Inputs:"
$IPT -A INPUT -m limit --limit 3/minute -j LOG --log-prefix "Illegale
Inputs:"
$IPT -A OUTPUT -m limit --limit 3/minute -j LOG --log-prefix "Illegale
Outputs:"
$IPT -A FORWARD -j LOG --log-prefix "Illegale Forwards:"
Yuri Rodrigues schrieb:
> Gentlemen,
>
> Thanks for helping. This is the result of the work after the
> suggestions implemented.
>
> Ansgar Wiechers, thank you for having looked so well my firewall. I
> tried to do everything you showed me. The firewall clean you sent to
> me some changes needed to be functional. But finally it was ready.
> Thank you. What do you think of the firewall now?
>
> Stefan Weihartner, its rules were essential for debugging so that I
> could make the firewall functional. Thank you.
>
> Andreas Kuglgruber, I am testing the fwbuilder and am enjoying very
> much. But I am still in a phase of studies on the subject of how to
> use the program.
>
> Someone of you (who attends the list) has a list of rules for the
> firewall to prevent the main attacks? It would be of great utility.
>
> Thank you,
>
> Yuri Rodrigues
>
>
--
To UNSUBSCRIBE, email to debian-firewall-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
No comments:
Post a Comment