兑现PHP的编译执行分离(separating compilation and execution)
实现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)
{
?old_compile_file = zend_compile_file; //保存现场
?old_execute = zend_execute;
?zend_compile_file = my_compile_file; //截获
?zend_execute = my_execute;
?return SUCCESS;
}
在我们的my_compile_file中,判断我们的文件是否是编译过的文件,假设后缀名是*.ze。
static zend_op_array *my_compile_file(zend_file_handle *file_handle, int type TSRMLS_DC)
{
?if(strstr(file_handle->filename, ".ze") != NULL){
?? //是编译过的文件。直接返回文件内容.
?}
?zend_op_array *op_array;
?op_array = old_compile_file (file_handle, type TSRMLS_CC); //调用默认的compile,截获输出
?if(op_array){
?? //保存op_array;
?}
?return op_array;
}
这样,我们就实现了,对已经编译文件的支持,和对文件编译的支持。
然后,需要编写我们的执行函数:
static void my_execute(zend_op_array *op_array TSRMLS_DC)
{
?old_execute(op_array TSRMLS_DC); //简单交由默认执行函数执行。
}
也许你要问为什么要包装以后的执行函数,呵呵,我只是为了说明,一种方式,就是可以截获这个东东而已。有什么用?就看读者你有什么要求能通过这个方式实现了: )。
写到这里,你也许就明白了,如果想要对文件加密,那么就定义个加密文件类型,比如*.zec,然后在my_compile_file中,判断文件类型,如果是加密文件,那么就执行解密,嘿嘿,简单吧?
至于怎么加密,那就要问你自己了,你想用什么方式,但是,记住,要可逆的哦~~^_^。
?
原文地址: http://www.wangchao.net.cn/bbsdetail_1887829.html

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

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

Use Java's File.length() function to get the size of a file. File size is a very common requirement when dealing with file operations. Java provides a very convenient way to get the size of a file, that is, using the length() method of the File class. . This article will introduce how to use this method to get the size of a file and give corresponding code examples. First, we need to create a File object to represent the file we want to get the size of. Here is how to create a File object: Filef

How to convert php blob to file: 1. Create a php sample file; 2. Through "function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })} ” method can be used to convert Blob to File.

Use Java's File.renameTo() function to rename files. In Java programming, we often need to rename files. Java provides the File class to handle file operations, and its renameTo() function can easily rename files. This article will introduce how to use Java's File.renameTo() function to rename files and provide corresponding code examples. The File.renameTo() function is a method of the File class.

Use java's File.getParentFile() function to get the parent directory of a file. In Java programming, we often need to operate files and folders. When we need to get the parent directory of a file, we can use the File.getParentFile() function provided by Java. This article explains how to use this function and provides code examples. File class in Java is the main class used to operate files and folders. It provides many methods to obtain and manipulate file properties

The difference between executeupdate and execute: 1. Purpose and return value; 2. Parameters; 3. Execution time; 4. Exception handling; 5. Performance considerations; 6. Database interaction. Detailed introduction: 1. Purpose and return value. The "executeUpdate()" method is mainly used to execute SQL statements that modify data, such as INSERT, UPDATE or DELETE operations. The xecute() method is more general and can be used to execute any type of SQL. Statements, including querying data and modifying data, etc.

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

Use java's File.getParent() function to get the parent path of a file. In Java programming, we often need to operate files and folders. Sometimes, we need to get the parent path of a file, which is the path of the folder where the file is located. Java's File class provides the getParent() method to obtain the parent path of a file or folder. The File class is Java's abstract representation of files and folders. It provides a series of methods for operating files and folders. Among them, get
