How to protect your Linux home server from automated scripts

1
Categories: Linux
Posted on: 25th May 2009 by: Andrei

Recently, while I was browsing through some of the ftp logs on my Ubuntu server, I’ve noticed some unsuccessfull login attempts with random usernames, spamming every few seconds.

Here’s a few of the solutions I have found to avoid such scripts from wasting too many of your resources:

  • Change the default ports of your services (ftp, ssh etc)
  • Blockhosts.py - scans system logs, and looks for failed login attempts. It keeps a record of the number of times a particular IP address had a failed login. When the count exceeds a configured value, that IP address is added to /etc/hosts.allow with a deny flag, so the next time that IP address attempts to connect to that box, they will get a refused connection message.
  • 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.
  • SSH Dictionary Attack Prevention with iptables – can also be adapted for use with other ports

Fedora 11 will be released in 2 weeks

2
Categories: Linux, News
Posted on: 12th May 2009 by: Andrei

The new version of Fedora, codename “Leonidas” will be released on the 26th this month.
The most notable improvements are:

  • 20 second startup
  • Automatic Fonts & Mime Installer
  • ext4 Default file system
  • Windows Cross-compiler

For a complete list of features, go to http://fedoraproject.org/wiki/Releases/11/FeatureList.
The release schedule can be found here http://fedoraproject.org/wiki/Releases/11/Schedule

How to backup a remote site from your linux server/computer

2
Categories: Bash Scripting, Linux
Posted on: 6th May 2009 by: Andrei

This is a little script I’ve wrote to backup some of the remote sites I have on other servers.

This script will ask you for input but if you want to use this as a cron job, just define the variables with your server information.

All you need is the YAFC ftp client to download the files. You can, of course replace the part where I used that specific client with code that uses your own preferred client. YAFC is great for the job because of it’s ability to download folders recursively.

You can get YAFC at http://yafc.sourceforge.net/ or if you use an Ubuntu or Debian distribution just

sudo apt-get install yafc

.


#!/bin/bash

# ======= BACKUP PROCEDURE ======

echo -n "Server address: "
read SERVER
echo -n "Username: "
read USERNAME
echo -n "Password: "
read PASSWORD
echo -n "Remote folder: "
read RFOLDER

# get current date to use as a name for the backup
DATE=$(/bin/date +%Y%m%d)

# folder on local machine where the backups should be put
FOLDER="/media/storage/#BACKUPS/${SERVER}/"

# append the date to the folder name
BACKUP=${FOLDER}${DATE}

# create the backup folder in case it doesn't exist
mkdir $FOLDER

# create the current backup folder
mkdir $BACKUP

# we use the yafc ftp client to download the files from the remote server recursively
yafc <<**
open ftp://$USERNAME:$PASSWORD@$SERVER/
cd $RFOLDER
get -f -r -o $BACKUP *
close
**
# if you wish to backup a remote database too and put it in the current backup folder, uncomment the next line and replace {dbUser},{password},{dbName},{host} with your database information

#mysqldump -u {dbUser} -p{password} -h {host} {dbName} > ${BACKUP}/${DATE}.sql

# next we archive the current backup folder to take less space

tar -czf ${FOLDER}${DATE}.tgz $BACKUP

# we delete the backup folder since we have the archive now
rm -rf $BACKUP

# ====== END ======

How to copy a folder recursively in C#

0
Categories: .NET, C#, Linux
Posted on: 5th May 2009 by: Andrei

using System;
using System.IO;

namespace cpFolder
{
	class Program
	{
		static void Main( string[] args )
		{
			copyFolder( @"C:\source", @"C:\destination" );
            Console.ReadLine();
        }
        static public void copyFolder( string sourceFolder, string destFolder )
        {
            if (!Directory.Exists( destFolder ))
            Directory.CreateDirectory( destFolder );
            string[] files = Directory.GetFiles( sourceFolder );
            foreach (string file in files)
            {
                string name = Path.GetFileName( file );
                string dest = Path.Combine( destFolder, name );
                File.Copy( file, dest );
            }
            string[] folders = Directory.GetDirectories( sourceFolder );
            foreach (string folder in folders)
            {
                string name = Path.GetFileName( folder );
                string dest = Path.Combine( destFolder, name );
                copyFolder( folder, dest );
            }
        }
    }
}