linux:securingphp
                Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| linux:securingphp [2018/01/05 21:20] – rlunaro | linux:securingphp [2024/02/05 12:57] (current) – rlunaro | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| Here is my configuration / notes about securing a php installation.  | Here is my configuration / notes about securing a php installation.  | ||
| + | |||
| + | |||
| + | ===== Avoid common exploits ===== | ||
| + | |||
| + | These configuration rules are taken from the .htaccess file configuration of Joomla, and | ||
| + | I've adapted it for apache. They address some common exploits.  | ||
| + | |||
| + | < | ||
| + | |||
| + | < | ||
| + | RewriteEngine On | ||
| + |      | ||
| + | ## Begin - Rewrite rules to block out some common exploits. | ||
| + | # If you experience problems on your site block out the operations listed be | ||
| + | # This attempts to block the most common type of exploit `attempts` to Jooml | ||
| + | # | ||
| + | # Block out any script trying to base64_encode data within the URL. | ||
| + | RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR] | ||
| + |     # Block out any script that includes a < | ||
| + |     RewriteCond %{QUERY_STRING} (< | ||
| + | # Block out any script trying to set a PHP GLOBALS variable via URL. | ||
| + |     RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0, | ||
| + | # Block out any script trying to modify a _REQUEST variable via URL. | ||
| + |     RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0, | ||
| + | # Return 403 Forbidden header and show the content of the root homepage | ||
| + | RewriteRule .* index.php [F] | ||
| + | # | ||
| + | ## End - Rewrite rules to block out some common exploits. | ||
| + | |||
| + | |||
| + | ## Begin - Joomla! core SEF Section. | ||
| + | # | ||
| + |     RewriteRule .* - [E=HTTP_AUTHORIZATION: | ||
| + | # | ||
| + | # If the requested path and file is not /index.php and the request | ||
| + | # has not already been internally rewritten to the index.php script | ||
| + |     RewriteCond %{REQUEST_URI} !^/ | ||
| + | # and the request is for something within the component folder, | ||
| + | # or for the site root, or for an extensionless URL, or the | ||
| + | # requested URL ends with one of the listed extensions | ||
| + |     RewriteCond %{REQUEST_URI} / | ||
| + |     # and the requested path and file doesn' | ||
| + | RewriteCond %{REQUEST_FILENAME} !-f | ||
| + |     # and the requested path and file doesn' | ||
| + | RewriteCond %{REQUEST_FILENAME} !-d | ||
| + | # internally rewrite the request to the index.php script | ||
| + | # RewriteRule .* index.php [L] | ||
| + | # | ||
| + | ## End - Joomla! core SEF Section. | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | </ | ||
| + | |||
| + | ===== per directory configuration ===== | ||
| + | |||
| + | Not all the directories of your website are the same; some of them must allow the | ||
| + | execution of php, some others not. **In the case you have to allow php execution**,  | ||
| + | you have use " | ||
| + | |||
| + | < | ||
| + | < | ||
| + | |||
| + | Options -Indexes +FollowSymLinks | ||
| + | |||
| + | AllowOverride None | ||
| + | |||
| + | php_admin_flag engine On | ||
| + | |||
| + |     php_admin_value open_basedir " | ||
| + |     php_admin_value include_path " | ||
| + |     php_admin_value upload_tmp_dir " | ||
| + | # you can configure any php param here: | ||
| + |     php_admin_value post_max_size " | ||
| + |     php_admin_value upload_max_filesize " | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ==== Any other directory where won't be php files must have the php execution restricted ==== | ||
| + | |||
| + | PHP execution must be restricted in those directories where | ||
| + | it's clear won't be any php file in them: | ||
| + | |||
| + | < | ||
| + | < | ||
| + | # To avoid c99.php injection | ||
| + | php_admin_flag engine Off | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ==== Even it's a good idea to restrict any request to some directories at all ==== | ||
| + | |||
| + | For instance, you can restrict the access to your logs directory of your website at all | ||
| + | to avoid someone could guess the url and try to gain access to the logs content:  | ||
| + | |||
| + | < | ||
| + |     < | ||
| + | Require all denied | ||
| + |     </ | ||
| + | php_admin_flag engine Off | ||
| + | </ | ||
| + | |||
| + | ==== Ohter directories must only be executed from inside your website ==== | ||
| + | |||
| + | Imagine you have a directory of php files you only want through a cron | ||
| + | file: in that case, specify that those directory cannot be reached from | ||
| + | any place, but from inside of the machine only: | ||
| + | |||
| + | < | ||
| + | < | ||
| + |             < | ||
| + | Require local | ||
| + |             </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | |||
| Line 49: | Line 169: | ||
| - | ===== AQUI ME QUEDO ===== | + | ===== engine must be unset if the site doesn' | 
| + | |||
| + | Configure '' | ||
| + | |||
| + | Example: | ||
| + | |||
| + | < | ||
| + | php_admin_value engine On | ||
| + | </ | ||
| + | In any other website, turn it off. | ||
linux/securingphp.1515187206.txt.gz · Last modified: 2022/12/02 21:02 (external edit)
                
                