<?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>Dan&#039;s Thoughts &#187; security</title>
	<atom:link href="http://danboykis.com/category/security/feed/" rel="self" type="application/rss+xml" />
	<link>http://danboykis.com</link>
	<description>Thinking somewhat carefully</description>
	<lastBuildDate>Thu, 12 Aug 2010 21:28:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1-alpha</generator>
		<item>
		<title>IpTables</title>
		<link>http://danboykis.com/2008/07/ipchains/</link>
		<comments>http://danboykis.com/2008/07/ipchains/#comments</comments>
		<pubDate>Sun, 06 Jul 2008 01:04:43 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://danboykis.com/?p=68</guid>
		<description><![CDATA[Task Write a iptables script that blocks everything except ping (icmp) and ssh (port 22), http (80) and https (443). Solution #!/bin/bash export ipt=/sbin/iptables $ipt -F #Flush all the rules one by one &#160; #Allow SSH $ipt -A INPUT -p tcp --dport 22 -j ACCEPT #Allow HTTP $ipt -A INPUT -p tcp --dport 80 -j [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-weight: bold;">Task</span></p>
<p>Write a iptables script that blocks everything except ping (icmp) and ssh (port 22), http (80) and https (443).</p>
<p><span style="font-weight: bold;">Solution</span></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">ipt</span>=<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>iptables
<span style="color: #007800;">$ipt</span> <span style="color: #660033;">-F</span> <span style="color: #666666; font-style: italic;">#Flush all the rules one by one</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Allow SSH</span>
<span style="color: #007800;">$ipt</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--dport</span> <span style="color: #000000;">22</span> <span style="color: #660033;">-j</span> ACCEPT
<span style="color: #666666; font-style: italic;">#Allow HTTP</span>
<span style="color: #007800;">$ipt</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--dport</span> <span style="color: #000000;">80</span> <span style="color: #660033;">-j</span> ACCEPT
<span style="color: #666666; font-style: italic;">#Allow HTTPS</span>
<span style="color: #007800;">$ipt</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--dport</span> <span style="color: #000000;">443</span> <span style="color: #660033;">-j</span> ACCEPT
&nbsp;
<span style="color: #666666; font-style: italic;"># Set default policies for INPUT, FORWARD and OUTPUT chains</span>
<span style="color: #007800;">$ipt</span> <span style="color: #660033;">-P</span> INPUT DROP
<span style="color: #007800;">$ipt</span> <span style="color: #660033;">-P</span> FORWARD DROP
<span style="color: #007800;">$ipt</span> <span style="color: #660033;">-P</span> OUTPUT ACCEPT
&nbsp;
<span style="color: #666666; font-style: italic;"># Set access for localhost</span>
<span style="color: #007800;">$ipt</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-i</span> lo <span style="color: #660033;">-j</span> ACCEPT
&nbsp;
<span style="color: #666666; font-style: italic;"># Accept packets belonging to established and related connections</span>
<span style="color: #007800;">$ipt</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-m</span> state <span style="color: #660033;">--state</span> ESTABLISHED,RELATED <span style="color: #660033;">-j</span> ACCEPT
&nbsp;
<span style="color: #666666; font-style: italic;">#Allow pings</span>
<span style="color: #666666; font-style: italic;">#Ping requires the ability to accept packets and send packet back out.</span>
<span style="color: #666666; font-style: italic;">#Ping is a layer 3,ICMP operation.</span>
<span style="color: #666666; font-style: italic;">#In order to allow it our protocol now becomes icmp instead of tcp.</span>
<span style="color: #666666; font-style: italic;">#Ping packets are able to be received.</span>
<span style="color: #007800;">$ipt</span> <span style="color: #660033;">-I</span> INPUT <span style="color: #660033;">-p</span> icmp <span style="color: #660033;">-j</span> ACCEPT
&nbsp;
<span style="color: #666666; font-style: italic;">#Ping packets are able to be sent</span>
<span style="color: #007800;">$ipt</span> <span style="color: #660033;">-I</span> OUTPUT <span style="color: #660033;">-p</span> icmp <span style="color: #660033;">-j</span> ACCEPT
&nbsp;
<span style="color: #666666; font-style: italic;"># List rules</span>
<span style="color: #007800;">$ipt</span> <span style="color: #660033;">-L</span> <span style="color: #660033;">-v</span></pre></div></div>

<p>-m says load a module state which allows access to the connection tracking state of the packets<br />
--state precedes a comma separated list of the connection states to match. In this case it's NEW.<br />
NEW the packet has started a new connection or a connection that has not seen packets going in both directions<br />
-m tcp load the tcp module (just like we loaded the state module) this module allows us extra functionality with tcp<br />
-p tcp specifies protocol, in my case TCP<br />
--dport 22 feature provided by the -m tcp module, in this case I want the rule to be applicable to port 22 (ssh).<br />
-j ACCEPT means the results of this chain is to accept the packets. (-j specifies the target of the rule if the packet matches the rule. If I said -j DROP we would block all traffic to port 22).</p>
]]></content:encoded>
			<wfw:commentRss>http://danboykis.com/2008/07/ipchains/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
