1, 防止跳出 web 目录
首先修改 httpd.conf, 如果你只允许你的 php 脚本程序在 web 目录里操作, 还可以修改 httpd.conf 文件限制 php 的操作路径, 比如你的 web 目录是 / usr/local/apache/htdocs, 那么在 httpd.conf 里加上这么几行:
- php_admin_value open_basedir /usr/local/apache
- /htdocs
这样, 如果脚本要读取 / usr/local/apache/htdocs 以外的文件将不会被允许, 如果错误显示打开的话会提示这样的错误:
warning: open_basedir restriction in effect. file is in wrong directory in
/usr/local/apache/htdocs/open.php on line 4 等等.
2 防止 php 木马执行 webshell
打开 safe_mode, 在, php.ini 中设置
disable_functions= passthru,exec,shell_exec,system
二者选一即可, 也可都选.
3 防止 php 木马读写文件目录
在 php.ini 中的 disable_functions= passthru,exec,shell_exec,system, 后面加上 php 处理文件的函数, 主要有:
- fopen,mkdir,rmdir,chmod,unlink,dir
- fopen,fread,fclose,fwrite,file_exists
- closedir,is_dir,readdir.opendir
- fileperms.copy,unlink,delfile
- // 即成为
- disable_functions= passthru,exec,shell_exec,system,fopen,mkdir,rmdir,chmod,unlink,dir
- fopen,fread,fclose,fwrite,file_exists
- closedir,is_dir,readdir.opendir
- fileperms.copy,unlink,delfile
ok, 大功告成, php 木马拿我们没辙了, 遗憾的是这样的话, 利用文本数据库教程的那些东西就都不能用了.
如果是在 windos 平台下搭建的 apache 我们还需要注意一点, apache 默认运行是 system 权限, 这很恐怖, 这让人感觉很不爽, 那我们就给 apache 降降权限吧.
- net user apache fuckmicrosoft /add
- net localgroup users apache /del
ok. 我们建立了一个不属于任何组的用户 apche, 我们打开计算机管理器, 选服务, 点 apache 服务的属性, 我们选择 log on, 选择 this account, 我们填入上面所建立的账户和密码, 重启 apache 服务, ok,apache 运行在低权限下了.
实际上我们还可以通过设置各个文件夹的权限, 来让 apache 用户只能执行我们想让它能干的事情, 给每一个目录建立一个单独能读写的用户, 这也是当前很多虚拟主机提供商的流行配置方法, 不过这种方法用于防止这里就显的有点大材小用了.
来源: https://www.php1.cn/detail/php-e3452aa9d3.html