余温旧梦-王世彪的博客
关注公众号
  • 开发桌面程序
  • javascript教程
  • css样式
  • vuejs
  • 部署免费CDN
  • SEO搜索引擎优化
  • vuepress教程
  • redis分布式缓存
  • Golang
  • PHP
  • Python
  • Java
  • NodeJs
  • tomcat
  • ELK
  • Mysql数据库
  • Nosql数据库
  • C/C++底层开发
  • 音视频/流媒体
  • linux服务器
  • nginx服务器
  • 容器技术
  • 负载均衡
  • 项目管理
  • 持续集成
  • 大数据
  • 微信公众号运营
  • markdown用法
赞一个
友链
联系作者

王世彪

努力做个影响他人滴人
关注公众号
  • 开发桌面程序
  • javascript教程
  • css样式
  • vuejs
  • 部署免费CDN
  • SEO搜索引擎优化
  • vuepress教程
  • redis分布式缓存
  • Golang
  • PHP
  • Python
  • Java
  • NodeJs
  • tomcat
  • ELK
  • Mysql数据库
  • Nosql数据库
  • C/C++底层开发
  • 音视频/流媒体
  • linux服务器
  • nginx服务器
  • 容器技术
  • 负载均衡
  • 项目管理
  • 持续集成
  • 大数据
  • 微信公众号运营
  • markdown用法
赞一个
友链
联系作者
  • 分布式缓存

  • 分布式存储

  • mysql

  • postgresql

  • mongodb

  • c或c++

  • golang

  • php

    • centos安装php多版本管理工具phpbrew
    • php多版本管理工具phpbrew的用法
    • php curl扩展访问https地址返回502问题的排查
    • centos8安装php环境
    • 安装php7
    • 配置使apache可通过php用root权限执行命令
    • phpstorm自动同步(部署)本地代码到服务端
    • windows安装phalcon开发环境
    • linux安装phalcon开发环境
    • centos8安装phalcon开发环境
    • php代码加密-借助screw plus
      • 1. 安装依赖环境
      • 2. 下载 screw plus 工程代码
      • 3. 使用 phpize 动态生成扩展开发环境
      • 4. 配置
      • 5. 编译解密扩展
      • 6. 编译加密或解密程序
      • 7. 加密 php 文件
      • 8. 解密 php 文件
  • java

  • nodejs

  • python

  • tomcat

  • elk

  • 音视频流媒体

  • 后台
  • php
王世彪
2020-08-24
目录

php代码加密-借助screw plus

# php 代码加密-借助 screw plus

本文讲述如何使用 screw plus 对 php 代码进行加密和解密,达到保护 php 代码的目的。screw plus 是一个 php 扩展, 当要解释某个 php 代码文件时, screw 自动判断是否是经过加密的,如果是,则通过事先配置的密钥(是编译扩展的时候指定的,即秘钥已经编译进了扩展程序中)解密, 然后再交给 php 解释器处理。screw plus 总共会输出 2 个程序, 分别是 php 扩展(用于自动解密 php 文件)和可执行文件(手动加密或解密 php 文件)。借助 screw plus, 可以实现按需加密,即仅对需要加密的文件进行加密。

# 1. 安装依赖环境

安装 autoconf

提示

执行 phpize 生成扩展的过程中需要 autoconf

yum install autoconf -y

# 2. 下载 screw plus 工程代码

git clone https://gitee.com/splot/php-screw-plus.git

# 3. 使用 phpize 动态生成扩展开发环境

进入 screw plus 工程根目录,执行 phpize。
phpize 的路径替换为当前 php 环境下实际的路径即可。

[root@tadoo001 php-screw-plus]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version:         20170718
Zend Module Api No:      20170718
Zend Extension Api No:   320170718

# 4. 配置

进入 screw plus 工程根目录,执行./configure --with-php-config=/usr/local/php/bin/php-config
php-config 的路径替换为当前 php 环境下实际的路径即可。

[root@tadoo001 php-screw-plus]# ./configure --with-php-config=/usr/local/php/bin/php-config
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for a sed that does not truncate output... /usr/bin/sed
checking for cc... cc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether cc accepts -g... yes
checking for cc option to accept ISO C89... none needed
checking how to run the C preprocessor... cc -E
checking for icc... no
checking for suncc... no
checking whether cc understands -c and -o together... yes
checking for system library directory... lib
checking if compiler supports -R... no
checking if compiler supports -Wl,-rpath,... yes
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
checking for PHP prefix... /usr/local/php
checking for PHP includes... -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib
checking for PHP extension directory... /usr/local/php/lib/php/extensions/no-debug-non-zts-20170718
checking for PHP installed headers prefix... /usr/local/php/include/php
checking if debug is enabled... no
checking if zts is enabled... no
checking for re2c... no
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
checking for gawk... gawk
checking for php_screw_plus support... yes, shared
checking whether to enable php_screw_plus support... yes, shared
checking for ld used by cc... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for /usr/bin/ld option to reload object files... -r
checking for BSD-compatible nm... /usr/bin/nm -B
checking whether ln -s works... yes
checking how to recognize dependent libraries... pass_all
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking dlfcn.h usability... yes
checking dlfcn.h presence... yes
checking for dlfcn.h... yes
checking the maximum length of command line arguments... 1572864
checking command to parse /usr/bin/nm -B output from cc object... ok
checking for objdir... .libs
checking for ar... ar
checking for ranlib... ranlib
checking for strip... strip
checking if cc supports -fno-rtti -fno-exceptions... no
checking for cc option to produce PIC... -fPIC
checking if cc PIC flag -fPIC works... yes
checking if cc static flag -static works... no
checking if cc supports -c -o file.o... yes
checking whether the cc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... no

creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h
[root@tadoo001 php-screw-plus]#

# 5. 编译解密扩展

解密扩展指的是 php screw .so 扩展文件。
编译解密程序之前, 先自定义用于加密 php 文件的 key(CAKEY 常量的值)

[root@tadoo001 php-screw-plus]# cat ./php_screw_plus.h
#define CAKEY  "FwWpZKxH7twCAG4JQMO"
//如果只允许执行加过密的php文件 设置STRICT_MODE为1
//set STRICT_MODE to 1 if you only want the crypted php files to be executed
#define STRICT_MODE 0
#define STRICT_MODE_ERROR_MESSAGE "ACCESS DENIED"
const int maxBytes = 1024*1024*2;[root@tadoo001 php-screw-plus]#

php-screw-plus 根目录下,执行 make 命令, 编译解密扩展。
生成路径:./modules/php_screw_plus.so

[root@tadoo001 php-screw-plus]# ll ./modules/
总用量 72
-rw-r--r-- 1 root root   830 8月  24 17:16 php_screw_plus.la
-rwxr-xr-x 1 root root 68816 8月  24 17:16 php_screw_plus.so
[root@tadoo001 php-screw-plus]#

复制扩展文件到 php 扩展目录

提示

实际上在执行 phpize 的时候,已经自动生成了扩展文件到扩展目录,但是默认生成的扩展文件可能无法使用,且没有集成自定义加密 key, 所以此处需要做覆盖操作。

cp ./modules/php_screw_plus.so /usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/php_screw_plus.so

修改 php.ini 文件,配置扩展路径

提示

实际上在执行 phpize 的时候,已经自动配置了 php.ini 文件。此处您只需确认下即可。

[SCREW]
extension=php_screw_plus.so;

重启 php-fpm 服务

提示

重启 php-fpm 服务, php.ini 的配置修改才会生效。

# 6. 编译加密或解密程序

提示

加密和解密程序是同一个二进制文件。

安装依赖

yum install zlib-devel -y

进入 php-screw-plus/tools 目录, 执行 make,生成加密程序 screw。

[root@tadoo001 tools]# ll
总用量 44
-rw-r--r-- 1 root root    94 8月  24 15:24 Makefile
-rw-r--r-- 1 root root  4707 8月  24 15:24 screw.c
[root@tadoo001 tools]# make
gcc -o screw screw.c -lz
[root@tadoo001 tools]# ll
总用量 44
-rw-r--r-- 1 root root    94 8月  24 15:24 Makefile
-rwxr-xr-x 1 root root 31488 8月  24 17:22 screw
-rw-r--r-- 1 root root  4707 8月  24 15:24 screw.c
[root@tadoo001 tools]#

# 7. 加密 php 文件

命令格式: ./screw 目录或文件的路径

[root@tadoo001 tools]# ./screw /var/www/viyun/viyun_backend/app/controllers/LearnedController.php
Success Crypting - /var/www/viyun/viyun_backend/app/controllers/LearnedController.php
[root@tadoo001 tools]#

查看加密后的文件内容

[root@tadoo001 tools]# cat /var/www/viyun/viyun_backend/app/controllers/LearnedController.php
a57f658413a1a0672612]��M��&��~▒����_�W
b����Az.Zzԁi������t�D+�(t��N�`h�o<u۔�Z
v�a8��ʸj1�ɝb(G�Q��&Thm��dHk6�9�+r�^B��Ϸ�R����ݵ����Q�J���
5▒����G�����%W�H�xb������ګ�q��y���O^&�j\K�oF7�l�03�s�>)g
�<Ө[����~l�x��jC˭�f�x���6��'��m�1Z(;�>���O      �m;f{�|�+�'Z�WfpM!�������3�nM�=�|▒� �a
                                                                                      ��Um��1/,�K��Dy&#�h@�a�j>��%�fIx�;�]�0��W�=
                                                                                                                                 �;?�,L�=��Z▒5�(Qnxɮ�����iO�
                                                                                                                                                            \▒�?L/L�!��Bo�NF��<�+I����
                   � ޗ���|��a�W���A�ʂ�
�<����y:%4����#OE����k}�wG@▒3�_Xɡ�P��{r��������Y?g�l�I�s���nL�t�:y��~�R�rZO�Y㑧�;�PRhxBV�����▒�E]dL�'IRe1�?Nj������%#G󽛘;i���Y������}��E��K=��$��~W��[D�}
'��By�\�7
c��K%����e9Oi�/<���2�H�wy0�[F�u�u �{�ؤ�'�����������'Ԛ���#a��_���;�򣼸m'�ED죃��M����6�[;bAM�|�(���^�4�+��[�[YS�����.0q ��[6��Z�J�Wbly�\��
           �����Gq�}b�o4��d��³�Il"
                                  ��َ���8|�▒����m��QS���&X��-6'ʁ�Q       v��2�>�kw���%u����\SȪO��}4
                                                                                                  H�K�E�s��A1��x���;�(Q].M����*.G9�[���b�u���wԙf���8��������▒�?)�ho�-J��uۑ�L(�����M?���ʈ
                     ��dWu[���q�Y!�bÞB��Fc��7ގ��Ċ�,Xٝ��.g��d[�?
�▒d��7�nb�����X�H                                             t�}V%,��)+��x��M�!�<��������< �bU�A���

�������?�x▒87x�a
                r5�o2�����Ҥ-0>�MZQ�M��VA��
                                          ��:hL�l�v׼B��LE��G!�C�3w�X��\�q��Av�v��       ,'�Њ�R�-栅�4f��D+c��9��p+t(݈
/��l��.�(M������T�)6x�s���f
                           ���B�T����-%�X�����
|�tl�q�=/�s[���H���D��.��"�
                           cƫ�Fc%�l�a��o�����ɉti����s���ݷ
                                                         �,�~1\��]��8�C9[f(����dۓW{���r���
                                                                                          �F�   7.��f{  ��+�i
                                                                                                             FG��x⌌K�uS��^��m� �.�\�#���u(���|X�J�F���
�䛸5*�=s���<
            >��� 6aj$�������t-���Ѓwu�@���2��_J8@d��U��`O�S$��_���k�o����4:U�F�����߉�G#
                                                                                      �M'#�S\F�x���ǵh�%�+U����N�
                                                                                                                K* l���j�L��z��YW���Y�U�f�Y��Y� ��b�G{�z��9n�T<f}��Rw�����?
�)�i*i�����g�#C*���:�OEn�cZ7{�0��e2��g��E��&�����d-)    (z��a&�h��q�iX9�n�6�0��
                                                                               ����#7�qr���Y�[?�q淖��UX?�/L��*���5d��ڥ��$��^�G_ɈT@����  OC��yh
�uY�6��0<s�GX��Q�eB-��mM#��x�XK���Р�V�j 2c�tZ�@Ź����5Ѫf��                                                                                     B���ڟvc�ςjo���咾r
                                                         ��[�;8�����>�\9�n��Y�+�Ʊ~Ԁ�_���һC�����TѾ�
                                                                                                  lEV�z��▒~2�t��˯S�2��|we�▒�CW�0��&�b�{l�LJ'    ��P���~���6g�3`�W��[n�.��K���n[$56��%erKgSW�4 ���$e�3J�:�
                                      �P��~����O8e
�&QDp�g▒W���!S�l                                  l▒y!b������   Ό�ܛ������=Ő��$�
>|Jh�p��&�Dp�B�o��/?�sk,���▒}���,&�ɷ��~z�i^�A0ZVA�)W[ao=�B�պ�g�
[root@tadoo001 tools]#

# 8. 解密 php 文件

加密的命令后面加个-d 参数, 即代表解密。
命令格式: ./screw 目录或文件的路径 -d

[root@tadoo001 tools]# ./screw /var/www/viyun/viyun_backend/app/controllers/LearnedController.php -d
Success Decrypting - /var/www/viyun/viyun_backend/app/controllers/LearnedController.php
[root@tadoo001 tools]#

发现经过加密的文件已经被解密为明文。

#加密#php#screw plus
上次更新: 2020-09-18 18:28:18
centos8安装phalcon开发环境
springboot 2集成redis

← centos8安装phalcon开发环境 springboot 2集成redis→

最近更新
01
mysql创建用户
04-09
02
golang错误处理最佳实践
03-17
03
基于proto文件生成rpc接口定义文档
03-11
更多文章>
Theme by Vdoing | Copyright © 2019-2022 王世彪 | MIT License
冀ICP备19016776号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式