Oct 30

Removing X-Powered-By header for mod_rails

Tag: Security, UnixMichael Lind Mortensen @ 4:44 pm

I recently had a bit of a problem with a server running a Ruby on Rails application. I wanted to make sure the server didn’t give out any information about service versions, however mod_rails didn’t give any easy way of doing this within the module itself. I therefore began searching for another way of doing this, and found that an easy solution was to make Apache remove the headers via. the mod_headers module.

So how is it done? Very simple, just enable the module mod_headers and add the snippet below to httpd.conf or another included configuration file in Apache. Both actions have to be done as root of course.

Enable the mod_headers module (This example is Linux Debian - it might be different for your system)

# cd /etc/apache2/mods-available/
# a2enmod headers

Add these lines to httpd.conf

Header always unset "X-Powered-By"
Header always unset "X-Runtime"

Restart the Apache server (Again - this is Debian! It might be different for you)

# apache2ctl restart

And there you go. Try making e.g. a Nikto scan on the server and see if the headers aren’t there anymore.




Sep 28

Simple backup script with notification

Tag: UnixMichael Lind Mortensen @ 2:07 am

So I recently had to come up with a simple backup solution that didn’t involve any fancy system and basically just gave one the opportunity to choose what directories to backup, where to back them up and notify if something goes wrong.

So I came up with a solution which, in my own view, could be better and more “clean”, however this is how it is right now. You’re welcome to take the script, change and redistribute it - it’s released under the BSD license.

You might notice it’s refering to a bunch of files - these files are the error messages sent in e-mails and need to be placed somewhere, with corresponding changes in the script. If they don’t, e-mail notification won’t work.

Right now it’s sending e-mails when it’s succesful in making a backup. Depending on how often you want your cron job to do a backup, it might be irritating to get that many e-mails, so all you need to do is basically just remove the last part so that it only notifies you if something goes wrong.. That will however create the possibility that the e-mail functionality of the server will stop working and you won’t notice.

But yeah.. Here’s the script + all the error-messages zipped for download: backup_script

I’ll be the first to admit it’s not optimal in any way.. it’s a quick solution and has a bunch of DRY (Don’t Repeat Yourself) violations… but hey.. it works and given it’s low complexity I don’t wanna prioritize cleaning it up that much - but you are more than welcome to do so!

Here’s the code copied into the post:

#!/bin/bash
# Copyright (c) 2008, Michael Lind Mortensen
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of Michael Lind Mortensen nor the
#       names of his contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY MICHAEL LIND MORTENSEN ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL MICHAEL LIND MORTENSEN BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
# Define settings
DATA="/home/user1 /home/user2 /etc /var/www/virtual /var/log" #Location of backup files.
DISC="/mnt/backup" # Mount location of backup disc
DEVICE="/dev/sdb1" # Backup devicename
MAILREC="info@example.com" # Define message recipients. Comma-separate for more recipients.
SENDER="noreply@example.com" # Define sender e-mail for error-messaging.
LOG="logfile.txt" # Location of the error logfile
DBPASS="databasepassword" # The root password to the mysql server
 
set $(date)
 
#####################################################
#  Define email functionality for error messaging   #
#                                                   #
# Parameters: MESSAGE - E-mail content from file    #
#                                                   #
#####################################################
function error_mailer {
        MESSAGE=$1
        SUBJECT="Backup process encountered errors!"
        # Send the email using mail
    if mail -s "$SUBJECT" -r "$SENDER"  "$MAILREC" < $MESSAGE
    then
        :
    else
        echo "$3-$2-$6-$4 - ERROR: Mail functionality not working!" >> $LOG
    fi
}
 
#####################################################
#  Define email functionality for success messaging #
#                                                   #
# Parameters: MESSAGE - E-mail content from file    #
#                                                   #
#####################################################
function success_mailer {
        MESSAGE=$1
        SUBJECT="Backup created succesfully!"
        # Send the email using mail
        if mail -s "$SUBJECT" -r "$SENDER" "$MAILREC" < $MESSAGE
        then
                :
        else
                echo "$3-$2-$6-$4 - ERROR: Mail functionality not working!" >> $LOG
        fi
} 
 
# Mount backup disc
if mount $DEVICE $DISC
then
        :
else
        echo "$3-$2-$6-$4 - ERROR: error while mounting disc!" >> $LOG
    error_mailer "/etc/backup_inc/errors/mount_error.txt"
fi
 
# Perform daily full backup
if tar cfz "$DISC/data/data_$3-$2-$6.tgz" $DATA
then
        echo "Data saved successfully!"
else
        echo "$3-$2-$6-$4 - ERROR: error while saving data!" >> $LOG
    error_mailer "/etc/backup_inc/errors/data_error.txt"
fi
 
# Create SQL dumps of databases
if mysqldump -u root --password=$DBPASS --all-databases > "$DISC/database/db_$3-$2-$6.sql"
then
        :
else
        echo "$3-$2-$6-$4 - ERROR: error while making mysqldump!" >> $LOG
    error_mailer "/etc/backup_inc/errors/mysqldump_error.txt"
fi
 
if gzip "$DISC/database/db_$3-$2-$6.sql"
then
        echo "MySQL databases saved and compressed succesfully!"
else
        echo "$3-$2-$6-$4 - ERROR: error while saving mysql databases!" >> $LOG
    error_mailer "/etc/backup_inc/errors/db_error.txt"
fi
 
# Unmount backup disc
if umount $DISC
then
        success_mailer "/etc/backup_inc/success.txt"
else
        echo "$3-$2-$6-$4 - ERROR: error while unmounting disc!" >> $LOG
        error_mailer "/etc/backup_inc/error/umount_error.txt"
fi

The fast person will probably notice that Wordpress writes < and > as &lt; and &gt; .. that’s just the code plugin messing things up and I hope you can figure out how to fix that yourselves.. if not, the zipfile has the code without any odd layout errors.

All in all the code should be fairly self-explanatory.. if not, there are plenty of comments to help you use the script or change it to whatever you need.

Last but not least - Remember to add a cron job with the script.. If you don’t there won’t be much automatic backup about it.

That’s just about it.. Enjoy!




May 24

FreeBSD on Microsoft Hyper-V

Tag: UnixMichael Lind Mortensen @ 10:55 am

So I’ve been looking into the problem of getting FreeBSD running on Microsoft Hyper-V the last few days and then yesterday I finally got it working.

Apparently people have been quite upset that Hyper-V only offered support for Windows based systems and Linux SuSE systems, which is obviously not very bright or community friendly - to be quite honest I would rather use WMWare due to the very few *nix systems Hyper-V officially supports.

However you can’t always pick and choose, and in this particular situation - I couldn’t either! So I had to get it working, so first I tried FreeBSD 7.0 RELEASE with a bootonly .iso and tried to mount that in the virtual machine - however the only thing I got out of that was a disc trying to boot but failing almost immediately with the message:

“Can’t load kernel”

So I tried FreeBSD 6.3 STABLE instead since it was obviously due to the kernel loading with FreeBSD 7.0. But again - no luck and just the same message:

“Can’t load kernel”

So finally I tried FreeBSD 8.0 CURRENT and to my great surprise - this would actually boot and let me install. However there are a few quirks that I haven’t worked out yet. Like the fact that FreeBSD apparently doesn’t detect the network interface given to it by Hyper-V - something that is somewhat of a huge problem.

So anywho - FreeBSD 8.0 CURRENT works with Microsoft Hyper-V… so for all you people I’ve seen with this problem, use FreeBSD 8.0 for your install.

I’ll write more when I’ve done some more extensive benchmarking and configuration.




Apr 13

Mogrify - Unix command

Tag: UnixMichael Lind Mortensen @ 8:13 am

I just found one of the nicest unix commands I’ve had the pleasure of using. I recently had to convert a large number of images from one format to another (due to a crappy image display device without PNG support), so I stumbled onto this little wonder.

It’s very simple to use. So for example if you wanted to convert a directory of PNG images to JPEG, just do:

# mogrify -format jpeg *.png

Simple right?

It can also be used for a bunch of other stuff - like resizing pictures

# mogrify -resize 800x600! somepicture.tiff

Actually you can find a full set of examples here:

http://linux.about.com/od/commands/l/blcmdl1_mogrify.htm

So there you have it - juts thought I would share.