Results tagged “php”

隐藏PHP

http://www.php.net/manual/zh/security.hiding.php

隐藏 PHP

一般而言,通过隐藏的手段提高安全性被认为是作用不大的做法。但某些情况下,尽可能的多增加一份安全性都是值得的。

一些简单的方法可以帮助隐藏 PHP,这样做可以提高攻击者发现系统弱点的难度。在 php.ini 文件里设置 expose_php = off ,可以减少他们能获得的有用信息。

另一个策略就是让 web 服务器用 PHP 解析不同扩展名。无论是通过 .htaccess 文件还是 Apache 的配置文件,都可以设置能误导攻击者的文件扩展名:

Example#1 把 PHP 隐藏为另一种语言

# 使PHP看上去像其它的编程语言
AddType application/x-httpd-php .asp .py .pl
或者干脆彻底隐藏它:

Example#2 使用未知的扩展名作为 PHP 的扩展名

# 使 PHP 看上去像未知的文件类型
AddType application/x-httpd-php .bop .foo .133t
或者把它隐藏为 HTML 页面,这样所有的 HTML 文件都会通过 PHP 引擎,会为服务器增加一些负担:

Example#3 用 HTML 做 PHP 的文件后缀

# 使 PHP 代码看上去像 HTML 页面
AddType application/x-httpd-php .htm .html
要让此方法生效,必须把 PHP 文件的扩展名改为以上的扩展名。这样就通过隐藏来提高了安全性,虽然防御能力很低而且有些缺点。
 
实际上上述的隐藏只是改了一个名字,如果想彻底去掉 .php 该如何处理呢?
使用Apache 的Rewite搞定!
 
从上述文档后面的 User Contributed Notes 中,有几个测试后可用的:
 
RewriteEngine on
RewriteRule    ^$    /index.php    [L]
RewriteRule    ^([a-zA-Z0-9\-\_/]*)/$    /$1/index.php    [L]
RewriteRule    ^([a-zA-Z0-9\-\_/]*)\.(html|htm)$    /$1.php    [L]
RewriteRule    ^([a-zA-Z0-9\-\_/]*)$    /$1.php    [L]
 
Typing "sub.domain.foo/anything" loads "/anything/index.php" if 'anything' is a directory, else it loads "/anything.php".
I'm sure you can find mutch better, but it works great on my site :)
 
另外一个,使用index.php 程序做特殊处理:
<?php
echo $_SERVER['QUERY_STRING'];
echo "<BR>";
// PARSING QUERY STRING
$QS=explode("&",$_SERVER['QUERY_STRING']);
echo $QS[0];
echo "<BR>";
$QS=explode('/',$QS[0]);
// IF Modul is Undefined set it to index
if (!$QS[0]) $MODUL='index';
else $MODUL=strtolower($QS[0]);
// WE can make a Variable $_QUERY
// for alternative _GET
for ($i=1;$i<count($QS);$i+=2)
{
    $_QUERY[$NVAR]=$NVAR=$QS[$i];
    $$NVAR=$QS[$i+1];
}

// Check the Modul is exists?
if (!file_exists("modul_directory/{$MODUL}.php"))
    $MODUL="index";
#### THIS IS EXAMPLE TO IMPLEMENTATION THE SCRIPT
// Load The Template
//include("template.php");
// Load The Module
//include("modul_directory/{$MODUL}.php");
// Load The Footer
//include("footer.php");
 
$empty = $post = array();
foreach ($_GET as $varname => $varvalue) {
    if (empty($varvalue)) {
        $empty[$varname] = $varvalue;
    } else {
        $post[$varname] = $varvalue;
    }
}
print "<pre>";
if (empty($empty)) {
    print "None of the POSTed values are empty, posted:\n";
    var_dump($post);
} else {
    print "We have " . count($empty) . " empty values\n";
    print "Posted:\n"; var_dump($post);
    print "Empty:\n";  var_dump($empty);
    exit;
}
?>
 
we can access the modul in URL like this:
=================================
www.example.com/?forum/topic/20
- it mean load the modul forum.php, and set the _QUERY['topic']=20
www.foo.com/?voting/id/54/type/piechart&choice=2
- it mean load the modul voting.php, and set the _QUERY['id']=54 and _QUERY['type']='piechart' and set _GET['choice']=2
 
 
第三个:
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]
 
测试了一下,返回404那个规则总是导致500错误,怪异!
 
第四个:
直接在.htaccess中使用
Options   MultiViews 
不过这样应该服务器的很大负担?

28-Oct-2003 12:17
adding MultiViews to your apache Options config
lets you hide/omit .php in the url without any rewriting, etc...
 
内容协商
 
MultiViews是一个针对每个目录的选项,也就是说可以在httpd.conf或.htaccess(如果正确设置了AllowOverride)文件中的<Directory>、<Location>、<Files>配置段中,用Options指令来指定。注意,Options All 并不会设置MultiViews ,你必须明确地指定。
MultiViews的效果是:如果服务器收到对/some/dir/foo的请求,而/some/dir/foo并不存在,但是如果/some/dir启用了MultiViews ,则服务器会查找这个目录下所有的foo.* 文件,并有效地伪造一个说明这些foo.* 文件的类型表,分配给他们相同的媒体类型及内容编码,并选择其中最合适的匹配返回给客户。
MultiViews还可以在服务器检索一个目录时,用于DirectoryIndex指令搜索的文件名。如果设置了:
DirectoryIndex index
而index.html和index.html3并存,则服务器会作一个权衡;如果都没有,但是有index.cgi ,则服务器会执行它。
如果一个目录中没有任何文件具有mod_mime可以识别的表示其字符集、内容类型、语言和编码的后缀,那么其结果将取决于MultiViewsMatch指令的设置,这个指令决定了在MultiViews协商中将使用的处理器、过滤器和其他后缀类型。
 

有可能使 PHP 运作于 Apache 的 content negotiation(MultiViews 选项)吗?

如果到 PHP 文件的连接包含扩展名,一切都运行完美。本解答只针对到 PHP 文件的连接不包含扩展名时,而希望通过 content negotiation 来从不包含扩展名的 URL 来选择 PHP 文件的情况。在此种情况下,将 AddType application/x-httpd-php .php 替换为:

# PHP 4
AddHandler php-script php
AddType text/html php

# PHP 5
AddHandler php5-script php
AddType text/html php
 
 
 
 
 
 

--EOF--

php:显示php文件源代码

1:apache 配置文件中直接指明为php源代码文件:
AddType application/x-httpd-php-source .phps
则.phps后缀的文件都作为php源文件实现

2:php提供的相关函数:
show_source(filename)

show_source

(PHP 4, PHP 5)

show_source -- 别名 highlight_file()

说明

本函数是该函数的别名: highlight_file().



highlight_file

(PHP 4, PHP 5)

highlight_file -- Syntax highlighting of a file

说明

mixed highlight_file ( string $filename [, bool $return] )

Prints out or returns a syntax highlighted version of the code contained in filename using the colors defined in the built-in syntax highlighter for PHP.

Many servers are configured to automatically highlight files with a phps extension. For example, example.phps when viewed will show the syntax highlighted source of the file. To enable this, add this line to the httpd.conf:

AddType application/x-httpd-php-source .phps

参数

filename

Path to the PHP file to be highlighted.

return

Set this parameter to TRUE to make this function return the highlighted code.

返回值

If return is set to TRUE, returns the highlighted code as a string instead of printing it out. Otherwise, it will return TRUE on success, FALSE on failure.

更新日志

版本 说明
4.2.1 This function is now also affected by safe_mode and open_basedir.
4.2.0 The return parameter was added.

注释

小心

Care should be taken when using the highlight_file() function to make sure that you do not inadvertently reveal sensitive information such as passwords or any other type of information that might create a potential security risk.

注意: 该函数的内部依赖这个参数使用输出缓冲,所以它不能在 ob_start() 的回调函数中使用

参见

highlight_string()


--EOF--

1

Pages

March 2025

Sun Mon Tue Wed Thu Fri Sat
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31