<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Michael Kolb Fulda&#187; Web Engineering</title>
	<atom:link href="http://www.michael-kolb.co.uk/tag/script/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.michael-kolb.co.uk</link>
	<description>Web Engineering</description>
	<lastBuildDate>Mon, 01 Mar 2010 05:58:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Scripting a firewall with iptables</title>
		<link>http://www.michael-kolb.co.uk/linuxdaemons/scripting-a-firewall-with-iptables/</link>
		<comments>http://www.michael-kolb.co.uk/linuxdaemons/scripting-a-firewall-with-iptables/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 17:42:59 +0000</pubDate>
		<dc:creator>mk_michael</dc:creator>
				<category><![CDATA[Linux Daemons]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://www.michael-kolb.co.uk/?p=61</guid>
		<description><![CDATA[IPTables is a text based frontend for the ipchains kernel module. The ipchains support is compiled into most kernels of todays distributions. With iptables you don&#8217;t have to use a graphical tool. So it&#8217;s very suitable for scritpting.
First thing to do is installing neccessary packages, for Ubuntu / Debian:
apt-get update
apt-get install iptables
When the package is [...]]]></description>
			<content:encoded><![CDATA[<p>IPTables is a text based frontend for the ipchains kernel module. The ipchains support is compiled into most kernels of todays distributions. With iptables you don&#8217;t have to use a graphical tool. So it&#8217;s very suitable for <a href="http://www.michael-kolb.co.uk/tag/script/">scritpting</a>.</p>
<p>First thing to do is installing neccessary packages, for Ubuntu / Debian:<br />
<code>apt-get update<br />
apt-get install iptables</code></p>
<p>When the package is installed, create a file inside /etc/init.d/ called iptables (or whatever you want). Copy paste the lines below into.</p>
<p><span id="more-61"></span></p>
<p><code><br />
########################################<br />
#!/bin/sh<br />
# iptables start/stop script<br />
# location /etc/init.d/iptables<br />
########################################</p>
<p>PATH=/bin:/sbin:/usr/bin:/usr/sbin<br />
TABLEBIN=/sbin/iptables</p>
<p>#<br />
# INPUT - SERVICES<br />
#<br />
IN_TCP_SERVICES="80 443 25 110 995 21" # http https smtp pop3 SSL/TLS ftp<br />
IN_UDP_SERVICES=""</p>
<p>#<br />
# OUTPUT - SERVICES<br />
#<br />
OUT_TCP_SERVICES="80 443 25 43 2703 21" # http https smtp whois razor ftp<br />
OUT_UDP_SERVICES="53 123" # DNS ntp</p>
<p>if ! [ -x $TABLEBIN ]; then<br />
echo "IPTABLES does not exists"<br />
exit 0<br />
fi<br />
</code></p>
<p>With the INPUT/OUTPUT section, you&#8217;re able to define your services like http or smtp for remote access. Chmod file to 755 and ensure root to be the owner.</p>
<p><code><br />
chmod 755 /etc/init.d/iptables<br />
chown root:root /etc/init.d/iptables</code></p>
<p>Check and try the script. If iptables isn&#8217;t installed correctly, you wil get an error statement. Next step, calling iptables statements. Append following lines into the iptables script:</p>
<p><code><br />
#<br />
# start firewall<br />
#<br />
fw_start ()<br />
{<br />
# deny all<br />
$TABLEBIN -P INPUT DROP<br />
$TABLEBIN -P OUTPUT DROP<br />
# common<br />
$TABLEBIN -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT<br />
$TABLEBIN -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT</p>
<p># allow mysql from special IP adresses<br />
$TABLEBIN -A INPUT -p tcp -s  xxx.xxx.xxx.xxx --dport 3306 -j ACCEPT # MYSQL for clients<br />
$TABLEBIN -A OUTPUT -p tcp -s xxx.xxx.xxx.xxx --dport 3306 -j ACCEPT # MYSQL for clients</p>
<p># allow always<br />
$TABLEBIN -A INPUT -p tcp --dport 2222 -j ACCEPT # ssh on port 2233<br />
$TABLEBIN -A INPUT -i lo -j ACCEPT<br />
$TABLEBIN -I INPUT -s 127.0.0.1 -j ACCEPT<br />
$TABLEBIN -I OUTPUT -s 127.0.0.1 -j ACCEPT</p>
<p># deny special bad ip adresses<br />
$TABLEBIN -I INPUT -s xxx.xxx.xxx.xxx -j DROP # deny, because of ...<br />
$TABLEBIN -I INPUT -s xxx.xxx.xxx.xxx -j DROP # deny, because of ...<br />
$TABLEBIN -I INPUT -s xxx.xxx.xxx.xxx -j DROP # deny, because of ...</p>
<p># INPUTS<br />
if [ -n "$IN_TCP_SERVICES" ] ; then<br />
for PORT in $IN_TCP_SERVICES; do<br />
$TABLEBIN -A INPUT -p tcp --dport ${PORT} -j ACCEPT<br />
done<br />
fi</p>
<p>if [ -n "$IN_UDP_SERVICES" ] ; then<br />
for PORT in $IN_UDP_SERVICES; do<br />
$TABLEBIN -A INPUT -p udp --dport ${PORT} -j ACCEPT<br />
done<br />
fi</p>
<p># OUTPUTS<br />
if [ -n "$OUT_TCP_SERVICES" ] ; then<br />
for PORT in $OUT_TCP_SERVICES; do<br />
$TABLEBIN -A OUTPUT -p tcp --dport ${PORT} -j ACCEPT<br />
done<br />
fi</p>
<p>if [ -n "$OUT_UDP_SERVICES" ] ; then<br />
for PORT in $OUT_UDP_SERVICES; do<br />
$TABLEBIN -A OUTPUT -p udp --dport ${PORT} -j ACCEPT<br />
done<br />
fi<br />
}<br />
</code></p>
<p>The <a href="http://www.michael-kolb.co.uk/linuxdaemons/changing-ssh-port/" target="_top" alt="ssh port">ssh port</a> is binded to another one, because of improving security.<br />
The last step to complete the script is the stopping call:</p>
<p><code><br />
#<br />
# stop firewall<br />
#<br />
fw_stop ()<br />
{<br />
$TABLEBIN -F<br />
$TABLEBIN -t nat -F<br />
$TABLEBIN -t mangle -F<br />
$TABLEBIN -P INPUT ACCEPT<br />
$TABLEBIN -P FORWARD ACCEPT<br />
$TABLEBIN -P OUTPUT ACCEPT<br />
}</p>
<p>case "$1" in<br />
start)<br />
echo -n "starting firewall.."<br />
fw_stop<br />
fw_start<br />
echo "done."<br />
;;<br />
stop)<br />
echo -n "stopping firewall.."<br />
fw_stop<br />
echo "done."<br />
;;<br />
*)<br />
echo "Usage: $0 {start|stop}"<br />
exit 1<br />
;;<br />
esac<br />
exit 0<br />
</code></p>
<p>Now you start your iptables script with<br />
<strong>/etc/init.d/iptables start<br />
/etc/init.d/iptables stop<br />
</strong></p>
<p>To start the <a href="http://www.michael-kolb.co.uk/tag/script/">script</a> on booting process, put into the runlevel configuration (Debian/Ubuntu):</p>
<p><code><br />
update-rc.d timWall start 40 S . stop 89 0 6 .<br />
</code></p>
<p>Be careful!! Test it before manually, because you could block yourself out of the system. Firewall architecture with a DMZ:</p>
<p><img class="alignnone size-medium wp-image-365" title="reseau-dmz" src="http://www.michael-kolb.co.uk/wp-content/uploads/2008/11/reseau-dmz.jpg" alt="Firewall with DMZ" width="400" height="400" /><br />
Source: <a href="http://www.basic-security.com/MEDIA/US/RESEAU-DMZ.jpg" target="_blank">http://www.basic-security.com/MEDIA/US/RESEAU-DMZ.jpg</a></p>
<div class="download_banner">
Note: There is a file embedded within this post, please visit this post to download the file.</div>
<p><strong>Useful links:</strong></p>
<ul>
<li><em>(iptables &#8211; Die Firewall des Kernel 2.4 von Wolfgang Kinkeldei)</em><br />
<a href="http://www.pro-linux.de/NB3/artikel/2/print/761/6,6iptables-die-firewall-des-kernels-24.html" target="_blank">http://www.pro-linux.de/NB3/artikel/2/print/761/6,6iptables-die-firewall-des-kernels-24.html</a></li>
<li> <em>(iptables script generator from Tarjei Mandt)</em><br />
<a href="http://www.mista.nu/iptables/" target="_blank">http://www.mista.nu/iptables/</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.michael-kolb.co.uk/linuxdaemons/scripting-a-firewall-with-iptables/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
