查看: 2930|回复: 25

Nginx防跨目录、跨站设置方法之修改PHP源代码

[复制链接]
发表于 2013-1-24 10:22:38 | 显示全部楼层 |阅读模式

Nginx有一个缺陷,就是没有像apache的php_value_basedir给我们限制php文件访问目录,唉,没办法,我们只有在php上面下手,但是,PHP低版本下,fastcgi 模式下open_base设置无效,PHP在PHP5.3.3以上已经增加了HOST配置,可以起到防跨站、跨目录的问题。

Nginx+PHP防跨站、跨目录的安全设置 支持PHP5.3.3以上版本

那Nginx怎么防止跨目录、跨站设置方法呢?没办法,只得修改PHP的代码咯。。

1、解压php源代码不细说了。
2、执行编译./configure -- (自带参数)

3、修改源代码。此文件位于main/fopen_wrappers.c


01/* {{{ php_check_open_basedir

02*/

03PHPAPI int php_check_open_basedir_ex(const char *path, int warn TSRMLS_DC)

04{

05/* Only check when open_basedir is available */

06if (PG(open_basedir) && *PG(open_basedir)) {

07  char *pathbuf;

08  char *ptr;

09  char *end;

10

11  /*添加开始,  add by http://yi1.com.cn */

12  char *env_doc_root;

13  if(PG(doc_root)){

14   env_doc_root = estrdup(PG(doc_root));

15  }

16  else{

17   env_doc_root = sapi_getenv("DOCUMENT_ROOT", sizeof("DOCUMENT_ROOT")-1 TSRMLS_CC);

18  }

19  if(env_doc_root){

20   int res_root = php_check_specific_open_basedir(env_doc_root, path TSRMLS_CC);

21   efree(env_doc_root);

22   if (res_root == 0) {

23    return 0;

24   }

25   if (res_root == -2) {

26    errno = EPERM;

27    return -1;

28   }

29  }

30  // 添加的内容结束

31

32  pathbuf = estrdup(PG(open_basedir));

33  ptr = pathbuf;

34  while (ptr && *ptr) {

35   end = strchr(ptr, DEFAULT_DIR_SEPARATOR);

36   if (end != NULL) {

37    *end = '\0';

38    end++;

39   }

40   if (php_check_specific_open_basedir(ptr, path TSRMLS_CC) == 0) {

41    efree(pathbuf);

42    return 0;

43   }

44   ptr = end;

45  }

46  if (warn) {

47   php_error_docref(NULL TSRMLS_CC, E_WARNING, "open_basedir restriction in effect. File(%s) is not within the allowed path(s): (%s)", path, PG(open_basedir));

48  }

49  efree(pathbuf);

50  errno = EPERM; /* we deny permission to open it */

51  return -1;

52}

53/* Nothing to check... */

54return 0;

55}

56/* }}} */

然后执行

make ZEND_EXTRA_LIBS='-liconv' make installcp php.ini-dist /usr/local/php/etc/php.ini

最后修改php.ini中的open_basedir改为:open_basedir = "/var/tmp/:/tmp/"

OK啦。就这样了。这样,Nginx就防止跨目录、跨站,哈哈,准确的说是,利用了,PHP的特性。





发表于 2013-1-27 14:39:37 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2013-2-7 08:07:52 | 显示全部楼层
说嘛1~~~想说什么就说什么嘛~~  
发表于 2014-10-26 10:54:38 | 显示全部楼层
说的真有道理啊!
发表于 2014-11-5 19:29:58 | 显示全部楼层
支持~~  
发表于 2014-11-18 10:33:15 | 显示全部楼层
楼主,支持!  
发表于 2014-12-6 04:53:52 | 显示全部楼层
ding   支持  
发表于 2014-12-29 08:44:11 | 显示全部楼层
好贴坏贴,一眼就看出去  
发表于 2015-1-29 07:10:41 | 显示全部楼层
挤在北京,给首都添麻烦了……  
发表于 2015-3-3 08:18:20 | 显示全部楼层
我回不回呢 考虑再三 还是不回了吧 ^_^  
高级模式
B Color Image Link Quote Code Smilies

本版积分规则