Principles and Practices of modifying Zend engine to implement PHP source code encryption-PHP source code

Source: Internet
Author: User
Ec (2); PHP file source code is plain text, which is not suitable for some commercial purposes. Therefore, we consider using encryption to protect the source code. It is really impatient to wait for zend to exit the compiler, and compilation and encryption are not the same thing in nature. Start modification by yourself. 1. The basic principle is to intercept interfaces for PHP to read source files. At the beginning, I considered processing from the interface between Apache and PHP. For details, see apache srcmodulesphp4mod_php4 script ec (2); script




The source code of PHP files is plain text, which is not suitable for some commercial purposes.
Therefore, we consider using encryption to protect the source code.

It is really impatient to wait for zend to exit the compiler, and compilation and encryption are not the same thing in nature. Start modification by yourself.

I. Basic Principles

Consider intercepting interfaces for PHP to read source files. At the beginning, I considered processing from the interface between Apache and PHP. For more information, see src/modules/php4/mod_php4.c of apache. (This is PHP compiled into apache in static mode, make install file), intercept the file pointer in the send_php () function, use the temporary file method, decrypt and replace the file pointer. This method has been tested and proved to be feasible. However, two file operations are required, which is inefficient and not applicable to DSO. Shuangyuan nursing home
As a result, re-consider the process of intercepting PHP to read files and load them to the cache, after laborious search, found that the zend-scanner.c In the Zend engine is doing this. Start modifying this file. Lighting Engineering

II. Implementation Method Diagram

Libmcrypt is used as the encryption module, and the DES method is used for ECB mode encryption,

The source code of file encryption is as follows:

/* 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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.