0

I have a VPS that I use as an openvpn and web server. For some reason, my apache log files are filled with thousands of these hack attempts:

"POST /xmlrpc.php HTTP/1.0" 404 395

These attack attempts fill up 90% of my logs. I think it's a WordPress vulnerability they're looking for. Obviously they are not successful (I don't even have Wordpress on my server), but it's annoying and probably resource consuming as well. I am trying to write a bash script that will do the following:

  1. Search the apache logs and grab the offending IP's (even if they try it once),
  2. Sort them into a list with each unique IP on a seperate line,
  3. And then block them using the IP table rules.

I am a bash newb, and so far my script does everything except Step 3. I can manually block the IP's, but that's tedious and besides, this is Linux and it's perfectly capable of doing it for me. I also want the script to be customizable so that I (or anyone else who wants to use it) can change the variables to suit whatever situation I/they may deal with in the future. Here is the script so far:

#!/bin/bash
##IP LIST GENERATOR
##Author Chev Young
##Script to search Apache logs and list IP's based on custom filters
##
##Define our variables:
DIRECT=~/Script ##Location of script&where to put results/temp files
LOGFILE=/var/log/apache2/access.log ## Logfile to search for offenders
TEMPLIST=xml_temp ## Temporary file name
IP_LIST=ipstoban ## Name of results file
FILTER1=xmlrpc ## What are we looking for? (Requests we want to ban)
cd $DIRECT
if [ ! -f $TEMPLIST ];then touch $TEMPLIST ##Create temp file
fi
cat $LOGFILE | grep $FILTER1 >> $DIRECT/$TEMPLIST ## Only interested in the IP's, so:
sed -e 's/\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\).*$/\1/' -e t -e d $DIRECT/$TEMPLIST | sort | uniq > $DIRECT/$IP_LIST
rm $TEMPLIST ## Clean temp file
echo "Done. Results located at $DIRECT/$IP_LIST"

So I need help with the next part of the script, which should ban the IP's (incoming and perhaps outgoing too) from the resulting $IP_LIST file. I don't care if it utilizes UFW or IPTables directly, as long as it bans the IP's. I'd probably run it as a cron task. What I'm having trouble with is understanding how to use line of the result file as a seperate variable to do something like:

ufw deny $IP1 $IP2 $IP3, ect

Any ideas? Thanks.

4
  • 1
    Have you checked fail2ban? It seems it has a module for doing exactly what you want with apache... fail2ban.org/wiki/index.php/Apache
    – Rmano
    Jun 2, 2014 at 18:43
  • +1 for fail2ban, it already does exactly what you're looking to do. All you need to do is create a configuration file specifying what log files to watch and what regular expression applies to bad connection attempts. Jun 2, 2014 at 18:45
  • I've been reading about it today and trying to understand how to do this using it. Since I can already generate the list, if there is a way to have fail2ban automatically block whatever IP's are in that list then that would work if it would be easier. I got pretty lost trying to figure it out though. Can someone explain how I could do this with fail2ban?
    – Chev_603
    Jun 2, 2014 at 18:48
  • So I would create a file in /etc/fail2ban/filters.d containing the rules and then edit my jail.conf to apply it? Is this correct and could someone help me with the syntax?
    – Chev_603
    Jun 2, 2014 at 19:51

1 Answer 1

1

If you properly install fail2ban there's little need for the script above. Soon as the attacker attempts n times again they'll be banned for whatever time you set in the config.

2
  • Okay, well I have not been able to successfully get fail2ban to ban these IP's. Would someone please help me figure out the correct filter syntax to ban an IP with this expression from /var/log/apache2/error.log: "50.7.139.116 - - [08/Jun/2014:11:59:30 +0000] "POST /xmlrpc.php HTTP/1.0" 404 395 "-" "Mozilla/4.0 (compatible: MSIE 7.0; Windows NT 6.0)"
    – Chev_603
    Jun 10, 2014 at 21:53
  • This is an old question, but the issue has been resolved. As brad scott said, once fail2ban was successfully configured, there was no need for this script.
    – Chev_603
    Mar 1, 2015 at 17:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .