Contents
  1. 1. 安装suhosin
  2. 2. 额外的内容——eval相关的一些操作
    1. 2.1. eval的定义和用法
    2. 2.2. eval函数测试

本方法使用了suhosin,不适用于PHP7,可以参考PHP5 & PHP7 禁用 eval

安装suhosin

下载suhosin

可以参考官网地址http://suhosin.org/stories/download.html

suhosin

注意:PHP5.4以下的版本不要使用最新版
推荐0.9.29(http://download.suhosin.org/suhosin-0.9.29.tgz)。
另外,下面的操作都是0.9.38的,注意版本号的不同

1
wget https://download.suhosin.org/suhosin-0.9.38.tar.gz

解压

1
2
tar zxvf suhosin-0.9.38.tar.gz
cd suhosin-0.9.38

启动phpize

1
/usr/bin/phpize

找不到的话试试whereis 会显示phpize的路径

1
whereis phpize

编译安装(需要注意这里的php-config路径不一定是/usr/bin/php-config 找不到的话也可以使用whereis查看一下)

1
2
./configure --with-php-config=/usr/bin/php-config
make&&make install

安装好后会提示suhosin.so的路径,需要复制下来保存好(如果忘了存 重新安装会再次提醒该路径)

1
Installing shared extensions:     /usr/lib64/php/modules/

编辑PHP配置文件

1
vim /etc/php.ini

最后面加上两行

1
2
extension=/usr/lib64/php/modules/suhosin.so
suhosin.executor.disable_eval = on

注意这里extension=的路径就是刚刚复制的路径

最后重启APACHE

1
service httpd restart

额外的内容——eval相关的一些操作

近几年来服务器受到过多次木马攻击,从收集的木马样本来看,黑客门都喜欢使用eval()函数。对应的,我们排查木马的时候首先排查eval():

1
find -name "*.php" | xargs grep "\beval\b" > hack.log

这样就把所有含有evalphp后缀的文件都找出来了,然后再人工排查其中的可疑文件

为了服务器的安全考虑我们决定禁止PHP使用eval(自己的网站上也不在使用eval函数)

下面可以了解下eval:(摘自w3shool

eval的定义和用法

eval() 函数把字符串按照 PHP 代码来计算。
该字符串必须是合法的 PHP 代码,且必须以分号结尾。
如果没有在代码字符串中调用 return 语句,则返回 NULL。如果代码中存在解析错误,则 eval() 函数返回 false。

eval函数测试

1
2
3
4
5
6
7
8
<?php
$string = "beautiful";
$time = "winter";
$str = 'This is a $string $time morning!';
echo $str. "<br />";
eval("\$str = \"$str\";");
echo $str;
?>

如果可以使用eval,输出结果为:

1
2
This is a $string $time morning!
This is a beautiful winter morning!

如果不可以使用eval,不会输出第二行

可以测试下自己的服务器能不能运行eval

Contents
  1. 1. 安装suhosin
  2. 2. 额外的内容——eval相关的一些操作
    1. 2.1. eval的定义和用法
    2. 2.2. eval函数测试