实现PHP的编译执行分离(separating compilation and execution
刚刚在PHP群内和大家聊天,应承了大家要写一个关于如何实现PHP源码加密的文章,借着这会QA在冒烟的机会,就这个问题,我写点思路。 我以前的文章介绍过,ZE(Zend engine)执行一个PHP脚本会经历编译->执行,只不过它每次执行都会去重新编译PHP文件。并没有实现编译和执行分离。 在ZE的编译和执行阶段,有俩个重要的函数: ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type TSRMLS_DC); 和 ZEND_API void (*zend_execute)(zend_op_array *op_array TSRMLS_DC); zend_compile_file负责将要执行的脚本文件编译成由ZE的基本指令序列构成的op codes,然后将op codes交由zend_execute执行,从而得到我们脚本的结果。 所以,我们完全可以通过修改默认的zend_complie_file和zend_execute来实现,PHP的执行和编译分离,进一步,我们还可以再这个基础上实现,对我们脚本的加密和解密。 我们通过一个PHP扩展模块来实现这个功能,首先,我们需要在模块初始化的时候: PHP_MINIT_FUNCTION(sample) 在我们的my_compile_file中,判断我们的文件是否是编译过的文件,假设后缀名是*.ze。 static zend_op_array *my_compile_file(zend_file_handle *file_handle, int type TSRMLS_DC) op_array = old_compile_file (file_handle, type TSRMLS_CC); //调用默认的compile,截获输出 这样,我们就实现了,对已经编译文件的支持,和对文件编译的支持。 然后,需要编写我们的执行函数: static void my_execute(zend_op_array *op_array TSRMLS_DC) 也许你要问为什么要包装以后的执行函数,呵呵,我只是为了说明,一种方式,就是可以截获这个东东而已。有什么用?就看读者你有什么要求能通过这个方式实现了: )。 写到这里,你也许就明白了,如果想要对文件加密,那么就定义个加密文件类型,比如*.zec,然后在my_compile_file中,判断文件类型,如果是加密文件,那么就执行解密,嘿嘿,简单吧? 至于怎么加密,那就要问你自己了,你想用什么方式,但是,记住,要可逆的哦~~^_^。
{
old_compile_file = zend_compile_file; //保存现场
old_execute = zend_execute;
zend_compile_file = my_compile_file; //截获
zend_execute = my_execute;
return SUCCESS;
}
{
if(strstr(file_handle->filename, ".ze") != NULL){//是编译过的文件。
直接返回文件内容.
}
zend_op_array *op_array;
if(op_array){
保存op_array;
}
return op_array;
}
{
old_execute(op_array TSRMLS_DC); //简单交由默认执行函数执行。
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
