Want to secure your PHP web applications. But How? We have been developing PHP applications since last 10 years, but what still disappoints us is the way people handle PHP language. We often come across incidents when one or more of online web applications has been hacked or data has been stolen. Since majority of websites is made in PHP on web, this goes on to create an image that PHP applications are not secure which is totally wrong and unacceptable. Because of this, there were times when we faced strict opposition from client saying that they don’t trust PHP apps rather they prefer java or dot net apps when it comes to security.
What we have realized is that Security of web applications is not exactly a PHP language issue. Its more of a server configuration and coding standards issue. Once the developer company takes care of few necessary security guidelines, you can rest assured that your applications are secure enough to withstand common hacking attacks. Lets first start with understanding what exactly is Security?
That being said you can however start securing your php applications by securing you server environment first.
First step is to check whether these settings has been enabled or not. If not try setting the options as per below guidelines.
|safe_mode = On||Enabling safe_mode parameter ensures that PHP scripts are able to access files only when their owner is the owner of the PHP scripts. This is one of the most important security mechanisms built into the PHP. Effectively counteracts unauthorized attempts to access system files (e.g. /etc/paswd) and adds many restrictions that make unauthorized access more difficult.|
|safe_mode_gid = Off||When safe_mode is turned on and safe_mode_gid is turned off, PHP scripts are able to access files not only when UIDs are the same, but also when the group of the owner of the PHP script is the same as the group of the owner of the file.|
|open_basedir = directory[:…]||When the open_basedir parameter is enabled, PHP will be able to access only those files, which are placed in the specified directories (and subdirectories).|
|safe_mode_exec_dir = directory[:…]||When safe_mode is turned on, system(), exec() and other functions that execute system programs will refuse to start those programs, if they are not placed in the specified directory.|
|expose_php = Off||Turning off the “expose_php” parameter causes that PHP will not disclose information about itself in HTTP headers that are being sent to clients in responses to web requests (e.g., X-Powered-By: PHP/5.3.3)..|
|register_globals = Off||When the register_globals parameter is turned on, all the EGPCS (Environment, GET, POST, Cookie and Server) variables are automatically registered as global variables. Because it can pose a serious security threat, it is strongly recommended to turn this parameter off (starting from the version 4.2.0, this parameter is turned off by default)|
|display_errors = Off||If the display_errors parameter is turned off, PHP errors and warnings are not being displayed. Because such warnings often reveal precious information like path names, SQL queries etc., it is strongly recommended to turn this parameter off on production servers.|
|log_errors = On||When log_errors is turned on, all the warnings and errors are logged into the file that is specified by the error_log parameter. If this file is not accessible, information about warnings and errors are logged by the Apache server.|
|error_log = filename||This parameter specifies the name of the file, which will be used to store information about warnings and errors (attention: this file must be writeable by the user or group apache)|
In addition to the above settings, changing the file extension can be taken into account. Its quite possible for you to change the extension of files to be anything like asp, jsp, aspx. Such a change will make it difficult for any potential intruders to recognize the server-side technology that is being used. In order to change the extensions, all the *.php files should be renamed to *.jsp (for example), and the following line should be changed in /chroot/httpd/usr/local/apache/conf/httpd.conf:
[sourcecode language=”plain”]AddType application/x-httpd-php .php[/sourcecode]
to the new one:
[sourcecode language=”plain”]AddType application/x-httpd-php .jsp[/sourcecode]
Now web users will not see *.php extension in the URL address which is what immediately suggests that the PHP technology is being used at the server side.
Protection against XSS and SQL Injection attacks
XSS and Sql injection attacks are another form of attacks which needs your attention. This can be secured by implementing the logging of the GET and POST payloads, and implementing protection against Cross-Site-Scripting and SQL Injection attacks. In order to perform that, you should use the mod_security module, which can be enabled by adding the following line into httpd.conf:
To enable logging of the GET and POST requests, it suffices to add the following section to httpd.conf:
AddHandler application/x-httpd-php .php
The above commands will enable the Audit Engine, which is responsible for logging requests, and the Filtering POST Engine, which will make it possible to log POST requests. In order to protect web application against XSS attacks, the following lines should also be inserted before “</IfModule>”:
The first line causes that the server to return the “Internal Server Error” message when the request contains the search phrase from any SecFilter variable. The second line sets up the filter to search for HTML tags in the GET and POST requests.
One of the typical signatures of SQL Injection attack is the appearance of an apostrophe (‘) or quotation mark (“) in the GET or POST request. By rejecting all the requests containing those characters, we can make the use of SQL Injection technique very difficult:
Achieving a high level of a web server’s security using server-side technologies (PHP, ASP, JSP, CFM etc.) is a very difficult task in practice. Improper use of interactions with a web server in any significant way decreases the web server’s security. That is why server-side scripts should only be used where it is absolutely necessary and should be used intelligently.
Of course, this article doesn’t complete the subject of securing the PHP technology. And although applying them can increase the level of security for your application, we cannot forget that the security of the whole environment depends not only on Apache’s or PHP’s configuration, but also and foremost – on the web application itself. Next article in this series will guide you with the best practices that should be done to ensure that your web applications becomes more secure than ever.
Stay Tuned in 🙂