使用tonyenc扩展加密PHP文件

说明①:tonyenc不是PHP官方扩展。
说明②:仅支持PHP 7以上版本。
说明③:建议只加密部分重要代码文件,不要全部文件都加密。
 
把tonyenc.zip上传到服务器并解压
[root@localhost ~]# cd /src/
[root@localhost src]# rz -b
rz waiting to receive.
Starting zmodem transfer.  Press Ctrl+C to cancel.
Transferring tonyenc.zip...
  100%       6 KB       6 KB/sec    00:00:01       0 Errors
[root@localhost src]# unzip tonyenc.zip
Archive:  tonyenc.zip
a7359c2fbe9d3d0d7a87b213254c05c5af70e16f
   creating: tonyenc/
  inflating: tonyenc/.gitignore      
  inflating: tonyenc/config.m4       
  inflating: tonyenc/core.h          
  inflating: tonyenc/php_tonyenc.h   
  inflating: tonyenc/tonyenc.c       
  inflating: tonyenc/tonyenc.php     
[root@localhost src]# cd tonyenc # 重要提醒:必须把该目录下的tonyenc.php文件复制并保存好,加密时需要使用该文件
[root@localhost tonyenc]# 
 
修改加密特征头和密钥
[root@localhost tonyenc]# vim core.h
/* 这里定制你的加密特征头,不限长度,十六进制哦 */
const u_char tonyenc_header[] = {
        0x66, 0x88, 0xff, 0x4f,
        0x68, 0x86, 0x00, 0x56,
        0x11, 0x16, 0x16, 0x18,
};
 
/* 这里指定密钥,长一些更安全 */
const u_char tonyenc_key[] = {
        0x9f, 0x49, 0x52, 0x00,
        0x58, 0x9f, 0xff, 0x21,
        0x3e, 0xfe, 0xea, 0xfa,
        0xa6, 0x33, 0xf3, 0xc6,
};
[root@localhost tonyenc]# 
 
开始编译安装
[root@localhost tonyenc]# /program/php/bin/phpize
Configuring for:
PHP Api Version:         20170718
Zend Module Api No:      20170718
Zend Extension Api No:   320170718
[root@localhost tonyenc]# ./configure --with-php-config=/program/php/bin/php-config
[root@localhost tonyenc]# make
[root@localhost tonyenc]# make install
Installing shared extensions:     /program/php/lib/php/extensions/no-debug-non-zts-20170718/
[root@localhost tonyenc]# 
 
修改php.ini文件引入tonyenc扩展
[root@localhost ~]# vim /program/php/php.ini
;extension=tidy
;extension=xmlrpc
;extension=xsl
 
; 引入tonyenc扩展(不需要加文件扩展名.so)
extension=tonyenc
 
;;;;;;;;;;;;;;;;;;;
; Module Settings ;
;;;;;;;;;;;;;;;;;;;
[root@localhost ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[root@localhost ~]# 
使用phpinfo()就可以看到已经引入tonyenc扩展。
 
加密PHP文件需要在命令行模式进行(警告:加密前务必先备份),命令如下:
[root@localhost ~]# /program/php/bin/php tonyenc.php demo.php
说明①:tonyenc.php就是上面提到的要复制并保存好的文件。
说明②:demo.php是要加密的PHP文件,加密后该文件的内容会变成一堆乱码,但仍可以正常执行。
说明③:可以对指定目录下的所有PHP文件都加密,只要把demo.php换成目录名即可。

Copyright © 2024 码农人生. All Rights Reserved