How To Harden PHP5 With Suhosin On CentOS 5.4 - Page 2
4 Installing Suhosin
Suhosin can be downloaded from here: http://www.hardened-php.net/suhosin/download.html
To install the Suhosin patch, we need to recompile PHP5 from the sources, but we will use the CentOS 5.4 PHP5 .src.rpm package for this (using the rpmbuild command), so that we get new PHP5 .rpm packages (with Suhosin) that we can install. That way, we don't have to worry about the right PHP5 configuration options because rpmbuild will take care of this.
But first we grab a copy of the Hardened-PHP Project's release signaturekey and import it into our GNU Privacy Guard keychain:
Then we download a PHP5 .src.rpm package (that suits our currently installed PHP version, 5.1.6 in this example) from a CentOS 5.4 mirror to /usr/src and install it:
(As you see above, I download the newest PHP5 .src.rpm from the updates/ directory of my CentOS mirror; of course, you can also download the original .src.rpm from the os/ directory, e.g. http://ftp-stud.fht-esslingen.de/pub/Mirrors/centos/5.4/os/SRPMS/php-5.1.6-23.2.el5_3.src.rpm. If you do so, you'll have to adjust the filenames in the rest of this tutorial.)
rpm -ivh php-5.1.6-24.el5_4.5.src.rpm
If you get warnings like these ones:
warning: user mockbuild does not exist - using root
you can ignore them.
Next we download the Suhosin patch that suits our PHP version to /usr/src/redhat/SOURCES (you can find all available patches on the Suhosin downloads page):
We should check now that the MD5 sum of the downloaded patch is identical to the one published on the Suhosin downloads page:
If the MD5 sum is ok, we can check the digital signature like this:
If you see this line in the output:
gpg: Good signature from "Hardened-PHP Signature Key"
everything is ok with the downloaded patch, and we can proceed.
Now we unpack the Suhosin patch, rename it so that it fits into the CentOS naming scheme, and modify the file /usr/src/redhat/SPECS/php.spec so that the rpmbuild command knows it has to include the Suhosin patch when it rebuilds PHP5:
Add Patch112: php-5.1.6-suhosin.patch to the stanza where all patches are listed and comment out the line Patch14: php-5.1.6-ecalloc.patch in the same stanza (the ecalloc patch conflicts with Suhosin), and then add %patch112 -p1 -b .suhosin to the %setup -q stanza and comment out the line %patch14 -p1 -b .ecalloc in the same stanza:
If you are on an x86_64 system, you need to install apr-devel.x86_64 (don't do it if you are on an i386 system!)...
yum install apr-devel.x86_64
... because otherwise you will see the following error message when you try to rebuild PHP5:
cannot open /httpd/build/config_vars.mk: No such file or directory at /usr/sbin/apxs line 201.
Now we rebuild PHP5:
rpmbuild -ba php.spec
Depending on what PHP5 modules you have installed, rpmbuild will most likely complain about missing packages that it needs to build new packages for the various PHP5 modules:
[root@server1 SPECS]# rpmbuild -ba php.spec
If you see an error like this, install the missing packages, e.g. like this:
yum install aspell-devel httpd-devel libjpeg-devel libpng-devel pcre-devel libc-client-devel mysql-devel postgresql-devel unixODBC-devel net-snmp-devel gd-devel freetype-devel
Afterwards, run rpmbuild again:
rpmbuild -ba php.spec
This should now compile PHP5 and all installed PHP5 modules again and create new .rpm packages in the /usr/src/redhat/RPMS/i386 directory (if you are on an i386 system) or in the /usr/src/redhat/RPMS/x86_64 directory (if you are on an x86_64 system). This can take some time, so please be patient.
Afterwards, we can install the new PHP5 packages like this:
That's it for the Suhosin patch.
Now we are going to build the Suhosin PHP extension. First we download its sources to the /usr/src directory (the sources of the Suhosin extension are available on the Suhosin downloads page):
Next we check the MD5 sum and the signature again:
Then we unpack the sources and build the extension like this:
tar xvfz suhosin-0.9.29.tgz
To enable the Suhosin extension, we create the file /etc/php.d/suhosin.ini and put the line extension=suhosin.so into it:
All that is left to do now is restart Apache2:
Now let's call our info.php page again in a browser (e.g. http://192.168.0.100/info.php). If everything went ok, you should now see Suhosin mentioned in two places on the page:
That's it. If you like you can configure Suhosin (see http://www.hardened-php.net/suhosin/configuration.html), although Suhosin will work out of the box with its default configuration, so be sure that you know what you're doing.