Wednesday, January 2, 2013

Iptables, Logging, and Firewalls in general.

Firewalls are great. Firewalls can now be found on almost any modern operating system. Firewalls are only one step to a secure computer.
A prefect firewall would only allow the good stuff through and stop the bad stuff. The problem is how do you define the good stuff. Is one port or packet better/safer than another? Is this good looking packet carrying some dangerous data? This is why a firewall can stop everything. It can only stop some bad things. So remember firewalls are only one tool in the IT security toolbox.

Iptables:
Iptables is found on Linux and functions as its standard firewall. Iptables uses a ruleset to define what is allowed and what is denied. Don't assume that since you have iptables installed that it will deny incoming packets by default. Some Linux distributions have iptables set to allow incoming traffic as default. Now down to some iptable rules.

The iptables ruleset starts out by defining what iptables is going to do. The ruleset can have both a NAT section and filter section. Usually you will only see the filter section. 

*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]

This part of the filter section defines the default policies for incoming, outgoing, and forwarded packets. In this example all incoming and forwarded packets are dropped by default. This is what you want because you wouldn't want the firewall to allow just any packet on any port from any IP. Having all outgoing packets allowed is fine unless you really what to lock down the system then dropping by default is probably what you will want. Just make sure you create rule to allow outgoing traffic. 

-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

These two rules allows incoming traffic from the loopback and traffic to port 22. The -i defines the incoming interface. The -p and --dport define the protocol and the destination port.

Now the way iptables works is that it goes down the ruleset until it find a match. If it never finds a matching rule then is should use the default but its better safe than sorry. So what we do is add a drop rule at the end of the ruleset. It is also a good idea to log the dropped packets so that you know what is trying to access the system and on what ports. To do this we add a log rule just before the drop rule.

-A INPUT -j LOG --log-prefix "iptables: " --log-level 4
-A INPUT -j DROP

The first line the the log rule. Each packet that doesn't meet the the previous rules is logged and as a prefix of iptables: so it will be easier to identify and if using rsyslog easier to create a log file just for iptables. The log level is the kernel logging level. In this case 4 is kern.warn.

That is a real basic iptables ruleset. I did leave out a few common things like established states. Most of the time there is a simple ruleset on the Linux system already with the most common rules defined for you.

Logging:
Now that we have iptables configured and set to log dropped packets we need to configure the logging system to handle the data from iptables. If your using syslog then setting up the iptables log is fairly simple. Add kern.warn logfile destination to syslog.conf. The issue I have with using syslog for iptables is that iptables will also be logged into the messages log. I'm sure there is a way to configure syslog so that  iptables is only logged to the iptables log but I haven't found one and rsyslog is becoming very common. 

For rsyslog I suggest creating an iptables.conf file in rsyslog.d. This just makes it easier to configure rsyslog later on when adding other log configurations. In iptables.conf add :msg, startswith, "iptables: " -/var/log/iptables.log. If you are using a RHEL 5 based system there are some configurations that needs to be made to /etc/sysconfig/rsyslog and /etc/rsyslog.conf before rsyslog will be able to properly log iptables with the iptable log config. In sysconfig/rsyslog change -m 0 to -c 3 and in /etc/rsyslog.conf add $IncludeConfig /etc/rsyslog.d/*.conf.

Some systems have log rotation configured and its usually on every boot. When you have a system that is up for long periods of time, say months, an on boot log rotation can get messy. To control the log rotation I use logrotate to manage when to rotate the logs. Here is a config file I used for the iptables log. 

/var/log/iptables.log
{
        rotate 7
        daily
        missingok
        notifempty
        dateext
        delaycompress
        compress
        postrotate
                 invoke-rc.d rsyslog reload > /dev/null
        endscript
}

The log is rotated daily for 7 rotations and the older logs are compressed. 

In my example setup iptables will drop all incoming packets except those that are from the loopback or going to port 22. It will also log all the dropped packets in /var/log/iptables.log. The log file iptables.log is rotated everyday and only 7 rotations are kept. 

Thursday, June 28, 2012

Useful utilities for the secure VM

Now that we have a VM that sits on an encrypted data store. We can install utilities that can make the our guest OS more secure.
Since I opted for XUbuntu as my guest OS I'll be using apt-get and ubuntu's package management system.

Ok first I think we need a secure way of deleting data. Since deleting data doesn't actually overwrite the data. We'll need something that does that. Two programs I use are wipe and scrub.

Now we need a way to browse the Internet. I prefer firefox but chrome/chromium is also a good choice. If you want to take to the next level I suggest using tor. Either install tor and a proxy server on the OS or get the tor bundle. The bundle include firefox with some addons that promote safe browsing.


Deletion
wipe
scrub

Internet Browsing
chrome/chromium
firefox
tor bundle

Email/Messaging
Thunderbird
pidgin
torchat
enigmail

aide
snort
apg
ntop
darkstat
nmap
openvpn
strongswan
ufw
firestarter
denyhost
fail2ban

disable root ssh login




Thursday, June 14, 2012

Secure Virutal Machine. Creating

Creating a secure VM using Linux, Encfs, and Virtutal Box.

First install and setup Encfs.
To create an Encfs directory run.

encfs /path/to/encrypted-directory /path/to/mount-directory
encfs ~/.encOS ~/encOS;

This command also mount the directory.

Now time to choose the OS. For this example I chose XUbuntu.

Install Virtual Box and create your VM using the Encfs directory.

Create the VM image in the Encfs directory. I feel an image is more portable than any of the other disk options in Virtual Box.

For an added level of complexity you might want to encrypt the system or your home directory in the VM.
Also use a complex password. I suggest a password with at least 14 characters including letters, numbers, and special characters.
GRC has a password calculator if you to see how complex your password is.

Once you have finished installing your OS. You are done with the first part of creating a secure VM. Now all data on the VM is encrypted and if you enabled home directory encryption your personal files in your home directory are double encrypted.

Later we'll add utilities to add more security to the VM.

Thursday, June 7, 2012

Nagios server and client on CentOS 6

Install Centos 6 and configure as needed

Nagios Server install

  • Install epel
Get the epel release rpm installed it can be found here.
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-7.noarch.rpm

  • Disable selinux
Edit /etc/sysconfig/selinux. Change enforcing to disabled. The reboot


  • Install Nagios packages
Install nagios, nagios-plugins-all, nagios-plugins-nrpe, php-pear, mod_ssl, net-snmp-utils, sendmail

yum install nagios nagios-plugins-all nagios-plugins-nrpe php-pear mod_ssl net-snmp-utils sendmail


  • Create nagios user and password or web interface
htpasswd /etc/nagios/passwd nagiosadmin


  • Iptables
Allow port 443 in iptables


  • Add apache to nagios group
usermod -a -G nagios apache


  • configure services
chkconfig nagios on
chkconfig httpd on
chkconfig sendmail on
service sendmail restart


  • Edit httpd.conf and enable SSL in nagios.conf
Make changes to /etc/httpd/conf/httpd.conf if needed.
uncomment SSLRequireSSL in /etc/httpd/conf.d/nagios.conf
restart httpd


  • Configure for nrpe
Uncomment cfg_dir=/etc/nagios/servers in /etc/nagios/nagios.cfg
Add check nrpe command.
Add the following to /etc/nagios/objects/commands.cfg.

define command{
command_name check_nrpe
command_line /usr/lib64/nagios/plugins/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}

make the servers directory
mkdir /etc/nagios/servers
chown root:nagios /etc/nagios/servers
service nagios restart

(a reboot might be necessary)
The Nagios server should working and accessible from the web interface.


On a separate machine install Centos 6 with minimal packages. Configure the system as needed.

Client nrpe install

  • Install epel
Get the epel release rpm installed it can be found here.
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-7.noarch.rpm

  • Disable selinux
Edit /etc/sysconfig/selinux. Change enforcing to disabled. The reboot



  • Install Nagios nrpe packages
Install nagios-plugins-nrpe, nagios-plugins-al,l nagios-nrpe, openssl
yum install nagios-plugins-nrpe nagios-plugins-all nagios-nrpe openssl


  • Add allowed hosts
Edit allowed hosts in /etc/nagios/nrpe.cfg add the IP of the nrpe and the nagios server
allowed_hosts=127.0.0.1,x.x.x.x
Also change the server_address to the nrpe client IP
chown nrpe:nrpe /etc/nagios/nrpe.cfg


  • nrpe services
Add nrpe 5666/tcp to /etc/services
chkconfig nrpe on
service nrpe restart
Allow port 5666 in iptables
Check connections from both nrpe and nagios server
/usr/lib64/nagios/plugins/check_nrpe -H (IP of nrpe)


  • Add the nrpe client to the nagios server
Create a server config file in /etc/nagios/servers on the nagios server and insert the following

define host{
        use linux-server
        host_name (nrpe client name)
        alias CentOS 6
        address (nrpe client)
}


service nagios restart
There should now be two hosts on in the web interface.

Thursday, May 31, 2012

The beginning of an ISCSI server with LVM and CentOS

Creating an iscsi storage server. The hardware I'm using is an old server with 8 250GB drives. The plan is to install CentOS even though something like FreeNAS would work fine. Setup the disks in a LVM layout with the majority of the free space is used for data storage. Ideally the OS would be on a raided disk array separate from the data storage. But just to get a simple setup going I'll have the OS on a single disk.

General steps:

  1. create and setup logical volume
  2. install iscsi target
  3. configure iscsi
  4. test

  • LVM 
To create a physical volume for use in a logical volume we'll use pvcreate.
pvcreate --zero y /dev/sde will zero the first 2048 bytes of sde and initialize sde for use.
I created 4 PVs using pvcreate --zero y /dev/sde /dev/sdf /dev/sdg /dev/sdh.
Next is to create a volume group.

To create a volume group use vgcreate. vgcreate VG_stor /dev/sde, this will create a volume group with the name VG_stor which contains the PV sde.
For my VG I ran vgcreate VG_stor /dev/sde /dev/sdf /dev/sdg /dev/sdh
Now time to create logical volumes

Logical volumes are created using lvcreate. To create a 100GB volume the command would look like lvcreate -L 100G -n vol_scsi VG_stor. This creates a 100GB volume with the name vol_scsi from the volume group VG_stor.
The command I used was lvcreate -L 100G -n vol_scsi VG_stor

In short
pvcreate --zero y /dev/sde /dev/sdf /dev/sdg /dev/sdh;
vgcreate VG_stor /dev/sde /dev/sdf /dev/sdg /dev/sdh;
lvcreate -L 100G -n vol_scsi VG_stor;


  • iSCSI target setup
Now that the the LV has been created the iSCSI needs to be configured.
To config file for the iSCSI target is /etc/tgt/targets.conf.
This setup will be very simple. Edit targets.conf and add the following:
<target iqn.2012-05.host.server:target0>
     backing-store /dev/VG_stor/vol_scsi
</target>

Now start the target daemon and have it startup on boot.
service tgtd start;
chkconfig tgtd on;

Check the target information
tgtadm --mode target --op show;

Allow port 3260 via tcp in iptables.

This should show the targets that are configured.

All that is left is to create and setup the iSCSI initiator.


  • iSCSI initiator setup


install iscsi-initiator-utils and configure /etc/iscsi/iscsid.conf if needed.
Now time to discover the target. iscsiadm -m discovery -t sendtargets -p iscsi-target-ip
There should be a target listed with the iqn of the iscsi target.

start the iscsi services
service iscsi start;
service iscsid start;

Look at the discovered targets.
iscsiadm -m node -o show;

Time to login to the target and confirm session.
iscsiadm -m node --login;
iscsiadm -m session -o show;

There should be a new entry in /proc/partitions. In my case it was sdd.

Now that is a basic run through on using LVM and iSCSI. It can get much more complex.