


The principle and practice of modifying the Zend engine to implement PHP source code encryption
1. Basic Principles
Consider intercepting the interface for PHP to read source files. At first, I considered dealing with the interface between Apache and PHP, see apache's src/modules/php4/mod_php4.c (this is the file that PHP statically compiles into apache, make install), in send_php() The file pointer is intercepted in the function, using the method of temporary file, and the file pointer is replaced after decryption. This method has been tested and proven to be feasible. However, two file operations must be used, which is inefficient and cannot be used for DSO. Shuangyuan Nursing Home
Therefore, I reconsidered the process of intercepting PHP reading files and loading them into the cache. After a strenuous search, I found that zend-scanner.c does this in the Zend engine. Start modifying this file. Lighting project
2. Implementation method
Using libmcrypt as the encryption module, now using the DES method ECB mode encryption,
The following is the source code of file encryption:
C++ code
/* ecb.c--- ----------------cut here-----------*/
/* encrypt for php source code version 0.99 beta
we are using libmcrypt to encrypt codes , please
install it first.
compile command line:
gcc -O6 -lmcrypt -lm -o encryptphp ecb.c
please set LD_LIBRARY_PATH before use.
GNU copyleft, designed by wangsu , miweicong */
#define MCRYPT_BACKWARDS_COMPATIBLE 1
#define PHP_CACHESIZE 8192
#include < mcrypt.h >
#include < stdio.h >
#include < stdlib.h >
#include < math.h >
#include < sys/types.h >
#include < sys/stat.h >
#include < fcntl.h >
main(int argc, char** argv)
{
int td, i ,j,inputfilesize,filelength;
char filename[255];
char password[12];
FILE* ifp;
int readfd;
char *key;
void *block_buffer;
void *file_buffer;
int keysize;
int decode=0;
int realbufsize=0;
struct stat *filestat;
if(argc == 3) {
strcpy(password,argv[1]);
strcpy(filename,argv[2]);
} else if(argc == 4 && !strcmp(argv[1],"-d")){
strcpy(password,argv[2]);
strcpy(filename,argv[3]);
decode= 1;
printf("Entering decode mode ... n");
} else {
printf("Usage: encryptphp [-d] password filenamen");
exit(1);
}
keysize=mcrypt_get_key_size (DES);
key=calloc(1, mcrypt_get_key_size(DES));
gen_key_sha1( key, NULL, 0, keysize, password, strlen(password));
td=init_mcrypt_ecb(DES, key, keysize);
if((readfd=open(filename,O_RDONLY,S_IRUSR|S_IWUSR|S_IRGRP))==-1){
printf("FATAL: Can't open file to read");
exit(3);
}
filestat=malloc(sizeof(stat));
fstat(readfd,filestat);
inputfilesize=filestat- >st_size;
printf("filesize is %d n",inputfilesize);
filelength=inputfilesize;
inputfilesize =((int)(floor(inputfilesize/PHP_CACHESIZE))+1)*PHP_CACHESIZE;
if((file_buffer=malloc(inputfilesize))==NULL){
printf("FATAL: can't malloc file buffer.n ");
exit(2);
}
if((block_buffer=malloc(PHP_CACHESIZE))==NULL){
printf("FATAL: can't malloc encrypt block buffer.n");
exit(2) ;
}
j=0;
while(realbufsize=read (readfd,block_buffer, PHP_CACHESIZE)){
printf(".");
if(!decode){
if(realbufsize< PHP_CACHESIZE){
for( i=realbufsize;i< PHP_CACHESIZE;i++){
((char *)block_buffer)[i]=' ';
}
}
mcrypt_ecb (td, block_buffer, PHP_CACHESIZE);
} else {
mdecrypt_ecb (td, block_buffer , realbufsize);
}
memcpy(file_buffer+j*PHP_CACHESIZE,block_buffer,PHP_CACHESIZE);
j++;
}
close(readfd);
if((ifp=fopen(filename,"wb"))== NULL){
printf("FATAL: file access error.n");
exit(3);
}
fwrite ( file_buffer, inputfilesize, 1, ifp);
free(block_buffer);
free(file_buffer);
free(filestat);
fclose(ifp);
printf("n");
return 0;
}
/*--- end of ecb.c ----------- --------------------------*/
Because ECB mode is a block encryption with a determined block length, some null characters are filled in here. International Exhibition
Then, modify Zend/zend-scanner.c in the php code as follows:
(My php version is 4.01pl2, SUNsparc/solaris 2.7, gcc 2.95;)
Add before the file:
#define MCRYPT_BACKWARDS_COMPATIBLE 1
#include < mcrypt.h >
Then, comment out the definition of YY_INPUT around line 3510.
Then, modify the yy_get_next_buffer() function around line 5150:
Add the definition to the function header:
void *tempbuf;
char *key;
char debugstr[255];
int td,keysize;
int x,y;
FILE *fp;
Then, comment out the sentence
YY_INPUT( (&yy_current_buffer- >yy_ch_buf[number_to_move]),
yy_n_chars, num_to_read );
.
Change to:
tempbuf=malloc(num_to_read);
if((yy_n_chars=fread(tempbuf,1,num_to_read,yyin))!=0){
/*decode*/
#define password "PHPphp111222"
# debug 0
keysize=mcrypt_get_key_size(DES);
key=calloc(1, mcrypt_get_key_size(DES));
gen_key_sha1( key, NULL, 0, keysize, password, strlen(password));
td=init_mcrypt_ecb(DES define , key, keysize);
mdecrypt_ecb(td, tempbuf, yy_n_chars);
memcpy((&yy_current_buffer- >yy_ch_buf[number_to_move]),tempbuf,yy_n_chars);
if(debug){
fp=fopen("/tmp/ logs","wb");
fwrite("nstartn",7,1,fp);
fwrite(tempbuf,1,yy_n_chars,fp);
fwrite("nenditn",7,1,fp);
fclose (fp);
}
}
free(tempbuf);
Then, compile php and install it in the normal way. Because I am not familiar with libtool, I chose the static method and added -- when configuring. with-mcrypt, so I don’t have to manually modify the Makefile cable tray
3. Tests and results
After compiling php and apache, I used encryptphp compiled by ecb.c to encrypt several files, which are < 1K, 10K+ , and 40K+, an error occurs when processing 40K size files, and other files are normal. Plastic floor
This is because the ECB encryption method of the block determines that fixed-length blocks must be used. Therefore, please give me some advice on which stream encryption method can be used to take into account zend's cache processing method of reading 8192 bytes each time. (The block length read by zend each time on other platforms may be different)
For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

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



PHP implementation framework: ZendFramework introductory tutorial ZendFramework is an open source website framework developed by PHP and is currently maintained by ZendTechnologies. ZendFramework adopts the MVC design pattern and provides a series of reusable code libraries to serve the implementation of Web2.0 applications and Web Serve. ZendFramework is very popular and respected by PHP developers and has a wide range of

How to use ACL (AccessControlList) for permission control in Zend Framework Introduction: In a web application, permission control is a crucial function. It ensures that users can only access the pages and features they are authorized to access and prevents unauthorized access. The Zend framework provides a convenient way to implement permission control, using the ACL (AccessControlList) component. This article will introduce how to use ACL in Zend Framework

According to news on October 8, the U.S. auto market is undergoing a change under the hood. The previously beloved six-cylinder and eight-cylinder power engines are gradually losing their dominance, while three-cylinder engines are emerging. News on October 8 showed that the U.S. auto market is undergoing a change under the hood. The beloved six-cylinder and eight-cylinder power engines in the past are gradually losing their dominance, and the three-cylinder engine is beginning to emerge. In most people's minds, Americans love large-displacement models, and the "American big V8" has always been the Synonymous with American cars. However, according to data recently released by foreign media, the landscape of the U.S. auto market is undergoing tremendous changes, and the battle under the hood is intensifying. It is understood that before 2019, the United States

The intelligent NPC created by Academician Huang in "Cyberpunk 2077" can already speak Chinese? Qubit's first-hand experience, witnessing NPCs conversing fluently in both Chinese and English, with natural expressions and movements, and matching mouth shapes... If there wasn't a screen in front of me, it would really feel like being there. . At this year's CES exhibition, Nvidia used its intelligent engine Avatar Cloud Engine (ACE) to make game NPCs "alive", which caused quite a shock. △The intelligent NPC displayed at CES uses ACE. The characters in the game can have realistic voice conversations with players, while showing vivid expressions and body movements without having to prepare a script in advance. At the time of its debut, there were Ubisoft, Tencent, NetEase, MiHoYo and other countries.

Real-time global illumination (Real-time GI) has always been the holy grail of computer graphics. Over the years, the industry has proposed various methods to solve this problem. Common methods include constraining the problem domain by utilizing certain assumptions, such as static geometry, a rough scene representation, or tracking rough probes, and interpolating lighting between the two. In Unreal Engine, the global illumination and reflection system Lumen technology was co-founded by Krzysztof Narkowicz and Daniel Wright. The goal was to build a solution that was different from its predecessors, capable of achieving uniform lighting and a baked-like lighting quality. Recently, at SIGGRAPH 2022, Krzysztof Narko

Redmi officially announced today that the new Redmi GPro 2024 will be officially released on March 4. In other words, next week we will have the release of this exciting new product. RedmiGPro2024 makes its full debut as an e-sports flagship, deeply integrating the mobile phone industry capabilities into the notebook business, presenting 210W super performance release, and Redmi performance reaching a new high. Equipped with i9-14900HX processor and RTX4060 graphics card, it perfectly combines e-sports and creation to achieve dual evolution. From this point of view, the performance of this new product will be improved again, and the actual effect is exciting. It was mentioned in the official warm-up that the new Redmi G Pro 2024 brings the PC version of the violent engine. Mobile phone technology empowerment, three major factors lead

To solve engine problems when SAS connects to Oracle database, specific code examples are required. When using SAS software for data analysis, it is often necessary to connect to the database to obtain data for analysis and processing. Among them, connecting to the Oracle database is a relatively common operation. However, sometimes engine problems occur when connecting to the Oracle database, causing the connection to fail or not operate normally. This article will introduce how to solve engine problems when connecting to Oracle database in SAS software, and provide specific code

PHP does not recognize ZendOptimizer, how to solve it? In PHP development, sometimes you may encounter a situation where PHP cannot recognize ZendOptimizer, which will cause some PHP codes to not run properly. In this case, we need to take some measures to solve the problem. Some possible workarounds are described below, along with specific code examples. 1. Confirm whether ZendOptimizer is installed correctly: First, we need to confirm that ZendOptimizer
