Chrooting ApacheHow to install Apache in a chroot jail on a Linux box.
by Mike Peters, May 2004 Introduction The Installing Apache in a chroot jail does not make Apache itself any more secure, rather, it serves to restrict the access of Apache and it's child processes to a small subset of the file system. The advantage in chrooting a process is not in prevention, rather containment of a potential threat. Before deciding whether you need to chroot your web server you should consider the advantages and disadvantages of such a setup. Advantages If Apache is compromised, the intruder will only have access to the files within the chroot jail. Potentially dangerous cgi scripts will not have access to your server's file system. Your web-tree is contained in one easy to back-up and move area. Disadvantages A chroot environment is more difficult to set up than a traditional install, especially if you run external features such as perl, php, MySQL or Python etc. The process is only viable if your entire web tree can exist on a single file system. Compiling/Installing the Apache Binary There are no special steps needed to build the Apache binary in order to install it in a chroot jail. Therefore I am not going to detail how to compile Apache. The following steps apply equally to a precompiled binary (such as an RPM) or one you have compiled yourself. Just make sure that you are using the latest patched version of the server and install normally. Once you have installed Apache normally, ensure that it is working as expected. Starting with a working binary will help with de-bugging later. Finally, make sure you configure apache to run with its own user and group ID's. Create a user and group with the commands:
This will create a regular user Creating the Chroot Tree Our chroot jail is a mini-version of the Linux file system. I prefer to use a
seperate partition mounted as
# mkdir /chroot/httpd/dev
# mkdir /chroot/httpd/lib
# mkdir /chroot/httpd/etc
# mkdir -p /chroot/httpd/usr/sbin
# mkdir /chroot/httpd/usr/lib
# mkdir /chroot/httpd/usr/libexec
# mkdir -p /chroot/httpd/var/run
# mkdir -p /chroot/httpd/var/log/apache
# mkdir -p /chroot/httpd/home/httpd
Now set the permissions on you directory structure:
# chown -R root /chroot/httpd
# chmod -R 0755 /chroot/httpd
# chmod 750 /chroot/httpd/var/log/apache/
Your exact structure may vary slightly depending upon what features of Apache you are using and where the nescessary libraries live on your main file system. Once we have created the nescessary directories we need to create the
# mknod /chroot/httpd/dev/null c 1 3
# chown root.sys /chroot/httpd/dev/null
# chmod 666 /chroot/httpd/dev/null
We need the Copying the Nescessary Files Now shut down apache,
Next, your Apache DocumentRoot and cgi scripts:
Now for your
If you use mod_ssl you need to copy the
You should also copy any modules from your original install:
Once we have copied Apache itself (and ssl if needed) we need to copy all of
the shared libraries which apache relies on to run. To find out which libraries we
need, execute This should give output something like:
/lib/libsafe.so.2 => /lib/libsafe.so.2 (0x40017000)
libm.so.6 => /lib/libm.so.6 (0x40037000)
libcrypt.so.1 => /lib/libcrypt.so.1 (0x40059000)
libdb.so.2 => /lib/libdb.so.2 (0x40086000)
libexpat.so.0 => /usr/lib/libexpat.so.0 (0x40096000)
libdl.so.2 => /lib/libdl.so.2 (0x400b6000)
libc.so.6 => /lib/libc.so.6 (0x400b9000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
The exact output will depend upon how your httpd binary was built in the first place. Copy the required files to their respective directories in your chroot:
# cp /lib/libsafe* /chroot/httpd/lib/
# cp /lib/libm* /chroot/httpd/lib/
# cp /lib/libcrypt* /chroot/httpd/lib/
# cp /lib/libdb* /chroot/httpd/lib/
# cp /usr/lib/libexpat* /chroot/httpd/usr/lib/
# cp /lib/libdl* /chroot/httpd/lib/
# cp /lib/libc* /chroot/httpd/lib/
# cp /lib/ld-* /chroot/httpd/lib/
You will need certain libraries for some standard networking functionality as follows:
# cp /lib/libnss_compat* /chroot/httpd/lib/
# cp /lib/libnss_dns* /chroot/httpd/lib/
# cp /lib/libnss_files* /chroot/httpd/lib/
# cp /lib/libnsl* /chroot/httpd/lib/
The /chroot/httpd/etc Configuration Files For Apache to function properly we also need several configuration files from
Firstly we edit the
/etc/passwd:
apache:x:12347:12348:Apache Server:/dev/null:/bin/false
/etc/group:
apache:x:12347:
Several network configuration files are also needed:
# cp /etc/hosts /chroot/httpd/etc/
# cp /etc/host.conf /chroot/httpd/etc/
# cp /etc/resolv.conf /chroot/httpd/etc/
# cp /etc/nsswitch.conf /chroot/httpd/etc/
For extra security we can set the immutable bit on these configuration files. This means that before the files can be modified, root, has to specifically unset the immutable bit making it much harder for an intruder to tamper with the files:
# chattr +i /chroot/httpd/etc/hosts
# chattr +i /chroot/httpd/etc/host.conf
# chattr +i /chroot/httpd/etc/resolv.conf
# chattr +i /chroot/httpd/etc/nsswitch.conf
# chattr +i /chroot/httpd/etc/passwd
# chattr +i /chroot/httpd/etc/group
In order that our log files be written with the correct time, we need to check
By default, For
echo -n " /usr/sbin/syslogd"
/usr/sbin/syslogd
to:
echo -n " /usr/sbin/syslogd"
/usr/sbin/syslogd -m 0 -a /chroot/httpd/dev/log
It is a good idea to create the nescessary log files and set the appendable bit on them too.
# touch /chroot/httpd/var/log/apache/access_log
# touch /chroot/httpd/var/log/apache/error_log
# chmod 600 /chroot/httpd/var/log/apache/*
# chattr +a /chroot/httpd/var/log/apache/*
Finally, we need to change our httpd start-up script to run our chrooted
httpd. Depending on your distro, open up Testing the Server If you have not already done so you should shut down the httpd daemon now.
Next we need to restart the syslog daemon If there are no errors, check the daemon is running,
If something has gone wrong, you should try running your chrooted Once everything is running you can remove your original Apache install. Summary Although |
Save This Page
|
|


Save This Page