DenyHosts on Snow Leopard
Ever noticed your system log is chock full of this crap?
Jan 31 01:11:43 hostname sandboxd[18371]: sshd(18375) deny mach-per-user-lookup
Jan 31 01:11:46: --- last message repeated 8 times ---
Jan 31 01:11:46 hostname sandboxd[18371]: sshd(18377) deny mach-per-user-lookup
Jan 31 01:11:49: --- last message repeated 8 times ---
Jan 31 01:11:49 hostname sandboxd[18371]: sshd(18379) deny mach-per-user-lookup
Jan 31 01:11:52: --- last message repeated 4 times ---
Check the secure log file and you'll likely find thousands of ssh login attempts from a small number of IP addresses, trying various generic usernames.
Enter DenyHosts. It's a mature, configurable Python script that monitors your log and adds entries to /etc/hosts.deny if things look suspicious.
It's quite portable and there are various instructions for older versions of Mac OS X, but there's a couple of gotchas for Snow Leopard, Mac OS X 10.6, that don't appear to be addressed in one location anywhere else. Here's how to get DenyHosts up and running on OS X 10.6:
- Download the tar.gz file from the download page.
- Unless you're a command line purist who doesn't need to look up the tar man page every time you use, just double click the downloaded file to unpack it.
- Now drop into Terminal and cd to the freshly unpacked DenyHosts directory.
- Run the installer: sudo python setup.py install
- Optionally, move the installed files into local: sudo mv /usr/share/denyhosts /usr/local/share/
- Change to the install directory: cd /usr/local/share/denyhosts
- Copy the example config file: sudo cp denyhosts.cfg-dist denyhosts.cfg
- Edit it: sudo vi denyhosts.cfg
- Find and set these settings:
- SECURE_LOG = /private/var/log/secure.log
- WORK_DIR = /usr/local/share/denyhosts/data
- LOCK_FILE = /var/run/denyhosts.pid
- DAEMON_LOG = /private/var/log/denyhosts
- Optionally set this to allow entries older than 10 weeks to be removed: PURGE_DENY = 10w
- Copy the example run script: sudo cp daemon-control-dist daemon-control
- Edit it: sudo vi daemon-control
- Find and set these settings:
- DENYHOSTS_BIN = "/Library/Frameworks/Python.framework/Versions/2.4/bin/denyhosts.py"
- DENYHOSTS_LOCK = "/var/run/denyhosts.pid"
- DENYHOSTS_CFG = "/usr/local/share/denyhosts/denyhosts.cfg"
- PYTHON_BIN = "/usr/bin/env python2.4"
- Create the hosts.deny file in case it's not there: sudo touch /etc/hosts.deny
- And finally, kick off the daemon: sudo ./daemon-control start
You can monitor progress via the log at /var/log/denyhosts. You could also create a launchd service to ensure the daemon runs at boot up, but if you reboot as rarely as me, you might save yourself 10 minutes and skip it.
Comments
Well this seemed like a glimmer of hope after the macports version didn't seem to want to work. Alas, you're method does not work for me. I'm running Snow Leopard 10.6.2 and my OS doesn't come with python 2.4. It has 2.6 instead, and thus the /Library/Frameworks/Python.framework/Versions directory only contains 2.6. I tried adjusting the DENYHOSTS_BIN = "/Library/Frameworks/Python.framework/Versions/2.6/bin/denyhosts.py" and PYTHON_BIN = "/usr/bin/env python2.6" but that didn't work, I get the following message:
starting DenyHosts: /usr/bin/env python2.6 /Library/Frameworks/Python.framework/Versions/2.6/bin/denyhosts.py --daemon --config=/usr/share/denyhosts/denyhosts.cfg
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.6/bin/denyhosts.py", line 5, in module
import DenyHosts.python_version
ImportError: No module named DenyHosts.python_version
Any thoughts?
Posted by: Jeremy A | February 12, 2010 2:09 PM
Jeremy, have a look in
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/
or similar for a folder called DenyHosts. Some how it looks like the bin (denyhosts.py) has been installed in Versions/2.6, but the site-packages hasn't.
Posted by: Heath Raftery | February 12, 2010 5:24 PM
Yeah well see, in my /Library/Frameworks/Python.framework/Versions/ I only have 2.6. I didn't think Snow Leopard came with 2.4.
Posted by: Jeremy A | February 13, 2010 10:35 AM
Same problem with MacOSX Server 10.6.2. It doesn't have /Library/Frameworks/Python.framework, but /System/Library/Frameworks/Python.framework. Any solution? Thanks in advance
Posted by: Juan F. | March 13, 2010 4:15 AM