Wednesday, February 23, 2011

Kernel Compile with Grsecurity Patch



Kernel Compile with Grsecurity Patch

This is a tutorial to compile kernel 2.6.11.12 with grsecurity. Works cool specially with exec logging you can trace just any minor activity on your server. Here are the commands I used for the compilation :

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#cd /usr/local/src/
#wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.24.5.tar.bz2
#wget http://grsecurity.net/grsecurity-2.1.11-2.6.24.5-200804211829.patch.gz
#tar xvfj linux-2.6.24.5.tar.bz2
#gunzip grsecurity-2.1.11-2.6.24.5-200804211829.patch.gz
#patch -p0 < grsecurity-2.1.11-2.6.24.5-200804211829.patch.gz
#cd linux-2.6.24.5
#cp /boot/config-'uname -r' .config
#make menuconfig
#make
#make modules_install
#make install
#grubby --bootloader-probe
#pico /etc/grub.conf
#grub-install /dev/hda
#reboot
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Try at your own risk :D . Best of luck. :)


Monday, February 21, 2011

Secure /tmp Dir.


Every system needs temporary folders that any user is able to read and write BUT these directories should not be able to execute programs or scripts. Though this will only protect you from somebody running the script directly it will help with a large portion of the automated rootkits and trojans that script kiddies use. They will still be able to put the files on the system but they will be unable to execute them and create the back door. One of the biggest problems is php injection via apache in which people will have apache download and then run an exploit. Securing the temp directories is probably the single biggest thing you can do towards securing your server.
 This guide will work fine with cPanel, ensim, plesk, and of course with no control panel. It is designed for Redhat but should work on any linux varient.
The first step is to check if /tmp is already secure. Some datacenters do not create a /tmp partition while others do.

#df -h |grep tmp

If that displays nothing then go below to create a tmp partition. If you do have a tmp partition you need to see if it mounted with noexec.

#cat /etc/fstab |grep tmp

If there is a line that includes /tmp and noexec then it is already mounted as non-executable. If not follow the instructions below to create one without having to physically format your disk. Idealy you would make a real partition when the disk was originally formated, that being said I have not had any trouble create a /tmp partition using the following method.

Create a 190Mb partition

#cd /dev/; dd if=/dev/zero of=tmpMnt bs=1024 count=800000

Format the partion

#mke2fs /dev/tmpMnt

When it asks about not being a block special device press Y

Make a backup of the old data

#cp -Rp /tmp /tmp_backup

Mount the temp filesystem

#mount -o loop,noexec,nosuid,rw /dev/tmpMnt /tmp

Set the permissions

#chmod 0777 /tmp

Copy the old files back

#cp -Rp /tmp_backup/* /tmp/


Once you do that go ahead and restart mysql and make sure it works ok. We do this because mysql places the mysql.sock in /tmp which neeeds to be moved. If not it migth have trouble starting. If it does you can add this line to the bottom of the /etc/fstab to automatically have it mounted:

Open the file in nano:

#nano -w /etc/fstab

Now add this single line at the bottom:

[/dev/tmpMnt /tmp ext2 loop,noexec,nosuid,rw 0 0]

While we are at it we are going to secure /dev/shm. Look for the mount line for /dev/shm and change it to the following:
none /dev/shm tmpfs noexec,nosuid 0 0

Umount and remount /dev/shm for the changes to take effect.

#umount /dev/shm
#mount /dev/shm

Next delete the old /var/tmp and create a link to /tmp

#rm -rf /var/tmp/
#ln -s /tmp/ /var/

If everything still works fine you can go ahead and delete the /tmp_backup directory.

#rm -rf /tmp_backup

You /tmp, /var/tmp, and /dev/shm are now mounted in a way that no program can be directly run from these directories. Like I have said in other articles there are still ways in but this is one of the many layers of security you should have on your system.