$7.49 .com domain with free InstantPage Website Builder$1.99 Web Hosting   捷飞网络官方淘宝店   Godaddy 优惠码
返回列表 发帖
分享到:




[安全] 这样完美解决Nginx的跨站(防WEB shell)安全问题

看了很多方法,试了一下这个最好用,需要更改php源程序后,重新编译php。在使用fpm方式安装时,打补丁过程中会修改php的文件,所以需要在打完fpm补丁后再修改php源程序。; d6 r" k, S# X2 w
- D! a$ W# U  t5 o$ R
tar zxvf php-5.2.14.tar.gz
! q6 X" w3 a: a, F/ Qgzip -cd php-5.2.14-fpm-0.5.14.diff.gz | patch -d php-5.2.14 -p1  O8 g: z' k. {" _* s
cd php-5.2.14/
) e8 [- B2 P3 {) Z9 K- E( k2 ?* ?# ]0 y9 b% G4 O" m+ h
vi  main/fopen_wrappers.c
2 W! W( a& [% n. }4 f, N7 Z2 v" d: g

* }% a) I- I" J  M- [  U9 N) u4 h1 R9 x0 J0 L# B2 L
  1. /* {{{ php_check_open_basedir
  2. */
  3. PHPAPI int php_check_open_basedir_ex(const char *path, int warn TSRMLS_DC)
  4. {
  5.         /* Only check when open_basedir is available */
  6.         if (PG(open_basedir) && *PG(open_basedir)) {
  7.                 char *pathbuf;
  8.                 char *ptr;
  9.                 char *end;

  10. // 添加的内容开始
  11.                char *env_document_root = sapi_getenv("DOCUMENT_ROOT", sizeof("DOCUMENT_ROOT")-1 TSRMLS_CC);
  12.                 if (php_check_specific_open_basedir(ptr, path TSRMLS_CC) == 0) {
  13.                         efree(env_document_root);
  14.                         return 0;
  15.                 }
  16.                 // 添加的内容结束

  17.                 pathbuf = estrdup(PG(open_basedir));

  18.                 ptr = pathbuf;

  19.                 while (ptr && *ptr) {
  20.                         end = strchr(ptr, DEFAULT_DIR_SEPARATOR);
  21.                         if (end != NULL) {
  22.                                 *end = '\0';
  23.                                 end++;
  24.                         }

  25.                         if (php_check_specific_open_basedir(ptr, path TSRMLS_CC) == 0) {
  26.                                 efree(pathbuf);
  27.                                 return 0;
  28.                         }

  29.                         ptr = end;
  30.                 }
  31.                 if (warn) {
  32.                         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));
  33.                 }
  34.                 efree(pathbuf);
  35.                 errno = EPERM; /* we deny permission to open it */
  36.                 return -1;
  37.         }

  38.         /* Nothing to check... */
  39.         return 0;
  40. }
复制代码

# S9 i2 ]; W3 x+ j% t4 E  r+ f9 W4 K
需要添加的内容用红色表示出来了,然后重新编译安装php即可。经亲测可用。
" y! Y# `5 y) H2 ^
2 d  {6 [9 H4 g/ M8 L( ^  r" {使用phpspy2008测试,无法浏览其他目录。
9 Z6 h  V! ?. z: h, w+ K参考: http://www.xpb.cn/blog/743.html

补充:7 i( N5 U  e7 |( G3 e! q
http://museum.php.net/php5/php-5.2.10.tar.gz& |% y# F. v8 O: s# Z

' O4 P' l& h/ X- ihttp://cn.php.net/get/php-5.2.15.tar.gz/from/this/mirror
- g0 A, A2 }" N; b1 {1 ^
, h6 @% T5 u% x* f5 chttp://php-fpm.org/downloads/php-5.2.10-fpm-0.5.13.diff.gz


TOP





版本二:完美解决Nginx的跨站(防WEB shell)安全问题

本帖最后由 jim9 于 2010-12-13 06:11 编辑   w# @+ r5 B+ R
# L* L; k% l4 J7 c
转载请注明出自安兴软件工作室: http://www.anxsoft.com3 l4 s( x% O' c4 F1 D7 r
- o. U) I' V3 O3 \" D# I
以前曾发过一个解决的思路:
4 t! r2 k# D5 |( r# r- _# x2 i+ {( mhttp://www.hostloc.com/viewthread.php?tid=1862&highlight=%2Bfreebsd6 l* c) N- n5 Z# U6 c' Q

% o; }1 m1 [; l) I4 e  J( ?; Z当时用phpspy2008测试权限,浏览等,都没有什么问题,但是没有在接近实际的环境中测试过,后来网友们反映还是经常有出现No input file specified.
4 i- @9 b1 g* M. F后我本人又重新测试了一下,发现web服务器刚启动时正常,过了几分钟以后随机性的会出现 No input file specified. 或者返回404。. N/ N. z* p' `7 [1 f. W1 K

* J: S5 ^3 @( b心情很是郁闷,加上近几天自己的网站也准备加强一下安全措施,于是干脆着手修改php的源代码解决,经过几个小时的研究,终于完美解决了这个问题,而且由于本办法是修改php来实现的,故使用范围应该不限于nginx,也可以应用到其他的web服务器上。) R' O8 H% j! @( U# [! [" b/ U

$ l6 ~6 @, E2 ?现将修改办法公布如下,以CentOS 5.3 php 2.5.10 nginx 0.8.27为例& i4 O9 f# {7 P' R( ]2 \
# ]! T  ^5 o4 @; m) N* y% N. P4 J
wget http://cn.php.net/get/php-5.2.15.tar.gz/from/this/mirror
/ {- n6 ~8 V* n# M, Kwget http://php-fpm.org/downloads/php-5.2.10-fpm-0.5.13.diff.gz
! T% l& J8 }6 L2 V# F: Atar zxvf php-5.2.10.tar.gz
/ `' x9 e1 x- G- igzip -cd php-5.2.10-fpm-0.5.13.diff.gz | patch -d php-5.2.10 -p1 //如果你用补丁的话先打补丁再改比较妥当,我没检查过补丁有没动这个文件
) K2 v  l. S7 xcd php-5.2.10/
- i6 R. }7 c9 K0 z' _( t9 d./configure --prefix=...............此处省略N多配置选项
3 J# b: b; a. @3 N5 j5 Cvi main/fopen_wrappers.c: }, `: d  m+ N: v
找到
$ |- S2 Y# F0 v$ O, @' E* D
  A3 D/ p3 Q% h8 d5 G
  1. /* {{{ php_check_open_basedir
  2. */
  3. PHPAPI int php_check_open_basedir_ex(const char *path, int warn TSRMLS_DC)
  4. {
  5. /* Only check when open_basedir is available */
  6. if (PG(open_basedir) && *PG(open_basedir)) {
  7. char *pathbuf;
  8. char *ptr;
  9. char *end;
  10. // add by anxsoft.com
  11. char *env_doc_root;
  12. if(PG(doc_root)){
  13. env_doc_root = estrdup(PG(doc_root));
  14. }else{
  15. env_doc_root = sapi_getenv("DOCUMENT_ROOT", sizeof("DOCUMENT_ROOT")-1 TSRMLS_CC);
  16. }
  17. if(env_doc_root){
  18. int res_root = php_check_specific_open_basedir(env_doc_root, path TSRMLS_CC);
  19. efree(env_doc_root);
  20. if (res_root == 0) {
  21. return 0;
  22. }
  23. if (res_root == -2) {
  24. errno = EPERM;
  25. return -1;
  26. }
  27. }
  28. // add by anxsoft.com


  29. pathbuf = estrdup(PG(open_basedir));

  30. ptr = pathbuf;

  31. while (ptr && *ptr) {
  32. end = strchr(ptr, DEFAULT_DIR_SEPARATOR);
  33. if (end != NULL) {
  34. *end = '\0';
  35. end++;
  36. }

  37. if (php_check_specific_open_basedir(ptr, path TSRMLS_CC) == 0) {
  38. efree(pathbuf);
  39. return 0;
  40. }

  41. ptr = end;
  42. }
  43. if (warn) {
  44. 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));
  45. }
  46. efree(pathbuf);
  47. errno = EPERM; /* we deny permission to open it */
  48. return -1;
  49. }

  50. /* Nothing to check... */
  51. return 0;
  52. }
  53. /* }}} */
复制代码

4 @9 G! O4 |( j: n- j
) O" i9 @# r- z5 T两个 add by anxsoft.com 中间的是修改加上去的1 k- J0 L" R0 ^( X% i
) A" S: B* |! b3 ]
然后保存,退出。% |3 a4 B2 p/ ?# T
8 a! N2 p% {4 V$ `: O$ P' n1 [. L  A
make ZEND_EXTRA_LIBS='-liconv'! R$ m6 m9 j1 N
make install
3 }7 a, C& k: a; V$ y& `. q! f
0 e1 P/ f1 q9 U* [OK,接下来该干嘛干嘛去~~~
( E# e; Z; u; g& K5 w& w+ q# I: q# C9 n0 D  h4 i8 u. r. p# V3 ~9 r% u! F
php.in的open_basedir配置
) s* v% {! r. o+ v! P6 ^$ M! Z# C* ?- \; P9 W7 ^  ]
+ h$ k. T3 j9 q
& d' M& p# |5 i# _- P$ {
nginx的配置无须做任何变动(无须添加 ../../../../../ 等繁琐的东西),完美限制在网站的根目录上,无法访问其他同一服务器上网站的文件。4 }1 u; A& Q1 \) `
5 k( ?7 v3 A/ j- _
另外出现 no input files 绝不是网上某人说的原因。
8 V0 B4 X; v1 k. a5 I  |. V+ _4 C! T& Z! J$ m) n/ g3 |
参考:http://hi.baidu.com/goodtangyb/b ... f4b20da08bb7eb.html

美国VPS、域名代购:http://tu8l.taobao.com

TOP

完美解决同一服务Nginx的跨站(防WEBshell)访问限制问题(亲测)

本问题已成功测试并解决. 见此贴:
6 i, U+ V4 Y3 c# h, @: z  P: Q
1 ~; A5 ]1 J" x+ w9 l' @1 s完美解决同一服务Nginx的跨站(防WEBshell)访问限制问题(亲测)7 ^* h0 u) ]: M. I
详细出处参考:http://bbs.fjbjdd.com/thread-340-1-1.html




TOP

返回列表
Namecheap
Namecheap.com - Cheap domain name registration, renewal and transfers - Free SSL Certificates - Web Hosting
互联网安全