Gallery:安全性

来自站长百科
Firebrance讨论 | 贡献2008年7月21日 (一) 14:46的版本
跳转至: 导航、​ 搜索

Gallery2的安全性

本文解释有关Gallery2安全性的各个方面,还会指导如何使你的服务器/托管账号以及Gallery2远离可能遭受的攻击,但不对Gallery2的权限系统多加描述。本文还专门解释以linux/unix为基础的服务器,但相关的大多数指导都可以直接转译给Windows所用。如果我们谈及权限,就忽略出现类似777的数字而只需关注read+write+execute…

介绍

Gallery2的安全离不开WEB服务器的安全。如果服务器/WEB服务器没有安全性的保障,那么Gallery2的安全性也就无从谈起了。

此文中我们涉及以下几个话题:

  • 确保除了你以外没有人可以读/写你的Gallery2文件
  • 确保账户都具有适当的权限
  • 如无必要,不要暴露更多的有关Gallery2/G2版本的信息
  • 如何辨别出不安全的网络托管
  • 与杂项安全有关的话题

有关联系方法

Gallery项目对安全性问题态度极为认真。如果你发现了安全性方面的瑕疵,请不要发布公共BUG报告或在公共论坛里谈及。你可以通过Email的方式直接递交给我们的安全性问题处理小组(gallery.menalto.com)。请提供尽可能多的信息,包括你使用的Gallery版本以及对安全性瑕疵的描述。如果能提供服务器日志或情景重现的类似数据,那将对问题的澄清有质的影响。如果你在某安全性问题公示以前就提交给我们了,你就会得到认可并有资格获得现金奖励

短小的查漏清单

如果你暂时没有那么多时间通读此文档,你可以查看以下所列最为重要的安全性问题。尽管如此,我们还是建议你稍后返回这里,将整个文档都看一下,以保证你尽可能地保证Gallery的安全。

  • config.php的文件系统权限:请保证只有你和WEB服务器能够读(或写)config.php文件。使用它FTP软件将config.php文件的权限设置为444(即所有人可读)。绝大多数的共享网络托管安装不允许权限设置超越“所有人可读”。在某些情况下,权限设置可以更为严格。详细信息请见后文。
  • 禁止查看Gallery代码:如果在你的gallery2/文件夹下已有一个名为.htaccess的文件,就在它的顶部加入(复制粘贴即可)如下代码。相反,如果没有发现此文件,就用文本编辑器创建一个。你可能需要使用FTP来将其重命名为.htaccess,因为Windows不允许扩展名(即“.”)之前空空如也。以下的代码应作为该.htaccess文件的一部分:
 <Files ~ "\.(inc|class)$">
   Deny from all
 </Files>
注: 有关.htaccess的知道仅对托管于Aapche服务器上的网站适用。同样地,如果你被允许访问该WEB服务器上的配置文件的话,你可以直接将这些改变应用于httpd.conf而无需是.htaccess。
  • gallery2/文件夹的文件系统权限:确保没有人可以写入gallery2/目录,WEB服务器用户也不可以。使用FTP软件将gallery2/目录的权限修改为555(所有人具有读和列表权限)。在某些情况下,权限设置可以更为严格。详细信息请见后文。
  • 存储文件夹位置:存储文件夹(缺省为'g2data/')不能够为web-accessible,即它应当置于文档根目录之外。

初步了解你的WEB服务器

无论你是使用托管WEB服务器还是专用的WEB服务器,首先需要初步了解你的环境。我们会对以下设定感兴趣:

  • 网站中用以执行所有PHP脚本的帐户
  • PHP open_basedir设定的(本地)值
  • 服务器API,无论PHP是如何运行的,Apache / IIS / ... Module或(fast-)cgi

创建一个名为“info.php”的文件用来查找这些设定的值,该文件内容如下:

<?php print "PHP runs under the user: [" . system('whoami') . "]<br>"; phpinfo(); ?>

将其放在gallery2文件夹下,或是预备给上传gallery2用的文件夹。接着使用WEB浏览器浏览该文件。

PHP是在你自己的帐户下运行还是在一个通用帐户下?

一般来讲,WEB服务器中PHP脚本的运行有两种不同的方式。

  • WEB服务器中所有的PHP脚本都在同一个帐户下运行。而在一个共享的网络托管计划中,所有域名/帐户下的PHP脚本都在同一个通用帐户(generic user)下运行。
  • WEB服务器根据不同的帐户/域名而有所区别。意即在一个共享的虚拟主机上,你的PHP脚本是为“你的用户”所运行的(大多数情况下就是你的FTP帐户);而该主机上的其他网站则在其他帐户下运行。为了安全起见,这无疑是更好的方式!

如果你的PHP 服务器APIApacheIIS或Module / mod_php,那么此服务器上的所有PHP脚本都于一个通用帐户下运行。将PHP脚本作为WEB服务器模块并运行于不同的帐户下是不可能的。mod_php则意味着该WEB服务器上的所有脚本都运行于相同的通用帐户下。info.php极有可能在[]标记中显示不同于你的用户/帐户名称的其他通用名称。

如果你的PHP服务器API是php-cgiphp-fastcgi,那么PHP脚本很有可能在你的帐户下运行。如果info.php页面在[]中显示你的用户/帐户名称的话,那很明显地是在你的帐户下运行。

注:可能在[]中没有任何东西,甚至有可能你会得到函数“system”被禁用的警告。

设置Gallery2文件和目录的读写权限

在此我们要确保:

  • 除了你没有人可以阅读你的config.php文件内容(安装密码存放之处),并且
  • 没有人可以读/写你的g2data (存储)目录。

我们需要区分以下三种情况:

a) PHP为mod_php而open_basedir没有设置或不够严格

如果你的PHP运行为mod_php(所有脚本的通用用户),那么你的唯一希望就是PHP open_basedir(见info.php)设置的足够严格。PHP open_basedir应设置为除了你的g2data文件夹和gallery2文件夹没有其他帐户在此路径下。如果你的PHP服务器API是Apache而open_basedir为空(无值),那你就得跟你的主机供应商谈谈,因为这是一个很大的安全隐患。在此情况下,除非你有一个专用服务器否则无法保证Gallery2的安全。那么在这样的环境中会发生些什么呢?

  • 某个客户可以使用PHP读取你的config并获取密码来肆意而为。
  • 如果你使config.php设置为可写,其他某个用户就有可能写入不可用的配置或是调用其他数据的配置,从而一定程度上对你造成损害。
  • 某个客户可以使用PHP 读/写你的Gallery2存储目录(g2data)

在此种环境中你所能做的有:

  • 设置config.php为444 (所有人可读权限)
  • 为你的G2管理员帐户启用另外一套密码取代安装密码(G2安装时输入的密码)
  • 删除upgrade/文件夹,因为某人可以使用存储在config.php中的安装密码访问此文件夹
  • 另见 如何保证Gallery2的安全

b) PHP为mod_php且open_basedir设置足够严格

尽管你的WEB服务器并非固若金汤,但严格设置的open_basedir聊胜于无。你需要保证config.php文件为全球/公共范围可读。Open_basedir将会确保仅你所在帐户下的PHP代码可以打开你的帐户下的文件。

  • 将config.php的权限设置为444(所有者,组和世界/公共范围可读权限)
  • 将g2data的权限设置为777(所有人具有读/写/执行的权限)。递归地应用此权限设置(包括所有次级文件夹和文件等)。

另外一个办法就是请求管理员改变config.php文件和g2data目录(递归地包括下属文件夹)的所有者,这样通用WEB服务器用户就会享有这些文件了牡蛎了。(命令:chown -R username g2data,此命令只能被管理员执行)。那么为什么这样就可以提高安全性了呢?在此情形下,其他人无法再通过shell(telnet/ssh)读/写你的文件了。接着你可以设置权限为:

  • 设置config.php的权限为400(所有者可读,无其他权限)
  • 设置g2data的权限为700 (所有者可读/写/执行权限,无其他权限)。递归地应用此权限设置(包括所有次级文件夹和文件等)。

注: Open_basedir仅保证PHP下的安全性,不包括其他脚本语言。如果WEB服务器也允许Perl或其他脚本语言,那么你的Gallery2还无法保证绝对的安全—对于来自其他人或其他网页脚本的攻击。

c) PHP为cgi并且所有脚本运行于你的帐户下

你很走运。如果PHP仅在你的用户帐户下运行,那我们就可以将其他用户的所有文件系统移除了。

  • 将config.php的权限设置为400 (所有者/你的可读权限,其他任何人均无此权限)。
  • 将g2data的权限设置为700 (所有者/你的读/写/执行权限,其他任何人均无此权限)。递归地应用此权限设置(包括所有次级文件夹和文件等)。
  • 在G2的site admin -> general中,为新的目录设置权限为700,而新文件设置为600。

限制WEB服务器用户的权利

你应当明确,Gallery可能存在的易损性是不应扩大的。尽可能地限制运行WEB服务器/PHP脚本的用户的权限。比如,不将WEB服务器作为根目录运行。

用户应该仅享有以下权利

  • 对g2data及下属文件夹的读/写/执行
  • 对g2data及下属文件夹中文件的读/写/执行
  • 对gallery2文件夹及下属文件夹的读/写/执行
  • gallery2及下属文件夹中所有文件可读
  • 二进制及二进制所在文件夹的读和执行(imagemagick,netpbm,ffmpeg,dcraw,unzip和zip...)


  • 同时你应当阻止所有对存储目录(缺省为'g2data/')的访问。添加以下内容到存储文件夹下名为.htaccess文件中:
Deny From All

请勿暴露有关你的Gallery2的信息

Gallery2的编写是将安全性考虑在内的。尽管如此,安全性的薄弱环节还是存在于Gallery2中的(所有已知问题均被修复)。你应当尽量不让网站访问者知道你的Gallery2的版本。因为一旦攻击者知道你的Gallery2版本的话,他们就有可能钻空子。比如说,你在首页显示了完整的版本号,某个攻击者就会用GOOGLE查到一列易损的安装,其中包括有你使用的。

  • 在你的WEB服务器配置中(Apache的httpd.conf/.htaccess),可以禁止所有对源代码文件和文件完整性checksum文件(MANIFEST)的访问。
<Files *.inc>
Deny From All
</Files>
<Files *.class>
Deny From All
</Files>
<Files MANIFEST>
Deny From All
</Files>
  • 移除你的网站上的目录列表。加入:
<Directory YOUR WEBROOT PATH>
 Options -Indexes
</Directory>

你可以打开modules/来测试是否暴露了。如果你可以列出所有已安装的模块,Apache就会列出目录。

  • 将gallery2文件夹下的README.html删除
  • 降低PHP错误报告级别,因为PHP Notices/Warnings等能够暴露确切的文件系统路径及其他信息。在php.ini(PHP配置文件)中,将error_reporting级别至少改为:
error_reporting = E_ALL &~ (E_WARNING | E_NOTICE)

或者

error_reporting = E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR

请注意抑制的错误越多,对真正问题的诊断就越难。一旦你遇上问题,你应当将error_reporting修改为E_ALL。

如何辨认出不安全的Webhost

一个共享的服务器,如果是在相同的通用帐户而不是特定帐户下运行所有的PHP脚本的话,其安全性就会被限制在一个很小的范围内。所有基于mod_php的webservers/webhosts都在此列。如果在你的info.php中没有看见php-cgi或php-fastcgi作为服务器API的话,那么你的webhost就在此列。

另外如果open_basedir过于公开或根本没有设置的话,我们建议还是不要再此服务器上使用Gallery2了。

现状分析: 大半部分网络托管计划均在上述行列之中。而我们听说类似Gallery2一夜之间被删除的例子几乎没有,或者可能的话也就是一年一次。所以,这并没有之前所说的那么不幸。因此在一个webhost中与恶意用户为伴的可能性小之又小。

此部分的目的在于让你明白你的webhost并非想象的那样安全。至少你应该要求你的webhost改变现状(一个请求并不过分,而且这是大势所趋,谁都希望有安全的网络托管环境)。如果你想要一个真正安全的环境,很多专业网络托管公司至少会为每个帐户严格配置open_basedir路径。而很小一部分低端中端的则会提供基于php-cgi的网络托管。

如何配置一个安全的WEB服务器

这个话题需要大量篇幅才能说清楚。而此部分只浅酌基础:

  • 如果你在使用一个专用服务器,你可以使用mod_php。请确保为WEB服务器的运行另开一个用户,并赋予最低限度的权限(前部分所述)。
  • 如果你使用共享的网络托管,就不要使用mod_php。请使用php-fastcgi+suexec+apache2+chroot jails来为你的客户营造一个安全的环境。在这里,如果产生任何损害,则一个用户只会被自己的脚本损害,而不可能是其他人的脚本。与php-cgi不同,php-fastcgi的速度可以与mod_php媲美,而且对系统造成的负担不会比mod_php大。但是与mod_php相比,如果使用于共享的网络托管环境下,php-fastcgi的整个系统可能需要更多些的内存。
  • 请勿定义一个crossdomain.xml。如果真的需要为跨站点flash访问提供服务的话,请务必严格限制crossdomain.xml。注意Macromedia/Adobe Flash的插件与version8有相关薄弱点。因此如果你需要crossdomain.xml,请在论坛中获取帮助来保护自己免受CSRF攻击。

Multisite / Embedded G2

Securing multisites / embedded G2's can be a little more complicated in some cases. Possible issues:

  • PHP open_basedir of embedded G2 / of the multisite does not include the Gallery 2 path.
    • Solution: change the open_basedir to include the path too or change the location/path of Gallery 2
  • PHP runs under your account and you installed it from one account/domain (multisite -> codebase, embedded G2 -> Gallery 2) and now you access Gallery 2 from the multisite or embedded with another domain/account.
    • Solution:
      • Multisite: After the initial installation from the codebase, you will never have to access the multisite from the codebase again. Even upgrading is done from the multisite. Thus, just run chown -R multisite_accountName multisite_g2data (replace the name/g2data paths with correct values), that is, just change the owner of g2data and config.php of the multisite to the multisite owner. Also, make sure the codebase is open enough that all multisite users can read the gallery2 files and read+execute the gallery2 folders.
      • Embedded G2: You'll need to make sure that both users (G2 standalone domain, Embedded g2 domain) have the necessary permissions described in the last section.

General Remarks

  • Use another password for Gallery 2 than for other applications / systems, use another password for your admin user than the one that you specified during the installation
  • Make use of the Gallery 2 image firewall. If the g2data directory is not outside the document root (the public_html, html, www, . folder), the image permissions can be worked around by browsing to an image in the g2data directory directly
  • Warning: If you add a path to your local upload path list in site admin -> general, then all your Gallery 2 users with add item permission can use add items -> from local server and add files from this local path.
  • All Gallery 2 site administrators can do everything in your Gallery 2 installation. Delete all albums with a single click, delete users, etc. Make sure you trust the users that you add to your site administrator group. Generally, we recommend to not add anyone to this group and we even recommend to use another Gallery 2 user acocunt for your everyday tasks / browsing for yourself, similar to how one should not use the linux root user / Windows administrator user for everyday work.

FAQ

What about PHP safe_mode?

Despite its name, PHP safe_mode doesn't imply a secure environment for PHP scripts. It's much too restrictive disabling functionality that would be quite useful even in a secure environment and PHP extensions and other script languages can still work around the safe_mode protections/limitations. Much has been said about PHP safe_mode and to cut a long story short, even the authors of PHP think offering safe_mode was a failure and PHP safe_mode will be removed in PHP 6, it won't even be available as option.

Also see G2 FAQ: Does G2 work with Safe_Mode?

What is PHP open_basedir?

See PHP open_basedir . If set restrictive enough, and if only PHP and no other computer language can be used on this server by other users and if no ssh/telnet access is granted to customers, open_basedir is not a bad protection for your Gallery 2 installation from other customers on the same webhost.

Please see How to configure open_basedir for G2.

How can other Customers on the same webhost harm my Gallery 2 installation?

  • Malicious customers can use PHP to read/write to your config.php / g2data directory if not properly secured
  • Other customers don't even have to be malicious, it suffices that a single PHP script installed by another customer has a security vulnerability and it could be exploited to not only do harm to the other customer's data / website, but to any other website on the same webhost too. Unless the webserver is properly secured.
  • Other programming languages can also be used for an attack, it doesn't have to be PHP. Any language that is available on this webhost can be a possible security threat.
  • If customers have telnet/ssh access, they can also access and manipulate your data / website, unless you set the permissions correctly / the webserver is properly secured.

What about SELinux?

SELinux doesn't work with Gallery 2 if not configured properly. See Gallery 2 Installation on a SELinux enabled Server for more information. We neither recommend SELinux nor discourage from using it, we merely explain how to install Gallery 2 on a SELinux enabled server.

What about Apache mod_security?

mod_security doesn't work by default with Gallery 2. It needs special configuration, see Known Issues.

What about using symlinks and open_basedir together?

Currently, you can't use symlinks and open_basedir together in Gallery 2. Please see: How to configure open_basedir for G2.

Do I need to delete the install/ and upgrade/ folders after the installation?

No, these folders don't need to be deleted. But you can delete them, Gallery 2 will still work even when they are deleted. One can access the installer only by creating a login.txt file in the gallery2 folder. Usually, that's just a single person. You can't create / edit login.txt with the webbrowser. You can access the upgrader only with the setup password (which is stored in config.php).

What about the Database?

Make sure that only your database user has permissions to connect to your database. Your database user needs quite a lot database permissions, including alter table, drop table, same for index etc. So you can't restrict the permissions of this user really well.

What does web-accessible mean / what is the document root?

We say a file or a folder is directly web-accessible if other people can access it through a web browser or through other web clients. Example: The gallery2/main.php file of your Gallery is web-accessible, everybody can browse to it.

Another example: The Gallery storage folder (e.g. 'g2data/') potentially has some sensitive data in it. If people can access this data just by browsing to a file within your storage folder, security is compromised. Therefore, for maximum security, you should ensure that your storage folder is not accessible from the web.

You can accomplish this by ensuring that the storage folder is above the web document root. The web document root is the filesystem folder in which all your website files and folders are. Usually the name for that folder is 'htdocs', 'www' or 'public_html'. All files and folders within the document root folder are web-accessible.

Example: '/home/yourusername/public_html/' could be your document root. '/home/yourusername/public_html/gallery/g2data/' is within the document root and therefore web-accessible. '/home/yourusername/g2data' is above the document root and therefore not web-accessible, which is how it's supposed to be for the storage folder.