Π‘ΠΎΠΊΡ€Ρ‹Ρ‚ΠΈΠ΅ PHP

Π’ ΠΎΠ±Ρ‰Π΅ΠΌ случаС внСсСниС нСясности Π½Π΅Π½Π°ΠΌΠ½ΠΎΠ³ΠΎ ΡƒΠ»ΡƒΡ‡ΡˆΠ°Π΅Ρ‚ Π·Π°Ρ‰ΠΈΡ‰Ρ‘Π½Π½ΠΎΡΡ‚ΡŒ систСмы. Но Π±Ρ‹Π²Π°ΡŽΡ‚ случаи, ΠΊΠΎΠ³Π΄Π° слСдуСт ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ ΠΌΠ°Π»Π΅ΠΉΡˆΡƒΡŽ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΡΡ‚ΡŒ.

НСсколько нСслоТных ΠΌΠ΅Ρ‚ΠΎΠ΄ΠΈΠΊ ΠΌΠΎΠ³ΡƒΡ‚ ΠΏΠΎΠΌΠΎΡ‡ΡŒ Π²Π°ΠΌ ΡΠΊΡ€Ρ‹Ρ‚ΡŒ PHP, Ρ‡Ρ‚ΠΎ услоТняСт Ρ€Π°Π±ΠΎΡ‚Ρƒ ΠΏΠΎΡ‚Π΅Π½Ρ†ΠΈΠ°Π»ΡŒΠ½ΠΎΠ³ΠΎ Π²Π·Π»ΠΎΠΌΡ‰ΠΈΠΊΠ°, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ пытаСтся Π½Π°ΠΉΡ‚ΠΈ Π±Ρ€Π΅ΡˆΡŒ Π² вашСй систСмС. Установив ΠΎΠΏΡ†ΠΈΡŽ expose_php Π² off Π² ΠΊΠΎΠ½Ρ„ΠΈΠ³ΡƒΡ€Π°Ρ†ΠΈΠΎΠ½Π½ΠΎΠΌ Ρ„Π°ΠΉΠ»Π΅ php.ini, Π²Ρ‹ ΡƒΠΌΠ΅Π½ΡŒΡˆΠΈΡ‚Π΅ количСство доступной Ρ…Π°ΠΊΠ΅Ρ€Ρƒ ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΠΈ.

Π•Ρ‰Ρ‘ ΠΎΠ΄Π½Π° ΠΌΠ΅Ρ‚ΠΎΠ΄ΠΈΠΊΠ° Π·Π°ΠΊΠ»ΡŽΡ‡Π°Π΅Ρ‚ΡΡ Π² настройкС Π²Π΅Π±-сСрвСра Ρ‚Π°ΠΊΠΈΠΌ ΠΎΠ±Ρ€Π°Π·ΠΎΠΌ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΎΠ½ ΠΎΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°Π» Ρ„Π°ΠΉΠ»Ρ‹ с Ρ€Π°Π·Π»ΠΈΡ‡Π½Ρ‹ΠΌΠΈ Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΡΠΌΠΈ ΠΊΠ°ΠΊ PHP-скрипты. Π­Ρ‚ΠΎ ΠΌΠΎΠΆΠ½ΠΎ ΡƒΠΊΠ°Π·Π°Ρ‚ΡŒ ΠΊΠ°ΠΊ Π² .htaccess Ρ„Π°ΠΉΠ»Π°Ρ…, Ρ‚Π°ΠΊ ΠΈ ΠΊΠΎΠ½Ρ„ΠΈΠ³ΡƒΡ€Π°Ρ†ΠΈΠΎΠ½Π½ΠΎΠΌ Ρ„Π°ΠΉΠ»Π΅ Apache. Π’ Ρ‚Π°ΠΊΠΎΠΌ случаС Π²Ρ‹ смоТСтС ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ ΠΏΡ€ΠΈ написании ΠΊΠΎΠ΄Π° нСстандартныС Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΡ:

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠœΠ°ΡΠΊΠΈΡ€ΠΎΠ²ΠΊΠ° PHP ΠΏΠΎΠ΄ Π΄Ρ€ΡƒΠ³ΠΈΠ΅ языки программирования

# Π’Π΅ΠΏΠ΅Ρ€ΡŒ PHP-скрипты ΠΌΠΎΠ³ΡƒΡ‚ ΠΈΠΌΠ΅Ρ‚ΡŒ Ρ‚Π΅ ΠΆΠ΅ Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΡ, Ρ‡Ρ‚ΠΎ ΠΈ Π΄Ρ€ΡƒΠ³ΠΈΠ΅ языки программирования
AddType application/x-httpd-php .asp .py .pl
Или ΡΠΊΡ€Ρ‹Ρ‚ΡŒ Π΅Π³ΠΎ совсСм:

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #2 ИспользованиС нСизвСстных Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΠΉ для PHP-скриптов

# Π’Π΅ΠΏΠ΅Ρ€ΡŒ PHP-скрипты ΠΌΠΎΠ³ΡƒΡ‚ ΠΈΠΌΠ΅Ρ‚ΡŒ нСизвСстныС Ρ‚ΠΈΠΏΡ‹ Ρ„Π°ΠΉΠ»ΠΎΠ²
AddType application/x-httpd-php .bop .foo .133t
Π’Π°ΠΊΠΆΠ΅ ΠΌΠΎΠΆΠ½ΠΎ ΡΠΏΡ€ΡΡ‚Π°Ρ‚ΡŒ Π΅Π³ΠΎ ΠΏΠΎΠ΄ Π²ΠΈΠ΄ΠΎΠΌ HTML-ΠΊΠΎΠ΄Π°, Ρ‡Ρ‚ΠΎ ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Ρ‚ ΠΊ ΠΏΠΎΡ‚Π΅Ρ€Π΅ ΠΏΡ€ΠΎΠΈΠ·Π²ΠΎΠ΄ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΡΡ‚ΠΈ, Ρ‚Π°ΠΊ ΠΊΠ°ΠΊ всС HTML-Ρ„Π°ΠΉΠ»Ρ‹ Π±ΡƒΠ΄ΡƒΡ‚ ΠΎΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°Ρ‚ΡŒΡΡ PHP:

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #3 ΠœΠ°ΡΠΊΠΈΡ€ΠΎΠ²ΠΊΠ° PHP-Ρ„Π°ΠΉΠ»ΠΎΠ² ΠΏΠΎΠ΄ HTML

# Π’Π΅ΠΏΠ΅Ρ€ΡŒ PHP-скрипты выглядят ΠΊΠ°ΠΊ ΠΎΠ±Ρ‹ΠΊΠ½ΠΎΠ²Π΅Π½Π½Ρ‹ΠΉ HTML
AddType application/x-httpd-php .htm .html
Π§Ρ‚ΠΎΠ±Ρ‹ Π΄ΠΎΡΡ‚ΠΈΡ‡ΡŒ ΠΆΠ΅Π»Π°Π΅ΠΌΠΎΠ³ΠΎ эффСкта, Π²Ρ‹ Π΄ΠΎΠ»ΠΆΠ½Ρ‹ ΠΏΠ΅Ρ€Π΅ΠΈΠΌΠ΅Π½ΠΎΠ²Π°Ρ‚ΡŒ всС ваши PHP-скрипты Π² соотвСтствии с Π²Ρ‹Π±Ρ€Π°Π½Π½Ρ‹ΠΌ Π²Π°ΠΌΠΈ Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΠ΅ΠΌ. ОписанноС Π² этом Ρ€Π°Π·Π΄Π΅Π»Π΅ Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚Π°Ρ†ΠΈΠΈ ΠΏΠΎΠ²Ρ‹ΡˆΠ΅Π½ΠΈΠ΅ бСзопасности Ρ‡Π΅Ρ€Π΅Π· сокрытиС являСтся нСбольшой ΠΏΡ€Π΅Π²Π΅Π½Ρ‚ΠΈΠ²Π½ΠΎΠΉ ΠΌΠ΅Ρ€ΠΎΠΉ ΠΏΡ€ΠΈ ΠΌΠ°Π»Ρ‹Ρ… Π·Π°Ρ‚Ρ€Π°Ρ‚Π°Ρ….
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 22 notes

up
44
rustamabd at google mail ΒΆ
19 years ago
So far I haven't seen a working rewriter of /foo/bar into /foo/bar.php, so I created my own. It does work in top-level directory AND subdirectories and it doesn't need hardcoding the RewriteBase.

.htaccess:

RewriteEngine on

# Rewrite /foo/bar to /foo/bar.php
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]

# Return 404 if original request is /foo/bar.php
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]

# NOTE! FOR APACHE ON WINDOWS: Add [NC] to RewriteCond like this:
# RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$" [NC]
up
26
anon at example dot com ΒΆ
12 years ago
The session name defaults to PHPSESSID.  This is used as the name of the session cookie that is sent to the user's web browser / client. (Example: PHPSESSID=kqjqper294faui343o98ts8k77).

To hide this, call session_name() with the $name parameter set to a generic name, before calling session_start().  Example:

session_name("id");
session_start();

Cheers.
up
6
Sajith Karunatilake @ ΒΆ
3 years ago
Just hiding it doesn't look like good "security" if the code itself is flawed. At the end of the day the code has to run regardless of its file extension. There could be some advantages to this. But it does not prevent someone (who is not a script-kiddie or some kind of automated bot) from exploiting the flaws in the code.

Just a thought.

Just leaving this comment to prevent a beginner from using this as a legitimate security measure (assuming they read documentation). Cool feature though.
up
14
mmj ΒΆ
22 years ago
You can see if somebody's using PHP just by adding the following to the end of the URL:
?=PHPB8B5F2A0-3C92-11d3-A3A9-4C7B08C10000
If the page is using PHP, this will show the PHP credits.

Setting expose_php to Off in php.ini prevents this.
up
10
Anonymous ΒΆ
23 years ago
PS. If you want to use pretty URLs (i.e. hide your .php extensions) AND you have safe-mode=on, the previous example (ForceType) won't work for you.  The problem is that safe-mode forces Apache to honor trailing characters in a requested URL.  This means that:

http://www.example.com/home 

would still be processed by the home script in our doc root, but for:

http://www.example.com/home/contact_us.html

apache would actually look for the /home/contact_us.html file in our doc root.

The best solution I've found is to set up a virtual host (which I do for everything, even the default doc root) and override the trailing characters handling within the virtual host.  So, for a virtual host listening on port 8080, the apache directives would look like this:

<VirtualHost *:8080>
    DocumentRoot /web/doc_root
    Alias /home "/web/doc_root/home.php"
    AcceptPathInfo On
</VirtualHost>

Some people might question why we are overriding the trailing characters handling (with the AcceptPathInfo directive) instead of just turning safe-mode=off.  The reason is that safe mode sets global limitations on the entire server, which can then be turned on or left off for each specific virtual host.  This is the equivilent of blocking all connections on a firewall, and then opening up only the ones you want, which is a lot safer than leaving everything open globally, and assuming your programmers will never overlook a possible security hole.
up
10
sandaimespaceman at gmail dot com ΒΆ
17 years ago
Set INI directive "expose_php" to "off" will also help.
You can spoof your PHP to ASP.NET by using:
<?php
error_reporting(0);
header("X-Powered-By: ASP.NET");
?>
up
13
marpetr at NOSPAM dot gmail dot com ΒΆ
20 years ago
I think the best way to hide PHP on Apache and Apache itself is this:

httpd.conf
-------------
# ...
# Minimize 'Server' header information
ServerTokens Prod
# Disable server signature on server generated pages
ServerSignature Off
# ...
# Set default file type to PHP
DefaultType application/x-httpd-php
# ...

php.ini
------------
; ...
expose_php = Off
; ...

Now the URLs will look like this:
http://my.server.com/forums/post?forumid=15

Now hacker knows only that you are using Apache.
up
10
CD001 ΒΆ
15 years ago
It's a good idea to "hide" PHP anyway so you can write a RESTful web application.

Using Apache Mod Rewrite:

RewriteEngine On
RewriteRule ^control/([^/]+)/(.*)$ sitecontroller.php?control=$1&query=$2

You then use a function like the following as a way to retrieve data (in a zero indexed fashion) from the $_GET superglobal.

<?php
function myGET() {
  $aGet = array();

  if(isset($_GET['query'])) {
    $aGet = explode('/', $_GET['query']);
  }

  return $aGet;
}
?>

This is only a really basic example of course - you can do a lot with Mod Rewrite and a custom 'GET' function.
up
8
Pyornide ΒΆ
17 years ago
The idea of hiding the X-Powered-By in PHP is a flawed attempt at establishing security. As the manual indicates, obscurity is not security. If I were exploiting a site, I wouldn't check what scripting language the site runs on, because all that would matter to me is exploiting it. Hiding the fact that you use [x] language isn't going to prevent me from bypassing poor security.
up
7
benjamin at sonntag dot fr ΒΆ
21 years ago
In response to the previous messages, for apache, there is a easier way to set files without "." to be executed by PHP, just put this in a ".htaccess" file : 

DefaultType  application/x-httpd-php
up
8
yasuo_ohgaki at yahoo dot com ΒΆ
24 years ago
To hide PHP, you need following php.ini settings

expose_php=Off 
display_errors=Off

and in httpd.conf

ServerSignature Off
(min works, but I prefer off)
up
8
ldemailly at qualysNOSPAM dot com ΒΆ
22 years ago
adding MultiViews to your apache Options config
lets you hide/omit .php in the url without any rewriting, etc...
up
7
info at frinteractives dot com ΒΆ
10 years ago
try this
RewriteEngine On

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
up
5
jtw90210 ΒΆ
20 years ago
In order to get the PATH_INFO to work in order to pass parameters using a hidden program/trailing slash/"pretty url" in more recent versions of PHP you MUST add "AcceptPathInfo On" to your httpd.conf. 

AddType application/x-httpd-php .php .html
AcceptPathInfo On

Try it out with your phpinfo page and you'll be able to search for PATH_INFO. 

http://example.com/myphpinfo.php/showmetheway

If you want to drop the .php use one or both of these:
DefaultType application/x-httpd-php
ForceType application/x-httpd-php
up
6
Anonymous ΒΆ
22 years ago
Keep in mind, if your really freaked out over hiding PHP, GD will expose you.

Go ahead - make an image with GD and open with a text editor.. Somewhere in there you'll see a comment with gd & php all over it.
up
5
l0rdphi1 at liquefyr dot com ΒΆ
22 years ago
More fun includes files without file extensions.

Simply add that ForceType application/x-httpd-php bit to an Apache .htaccess and you're set.

Oh yea, it gets even better when you play with stuff like the following:

<?php
substr($_SERVER['PATH_INFO'],1);
?>

e.g. www.example.com/somepage/55

And:

<?php
foreach ( explode('/',$_SERVER['PATH_INFO']) as $pair ) {
    list($key,$value) = split('=',$pair,2);
    $param[$key] = stripslashes($value);
}
?>

e.g. www.example.com/somepage/param1=value1/param2=value2/etc=etc

Enjoy =)
up
4
istvan dot takacsNOSPAM at hungax dot com ΒΆ
24 years ago
And use the
ServerTokens min
directive in your httpd.conf to hide installed PHP modules in apache.
up
4
m1tk4 at hotmail dot com ΒΆ
23 years ago
I usually do:

<code>
RewriteEngine on<br>
RewriteOptions inherit<br>
RewriteRule (.*)\.htm[l]?(.*) $1.php$2 [nocase]<br>
</code>

in .htaccess. You'll need mod_rewrite installed for this .
up
0
Bryce Nesbitt at Obviously.COM ΒΆ
23 years ago
Using the .php extension for all your scripts is not necessary, and in fact can be harmful (by exposing too much information about your server, and by limiting what you can do in the future without breaking links). There are several ways to hide your .php script extension:

(1) Don't hard code file types at all.  Don't specify any dots, and most web servers will automatically find your .php, .html, .pdf, .gif or other matching file. This is called canonical URL format:
     www.xxxxxx.com/page
    www.xxxxxx.com/directory/
This gives you great flexibility to change your mind in the future, and prevents Windows browsers from making improper assumptions about the file type.

(2) In an Apache .htaccess file use:
    RewriteEngine on
    RewriteRule page.html page.php

(3) Force the webserver to interpret ALL .html files as .php:
    AddType application/x-httpd-php .php3 .php .html
up
-1
simon at carbontwelevedesign dot co dot uk ΒΆ
19 years ago
I use the following in the .htaccess document

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

then the following simple code

<?php

$permalinks = explode("/",$_SERVER['REQUEST_URI']);

$varone = $permalinks[1];
$vartwo = $permalinks[2];

...

?>
up
-4
php at vfmedia dot de ΒΆ
21 years ago
I?ve found an easy way to hide php code and the uri is searchable by google and others...(only for unix or linux)

At first I have some rules in my hide.conf (i made an extra .conf for it (apache 2.0))

For example when I want to mask the index.php

<Files index>
 ForceType application/x-httpd-php
 </Files>

My problem is, that my code should be readable...

so I made an extra folder for example srv/www/htdocs/static_output

My phpcode is in the includefolder....(for ex. mnt/source/index.php)

Then I made a link in the shell  > ln mnt/source/index.php srv/www/htdocs/static_output/index

So the code is readable (with .php extension) in my includefolder and there is only the link in the srv folder without extension(which is called by the browser...).
up
-5
omolewastephen at gmail dot com ΒΆ
8 years ago
I used this on my site and it works great for me

# RewriteEngine on

# Rewrite /foo/bar to /foo/bar.php
# RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]

# Return 404 if original request is /foo/bar.php
# RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
# RewriteRule .* - [L,R=404]

# NOTE! FOR APACHE ON WINDOWS: Add [NC] to RewriteCond like this:
# RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$" [NC]