Saturday, March 21, 2009

Fighting Linux Rootkits

Recently having had an unpleasant experience with a rootkit that was installed in /var/tmp, better security measures were clearly needed.

Unfortunately, /tmp and /var/tmp are world-writable by necessity and are the favorite target of rookit users. It would be nice to have some sort of protection on these directories and the easiest way is to mount a filesystem image using the loopback device and mount it with noexec and nosuid options via /etc/fstab.

Create the file system image, change its permissions with chmod 1777 and keep it in /boot; 1,200,000 512-byte blocks should be a good size and not waste too much disk space for small files. If your needs are different, adjust accordingly.

# dd if=/dev/zero of=/boot/tmp.img bs=512 count=1200000

This produces a disk size of roughly 300M.

The filesystem format chosen for this particular task is Reiserfs because it handles large amounts of smaller files very well. We need to use the -f option because the file is not a block special device. If the system normally does not use the reiserfs module (in this case it does), use /usr/sbin/modprobe to load it and then run /usr/sbin /depmod -a. Add reiserfs to /etc/modprobe.preload to make sure Reiserfs support is up and running right away . You could also create a new /boot/init.rd containing reiserfs. Since you may use any Linux filesystem format you chose, the safe choice might be the filesystem format of your root partition, so probably EXT2 (EXT3 without journaling) would be a good choice.

# mkreiserfs -f /boot/tmp.img

or

# mke2fs -f /boot/tmp.img

Then edit /etc/fstab to add the following lines. The same image file will be mounted at two places on the filesystem tree.

/boot/tmp.img /tmp reiserfs loop,notail,noexec,nosuid,rw 0 0
/boot/tmp.img /var/tmp reiserfs loop,notail,noexec,nosuid,rw 0 0

Examine /tmp and /var/tmp for necessary files because mounting tmp.img will make them unavailable, but still on the disk. The files in /tmp are usually created at startup. KDE keeps user cache files in /var/tmp and they will be re-created automatically, but might be fairly large, so either temporarily copy them someplace safe, or delete them since they'll still be there taking up space even though tmp.img is covering them up. The directory /usr/tmp is a symlink to /var/tmp so nothing is needed for that directory. Or you could mount the image, copy the files to it, un-mount it and then just mount the image at and /tmp/var/tmp. So many choices.

If you run into trouble, it's easiest to re-boot the computer so the proper files and symlinks are created in /tmp.

If you have multiple partitions and have little space in / but lots of space in /home, place the tmp.img file there instead to free up disk space. Actually, using one of those older small, <2gb style="font-weight: bold;" face="courier new">nosuid and noexec as /tmp is a good idea as well.

Extending this idea further, you could image entire directories as .iso images and mount them, creating read-only filesystems that cannot be hacked.

If you want to test this for effectiveness, copy a small executable to /tmp and then attempt to execute it. Watch what happens: Permission denied !

No comments: