<?xml version="1.0" encoding="utf-8" ?>

<rss version="2.0" 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/"
   xmlns:content="http://purl.org/rss/1.0/modules/content/"
   xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule">
<channel>
    <title>freebsd.munk.me.uk - SSH</title>
    <link>http://freebsd.munk.me.uk/</link>
    <description>FreeBSD System Administration</description>
    <dc:language>en</dc:language>
    <generator>Serendipity 1.5.2 - http://www.s9y.org/</generator>
    
    <image>
        <url>http://freebsd.munk.me.uk/templates/default/img/s9y_banner_small.png</url>
        <title>RSS: freebsd.munk.me.uk - SSH - FreeBSD System Administration</title>
        <link>http://freebsd.munk.me.uk/</link>
        <width>100</width>
        <height>21</height>
    </image>

<item>
    <title>Block Brute Force Attacks Against sshd and proftpd Using blockhosts</title>
    <link>http://freebsd.munk.me.uk/archives/209-Block-Brute-Force-Attacks-Against-sshd-and-proftpd-Using-blockhosts.html</link>
            <category>FTP</category>
            <category>Security</category>
            <category>SSH</category>
    
    <comments>http://freebsd.munk.me.uk/archives/209-Block-Brute-Force-Attacks-Against-sshd-and-proftpd-Using-blockhosts.html#comments</comments>
    <wfw:comment>http://freebsd.munk.me.uk/wfwcomment.php?cid=209</wfw:comment>

    <slash:comments>4</slash:comments>
    <wfw:commentRss>http://freebsd.munk.me.uk/rss.php?version=2.0&amp;type=comments&amp;cid=209</wfw:commentRss>
    

    <author>nospam@example.com (munk)</author>
    <content:encoded>
    For a long time now I&#039;ve had a lot of problems with brute force attacks against sshd and proftpd - attacks where a host will attempt to login with a dictionary of common usernames and passwords, trying each one until they find a combination that works.  Apart from being a security issue, this uses up a lot of bandwidth so it&#039;s worth taking some measures to block these kind of attacks.&lt;br /&gt;
&lt;br /&gt;
Both sshd and ftpd services have their own individual means for blocking individual connections, but unfortunately neither have an inbuilt method for detecting brute force attacks - counting how many failed login attempts are made from each individual IP address and then blocking that IP address if the number of failed login attempts is more than a certain number.  This is where a 3rd party utility is required.&lt;br /&gt;
&lt;br /&gt;
There are a few utilities that can mitigate brute force attacks on services.  For a while now I&#039;ve used &lt;a href=&quot;http://denyhosts.sourceforge.net/&quot;  title=&quot;DenyHosts&quot;&gt;DenyHosts&lt;/a&gt; successfully to block sshd brute force attacks.  DenyHosts works by constantly monitoring sshd logfiles and keeping track of how many failed logins have occured per IP address over time.  If the number of failed logins reaches a certain threshold, DenyHosts adds an entry in /etc/hosts.allow that effectively blocks the IP address, stopping that host from connecting to the sshd service any more.&lt;br /&gt;
&lt;br /&gt;
DenyHosts is great, but unfortunately it&#039;s aimed only at blocking sshd brute force attacks and I need to protect the ftpd service as well as just sshd - and in future maybe adapt to block other services.  With this in mind I decided to move to using a very similar script called &lt;a href=&quot;http://www.aczoom.com/tools/blockhosts/&quot;  title=&quot;BlockHosts&quot;&gt;BlockHosts&lt;/a&gt; (the documentation for BlockHosts actually mentions that it was inspired by DenyHosts).  BlockHosts can scan a list of service logfiles in one go instead of just a single logfile as with DenyHosts, so is ideal for monitoring a number of different services for brute force attacks.&lt;br /&gt;
&lt;br /&gt;
The following describes how to install and configure BlockHosts on FreeBSD so it&#039;s executed every time the sshd or proftpd services are accessed using TCP_WRAPPERS - ie modifying /etc/hosts.allow so the blockhosts script is run each time sshd or proftpd are accessed.  The BlockHosts script will then check if this current connection attempt is part of a brute force attack and if so, add a blocking rule to /etc/hosts.allow to deny further access.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-weight:bold&quot;&gt;Installation of BlockHosts&lt;/span&gt;&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;&lt;a href=&quot;http://www.aczoom.com/cms/blockhosts/download&quot;  title=&quot;BlockHosts download page&quot;&gt;Download blockhosts from the download page&lt;/a&gt;, extract the distribution (note please check the download link for the latest version, the version below was latest at time of writing):&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;root@users&amp;#160;/home/munk/bin/python/blockhosts#&amp;#160;wget&amp;#160;http&amp;#58;//www.aczoom.com/tools/blockhosts/BlockHosts-1.0.5.tar.gz&lt;br /&gt;
root@users&amp;#160;/home/munk/bin/python/blockhosts#&amp;#160;tar&amp;#160;zxvf&amp;#160;BlockHosts-1.0.5.tar.gz&lt;br /&gt;
BlockHosts-1.0.5/&lt;br /&gt;
BlockHosts-1.0.5/Makefile&lt;br /&gt;
...&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;li&gt;Change to BlockHosts directory:&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;root@users&amp;#160;/home/munk/bin/python/blockhosts#&amp;#160;cd&amp;#160;BlockHosts-1.0.5&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;li&gt;Edit and save blockhosts.py to read:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;CONFIG_FILE&amp;#160;=&amp;#160;&quot;/usr/local/etc/blockhosts.cfg&quot;&lt;br /&gt;
...&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&quot;LOGFILES&quot;&amp;#58;&amp;#160;&amp;#40;&amp;#160;&quot;/var/log/auth.log&quot;,&amp;#160;&amp;#41;,&lt;/div&gt;&lt;br /&gt;
&lt;span style=&quot;font-weight:bold&quot;&gt;Note:&lt;/span&gt; may seem a bit odd editing the blockhosts.py script before it&#039;s installed - the reason for this is that the installation locations used by setup.py below are taken from blockhosts.py, so by modifying blockhosts.py like this we get the config file installed into /usr/local/etc/ (FreeBSD default for 3rd party software) instead of into /etc (default for linux 3rd party software).&lt;br /&gt;
&lt;br /&gt;
&lt;li&gt;Install blockhosts:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;root@users&amp;#160;/home/munk/bin/python/blockhosts/BlockHosts-1.0.5#&amp;#160;python&amp;#160;setup.py&amp;#160;-v&amp;#160;install&lt;br /&gt;
running&amp;#160;install&lt;br /&gt;
running&amp;#160;build&lt;br /&gt;
running&amp;#160;build_scripts&lt;br /&gt;
creating&amp;#160;build&lt;br /&gt;
creating&amp;#160;build/scripts-2.4&lt;br /&gt;
copying&amp;#160;and&amp;#160;adjusting&amp;#160;blockhosts.py&amp;#160;-&amp;#62;&amp;#160;build/scripts-2.4&lt;br /&gt;
changing&amp;#160;mode&amp;#160;of&amp;#160;build/scripts-2.4/blockhosts.py&amp;#160;from&amp;#160;644&amp;#160;to&amp;#160;755&lt;br /&gt;
running&amp;#160;install_scripts&lt;br /&gt;
copying&amp;#160;build/scripts-2.4/blockhosts.py&amp;#160;-&amp;#62;&amp;#160;/usr/local/bin&lt;br /&gt;
changing&amp;#160;mode&amp;#160;of&amp;#160;/usr/local/bin/blockhosts.py&amp;#160;to&amp;#160;755&lt;br /&gt;
running&amp;#160;install_data&lt;br /&gt;
copying&amp;#160;blockhosts.cfg&amp;#160;-&amp;#62;&amp;#160;/usr/local/etc&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
This installs the blockhosts.py script into /usr/local/bin and the config file blockhosts.cfg into /usr/local/etc.  Make sure to run &#039;rehash&#039; to reread the binary paths again so blockhosts.py will run from anywhere:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;root@users&amp;#160;/home/munk/bin/python/blockhosts/BlockHosts-1.0.5#&amp;#160;rehash&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;li&gt;Edit and save the /usr/local/etc/blockhosts.cfg file so it reads:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;LOGFILES&amp;#160;=&amp;#160;&amp;#91;&amp;#160;&quot;/var/log/auth.log&quot;,&amp;#160;&quot;/var/log/ftp.log&quot;&amp;#160;&amp;#93;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-weight:bold&quot;&gt;Important:&lt;/span&gt;&lt;br /&gt;
Add the logfiles you want blockhosts to monitor for brute force attacks here.  /var/log/auth.log is standard for sshd, /var/log/ftp.log is maybe not standard for all ftpd, this is just what I have setup here.&lt;br /&gt;
&lt;br /&gt;
At this point it&#039;s best to read through the documentation for blockhosts completely - the README, INSTALL and the blockhosts.py script itself.  The following section is pretty much copy/pasted from what&#039;s mentioned in there.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;li&gt;Edit and save /etc/hosts.allow to include the section that blockhosts.py will modify.  Make sure you allow your own IP blocks first and any trusted IPs so they don&#039;t get blocked accidentally:&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;GOTCHA LOOKOUT!&lt;/strong&gt;&lt;br /&gt;
One gotcha to watch out for in this is the line:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;ALL&amp;#160;&amp;#58;&amp;#160;ALL&amp;#160;&amp;#58;&amp;#160;allow&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
You &lt;strong&gt;MUST&lt;/strong&gt; remove this line - replace it with your IP block instead so you don&#039;t get locked out from your own address range.  If this line isn&#039;t removed/commented out, anything below it just isn&#039;t read/executed and blockhosts won&#039;t work.&lt;br /&gt;
&lt;br /&gt;
This is how my /etc/hosts.allow looks:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;#######################################################################&lt;br /&gt;
#&amp;#160;blockhosts&lt;br /&gt;
#######################################################################&lt;br /&gt;
#&amp;#160;----&lt;br /&gt;
#&amp;#160;see&amp;#160;&quot;man&amp;#160;5&amp;#160;hosts_access&quot;&amp;#160;for&amp;#160;details&amp;#160;of&amp;#160;the&amp;#160;format&amp;#160;of&amp;#160;IP&amp;#160;addresses,&lt;br /&gt;
#services,&amp;#160;allow/deny&amp;#160;options.&amp;#160;Also&amp;#160;see&amp;#160;&quot;man&amp;#160;hosts_options&quot;&lt;br /&gt;
#order&amp;#160;of&amp;#160;lines&amp;#160;in&amp;#160;this&amp;#160;file&amp;#160;is&amp;#160;important,&amp;#160;first&amp;#160;matched&amp;#160;IP&amp;#160;address&amp;#160;line&lt;br /&gt;
#is&amp;#160;rule&amp;#160;applied&amp;#160;by&amp;#160;hosts_access&lt;br /&gt;
#&lt;br /&gt;
#&amp;#160;permanent&amp;#160;whitelist&amp;#160;addresses&amp;#160;-&amp;#160;these&amp;#160;should&amp;#160;always&amp;#160;be&amp;#160;allowed&amp;#160;access&lt;br /&gt;
&lt;br /&gt;
ALL&amp;#160;&amp;#58;&amp;#160;213.152.51.192/255.255.255.248&amp;#160;&amp;#58;&amp;#160;allow&lt;br /&gt;
#&amp;#160;ALL&amp;#58;&amp;#160;127.0.0.1&amp;#160;&amp;#160;&amp;#58;&amp;#160;allow&lt;br /&gt;
#&amp;#160;ALL&amp;#58;&amp;#160;192.168.0.&amp;#160;&amp;#58;&amp;#160;allow&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;permanent&amp;#160;blacklist&amp;#160;addresses&amp;#160;-&amp;#160;these&amp;#160;should&amp;#160;always&amp;#160;be&amp;#160;denied&amp;#160;access&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;ALL&amp;#58;&amp;#160;10.&amp;#160;&amp;#160;&amp;#58;&amp;#160;deny&lt;br /&gt;
#&amp;#160;ALL&amp;#58;&amp;#160;192.&amp;#160;&amp;#58;&amp;#160;deny&lt;br /&gt;
#&amp;#160;ALL&amp;#58;&amp;#160;172.&amp;#160;&amp;#58;&amp;#160;deny&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;----------------------------------------&lt;br /&gt;
#&amp;#160;next&amp;#160;section&amp;#160;is&amp;#160;the&amp;#160;blockhosts&amp;#160;section&amp;#160;-&amp;#160;it&amp;#160;will&amp;#160;add/delete&amp;#160;entries&amp;#160;in&lt;br /&gt;
#&amp;#160;between&amp;#160;the&amp;#160;two&amp;#160;marker&amp;#160;lines&amp;#160;&amp;#40;#----&amp;#160;BlockHosts&amp;#160;Additions&amp;#41;&lt;br /&gt;
&lt;br /&gt;
#----&amp;#160;BlockHosts&amp;#160;Additions&lt;br /&gt;
#----&amp;#160;BlockHosts&amp;#160;Additions&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;----------------------------------------&lt;br /&gt;
#&amp;#160;finally,&amp;#160;the&amp;#160;command&amp;#160;to&amp;#160;execute&amp;#160;the&amp;#160;blockhosts&amp;#160;script,&amp;#160;based&amp;#160;on&lt;br /&gt;
#&amp;#160;connection&amp;#160;to&amp;#160;particular&amp;#160;service&amp;#160;or&amp;#160;services,&amp;#160;for&amp;#160;example,&amp;#160;for&lt;br /&gt;
#&amp;#160;sshd&amp;#160;and&amp;#160;proftpd&amp;#160;-&amp;#160;if&amp;#160;using&amp;#160;vsftpd,&amp;#160;pure-ftpd,&amp;#160;be&amp;#160;sure&amp;#160;to&amp;#160;use&amp;#160;those&lt;br /&gt;
#&amp;#160;words&amp;#160;instead&amp;#58;&lt;br /&gt;
&lt;br /&gt;
sshd,&amp;#160;proftpd&amp;#58;&amp;#160;ALL&amp;#58;&amp;#160;spawn&amp;#160;&amp;#40;/usr/local/bin/blockhosts.py&amp;#160;--verbose&amp;#160;--echo&amp;#160;&quot;%c-%s&quot;&amp;#160;&amp;#62;&amp;#62;&amp;#160;/var/log/blockhosts.log&amp;#160;2&amp;#62;&amp;amp;1&amp;#160;&amp;#41;&amp;amp;&amp;#160;&amp;#58;&amp;#160;allow&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;remove&amp;#58;&amp;#160;&amp;#160;&amp;#160;&amp;#62;&amp;#62;&amp;#160;/var/log/blockhosts.log&amp;#160;2&amp;#62;&amp;amp;1&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;if&amp;#160;logging&amp;#160;to&lt;br /&gt;
#&amp;#160;blockhosts.log&amp;#160;is&amp;#160;not&amp;#160;needed&amp;#160;-&amp;#160;it&amp;#160;will&amp;#160;still&amp;#160;log&amp;#160;to&amp;#160;syslog&amp;#160;&amp;#40;minimally&amp;#41;&lt;br /&gt;
#&amp;#160;see&amp;#160;examples&amp;#160;below&lt;br /&gt;
#&amp;#160;--&lt;br /&gt;
#&amp;#160;See&amp;#160;&quot;man&amp;#160;hosts.allow&quot;&amp;#160;for&amp;#160;info&amp;#160;on&amp;#160;%c&amp;#160;and&amp;#160;%s&amp;#160;identifiers&lt;br /&gt;
#----&lt;br /&gt;
#&amp;#160;for&amp;#160;non-verbose,&amp;#160;with&amp;#160;identification,&amp;#160;to&amp;#160;syslog&amp;#160;only&amp;#160;&amp;#40;/var/log/messages&amp;#41;&amp;#58;&lt;br /&gt;
#sshd,&amp;#160;proftpd,&amp;#160;in.proftpd&amp;#58;&amp;#160;ALL&amp;#58;&amp;#160;spawn&amp;#160;/usr/bin/blockhosts.py&amp;#160;--echo&amp;#160;&quot;%c-%s&quot;&amp;#160;&amp;amp;&amp;#160;&amp;#58;&amp;#160;allow&lt;br /&gt;
#----&lt;br /&gt;
#&amp;#160;minimal&amp;#160;logging,&amp;#160;to&amp;#160;syslog&amp;#160;&amp;#40;usually&amp;#160;goes&amp;#160;to&amp;#160;/var/log/messages&amp;#41;&amp;#58;&lt;br /&gt;
#sshd,&amp;#160;proftpd,&amp;#160;in.proftpd&amp;#58;&amp;#160;ALL&amp;#58;&amp;#160;spawn&amp;#160;/usr/bin/blockhosts.py&amp;#160;&amp;amp;&amp;#160;&amp;#58;&amp;#160;allow&lt;br /&gt;
#----&lt;br /&gt;
#&amp;#160;To&amp;#160;test&amp;#160;hosts.allow,&amp;#160;and&amp;#160;to&amp;#160;find&amp;#160;out&amp;#160;exact&amp;#160;names&amp;#160;of&amp;#160;SSH/FTP&amp;#160;services,&lt;br /&gt;
#&amp;#160;add&amp;#160;this&amp;#160;line&amp;#160;to&amp;#160;the&amp;#160;beginning&amp;#160;of&amp;#160;hosts.allow,&amp;#160;use&amp;#160;ssh/ftp&amp;#160;to&amp;#160;connect&lt;br /&gt;
#&amp;#160;to&amp;#160;your&amp;#160;server,&amp;#160;and&amp;#160;then&amp;#160;look&amp;#160;at&amp;#160;the&amp;#160;log&amp;#160;&amp;#40;/var/log/messages&amp;#160;or&lt;br /&gt;
#&amp;#160;blockhosts.log&amp;#41;&amp;#160;to&amp;#160;see&amp;#160;the&amp;#160;name&amp;#160;of&amp;#160;the&amp;#160;invoked&amp;#160;service.&lt;br /&gt;
#&amp;#160;IMPORTANT&amp;#58;&amp;#160;after&amp;#160;your&amp;#160;test&amp;#160;is&amp;#160;done,&amp;#160;remove&amp;#160;this&amp;#160;line&amp;#160;from&amp;#160;hosts.allow!&lt;br /&gt;
#&amp;#160;Otherwise&amp;#160;everyone&amp;#160;will&amp;#160;always&amp;#160;have&amp;#160;access.&lt;br /&gt;
#ALL&amp;#160;&amp;#58;&amp;#160;ALL&amp;#58;&amp;#160;spawn&amp;#160;&amp;#40;/usr/bin/blockhosts.py&amp;#160;--verbose&amp;#160;--echo&amp;#160;&quot;%c-%s&quot;&amp;#160;&amp;#62;&amp;#62;&amp;#160;/var/log/blockhosts.log&amp;#160;2&amp;#62;&amp;amp;1&amp;#160;&amp;#41;&amp;amp;&amp;#160;&amp;#58;&amp;#160;allow&lt;br /&gt;
#######################################################################&lt;br /&gt;
#&amp;#160;blockhosts&lt;br /&gt;
#######################################################################&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-weight:bold&quot;&gt;Important Note for ProFTPD users:&lt;/span&gt;&lt;br /&gt;
The following sections describes the configuration needed when using proftpd via inetd.  If you are using ProFTPD in standalone mode, you need to use the proftpd mod_wrap/mod_wrap_file functionality to have proftpd read and honour the TCP_WRAPPERS//etc/hosts.allow file(s) when denying/allowing hosts.  Additionally you need to specifiy the configure flag --enable-wrapper-options when building proftpd.  For a heavily used server, this might be worth doing but personally I don&#039;t get that many connections that I need to worry about inetd being overloaded so I can just go down the (easier to configure for blockhosts) inetd path.&lt;br /&gt;
&lt;br /&gt;
&lt;li&gt;Ensure proftpd is configured to run correctly via inetd.&lt;br /&gt;
&lt;br /&gt;
Edit and save /usr/local/etc/proftpd.conf to read:&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;ServerType&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;inetd&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-weight:bold&quot;&gt;Important:&lt;/span&gt; remember to delete or rename /usr/local/etc/rc.d/proftpd.sh so it&#039;s not run at boot time - the proftpd daemon doesn&#039;t need to be started at boot if you&#039;re using inetd, inetd handles all the proftpd connections, see below:&lt;br /&gt;
&lt;br /&gt;
Edit and save /etc/inetd.conf to read:&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;ftp&amp;#160;stream&amp;#160;&amp;#160;tcp&amp;#160;nowait&amp;#160;&amp;#160;root&amp;#160;&amp;#160;&amp;#160;&amp;#160;/usr/local/sbin/in.proftpd&amp;#160;proftpd&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
then restart inetd:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;root@users&amp;#160;/usr/local/etc#&amp;#160;kill&amp;#160;-HUP&amp;#160;`cat&amp;#160;/var/run/inetd.pid&amp;#160;`&lt;/div&gt;&lt;br /&gt;
This forces inetd to restart, rereading the config file changes made to /etc/inetd.conf.  ftp connections will now be handled by proftpd via inetd.&lt;br /&gt;
&lt;/ol&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We&#039;re now ready to run blockhosts.py for the first time.  BlockHosts will parse each logfile mentioned in blockhosts.cfg and check for any brute force attacks and if it finds any, blocks will be added to the /etc/hosts.allow file.  &lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-weight:bold&quot;&gt;Note:&lt;/span&gt; This initial check does not take into account the period over which failed logins took place, so any IP that has more than the default 7 failed login entries will look like a brute force attacker.  However, the ban BlockHosts adds will only last for the default 12 hours so this shouldn&#039;t cause a huge issue - just be aware of this and check the IPs that are added on the first run.&lt;br /&gt;
&lt;br /&gt;
For the very first time it&#039;s a good idea to try a &#039;dry run&#039; just to see what blockhosts finds and what it&#039;d do, without actually doing anything to the /etc/hosts.allow file.  To do this, run blockhosts with the &#039;--dry-run&#039; flag:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;root@users&amp;#160;/usr/local/etc#&amp;#160;/usr/local/bin/blockhosts.py&amp;#160;--verbose&amp;#160;--dry-run&lt;br /&gt;
blockhosts&amp;#160;1.0.5&amp;#160;started&amp;#58;&amp;#160;2006-12-30&amp;#160;14&amp;#58;15&amp;#58;30&lt;br /&gt;
&amp;#160;...&amp;#160;will&amp;#160;discard&amp;#160;all&amp;#160;host&amp;#160;entries&amp;#160;older&amp;#160;than&amp;#160;&amp;#160;2006-12-30&amp;#160;02&amp;#58;15&lt;br /&gt;
&amp;#160;...&amp;#160;load&amp;#160;blockfile&amp;#58;&amp;#160;/etc/hosts.allow&lt;br /&gt;
&amp;#160;...&amp;#160;found&amp;#160;both&amp;#160;markers,&amp;#160;count&amp;#160;of&amp;#160;hosts&amp;#160;being&amp;#160;watched&amp;#58;&amp;#160;0&lt;br /&gt;
&amp;#160;&amp;#160;Warning&amp;#58;&amp;#160;no&amp;#160;offset&amp;#160;found,&amp;#160;will&amp;#160;read&amp;#160;from&amp;#160;beginning&amp;#160;in&amp;#160;logfile&amp;#58;&amp;#160;/var/log/auth.log&lt;br /&gt;
&amp;#160;...&amp;#160;securelog,&amp;#160;loading&amp;#160;file,&amp;#160;offset&amp;#58;&amp;#160;/var/log/auth.log&amp;#160;0&lt;br /&gt;
&amp;#160;&amp;#160;Warning&amp;#58;&amp;#160;no&amp;#160;offset&amp;#160;found,&amp;#160;will&amp;#160;read&amp;#160;from&amp;#160;beginning&amp;#160;in&amp;#160;logfile&amp;#58;&amp;#160;/var/log/ftp.log&lt;br /&gt;
&amp;#160;...&amp;#160;securelog,&amp;#160;loading&amp;#160;file,&amp;#160;offset&amp;#58;&amp;#160;/var/log/ftp.log&amp;#160;0&lt;br /&gt;
&amp;#160;...&amp;#160;updates&amp;#58;&amp;#160;counts&amp;#58;&amp;#160;hosts&amp;#160;to&amp;#160;block&amp;#58;&amp;#160;9;&amp;#160;hosts&amp;#160;being&amp;#160;watched&amp;#58;&amp;#160;21&lt;br /&gt;
#----&amp;#160;BlockHosts&amp;#160;Additions&lt;br /&gt;
ALL&amp;#58;&amp;#160;&amp;#160;203.88.192.225&amp;#160;&amp;#58;&amp;#160;deny&lt;br /&gt;
ALL&amp;#58;&amp;#160;&amp;#160;&amp;#160;&amp;#160;200.71.192.7&amp;#160;&amp;#58;&amp;#160;deny&lt;br /&gt;
ALL&amp;#58;&amp;#160;&amp;#160;212.227.81.146&amp;#160;&amp;#58;&amp;#160;deny&lt;br /&gt;
ALL&amp;#58;&amp;#160;&amp;#160;&amp;#160;&amp;#160;218.25.62.75&amp;#160;&amp;#58;&amp;#160;deny&lt;br /&gt;
ALL&amp;#58;&amp;#160;&amp;#160;200.46.108.164&amp;#160;&amp;#58;&amp;#160;deny&lt;br /&gt;
ALL&amp;#58;&amp;#160;&amp;#160;&amp;#160;&amp;#160;201.57.163.2&amp;#160;&amp;#58;&amp;#160;deny&lt;br /&gt;
ALL&amp;#58;&amp;#160;&amp;#160;205.129.191.11&amp;#160;&amp;#58;&amp;#160;deny&lt;br /&gt;
ALL&amp;#58;&amp;#160;&amp;#160;&amp;#160;&amp;#160;200.68.51.91&amp;#160;&amp;#58;&amp;#160;deny&lt;br /&gt;
ALL&amp;#58;&amp;#160;&amp;#160;&amp;#160;&amp;#160;82.38.68.217&amp;#160;&amp;#58;&amp;#160;deny&lt;br /&gt;
&lt;br /&gt;
#bh&amp;#58;&amp;#160;ip&amp;#58;&amp;#160;&amp;#160;&amp;#160;85.184.10.200&amp;#160;&amp;#58;&amp;#160;&amp;#160;&amp;#160;1&amp;#160;&amp;#58;&amp;#160;2006-12-30-14-15&lt;br /&gt;
#bh&amp;#58;&amp;#160;ip&amp;#58;&amp;#160;&amp;#160;84.158.231.209&amp;#160;&amp;#58;&amp;#160;&amp;#160;&amp;#160;1&amp;#160;&amp;#58;&amp;#160;2006-12-30-14-15&lt;br /&gt;
#bh&amp;#58;&amp;#160;ip&amp;#58;&amp;#160;&amp;#160;&amp;#160;&amp;#160;82.38.68.217&amp;#160;&amp;#58;&amp;#160;&amp;#160;11&amp;#160;&amp;#58;&amp;#160;2006-12-30-14-15&lt;br /&gt;
#bh&amp;#58;&amp;#160;ip&amp;#58;&amp;#160;&amp;#160;&amp;#160;&amp;#160;82.153.28.16&amp;#160;&amp;#58;&amp;#160;&amp;#160;&amp;#160;2&amp;#160;&amp;#58;&amp;#160;2006-12-30-14-15&lt;br /&gt;
#bh&amp;#58;&amp;#160;ip&amp;#58;&amp;#160;&amp;#160;&amp;#160;67.113.225.66&amp;#160;&amp;#58;&amp;#160;&amp;#160;&amp;#160;1&amp;#160;&amp;#58;&amp;#160;2006-12-30-14-15&lt;br /&gt;
#bh&amp;#58;&amp;#160;ip&amp;#58;&amp;#160;&amp;#160;&amp;#160;59.108.34.228&amp;#160;&amp;#58;&amp;#160;&amp;#160;&amp;#160;2&amp;#160;&amp;#58;&amp;#160;2006-12-30-14-15&lt;br /&gt;
#bh&amp;#58;&amp;#160;ip&amp;#58;&amp;#160;&amp;#160;222.68.192.132&amp;#160;&amp;#58;&amp;#160;&amp;#160;&amp;#160;2&amp;#160;&amp;#58;&amp;#160;2006-12-30-14-15&lt;br /&gt;
#bh&amp;#58;&amp;#160;ip&amp;#58;&amp;#160;&amp;#160;&amp;#160;&amp;#160;218.25.62.75&amp;#160;&amp;#58;&amp;#160;&amp;#160;20&amp;#160;&amp;#58;&amp;#160;2006-12-30-14-15&lt;br /&gt;
#bh&amp;#58;&amp;#160;ip&amp;#58;&amp;#160;&amp;#160;217.83.162.157&amp;#160;&amp;#58;&amp;#160;&amp;#160;&amp;#160;1&amp;#160;&amp;#58;&amp;#160;2006-12-30-14-15&lt;br /&gt;
#bh&amp;#58;&amp;#160;ip&amp;#58;&amp;#160;&amp;#160;212.227.81.146&amp;#160;&amp;#58;&amp;#160;29499&amp;#160;&amp;#58;&amp;#160;2006-12-30-14-15&lt;br /&gt;
#bh&amp;#58;&amp;#160;ip&amp;#58;&amp;#160;&amp;#160;&amp;#160;210.1.132.178&amp;#160;&amp;#58;&amp;#160;&amp;#160;&amp;#160;4&amp;#160;&amp;#58;&amp;#160;2006-12-30-14-15&lt;br /&gt;
#bh&amp;#58;&amp;#160;ip&amp;#58;&amp;#160;&amp;#160;205.129.191.11&amp;#160;&amp;#58;&amp;#160;&amp;#160;20&amp;#160;&amp;#58;&amp;#160;2006-12-30-14-15&lt;br /&gt;
#bh&amp;#58;&amp;#160;ip&amp;#58;&amp;#160;&amp;#160;&amp;#160;204.141.87.14&amp;#160;&amp;#58;&amp;#160;&amp;#160;&amp;#160;3&amp;#160;&amp;#58;&amp;#160;2006-12-30-14-15&lt;br /&gt;
#bh&amp;#58;&amp;#160;ip&amp;#58;&amp;#160;&amp;#160;203.88.192.225&amp;#160;&amp;#58;&amp;#160;448&amp;#160;&amp;#58;&amp;#160;2006-12-30-14-15&lt;br /&gt;
#bh&amp;#58;&amp;#160;ip&amp;#58;&amp;#160;&amp;#160;202.108.40.109&amp;#160;&amp;#58;&amp;#160;&amp;#160;&amp;#160;1&amp;#160;&amp;#58;&amp;#160;2006-12-30-14-15&lt;br /&gt;
#bh&amp;#58;&amp;#160;ip&amp;#58;&amp;#160;&amp;#160;&amp;#160;&amp;#160;201.57.163.2&amp;#160;&amp;#58;&amp;#160;2867&amp;#160;&amp;#58;&amp;#160;2006-12-30-14-15&lt;br /&gt;
#bh&amp;#58;&amp;#160;ip&amp;#58;&amp;#160;&amp;#160;&amp;#160;&amp;#160;200.71.192.7&amp;#160;&amp;#58;&amp;#160;761&amp;#160;&amp;#58;&amp;#160;2006-12-30-14-15&lt;br /&gt;
#bh&amp;#58;&amp;#160;ip&amp;#58;&amp;#160;&amp;#160;&amp;#160;&amp;#160;200.68.51.91&amp;#160;&amp;#58;&amp;#160;&amp;#160;10&amp;#160;&amp;#58;&amp;#160;2006-12-30-14-15&lt;br /&gt;
#bh&amp;#58;&amp;#160;ip&amp;#58;&amp;#160;&amp;#160;200.46.108.164&amp;#160;&amp;#58;&amp;#160;170&amp;#160;&amp;#58;&amp;#160;2006-12-30-14-15&lt;br /&gt;
#bh&amp;#58;&amp;#160;ip&amp;#58;&amp;#160;&amp;#160;200.105.255.90&amp;#160;&amp;#58;&amp;#160;&amp;#160;&amp;#160;7&amp;#160;&amp;#58;&amp;#160;2006-12-30-14-15&lt;br /&gt;
#bh&amp;#58;&amp;#160;ip&amp;#58;&amp;#160;&amp;#160;152.104.125.14&amp;#160;&amp;#58;&amp;#160;&amp;#160;&amp;#160;3&amp;#160;&amp;#58;&amp;#160;2006-12-30-14-15&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
From this you can see nicely what blockhosts makes of the service logfiles and the addresses that have tried to connect unsuccessfully.  On my host, as you can see above, there are a few that are obviously dodgy (I would only expect a max of maybe 8 connections per ip per month, so clearly 29,499 connections is just &lt;span style=&quot;font-style:italic&quot;&gt;wrong&lt;/span&gt;!).&lt;br /&gt;
&lt;br /&gt;
Once you&#039;re happy that the output is correct, run blockhosts again without the &#039;--dry-run&#039; flag and the /etc/hosts.allow file will be modified.  Also from now on the logfiles will only be read from the last recorded offset which saves a lot of time if your logfiles are very big.&lt;br /&gt;
&lt;br /&gt;
Big thanks to the BlockHosts author Avinash Chopde ! 
    </content:encoded>

    <pubDate>Sat, 30 Dec 2006 11:52:15 +0000</pubDate>
    <guid isPermaLink="false">http://freebsd.munk.me.uk/archives/209-guid.html</guid>
    <creativeCommons:license>http://creativecommons.org/licenses/by/2.5/</creativeCommons:license>
</item>
<item>
    <title>Let root see all files with locate</title>
    <link>http://freebsd.munk.me.uk/archives/207-Let-root-see-all-files-with-locate.html</link>
            <category>FreeBSD</category>
            <category>Shell</category>
            <category>SSH</category>
    
    <comments>http://freebsd.munk.me.uk/archives/207-Let-root-see-all-files-with-locate.html#comments</comments>
    <wfw:comment>http://freebsd.munk.me.uk/wfwcomment.php?cid=207</wfw:comment>

    <slash:comments>6</slash:comments>
    <wfw:commentRss>http://freebsd.munk.me.uk/rss.php?version=2.0&amp;type=comments&amp;cid=207</wfw:commentRss>
    

    <author>nospam@example.com (munk)</author>
    <content:encoded>
    The &lt;a href=&quot;http://nixdoc.net/man-pages/FreeBSD/locate.1.html&quot;  title=&quot;locate manpage&quot;&gt;locate&lt;/a&gt; utility on linux was one of the first tools I hit when I made the move to FreeBSD a few years back - knowing where files are is half the battle when you&#039;re trying to configure things and find documentation on how to do it. The trouble with locate though as &lt;a href=&quot;http://freebsd.amazingdev.com/blog/archives/000805.html&quot;  title=&quot;Locate This!&quot;&gt;jdarnold mentions in his article &#039;Locate This!&#039;&lt;/a&gt; is that if you build the locate database as &#039;root&#039;, you end up exposing everything to any user that runs the locate command.  The other problem he mentions is the locate db is only updated weekly on FreeBSD by default via the &lt;a href=&quot;http://www.freebsd.org/cgi/man.cgi?query=periodic&amp;apropos=0&amp;sektion=0&amp;manpath=FreeBSD+6.1-RELEASE&amp;format=html&quot;  title=&quot;periodic manpage&quot;&gt;periodic&lt;/a&gt; system which isn&#039;t really enough if you use your system regularly.&lt;br /&gt;
&lt;br /&gt;
I remember thinking along the same lines a while back and after reading through the man pages the solution I found was to create two separate databases - one for root and one for regular users.  The &#039;regular&#039; db is updated on a weekly basis as per the default on FreeBSD via periodic, whereas the other &#039;root&#039; locate db is built daily in a crontab so I can get the latest up to date details on which files are where.&lt;br /&gt;
&lt;br /&gt;
To get the root db built first you need to create a crontab entry - i put this in /etc/crontab:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;39&amp;#160;&amp;#160;2&amp;#160;&amp;#160;&amp;#160;&amp;#42;&amp;#160;&amp;#160;&amp;#160;&amp;#42;&amp;#160;&amp;#160;&amp;#160;&amp;#42;&amp;#160;&amp;#160;&amp;#160;root&amp;#160;&amp;#160;&amp;#160;&amp;#160;env&amp;#160;-i&amp;#160;LOCATE_CONFIG=/root/locate/conf/locate.rc&amp;#160;/usr/libexec/locate.updatedb&amp;#160;&amp;#62;&amp;#160;/dev/null&amp;#160;2&amp;#62;&amp;amp;1&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
This tells the locate.updatedb script to use a separate configuration file - /root/locate/conf/locate.rc - for building root&#039;s locate db.  The content of /root/locate/conf/locate.rc look like this:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;FCODES=&quot;/root/locate/db/locate.database.root&quot;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
which indicates that this db should be built in /root/locate/db/locate.database.root instead of the default locate in /var/db/locate.database.  You can safely run the command as root on the commandline to initialize your new db:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;root@users&amp;#160;/root#&amp;#160;env&amp;#160;-i&amp;#160;LOCATE_CONFIG=/root/locate/conf/locate.rc&amp;#160;/usr/libexec/locate.updatedb&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
Once the database is built you can move on to test the new db works ok:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;root@users&amp;#160;/root#&amp;#160;locate&amp;#160;-d&amp;#160;/root/locate/db/locate.database.root&amp;#160;.cshrc.root&lt;br /&gt;
/root/.cshrc.root&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
This file is only readable by root, so it seems to work ok.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To make things easier, add a shell alias in root&#039;s .cshrc file aliasing &#039;locate&#039; to the command &#039;locate -d /root/locate/db/locate.database.root&#039;:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;root@users&amp;#160;/root#&amp;#160;grep&amp;#160;locate&amp;#160;$cshrc&lt;br /&gt;
alias&amp;#160;locate&amp;#160;locate&amp;#160;-d&amp;#160;/root/locate/db/locate.database.root&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
With the &quot;-d /root/locate/db/locate.database.root&quot; switch, locate will use the db at /root/locate/db/locate.database.root instead of the default /var/db/locate.database and root will be able to use locate to find any files in the filesystem, not just those that are world readable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, one way to update the regular locate db as root but without making it list every world readable file is to perform the following:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;#!/bin/sh&lt;br /&gt;
#&amp;#160;make&amp;#160;sure&amp;#160;db&amp;#160;file&amp;#160;exists&amp;#58;&lt;br /&gt;
touch&amp;#160;/var/db/locate.database&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;then&amp;#160;change&amp;#160;ownership&amp;#160;to&amp;#160;the&amp;#160;nobody&amp;#160;user&amp;#58;&lt;br /&gt;
chown&amp;#160;nobody&amp;#160;/var/db/locate.database&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;make&amp;#160;it&amp;#160;writeable&amp;#160;by&amp;#160;nobody&amp;#160;and&amp;#160;readable&amp;#160;by&amp;#160;everyone&amp;#160;else&amp;#58;&lt;br /&gt;
chmod&amp;#160;644&amp;#160;/var/db/locate.database&amp;#160;&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;then&amp;#160;move&amp;#160;on&amp;#160;to&amp;#160;update&amp;#160;the&amp;#160;db...&lt;br /&gt;
#&amp;#160;first&amp;#160;make&amp;#160;sure&amp;#160;we&#039;re&amp;#160;in&amp;#160;the&amp;#160;/&amp;#160;folder&amp;#160;where&amp;#160;the&amp;#160;db&amp;#160;update&amp;#160;starts&amp;#58;&lt;br /&gt;
cd&amp;#160;/&amp;#160;&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;then&amp;#160;finally&amp;#160;run&amp;#160;the&amp;#160;updatedb&amp;#160;command&amp;#160;as&amp;#160;the&amp;#160;&#039;nobody&#039;&amp;#160;user&amp;#58;&lt;br /&gt;
echo&amp;#160;&quot;/usr/libexec/locate.updatedb&quot;&amp;#160;|&amp;#160;su&amp;#160;-&amp;#160;-fm&amp;#160;nobody&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
This is basically what the 310.locate periodic script does and results in a locate db that contains only files that are readable by the &#039;nobody&#039; user - essentially all &#039;world readable&#039; files.&lt;br /&gt;
&lt;br /&gt;
Comparing the sizes of the root db against the nobody db:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;root@users&amp;#160;/#&amp;#160;ls&amp;#160;-al&amp;#160;/var/db/locate.database&amp;#160;/root/locate/db/locate.database.root&lt;br /&gt;
-rw-r--r--&amp;#160;&amp;#160;1&amp;#160;root&amp;#160;&amp;#160;&amp;#160;&amp;#160;wheel&amp;#160;&amp;#160;4070484&amp;#160;Nov&amp;#160;18&amp;#160;02&amp;#58;45&amp;#160;/root/locate/db/locate.database.root&lt;br /&gt;
-rw-r--r--&amp;#160;&amp;#160;1&amp;#160;nobody&amp;#160;&amp;#160;wheel&amp;#160;&amp;#160;3280409&amp;#160;Nov&amp;#160;18&amp;#160;11&amp;#58;41&amp;#160;/var/db/locate.database&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
You can see the size difference there, not as many entries in nobody&#039;s db as root&#039;s.  Just to double check:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;root@users&amp;#160;/root#&amp;#160;locate&amp;#160;.cshrc.root&lt;br /&gt;
/root/bin/ktrace.out&lt;br /&gt;
/root/ktrace.out&lt;br /&gt;
/usr/local/etc/snort/ktrace.out&lt;br /&gt;
root@users&amp;#160;/root#&amp;#160;echo&amp;#160;&quot;locate&amp;#160;ktrace.out&quot;&amp;#160;|&amp;#160;su&amp;#160;-&amp;#160;-fm&amp;#160;nobody&lt;br /&gt;
/usr/local/etc/snort/ktrace.out&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
So from that you can see that &#039;nobody&#039; can see the ktrace.out files located in /root - apart from root of course :)  Sorted. 
    </content:encoded>

    <pubDate>Sat, 18 Nov 2006 10:38:40 +0000</pubDate>
    <guid isPermaLink="false">http://freebsd.munk.me.uk/archives/207-guid.html</guid>
    <creativeCommons:license>http://creativecommons.org/licenses/by/2.5/</creativeCommons:license>
</item>
<item>
    <title>Solving permission problems with parsepath.pl</title>
    <link>http://freebsd.munk.me.uk/archives/175-Solving-permission-problems-with-parsepath.pl.html</link>
            <category>Apache</category>
            <category>FreeBSD</category>
            <category>General</category>
            <category>Perl</category>
            <category>PHP</category>
            <category>Security</category>
            <category>Shell</category>
            <category>SSH</category>
    
    <comments>http://freebsd.munk.me.uk/archives/175-Solving-permission-problems-with-parsepath.pl.html#comments</comments>
    <wfw:comment>http://freebsd.munk.me.uk/wfwcomment.php?cid=175</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://freebsd.munk.me.uk/rss.php?version=2.0&amp;type=comments&amp;cid=175</wfw:commentRss>
    

    <author>nospam@example.com (munk)</author>
    <content:encoded>
    &lt;a href=&quot;http://sial.org/howto/debug/unix/parsepath&quot;  title=&quot;parsepath.pl - a script for solving file permission problems&quot;&gt;parsepath.pl&lt;/a&gt; is a brilliant perl script for fixing permissions problems on Unix based platforms by &lt;a href=&quot;http://sial.org/&quot;  title=&quot;Jeremy Mates&quot;&gt;Jeremy Mates&lt;/a&gt;.   Probably the most common type of permission problem from a sysadmin/webmaster&#039;s viewpoint is uploading a file to a directory in a website&#039;s document root folder and then trying to access the file or script in a web browser only to get the dreaded 403 error message:&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;Forbidden&lt;br /&gt;
You don&#039;t have permission to access /foo/bar/test.php on this server.&lt;br /&gt;
&lt;/blockquote&gt;&lt;br /&gt;
&lt;br /&gt;
Most time the solution is very simple, just change the permissions on &#039;test.php&#039; to make sure the user the webserver runs as can read the file correctly - the simplest and most common method being to change the mode of the file to &#039;755&#039;:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;chmod&amp;#160;755&amp;#160;test.php&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
Unfortunately sometimes it&#039;s not that easy and many times you see users asking &#039;I&#039;m getting &#039;access denied&#039; errors even though I&#039;ve changed the perms to 755&#039;.  The problem is that one of the subdirectories that the &#039;test.php&#039; file lives in has permissions set so that the webserver can&#039;t read the file properly.  Now that&#039;s where the headache comes in :)&lt;br /&gt;
&lt;br /&gt;
However, &lt;a href=&quot;http://sial.org/howto/debug/unix/parsepath&quot;  title=&quot;parsepath.pl - a script for solving file permission problems&quot;&gt;parsepath.pl&lt;/a&gt; can take the headache out of fixing permissions problems.&lt;br /&gt;
&lt;br /&gt;
Say you have a website document root directory tree /usr/local/www/web/www.munk.me.uk/foo/bar and you upload a web script &#039;test.php&#039; into that directory.  You try and access the file in a webbrowser but get the 403 permission denied error above.  First off you check the permissions on the file itself:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;&amp;#91;23&amp;#58;58&amp;#58;17&amp;#93;&amp;#160;root@users&amp;#160;/usr/local/www/web/www.munk.me.uk/foo/bar#&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;;&amp;#160;ls&amp;#160;-l&lt;br /&gt;
total&amp;#160;0&lt;br /&gt;
-rwxr-xr-x&amp;#160;&amp;#160;1&amp;#160;www&amp;#160;www&amp;#160;&amp;#160;0&amp;#160;Sep&amp;#160;&amp;#160;4&amp;#160;23&amp;#58;39&amp;#160;test.php&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
That looks ok, with permissions 755 and the owner/group set to &#039;www&#039; the webserver user &#039;www&#039; should be able to read the file ok.  So in this case the problem must be with the permissions on one of the parent subdirectories.  The old method of working out the perms would be either to trawl one by one through each directory checking the perms on each subdirectory or to change the permissions recursively on the document root folder so all subfolders have the read bit set for the webserver user/group.&lt;br /&gt;
&lt;br /&gt;
With parsepath.pl things are a lot simpler though - just run the following command:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;&amp;#91;0&amp;#58;03&amp;#58;21&amp;#93;&amp;#160;root@users&amp;#160;/usr/local/www/web/www.munk.me.uk/foo/bar#&amp;#160;parsepath.pl&amp;#160;user=www&amp;#160;+r&amp;#160;test.php&lt;br /&gt;
!&amp;#160;group=www&amp;#160;+rx&amp;#160;fails&amp;#58;&amp;#160;d&amp;#160;0700&amp;#160;root&amp;#58;www&amp;#160;/usr/local/www/web/www.munk.me.uk/foo&lt;br /&gt;
!&amp;#160;unix-other&amp;#160;+rx&amp;#160;fails&amp;#58;&amp;#160;d&amp;#160;0750&amp;#160;root&amp;#58;wheel&amp;#160;/usr/local/www/web/www.munk.me.uk/foo/bar&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
With this command parsepath.pl  recurses through each subdirectory below the file/path you feed it on the commandline and tells you the permissions problems - if any - for the user &#039;www&#039; (the user=www argument) to read (the +r argument) the file &#039;test.php&#039;.&lt;br /&gt;
&lt;br /&gt;
In the output, we&#039;re told that permissions to read the test.php by the user www fails on two counts:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;#&amp;#160;the&amp;#160;group&amp;#160;bit&amp;#160;on&amp;#160;the&amp;#160;folder&amp;#160;&#039;foo&#039;&amp;#160;doesn&#039;t&amp;#160;have&amp;#160;the&amp;#160;+rx&amp;#160;flag&amp;#160;set&amp;#58;&lt;br /&gt;
!&amp;#160;group=www&amp;#160;+rx&amp;#160;fails&amp;#58;&amp;#160;d&amp;#160;0700&amp;#160;root&amp;#58;www&amp;#160;/usr/local/www/web/www.munk.me.uk/foo&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;the&amp;#160;other&amp;#160;bit&amp;#160;on&amp;#160;the&amp;#160;folder&amp;#160;&#039;bar&#039;&amp;#160;doesn&#039;t&amp;#160;have&amp;#160;the&amp;#160;+rx&amp;#160;flag&amp;#160;set&amp;#58;&lt;br /&gt;
!&amp;#160;unix-other&amp;#160;+rx&amp;#160;fails&amp;#58;&amp;#160;d&amp;#160;0750&amp;#160;root&amp;#58;wheel&amp;#160;/usr/local/www/web/www.munk.me.uk/foo/bar&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
With this information it&#039;s easy enough to go in and make the changes necessary to fix the problem using &#039;chmod g+rx foo foo/bar&#039;.&lt;br /&gt;
&lt;br /&gt;
There are other ways of invoking parsepath.pl though.  Running it just with a file/path as an argument it&#039;ll tell you the permissions on each subdirectory under it:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;&amp;#91;0&amp;#58;10&amp;#58;33&amp;#93;&amp;#160;root@users&amp;#160;/usr/local/www/web/www.munk.me.uk/foo/bar#&amp;#160;&lt;br /&gt;
&amp;#62;&amp;#160;parsepath.pl&amp;#160;/usr/local/www/web/www.munk.me.uk/foo/bar/test.php&lt;br /&gt;
%&amp;#160;/usr/local/www/web/www.munk.me.uk/foo/bar/test.php&lt;br /&gt;
d&amp;#160;0755&amp;#160;root&amp;#58;wheel&amp;#160;/&lt;br /&gt;
d&amp;#160;0755&amp;#160;root&amp;#58;wheel&amp;#160;/usr&lt;br /&gt;
d&amp;#160;0755&amp;#160;root&amp;#58;wheel&amp;#160;/usr/local&lt;br /&gt;
d&amp;#160;0755&amp;#160;root&amp;#58;wheel&amp;#160;/usr/local/www&lt;br /&gt;
d&amp;#160;0770&amp;#160;www&amp;#58;wheel&amp;#160;/usr/local/www/web&lt;br /&gt;
d&amp;#160;0750&amp;#160;www&amp;#58;www&amp;#160;/usr/local/www/web/www.munk.me.uk&lt;br /&gt;
d&amp;#160;0700&amp;#160;root&amp;#58;www&amp;#160;/usr/local/www/web/www.munk.me.uk/foo&lt;br /&gt;
d&amp;#160;0750&amp;#160;root&amp;#58;wheel&amp;#160;/usr/local/www/web/www.munk.me.uk/foo/bar&lt;br /&gt;
f&amp;#160;0755&amp;#160;root&amp;#58;www&amp;#160;/usr/local/www/web/www.munk.me.uk/foo/bar/test.php&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
which can is better to see a whole tree in one go.  &lt;br /&gt;
&lt;br /&gt;
No permissions were harmed in the making of this article!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
I&#039;ll include the parsepath.pl script in the extended article just in case the original ever gets lost - big credit of course goes to the author of the script, &lt;a href=&quot;http://sial.org/&quot;  title=&quot;Jeremy Mates&quot;&gt;Jeremy Mates&lt;/a&gt;.  His site is actually very interesting from a sysadmin&#039;s point of view containing lots of interesting admin scripts and thoughts on system administration in general - spent quite a while grazing through his stuff there - cheers Jeremy.&lt;br /&gt;
 &lt;br /&gt;&lt;a href=&quot;http://freebsd.munk.me.uk/archives/175-Solving-permission-problems-with-parsepath.pl.html#extended&quot;&gt;Continue reading &quot;Solving permission problems with parsepath.pl&quot;&lt;/a&gt;
    </content:encoded>

    <pubDate>Mon, 04 Sep 2006 22:41:00 +0000</pubDate>
    <guid isPermaLink="false">http://freebsd.munk.me.uk/archives/175-guid.html</guid>
    <creativeCommons:license>http://creativecommons.org/licenses/by/2.5/</creativeCommons:license>
</item>
<item>
    <title>Eliminate SSH Passwords in Putty</title>
    <link>http://freebsd.munk.me.uk/archives/169-Eliminate-SSH-Passwords-in-Putty.html</link>
            <category>SSH</category>
            <category>Windows</category>
    
    <comments>http://freebsd.munk.me.uk/archives/169-Eliminate-SSH-Passwords-in-Putty.html#comments</comments>
    <wfw:comment>http://freebsd.munk.me.uk/wfwcomment.php?cid=169</wfw:comment>

    <slash:comments>4</slash:comments>
    <wfw:commentRss>http://freebsd.munk.me.uk/rss.php?version=2.0&amp;type=comments&amp;cid=169</wfw:commentRss>
    

    <author>nospam@example.com (munk)</author>
    <content:encoded>
    I use &lt;a href=&quot;http://www.chiark.greenend.org.uk/~sgtatham/putty/&quot;  title=&quot;Putty SSH Client&quot;&gt;the SSH client Putty&lt;/a&gt; to login to my FreeBSD server from my Windows workstation and entering passwords on the commandline can be a chore.  Starting up Putty and connecting to servers you use regularly via SSH can be made a lot easier though by using secure keys, allowing you to fire up putty and get &#039;auto logged in&#039; without the need for password entry.&lt;br /&gt;
&lt;br /&gt;
The idea is you create a pair of keys on the machine from which you&#039;ll be logging in via SSH, one public and one private.  The public key is stored on the server to which you&#039;re logging into and the private key is stored at the machine you&#039;ll be logging in from.  Once both keys are in place, you can associate your private key with the SSH session associated with the server where the public key lives at the other end.  Now when you login you don&#039;t need to enter a password - Putty sends the public key information along at the authentication phase, the remote server checks the private key against the public key it finds on the server and if everything checks out you&#039;re logged in without being prompted for a password.&lt;br /&gt;
&lt;br /&gt;
There&#039;s a great article for &lt;a href=&quot;http://www.unixwiz.net/techtips/putty-openssh.html#keypair&quot;  title=&quot;setting up a public / private key pair in puTTYgen&quot;&gt;setting up a key pair in the PuTTYgen utility and installing the key on the remote server on this site&lt;/a&gt;, the basic steps are:&lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;&lt;li/&gt;Run the PuTTYgen key pair generator to create a public and private key - &lt;a href=&quot;http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html&quot;  title=&quot;http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html&quot;&gt;PuTTYgen can be found here on the Putty site&lt;/a&gt;. Note if you don&#039;t want to have to enter any password at all (see security disclaimer below!), do not enter a passphrase to protect the private key&lt;br /&gt;
&lt;li/&gt;Copy the public key into the file ~/.ssh/authorized_keys (or ~/.ssh/authorized_keys2 for SSH2, though I think SSH2 still reads ~/.ssh/authorized_keys).  The public key usually looks similar to this:&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;ssh-rsa&amp;#160;AAAAB3NzaC1yc2EAAAABIwAAAIEApiVHreNg3Xnmrl9lo6nCpsb+&lt;br /&gt;
iytNHBZDztLshEhLaucQCs66b0K2fjpfl3KheQB9lpIBBtL0NaPZslo1fVGnJT8fF4ZywIyNuKzN8cYM+&lt;br /&gt;
zfIMjqe8UxQD1QfE1s4QxBhgyihGb6PwQkYJeWfMaINq5pnuHzx2Fc1kWRMdaGQvSU=&amp;#160;munk@winny&lt;br /&gt;
&amp;#40;actually&amp;#160;broken&amp;#160;up&amp;#160;so&amp;#160;it&amp;#160;displays&amp;#160;properly&amp;#160;here,&amp;#160;usually&amp;#160;a&amp;#160;single&amp;#160;line&amp;#41;&lt;/div&gt;&lt;br /&gt;
&lt;li/&gt;Ensure the ~/.ssh/authorized_keys file is readable only by the user:&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;chmod&amp;#160;600&amp;#160;~/.ssh/authorized_keys&lt;/div&gt;&lt;br /&gt;
&lt;li/&gt;Finally, tell Putty to use the private key for all SSH sessions with the remote server.  This is done by selecting the session settings in putty for the server that has the public key, clicking &#039;Load&#039;, selecting the  &#039;Connection, SSH, Auth&#039; setting page and then selecting the private key file on that page using the Browse button.  Save the settings by going back to the &#039;Session&#039; page tab and hitting &#039;Save&#039;.&lt;/ol&gt;&lt;br /&gt;
&lt;br /&gt;
If all went well and the private key was created without using a password phrase, this should allow you to login to the remote server without the need for a password.&lt;br /&gt;
&lt;br /&gt;
However &lt;strong&gt;IMPORTANTLY&lt;/strong&gt; it should be stressed for obvious reasons using no passphrase to protect the private key should only be contemplated on a machine on which you know noone else uses or has access to - either physically or across a network.  I feel like a dirty security whore in a way for even suggesting not using a security passphrase for the private key, but the truth of it is noone has physical access to this machine who would care about accessing my servers so ...&lt;br /&gt;
&lt;br /&gt;
I&#039;m off to setup a passphrase now...&lt;br /&gt;
&lt;br /&gt;
:) 
    </content:encoded>

    <pubDate>Wed, 30 Aug 2006 18:42:02 +0000</pubDate>
    <guid isPermaLink="false">http://freebsd.munk.me.uk/archives/169-guid.html</guid>
    <creativeCommons:license>http://creativecommons.org/licenses/by/2.5/</creativeCommons:license>
</item>

</channel>
</rss>