<?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; Tips &amp; tricks</title>
	<atom:link href="http://spdlab.net/category/tips-tricks/feed" rel="self" type="application/rss+xml" />
	<link>http://spdlab.net</link>
	<description></description>
	<lastBuildDate>Mon, 06 Sep 2010 10:31:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Postgresql asynchronous replication &#8211; londiste + autossh</title>
		<link>http://spdlab.net/postgresql-asynchronous-replication-londiste-autossh</link>
		<comments>http://spdlab.net/postgresql-asynchronous-replication-londiste-autossh#comments</comments>
		<pubDate>Wed, 01 Apr 2009 15:10:59 +0000</pubDate>
		<dc:creator>SPDLab</dc:creator>
				<category><![CDATA[Tips & tricks]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[replication]]></category>
		<category><![CDATA[skytools]]></category>

		<guid isPermaLink="false">http://78.46.87.37/~spdlab/?p=13</guid>
		<description><![CDATA[As a part of the project, I needed to make replication of postgresql database to be available on different server for analization use. Main database is being filled constantly with data while the replicated database needs to be accessible by several services in read-only mode on different segment of the network. Because of that and [...]]]></description>
			<content:encoded><![CDATA[<p>As a part of the project, I needed to make replication of postgresql  database to be available on different server for analization use.</p>
<p>Main database is being filled constantly with data while the  replicated database needs to be accessible by several services in  read-only mode on different segment of the network. Because of that and  need of local backup direct connection of those services to main  database wasn&#8217;t an option. So, plan for postgresql replication was  needed. Also, as these two servers aren&#8217;t connected by secure network or  any kind of VPN somehow they needed to have communication channel  between them for transfering of the replication data solved &#8211; in a  secure and least obtrusive way possible. Data status on the slave side  doesn&#8217;t affect main server and there is possiblity of temporary  out-of-syncs (in case of some network failure) which is good enough for  the requirements of the project so replication can be done  asynchonously.<span id="more-13"></span></p>
<p>Unfortunately, postgresql (still) doesn&#8217;t have native replication  mechanisms so some help from postgresql replication projects was needed.  Although <a href="http://pgfoundry.org/" target="_blank">pgFoundry</a> has quite  a few replication projects listed only few were a real option (working  with current versions of postgresql, no need to patch production  databases, project in relatively stable state, simple master-slave  asycnhronous replication). In the end <a href="https://developer.skype.com/SkypeGarage/DbProjects/SkyTools" target="_blank">SkyTools</a>, an open sourced project by the skype  developers (seems like they are using this also for replication of their  postgresql cluster) was chosen for the job.</p>
<p>As for obtaining secure communication between the servers part &#8211; good  ol&#8217; ssh is used, one port needed, one-way connection (from slave  replication server to master) to keep things secure as much as possible  on the replicated server.</p>
<p>So, with tools chosen here is how it&#8217;s all put together, we&#8217;ll use  some abbreviations to keep things simple:</p>
<ul>
<li>MasterIP  &#8211; IP of the master postgresql server</li>
<li>MPort &#8211; port of the postgresql server on master server</li>
<li>MDbase &#8211; master database name</li>
<li>SPort &#8211; port of the postgresql server on slave</li>
<li>MSPort &#8211; local tunneled port for postgresql master server on the  slave</li>
<li>SDbase &#8211; slave database name</li>
</ul>
<h2>Connection part</h2>
<p>For the use, simple tunneling of the master postgresql port to slave  with <span style="color: #808080;">ssh -L MSPort:127.0.0.1:MPort  MasterIP</span> fulfills our needs. Although, as things go, once  connection is broken it&#8217;s bye-bye replication so we need to make sure  this connection stays up and keeps the tunnel open for the data.</p>
<p>So, meet <a href="http://www.harding.motd.ca/autossh/" target="_blank">autossh</a>,  a ssh connection monitoring program. After install (either thru apt/yum  or manual compile) you just need to start it, this thing doesn&#8217;t have  much features but it does what it&#8217;s intended for remarkably well. It  doesn&#8217;t just monitor the ssh process, but also checks for traffic on the  connection and checks for the ssh connectivity (so it doesn&#8217;t try to  repeatedly login to server in case of wrong credentials or network  failure). Traffic checking on the connection is made with few additional  ports, their range is set by the -M parameter.</p>
<p>So to start the tunnel and keep it open (use of ssh authentication  keys is recommended here):</p>
<p><span style="color: #808080;"><br />
autossh -M 20000 -f ssh -L MSPort:127.0.0.1:MPort MasterIP<br />
</span></p>
<h2>Preparation of the postgresql server(s)</h2>
<p>As already stated, very few modifications need to be done on the  database servers itself, mainly it&#8217;s adding a few python functions and  tables needed for skytools&#8230;</p>
<p>Install the SkyTools from <a href="http://pgfoundry.org/projects/skytools/" target="_blank">pgFoundry  page</a> (simple <span style="color: #808080;">./configure &amp;&amp;  make &amp;&amp; make install</span> usually does the trick, although  some dependencies might be needed; like postgresql-devel and  python-psycopg2 packages). Additional note for the RedHat users:  skytools modules by default get installed in &#8220;/usr/local/lib/python..&#8221;,  while the system expects them at &#8220;/usr/lib/python&#8230;&#8221; &#8211; symlink or  addition to path will solve this without much problems.</p>
<p>Slave server should also contain the same schema for the database  that needs to be replicated as it&#8217;s existent on master server.</p>
<h2>SkyTools configuration</h2>
<p>SkyTools need to be installed on both master and slave (theoreticaly  it could work from slave only but it would probably be too slow for this  type of connection between the servers), and config files shall be  created by hand; to keep things neat and clean we make /etc/skytools  directory on both servers and make config files:</p>
<h3>On master server</h3>
<p>/etc/skytools/ticker.ini</p>
<p>[pgqadm]<br />
job_name = pgqadm_ticker<br />
db = dbname=MDbase port=MPort host=127.0.0.1<br />
# how often to run maintenance [seconds]<br />
maint_delay = 600<br />
# how often to check for activity [seconds]<br />
loop_delay = 0.1<br />
logfile = /tmp/%(job_name)s.log<br />
pidfile = /tmp/%(job_name)s.pid</p>
<h3>On slave server</h3>
<p>/etc/skytools/conf.ini</p>
<p>[londiste]<br />
job_name = SPDLABreplicate_dbase<br />
provider_db = dbname=MDbase port=MSPort host=127.0.0.1<br />
subscriber_db = dbname=SDbase port=SPort host=127.0.0.1<br />
# it will be used as sql ident so no dots/spaces<br />
pgq_queue_name = SPDLABreplicator<br />
logfile = /tmp/%(job_name)s.log<br />
pidfile = /tmp/%(job_name)s.pid</p>
<h2>Database preparation and replication start</h2>
<p>After this is done, we can start creating the neccessary  modifications to the databases and start the replication (commands  should be executed by the postgresql superuser, each command states at  which server):</p>
<p><strong>ON MASTER </strong>- <span style="color: #808080;">pgqadm.py  /etc/skytools/ticker.ini install</span> (installing the ticker sql on master  database)</p>
<p><strong>ON SLAVE</strong> &#8211; <span style="color: #808080;">londiste.py  /etc/skytools/conf.ini provider install</span> (installing the londiste provider  sql)</p>
<p><strong>ON SLAVE</strong> &#8211; <span style="color: #808080;">londiste.py  /etc/skytools/conf.ini subscriber install</span> (installing the londiste  subscriber sql)</p>
<p><strong>ON MASTER</strong> &#8211; <span style="color: #808080;">pgqadm.py  /etc/skytools/ticker.ini register SPDLABreplicator SPDLABreplicate_dbase</span> (registering  the consumer for ticker)</p>
<p><strong>ON MASTER</strong> &#8211; <span style="color: #808080;">pgqadm.py -d  /etc/skytools/ticker.ini ticker</span> (starting the ticker daemon; can be  stopped with -s switch)</p>
<p><strong>ON SLAVE</strong> &#8211; <span style="color: #808080;">londiste.py  /etc/skytools/conf.ini provider add &#8211;all</span> (adding all tables on provider  for replication)</p>
<p><strong>ON SLAVE</strong> &#8211; <span style="color: #808080;">londiste.py  /etc/skytools/conf.ini subscriber add &#8211;all</span> (adding all  tables on subscriber for replication)</p>
<p><strong>ON SLAVE</strong> &#8211; <span style="color: #808080;">londiste.py -d  /etc/skytools/conf.ini replay</span> (starting the londiste daemon, can be  stopped with -s switch)</p>
<p>Well, after this replication should have started and slave would be  in sync and kept in that state. In case that&#8217;s not the so, check the log  files defined in config files and use these commands for debugging:</p>
<p><strong>ON MASTER</strong> &#8211; <span style="color: #808080;">pgqadm.py  /etc/skytools/ticker.ini status</span> (checking the status of the ticker and  updates)</p>
<p><strong>ON SLAVE</strong> &#8211; <span style="color: #808080;">londiste.py  /etc/skytools/conf.ini subscriber tables</span> (checking the state of the tables)</p>
<h2>Additional notes</h2>
<p>SkyTools seems to be quite undocumented regarding these procedures of  installing and maintaing sync for replication and steps explained above  seem to be working fine for me, while you might get problems somewhere  along the way; so it&#8217;s good to keep an eye on the <a href="http://skytools.projects.postgresql.org/doc/" target="_blank">original  documentation</a> when some additional help is needed&#8230;</p>
<h2>Additional notes 2</h2>
<p>Sometimes things can go wrong and replication stops &#8211; in order to cope with that try to:</p>
<p>Restart the service <strong>ON SLAVE</strong> &#8211; <span style="color: #808080;">londiste.py  /etc/skytools/conf.ini -r</span></p>
<p>Still no progress?</p>
<p>Try to make a full new copy:</p>
<p><strong>ON SLAVE &#8211; </strong><span style="color: #808080;">londiste.py /etc/skytools/conf.ini subscriber remove &#8211;all</span></p>
<p><strong>ON SLAVE &#8211; </strong><span style="color: #808080;">londiste.py /etc/skytools/conf.ini subscriber add &#8211;all</span></p>
<p><strong>ON SLAVE &#8211; </strong>disable constraints, triggers and such which could slow down the database</p>
<p><strong>ON SLAVE &#8211; </strong><span style="color: #808080;">londiste.py /etc/skytools/conf.ini copy</span> (if not possible try with &#8211;reset option to start as completely new copy with truncate, replay option can also be used but gives less info on the process)</p>
<p><strong>ON SLAVE -</strong> In the end re-add the constraints on tables</p>
<p>When it&#8217;s really stuck, you can even try with dropping the whole database on subscriber end and reinstall and re-add subscriber stuff, along with initial copy (in the original docs it suggests you make initial copy before adding the tables for subscriber).</p>
]]></content:encoded>
			<wfw:commentRss>http://spdlab.net/postgresql-asynchronous-replication-londiste-autossh/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SSH &#8211; tips and tricks</title>
		<link>http://spdlab.net/ssh-tips-and-tricks</link>
		<comments>http://spdlab.net/ssh-tips-and-tricks#comments</comments>
		<pubDate>Thu, 25 Oct 2007 15:08:22 +0000</pubDate>
		<dc:creator>SPDLab</dc:creator>
				<category><![CDATA[Tips & tricks]]></category>
		<category><![CDATA[forwarding]]></category>
		<category><![CDATA[socks proxy]]></category>
		<category><![CDATA[ssh]]></category>

		<guid isPermaLink="false">http://78.46.87.37/~spdlab/?p=11</guid>
		<description><![CDATA[Many people tend to think of SSH as a secure substitution for insecure telnet remote administration. While basically that is it&#8217;s main purpose (SSH is short for secure shell) what you can do with this tool is definitely not limited by only one task&#8230; What will be explained here is mostly about open source implementation [...]]]></description>
			<content:encoded><![CDATA[<p>Many people tend to think of SSH as a secure substitution for  insecure telnet remote administration. While basically that is it&#8217;s main  purpose (SSH is short for secure shell) what you can do with this tool  is definitely not limited by only one task&#8230;<span id="more-11"></span></p>
<p>What will be explained here is mostly about open source  implementation of ssh &#8211; OpenSSH, so some things might be different if  you&#8217;re using something else.</p>
<p>Let&#8217;s first see about SSH security &#8211; is it really so secure? While  it&#8217;s hard to be exact on this issue one thing is certain &#8211; if you&#8217;re  using SSH protocol use only it&#8217;s version 2. SSH protocol version 1 is  known to have exploits for some time now and it is definitely not  advisable to use it. Sure, it&#8217;s not as bad as connecting with telnet,  but any reasonably well configured SSH server nowdays will offer you a  SSHv2 connection and even might be limited to this option only.</p>
<p>Some specific issues with SSH &#8211; if you&#8217;re new to it some things might  confuse you. For example &#8211; when logging in a system that you&#8217;ve never  logged in before it will prompt you for a authenticity check and display  you a key fingerprint.</p>
<p><span style="color: #808080;"><br />
The authenticity of host &#8216;somerandomserver.com (0.0.0.0)&#8217; can&#8217;t be  established.<br />
DSA key fingerprint is 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00.<br />
Are you sure you want to continue connecting (yes/no)?</span></p>
<p>Huh? What happens here &#8211; to prevent man-in-the-middle attacks you  must be sure that you are always communicating with the right system or  you might be sending passwords and other confidential information to  anyone who impersonates this system. IP addresses can be spoofed, DNS  entries are easily changed ARP poisoning can redirect your traffic and  so on&#8230; Once you&#8217;ve confirmed that this is exactly the system that you  want to login to its key will be memorized for later use and you wont  see it again &#8230;unless somebody tries to fool you &#8211; then you&#8217;ll get a  nice big warning about not matching the hosts keys. Needless to say, if  you are not absolutely sure that system you are logging to hasn&#8217;t  changed it&#8217;s keys stay away from further login!</p>
<p><span style="color: #808080;">@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@<br />
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @<br />
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@<br />
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!<br />
Someone could be eavesdropping on you right now (man-in-the-middle  attack)!<br />
It is also possible that the DSA host key has just been changed.<br />
The fingerprint for the DSA key sent by the remote host is<br />
00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:01.<br />
Please contact your system administrator.</span></p>
<p>So, what exactly is it that is possible to achieve with this tool, or  should I say toolbox?</p>
<h3>Remote logins</h3>
<p>Basic use of ssh client is<br />
<em> ssh hostname</em></p>
<p>Which will attempt login on host &#8220;hostname&#8221; on standard SSH port 22.  After successful login you can work normally as with console or telnet.<br />
Additional interesting basic options:<br />
-p : port to which you are connecting to (default 22)<br />
-l : login with different username then default (default is current  users username; this can be also accomplished as with username@host  syntax)<br />
-q and -v : first is for no messages at all (quiet) and second is for  verbose debugging messaging<br />
-t and -T : disabling and enabling pseudo-tty generation at servers end  (default enabled) &#8211; useful if you&#8217;re just making port forwarding<br />
-C : request compression of the transmission data (useful for slow  connections)<br />
-o : define a option value so values from configuration file can be  overridden<br />
-1 or -2 : try only version 1 or 2 of the protocol</p>
<h3>SFTP/SCP</h3>
<p>Secure alternatives for commonly used (unsecure) protocols &#8211; FTP and  rcp (remote copy)<br />
scp and sftp accept most of the normal ssh client&#8217;s options (attention:  port is here defined with -P unlike -p with ssh) but are used only for  file transfers. scp for single file transfer and sftp for multiple file  transfers.<br />
scp use:<br />
scp ~/somefile hostname:/tmp/somefile</p>
<p>This will copy &#8220;somefile&#8221; from your home directory to &#8220;/tmp/somefile&#8221;  on remote ssh server. If you ommit path on remote server files will be  copied to your home directory on server.<br />
Retrieval of files from remote server isn&#8217;t much harder either:<br />
scp hostname:/tmp/somefile ~/somefile_again</p>
<h3>Passwordless logins</h3>
<p>More appropriate for automated tasks but might help you if you tend  to forget passwords. Basically have a private/public key with which you  are authenticated on server. Of course, it&#8217;s more secure if you have  password also setup on your private key &#8211; otherwise anyone who steels  your private key can login into your servers. Yikes.<br />
By default when connecting your private keys will be searched within  ~/.ssh/ folder and are called id_dsa || id_rsa (depending on encryption  used), but private key file location can easily be defined with -i  option so you can use different keys for different servers:<br />
ssh -i ~/secretfiles/mykey_at_hostname hostname</p>
<p>Of course the same applies for scp and sftp&#8230;</p>
<h3>Remote command execution</h3>
<p>Who says you even need a terminal?<br />
ssh hostname df</p>
<p>If system command is added as parameter of the ssh connection command  (in this example it&#8217;s df) after successful login it will be executed  and logout happens as soon as it&#8217;s execution is finished. Useful for  checking on some services; or in this case &#8211; file system capacity  status.</p>
<h3>Port forwarding (poor man&#8217;s VPN)</h3>
<p>Forwarding a port thru remote system &#8211; weather you need it to secure  traffic over not so secure link or connect to the service which is  behind the firewall &#8211; this one is all time favorite ssh trick<br />
So, presumably you need to check on some webpage which is available only  inside LAN of your network from internet and you have only ssh access  to server there. Imposible? Not with ssh:<br />
ssh -L 8080:webserver:80 hostname<br />
where 8080 is port on your client ssh computer, webserver is LAN address  of the webserver in LAN and 80 is webservers http port</p>
<p>Now type in http://localhost:8080 in your browser and work like  you&#8217;re there&#8230; As you can see -L is a very powerful option and it does  forwards one port from some host (ports for localhost and remote hosts  port can be set up as needed) to your local computer.<br />
Additional use of this is anonymous surfing (or some other form of  activity) &#8211; ports can be forwarded from whichever hosts your ssh server  has connectivity to and port forwarding has a very interesting feature:  all traffic made to that remote host seems like it originates only from  ssh server and without any trace of your local computers address.</p>
<h3>Reverse (remote) port forwarding</h3>
<p>Forwarding a port on a local system to be available on remote system<br />
Similar to port forwarding, but of reverse nature like it&#8217;s name says &#8211;  expose your local network hosts to anybody who has access to remote  servers forwarded port. Use with caution &#8211; you never know who&#8217;s  listening on the other end&#8230;<br />
ssh -R 8080:webserver:80 hostname<br />
where 8080 is port on remote ssh server, webserver is LAN address of the  webserver in LAN and 80 is webservers http port</p>
<p>Anyone who can access remote ssh servers 8080 port can now browse  webserver in your LAN (do you really want this?). Also, with this your  computer works as anonymous proxy for chosen webserver (or any other  protocol depending on ports used).</p>
<h3>SOCKS Proxy</h3>
<p>Believe it or not, SSH can serve as SOCKS proxy<br />
ssh -D 12345 hostname<br />
where 12345 is port on your local computer</p>
<p>Huh? Is it this easy? Yep &#8211; dynamic port forwarding as it is usually  called does just this &#8211; makes a SOCKS v4/5 out of your computer and all  you need to do is point your SOCKS-aware applications to the chosen  port&#8230;</p>
<h3>Punching holes in HTTPS proxy</h3>
<p>OK, now this isn&#8217;t exactly a ssh-only feature (proxytunnel does most  of the work here) but might be the only way to make your way thru the  corporate restrictive web policies</p>
]]></content:encoded>
			<wfw:commentRss>http://spdlab.net/ssh-tips-and-tricks/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>

