<?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>SPDLab &#187; port knocking</title>
	<atom:link href="http://spdlab.net/tag/port-knocking/feed" rel="self" type="application/rss+xml" />
	<link>http://spdlab.net</link>
	<description></description>
	<lastBuildDate>Mon, 31 May 2010 15:16:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0-RC1</generator>
		<item>
		<title>Iptables port knocking &#8211; single port used</title>
		<link>http://spdlab.net/iptables-port-knocking-single-port-used</link>
		<comments>http://spdlab.net/iptables-port-knocking-single-port-used#comments</comments>
		<pubDate>Mon, 15 Oct 2007 15:06:40 +0000</pubDate>
		<dc:creator>SPDLab</dc:creator>
				<category><![CDATA[Tips & tricks]]></category>
		<category><![CDATA[firewall]]></category>
		<category><![CDATA[iptables]]></category>
		<category><![CDATA[port knocking]]></category>

		<guid isPermaLink="false">http://78.46.87.37/~spdlab/?p=6</guid>
		<description><![CDATA[There are many things to make your computer more secure and sometimes something as simple as port knocking can do the trick. Port knocking as technique is used for a long time now and many implementations are already out there although for my specific use none of them seemed to be appropriate. For complete listing [...]]]></description>
			<content:encoded><![CDATA[<p>There are many things to make your computer more secure and sometimes  something as simple as port knocking can do the trick.</p>
<p>Port knocking as technique is used for a long time now and many  implementations are already out there although for my specific use none  of them seemed to be appropriate.<br />
For complete listing of these implementations please visit <a href="http://www.portknocking.org">portknocking.org</a> website.  There you can also find a complete explanation of the methods as well as  recommendations of use.<span id="more-6"></span></p>
<p>Requirements (or why no other port knocking solution worked for me):<br />
Knocking and service must share the same port (for example &#8211; let&#8217;s  suppose you have a computer behind some router or firewall and have no  access to this device; all you do have is one forwarded port to your  computer &#8211; let&#8217;s say for ssh as most commonly used for system  administration). I guess you can see now where the problem is with other  implementations of port knocking: lack of ports that can be used for  knocking.<br />
Also, as I prefer a portable solution that is deployable anywhere  (without any additional software clients) I want to make it possible so  that it can be used with simple tools available on any system.</p>
<p>I&#8217;m trying to make this as simple as possible so all I&#8217;ll use is  iptables. Why? Imagine a following scenario: something gets broken on a  port knocking script/program/server and it&#8217;s not listening to knocks  anymore. There comes a need for a visit to the computer to fix this  directly as our ssh connection don&#8217;t let us do it remotely anymore. If  iptables gets broken there is no firewall anyway so you&#8217;ll have a  completely different set of problems, at least connecting to fix it  wouldn&#8217;t be one of them.</p>
<p>One thing that is needed beside plain iptables is their recent module  &#8211; either compiled in or as separate module. Few words about recent  module parameters used here:<br />
&#8211;name [name] defines a name of the list<br />
&#8211;set puts the source IP matched in a list defined by &#8211;name (if name is  not defined all matches go in some default list)<br />
&#8211;rcheck does a rulecheck on a IPs from list<br />
&#8211;seconds [seconds] is used to filter out the list for IPs seen within  the indicated number of seconds<br />
&#8211;hitcount [hits] filters out the list for minimum number of packets  from some IP, can be combined with seconds to make the filtering more  accurate</p>
<p>OK, so we&#8217;ll start with a standard firewall passing new ssh  connections (I&#8217;ll use the iptables-save format as it&#8217;s easier to tweak  the firewall and set comments though you can make this also with usual  iptables commands) &#8211; port 22 is passed thru without any additional  checks:</p>
<p><span style="color: #808080;"> # pass established connections<br />
-A INPUT -m state &#8211;state ESTABLISHED,RELATED -j ACCEPT<br />
# pass new ssh connection<br />
-A INPUT -m state &#8211;state NEW -m tcp -p tcp &#8211;dport 22 -j ACCEPT</span></p>
<p><span style="color: #333333;">For starters we&#8217;ll need to start  listening on this port:</span></p>
<p><span style="color: #808080;"><br />
# pass established connections<br />
-A INPUT -m state &#8211;state ESTABLISHED,RELATED -j ACCEPT<br />
# listen to new connections on ssh port<br />
-A INPUT -m state &#8211;state NEW -m tcp -p tcp &#8211;dport 22 -m recent &#8211;set  &#8211;name sshknock -j LOG &#8220;knock knock: &#8221;<br />
# pass new ssh connection<br />
-A INPUT -m state &#8211;state NEW -m tcp -p tcp &#8211;dport 22 -j ACCEPT</span></p>
<p>Now, after initiating a ssh connection we have a entry in  /proc/net/ip_recent/sshknock list showing us the address and main  parameters of the packet. As connection was initiated one rule later and  all later packets were are picked up by ESTABLISHED state rule that&#8217;s  all we&#8217;re going to get for now.</p>
<p>So let&#8217;s start filling in this list with new packets by dropping them  instead of accepting and also enabling the port knocking mechanism:</p>
<p><span style="color: #808080;"><br />
# pass established connections<br />
-A INPUT -m state &#8211;state ESTABLISHED,RELATED -j ACCEPT<br />
# if at least 2 new packets inside 5 seconds open ssh port<br />
-A INPUT -m state &#8211;state NEW -m tcp -p tcp &#8211;dport 22 -m &#8211;rcheck  &#8211;name sshknock &#8211;hitcount 2 &#8211;seconds 5 -j ACCEPT<br />
# listen to new connections on ssh port<br />
-A INPUT -m state &#8211;state NEW -m tcp -p tcp &#8211;dport 22 -m recent &#8211;set  &#8211;name sshknock -j LOG &#8220;knock knock: &#8221;<br />
# pass new ssh connection<br />
-A INPUT -m state &#8211;state NEW -m tcp -p tcp &#8211;dport 22 -j DROP</span></p>
<p>We might stop at this as we have achieved our goal &#8211; 2 packet knock  in a short amount of time is needed to open the port which will go under  a radar for most port sniffers marking it as filtered port (of course,  tweaking with more knocks and probably larger timespan is recommended to  be more sure).<br />
But, as we have a very well known port here which is targeted quite  often let&#8217;s add an additional twist to this. By simply bombing the ssh  port it will open without any defense at all.</p>
<p><span style="color: #808080;"> # pass established connections<br />
-A INPUT -m state &#8211;state ESTABLISHED,RELATED -j ACCEPT<br />
# if more then 2 new packets inside 3 seconds arrive drop<br />
-A INPUT -m state &#8211;state NEW -m tcp -p tcp &#8211;dport 22 -m &#8211;rcheck  &#8211;name sshknock &#8211;hitcount 2 &#8211;seconds 3 -j ACCEPT<br />
# if at least 2 new packets inside 5 seconds open ssh port<br />
-A INPUT -m state &#8211;state NEW -m tcp -p tcp &#8211;dport 22 -m &#8211;rcheck  &#8211;name sshknock &#8211;hitcount 2 &#8211;seconds 5 -j ACCEPT<br />
# listen to new connections on ssh port<br />
-A INPUT -m state &#8211;state NEW -m tcp -p tcp &#8211;dport 22 -m recent &#8211;set  &#8211;name sshknock -j LOG &#8220;knock knock: &#8221;<br />
# pass new ssh connection<br />
-A INPUT -m state &#8211;state NEW -m tcp -p tcp &#8211;dport 22 -j DROP</span></p>
<p>Another line to firewall is added &#8211; but what does it actually do?<br />
If someone attempts to flood the port with packets it will refuse to  open; and instead of only having number and time of knocks defined also  rate at which these knocks must be employed is set &#8211; sending them at too  high or too low frequency has no effect on opening the port &#8211; exactly 2  knocks is needed for 3rd packet to get to ssh server inside 3 &#8211; 5  seconds of the first knock.</p>
<p>So there it is, port knocking with only a few additional rules to  iptables. Knocking packets can be generated with a simple telnet or ssh  client. It most certainly isn&#8217;t the complete solution for the security  and maybe isn&#8217;t as secure as some other port knocking solutions  (additional encryption of the knocks, more complicated schemes of  knocking) but it&#8217;s easily deployable and rises the security of the  system significantly.</p>
]]></content:encoded>
			<wfw:commentRss>http://spdlab.net/iptables-port-knocking-single-port-used/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
