Sabtu, 18 Februari 2012

ipTables Tutorial | ipTables Ubuntu *Updated*

ipTables Tutorial


ipTables is definitely confusing to begin with, but for an unmanaged VPS server it is a completely necessary part. This ipTables tutorial will not necessarily be as in-depth as some which are available, simply as I don't know as much! However, I will link to any ipTables tutorial which I have used or I think is worthy of a read.

UPDATE: Automatically update your iptables with Spamhaus Drop List of Spam IPs

Skip to it here

ipTables, Tables, Chains and Rules


There a different parts of ipTables which make up the structure, but it is only that; a structure. It isn't particularly hard to underdtand the structure, but it will make a big difference as to understanding ipTables.

Structure - you can have multiple tables, and with those tables, multiple chains, and within those chains, multiple rules.


Table:

  Chain 1

        Firewall Rule 1

        Firewall Rule 2

  Chain 2

        Firewall Rule 1

        Firewall Rule 2

        Firewall Rule 3


So, a collection firewall rules make up a chain, and a collection of chains make up a table.

There are a number of pre-configured tables when you install ipTables, and they are "Filter Table", "NAT Table", "Mangle Table", and "Raw table".

"Filter" is the default table which is used if you do not add your own table, and This is the table I will be altering.

Filter contains the following chains:


INPUT - Incoming

OUTPUT - Outgoing

FORWARD - For sending data to other computers on the same server


ipTables rules work in a similar way to conditional statements in languages such as PHP. They contain a condition which is either matched or not matched and a consequent ("target") if the condition is met. The consequent can either be rules or the execution of special values


ipTables use the structure:


[rule number] [target] [protocol] [option] [source] [destination]

The values which can be specified in "Target" are:



[Accept] The firewall let the data through
[Drop] The firewall ignore the data
[Queue] The firewall the data is made available to "userland" applications
[Return] The next set of rule in the chain are ignored, and instead, control is returned to the "calling" chain.


Before I add any rules, I first need to install ipTables using the command "sudo apt-get install iptables". For good practice, I will export the default ruleset with the command "sudo iptables-save > /home/randoof/iptables.default.rules

Next, I need to create my ruleset for the "Filter" table by first creating a file "nano /home/randoof/iptables.rules.new" and then using the following rules (which you can find here)


*filter

# Allows all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
-A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT

# Accepts all established inbound connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# Allows all outbound traffic
# You could modify this to only allow certain traffic
-A OUTPUT -j ACCEPT

# Allows HTTP and HTTPS connections from anywhere (the normal ports for websites)
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT

# Allows SSH connections for script kiddies
# THE -dport NUMBER IS THE SAME ONE YOU SET UP IN THE SSHD_CONFIG FILE
-A INPUT -p tcp -m state --state NEW --dport 30000 -j ACCEPT

# Now you should read up on iptables rules and consider whether ssh access
# for everyone is really desired. Most likely you will only allow access from certain IPs.

# Allow ping
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT

# log iptables denied calls (access via 'dmesg' command)
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7

# Reject all other inbound - default deny unless explicitly allowed policy:
-A INPUT -j REJECT
-A FORWARD -j REJECT

COMMIT


Once I have saved the file, I then need to import it into iptables with the command "sudo iptables-restore < iptables.new.rules"

The next thing I need to do is ensure that my ruleset is restored to iptables at every system reboot. I can do this by editing the "rc.local" file located in the "etc" directory, which executes any listed commands when the system reboots. So "sudo nano /etc/rc.local". Currently, my rc.local file has no commands, so I just need to add the iptables-restore command so it will be run automatically.

The iptables binaries are stored in the /sbin directory, so it's important to include that in the restore command, so: "/sbin/iptables-restore < /home/randoof/iptables.new.rules".

Make sure to change the port number to the one you configured in the "sshd_config" file.






Once that is complete, all I need to do is reboot restart ssh with the command "/etc/init.d/ssh restart" and then I will reboot the system - "reboot" - and log back in, then check my ipTables configurations have been loaded using the command "sudo iptables -L", which then lists all of my tables, chains and rules.

Automatically add Spamhaus Drop List to IPtables
Whether you have read the iptables tutorial above or not, one of the most helpful things it to be able to block ips with iptables. You can do this manually by updating your iptables rules file with the following line:

-A INPUT -s 123.456.1.18 -j DROP

All you need to do is input the ip address you wish to block instead of 123.456.1.18

That's all well and good, but what if, rather than reactive, you wish to be proactive and block ips with iptables automatically? Then you need to to automatically add Spamhaus drop list to iptables. That is possible with a little scripting. Below is a file which I have modified slightly in order for me to be able to automatically add Spamhaus drop list to iptables using cron

Let's check our current IPtables rules:
sudo iptables -L

Firstly, create a bash script

nano iptables_update.sh

and paste the following code:



#download the latest spamhaus ip drop list 

wget http://www.spamhaus.org/drop/drop.lasso -O drop.lasso

if [ -n "$1" ]; then
DropList="$1"
else
DropList="./drop.lasso"
fi

cat "$DropList" \
| sed -e 's/;.*//' \
| grep -v '^ *$' \
| while read OneNetBlock ; do
/sbin/iptables -I INPUT -s "$OneNetBlock" -j DROP
/sbin/iptables -I OUTPUT -d "$OneNetBlock" -j DROP
/sbin/iptables -I FORWARD -s "$OneNetBlock" -j DROP
/sbin/iptables -I FORWARD -d "$OneNetBlock" -j DROP
done

Basically, this downloads the latest Spamhaus ip Droplist, and steps through the list, creating the rules for each ip address, automatically adding the ip address to the iptables filter.
Please note in order to automatically add the Spamhaus ip drop list to iptables, you first require wget to be installed (sudo apt-get install wget).

Now, if we check
sudo iptables -L
  

it will now list all of the Spamhaus ip drop list (albeit probably quite slowly, in which case you can cancel with CTRL+C)

Spamhaus requests that you do not download the drop list more than once an hour, and advises just once a day, which is what I do. I run the bash script automactically during a quiet time for my server using Crontab:
 
sudo crontab -e
 
and then adding the line:
 
#automatically add Spamhaus drop list to iptables at 3.45am
45   3   *   *   *   /bin/bash /home/randoof/iptables_update.sh
 
This line will run the bash script as sudo (required to update iptables) at 3.45am every morning.

That's it for another entry, please remember to click an advert if I have helped you :)

As promised, here are some helpful links which I used as reference for what has definitey been the most complicated step in my unmanaged VPS ipTables Tutorial:


Ubuntu Forums
The Geek Stuff ipTables introduction - Extremely helpful.
Debian Wiki ipTables ruleset basic configuration

Kamis, 16 Februari 2012

Ubuntu Change Hostname | Linux Change Hostname | Ubuntu Server Tips

Ubuntu Change Hostname

Not a necessity, but probably worth doing is the Ubuntu change hostname command. By default, an Ubuntu server will have a hostname which has probably been set by your unmanaged server host.


In my case, the hostname is a unique string of numbers. Whilst a unique name is good practice, it is not particularly helpful. For this reason, I am going to change my hostname to something more memorable. The main reason for an Ubuntu change hostname command is so that when you are using SSH, something more recognisable is displayed, so if you have a whole host of servers, you can quickly and easily make sure you are using the correct one.
In order to be safe I will first make a backup of the file responsible for the hostname ("hostname" oddly enough), as I am prone to typos. This makes the process a bit longer, but it also a good opportunity to try some different commands and that's always fun.
So the first command is to copy (cp ) the hostname file which is located in the etc directory so "sudo cp /etc/hostname /etc/hostname.old".
Then I can edit the hostname file to name my hostname to something more helpful. "sudo nano /etc/hostname"
I also need to change the "hosts" file, which follows the same steps: "sudo cp /etc/hosts /etc/hosts.old" and then "sudo nano /etc/hosts".
This hosts file however has a bit more structure to it, and is as follows:

127.0.0.1 localhost.localdomain localhost
12.34.56.78 hostname.domain.com hostname

So for my server, that will become:

127.0.0.1 localhost.localdomain localhost
12.34.56.78 randoof.domain.com randoof

All that's left is to reboot "sudo reboot".

Server Security Tips | Update Ubuntu 10.04

Sever Security Tips: Update and Upgrade Ubuntu

The reason I chose to use Ubuntu 10.04 LTS support is due to the fact it has been out for a while, it is very secure, and there have been a number of updates which patch potential security holes.


However, it's never safe to assume that the version your server has provided is the most upto date version - in fact it would be safe to assume it isn't.
So nezt up on my to do list is to update and upgrade Ubuntu using the two commands "sudo apt-get update" and "sudo apt-get upgrade".
The "apt-get update" command actually updates the APT package index meaning it will download the most up to date packages. Then I am good to go ahead and run the "apt-get upgrade" command to upgrade the system.
With an older version of Ubuntu such as 10.04, there will be quite a bit of updating to do, but that's a good sign - it means the system will be more secure.
Short and sweet.

Rabu, 15 Februari 2012

OpenSSH Add User | Basic SSH Server Security

Basic SSH Server Security

Before I get going with installing new software on my Ubuntu unmanaged server, I want to look at some simple SSH server security in order to give some basic level protection to my server from the get-go.
So the first step for my SSH server security is to remove root access. This basically then means that anyone who wishes to log on to my server will need to know (or guess) the username.



OpenSSH Add User

Now, you may have realised that if I remove root access then that means I will have no way to log on to my server, so obviously I will need to add another user. I will do with the the OpenSSH add user command which is, in its most simplest form "useradd ".

However, I want to set up my user with a diretory, and (in some circumstances full contact details) so I will use the OpenSSH add user command "adduser "
This way, the shell will create a new group, a home directory for the user and will prompt me for a password and any further information that I want to add.
As I am already logged in as the "root" user, who has maximum priveleges, I can basically do whatever I want on the server. However, when you create new users, their privileges are more limited, these can be altered which I will look at as I work through the process.

OpenSSH Add User


Now is probably the opportune moment to point out two points about the PuTTY terminal which Windows users (in particular) may not be familiar with. Firstly, when you enter a password in PuTTY, nothing appears, giving onlookers no idea even how many characters you have typed. Secondly, you cannot paste with ctrl+v, rather you just right click the mouse, and that will paste in whatever you have copied in your clipboard. If you wish to copy and paste in PuTTY, then select the text as normal with the mouse and then right click, and the text will be pasted at the cursor location.

So, now I have my new user "randoof" set up, with limited privleges, a home directory and a password, so the best thing to do is to "logout" and try and log back in as randoof.
Before I can disable the root user login, whenever I need to do anything with more privleges than my user has been granted, I will need to use the "sudo" command, which means "Super User do", at which point I will be prompted to enter my password.
So in order to do that, I will need to log back in as root, and edit a configuration file where you can specify user priviledges.
Logged is as root, the configuration file is called by using the command "nano /etc/sudoers" and adding the line "randoof ALL=(ALL) ALL", and the ctrl+o to save the file.

visudo


The reason this is clever SSH server security, is it means that every command received is logged to a specific user, so if you have a server and 100 possible users who can access it, you can easily figure out which user completed a command. Brilliant, safe and secure.

Disable Root Access

Disable root access is the first step of our SSH server security and it is still a relatively simple process.
The first step is to move to the directory which holds the SSH configuration file, from the root of the server - that is the very top directory - I will use the "cd" command (Change Directory) to navigate to the directory.

cd /etc/ssh

Out of interest, to leading slash means that you will navgate from the root of the
server first. You can always get back to the root of the directory with "cd /".
I have got into the habit of listing the files in the current directory with the "ls" command. This is purely because I am so used to navigating with a gui that I double check I am in the correct location with a list of folders and files.

Now I will use "nano", the built in text editor to edit the SSH configuration file named "sshd_config" as the root user by using the "sudo" command.

sudo nano sshd_config

Once in the file, I look for the line "PermitRootLogin yes" and simply change it to no.

Whilst I am in the sshd_config file, I am also going to change the default port. Again, this is a default setting (port 22), which hackers will know, so I am going to change this to something different by editing the line:
# What ports, IPs and protocols we listen for
Port 22

I am also going to disable X11 forwarding. X11 forwarding is used for graphical access (ie a GUI) and I won't be doing that, so it's just an extra little bit of security.

There's one final line I am going to add for that extra little bit of security and that is simple "AllowUsers randoof simon". This then limits the users who can log in to my server to Randoof and Simon. Perfect.

ctrl+o to write Out to the file (save it), followed by enter to confirm the filename and ctrl+x to exit.





Following that, the SSH server needs to be restarted, using the command
"/etc/init.d/ssh restart". If all went well, you should be informed with the line
* Restarting OpenBSD Secure Shell server sshd [ OK ]

Now if I try to login I will need to change the default port in PuTTY to my newly configured number.
An then if I try to log in as root, I am prompted for a password, and then simply "access denied".

Selasa, 14 Februari 2012

Install Ubuntu 10.04 LTS on an Unmanaged Server

Installing Ubuntu 10.04 LTS on an unmanaged server

So installing Ubuntu 10.04 LTS on an unmanaged server is a pretty straight forward affair and although the process will differ between unmanaged VPS hosts, I am pretty confident in saying all you really need to do is select the Linux version you wish to install and hitting "install".



There are over 30 Linxu distros to choose from for Bhost, but I am going to be installing Ubuntu 10.04 LTS on my unmanaged Server. Specifically, I am opting for "Ubuntu 10.04 minimal i386", there are other versions available with BHost, including "Ubuntu 10.04 minimal amd64" as well as Ubuntu with LAMP (Linux, Apache, MySQL and PHP/Perl/Python) already installed but I wanted a completely clean install so I can configure it exactly how I want it, with no extraneous software hogging the CPU.
For those unfamiliar with the terminology of Linux and Ubuntu, LTS stands for Long Term Support, you can read more about what that means on the Ubuntu wiki.


And with that done, it's on to the fun bit. Next up, downloading PuTTY and getting in on some SSH action to make our unmanaged VPS more secure.

The Best unmanaged VPS | Cheapest unmanaged VPS

The best unmanaged VPS for you

So the first decision you need to make when setting up an unmanaged VPS is to select the best unmanaged VPS for you. This will need to be host who provides the system specifications you need at a price you think is right and that you can afford.


To find the best unmanaged VPS, you need to look at location of the server (choosing one as close the majority of your visitors as possible), the specifications of the virtual server, including the amount of storage space you get, the CPU power, the memory (guaranteed and burst) and the amount of traffic.
Whilst I think the majority of the system specifications are self explanatory, I think it is worth explaining the difference between burst memory and guaranteed memory.
Basically, burst memory is made available to you server in unusual situations, for example if you site suddently made it to the front page of reddit and you saw a mass of people accessing your website - and therefore stressing your server, the extra memory would be made available to help serve up your pages ina timely fashion. It's like when you're exercising, you give yourself a extra push just to get over the brow of the hill or lift that last rep.

Cheapest unmanaged VPS

Whilst it's true that cheapest is not always better, there are exceptions to this rule, and I think that cheapest unmanaged servers may (to some extent) be one of them.
As the name "unmanaged" suggests, there is not a lot of inout on the side of the host, everything is down to you, so you need to make sure you have setup an unmanaged server correctly, covering yourself with firewalls, security and caching software to protect your site from an influx of visitors (we will cover this later).
Personally, I will be setting up Ubuntu on an unmanaged VPS with BHost.net, who from my research offer the cheapest unmanaged servers, atleast in the UK.


So that will be my next step..and blog entry.