Fail2ban on Leopard Server

So here I am running my own server — for almost 2 years now. It’s been a real learning experience and I’ve tried to share. My latest add-on has been fail2ban. I got tired of looking into my logs and seeing where script kiddies or bots were trying to take control of my server. Fortunately, none have.

After a bit of googling, I found fail2ban. It’s a collection of python scripts.

Fail2ban scans log files like /var/log/pwdfail or /var/log/apache/error_log and bans IP that makes too many password failures. It updates firewall rules to reject the IP address.

There are a few tricks I’ve discovered along the way to make it work on my installation and likely on Mac OS X Server in general.

First is that fail2ban creates a PID and socket file in a directory that it fails to create. Yeah, that’s a bug. Since I didn’t want to mess around with the actual scripts in the program, I created a plist that issues the mkdir /var/run/fail2ban command. I placed this in /System/Library/LaunchDaemons and set it to Run at Load. Lingon is your friend, but’s now inactive. :-(

After creating the file you have to use the command line to move it to the /System/Library/LaunchDaemons directory. I also created another launchd plist to reload fail2ban every day. I did this because I run multiple virtual websites and the error logs for those sites get rotated and the names have some time code or something tacked on the end of the filename.

OK, problem 1 solved. Next I discovered that since fail2ban is really running on a multitude of linux boxes all the different methods of IP tracking, sorting etc. were really useless on my OS X Server. I run ipfw firewall and fortunately there’s a module for that in fail2ban. Unfortunately it’s not quite set up correctly, at least it wasn’t for me. I had to tweak it a bit.

What this means is that your action is always going to be ipfw. I tweaked the ipfw.conf file a bit. Now it does the following.

  1. Logs it’s action to ipfw.log
  2. Adds a rulenum to the ipfw command. I did this because some other rule in my setup was allowing the IP before my deny could take effect. By lowering the rulenum my deny now fires off first.
  3. Abstracted the protocol (tcp, udp) to pass as a variable. Just in case something you want to block isn’t tcp.

I also created another filter as I found many times some machine would excessively hit my Apache server looking for nonexistent files. Since it sounds like something a bot would do I decided to ban it. This was the simple creation of a new filter.

I created a jail.local file to hold all my prefs and through trial and error discovered that the examples of how to call for a jail weren’t working for me. Perhaps I just didn’t understand the examples. I soon discovered that parameters for the jail action needed to be passed inside of square brackets in the prefs.

I’m sure, if you’ve gotten this far that you’re either very confused by this whole post or that you’ve had an epiphany. To further the epiphany along I’ve uploaded my file changes.

You should be able to figure out what file goes where from the folder structure of the upload.

A couple of things in summary to remember. First, turn on your server’s firewall. Then make sure you change your server’s local IP address in the files to match your own. That’s the setting for localhost.

Good luck. If you have any questions leave a comment.

Related posts:

  1. Fail2ban Problems and Solutions If you use Fail2ban then you are probably aware of...
  2. Fail2ban and OS X Server, part deux As some of you might know I run my own...
  3. Checking Fail2ban regex I’ve just stumbled across a great command in Fail2ban to...
  4. Forwarding Email in Leopard Server, part 2 I’ve previously written about problems with Leopard server and forwarding...
  5. Setting up Snow Leopard iCal Server After scouring through the official documentation, and finding that lacking,...

Related posts brought to you by Yet Another Related Posts Plugin.

This entry was posted in code, osx-server and tagged , , . Bookmark the permalink.

7 Responses to Fail2ban on Leopard Server

  1. Robert Tolton says:

    Hi,

    I’m trying to run this but I get:

    cp: /etc/fail2ban/action.d: No such file or directory cp: /etc/fail2ban/filter.d: No such file or directory cp: /etc/fail2ban/filter.d: No such file or directory cp: /etc/fail2ban/filter.d: No such file or directory cp: /etc/fail2ban/filter.d: No such file or directory cp: /etc/fail2ban/filter.d: No such file or directory usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] sourcefile targetfile cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] sourcefile … targetdirectory

    Any ideas? Many thanks!

  2. Andy Fragen says:

    Have you already installed Fail2ban prior to running my updates?

  3. Robert Tolton says:

    Ah, no I don’t think I did sorry ^^;

    Is there an easy install for it?

  4. Andy Fragen says:

    @Robert, got to the main Fail2ban page, linked above, and download the Mac OS X install package. After that you should try running my mods. ;-)

    Let me know how it works.

  5. Robert Tolton says:

    The link to the Mac OS X Installer doesn’t seem to be loading?

    http://macenv.lsa.umich.edu/software.php

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.