:: Shashank.net :: Personal Web

  • Increase font size
  • Default font size
  • Decrease font size
Home Articles How-To: Secure your temp directories

How-To: Secure your temp directories

E-mail Print PDF

 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.
-----command-----
df -h |grep tmp
-----command-----


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.
-----command-----
cat /etc/fstab |grep tmp
-----command-----

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
-----command-----
cd /dev/; dd if=/dev/zero of=tmpMnt bs=1024 count=800000
-----command-----

Format the partion
-----command-----
mke2fs /dev/tmpMnt
-----command-----
When it asks about not being a block special device press Y


Make a backup of the old data
-----command-----
cp -Rp /tmp /tmp_backup
-----command-----

Mount the temp filesystem
-----command-----
mount -o loop,noexec,nosuid,rw /dev/tmpMnt /tmp
-----command-----

Set the permissions
-----command-----
chmod 0777 /tmp
-----command-----

Copy the old files back
-----command-----
cp -Rp /tmp_backup/* /tmp/
-----command-----

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 pico:
-----command-----
pico -w /etc/fstab
-----command-----
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.
-----command-----
umount /dev/shm
mount /dev/shm

-----command-----

Next delete the old /var/tmp and create a link to /tmp
-----command-----
rm -rf /var/tmp/
ln -s /tmp/ /var/
-----command-----

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


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.

Thanks to eth0.us for this excellent resource.

 

Last Updated ( Monday, 04 February 2008 18:19 )  

Search


Who's Online

We have 7 guests online

Statistics

Content View Hits : 11394

Syndication

Top Stories

Top Stories
Yahoo! News
  • UAW to renegotiate labor terms, suspend jobs bank (AP)

    United Auto Workers (UAW) President Ron Gettelfinger (L) and General Motors CEO Richard Wagoner on Capitol Hill, November 19, 2008. (Kevin Lamarque/Reuters)AP - The United Auto Workers said Wednesday it is willing to change its contracts with U.S. automakers and accept delayed payments of billions of dollars to a union-run health care trust to do its part to help the struggling companies secure $34 billion in government loans.


  • Cruise passengers describe "cheeky" pirate attack (AP)

    Pirates ride alongside the luxury American cruise ship M/S Nautica, in this photo take from the Nautica, during a hijack attempt on the vessel, off the coast of Yemen in the Gulf of Aden, Sunday, Nov. 30, 2008.  The M/S Nautica, carrying 656 international passengers and 399 crew members, was sailing through the Gulf of Aden on Sunday when it encountered six bandits in two speedboats.  The pirates fired at the passenger liner but the larger ship was faster than the pirates' vessels, and escaped being boarded.(AP Photo)AP - Ordered to get inside and stay down, Oregon tourist Clyde Thornburg heard the pirates' rifle shots hit the side of the luxury cruise liner — "Pop! Pop! Pop!" — then felt the ship speed up to escape.


  • Paulson said weighing bailout's next $350 billion (Reuters)

    Treasury Secretary Henry Paulson speaks at a news conference at the Treasury Department in Washington, November 25, 2008. (Jason Reed/Reuters)Reuters - Treasury Secretary Henry Paulson is weighing whether to ask Congress for the remaining $350 billion of the financial bailout fund, with White House aides approaching President-elect Barack Obama's transition team about the idea, an Obama aide said on Wednesday.