【发布时间】:2016-06-23 13:33:32
【问题描述】:
我正在使用 blenc 函数来加密一些 PHP 代码。
环境如下:
- Lighttpd 作为启用了 fastcgi 模块的 Web 服务器
- 存储在 /usr/local/etc/blenckeys 中的密钥
- 一个加密的 PHP 脚本,可以在 cli 或 web 模式下使用
- cgi和cli之间的php.ini完全一样
如果我在cli模式下使用加密脚本,没有问题。
如果我通过 lighttpd (cgi) 使用相同的加密脚本,我会收到 500 内部错误 -
http://x.x.x.x/myscript.php 的第一次调用有效,但以下将失败并出现此错误:
2016-03-09 08:48:45: (mod_fastcgi.c.2562) 意外的文件结束 (也许fastcgi进程死了):pid:1886 socket: Unix:/tmp/php.socket-0 2016-03-09 08:48:45: (mod_fastcgi.c.3346) 未收到响应,已发送请求:套接字上的 1078:unix:/tmp/php.socket-0 for /myscript.php,关闭连接
编辑
感谢我的同事,我发现了问题并知道如何解决。
问题来自 Zend OPcache,它与 blenc 扩展不兼容。
[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=0
编辑
当然可以分享一部分代码:
在包装中(我加密代码的地方),我将再分发密钥存储到一个“ekey”文件中:
由于我对所有加密文件使用相同的密钥,因此所有文件的重新分发密钥都相同:
$encryptionKey = md5("myApp".rand (1, 65534).time());
if ($obfuscate) {
$redistributionKey = blenc_encrypt("someText", "/tmp/ekey", $encryptionKey);
file_put_contents(__DIR__."/ekey", $redistributionKey);
}
下一步是使用相同的$encryptionKey 加密文件 - PHP 标签被剥离:
$oFileNoTags = preg_replace(array('/^<(\?|\%)\=?(php)?/', '/(\%|\?)>$/'), array('',''), $oFile);
if (function_exists("blenc_encrypt")) {
$redistributionKey = blenc_encrypt($oFileNoTags, $destFolder.basename($file), $encryptionKey);
} else {
$this->errror(570, "blenc_encrypt function not available. Please install first using PECL");
}
最后,在客户端上安装新包时,会读取“ekey”文件并覆盖现有密钥:
$newKeyEncryptionValue = file_get_contents(__DIR__."/ekey");
if (strpos($keyFileContent, $newKeyEncryptionValue) === false) {
// file_put_contents($keyFile, $newKeyEncryptionValue."\n", FILE_APPEND);
$this->system("echo '".$newKeyEncryptionValue."' | sudo tee ".$keyFile);
}
我猜 blenc 和 fast-cgi 有一个错误,我无法解释或者配置错误。
【问题讨论】:
-
不要一直添加答案,在问题中添加额外的 ind=formation。
标签: php fastcgi lighttpd blenc