Corrected the principles and practices of Zend Engine to implement PHP source code encryption

Source: Internet
Author: User
The source code of the PHP file is plain text, which is not suitable for some commercial use. Therefore, the application encryption method is used 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 and start on your own

The source code of the PHP file is plain text, which is not suitable for some commercial use.
Therefore, the application encryption method is used 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. Do it yourself and start fixing.

I. Basic principles

Consider intercepting interfaces for PHP to read source files. At the beginning, I want to handle the problem from the interface between Apache and PHP. For more information, see src/modules/php4/mod_php4.c of apache. (This is why PHP compiled into apache using the static method, make install file), intercept the file pointer in the send_php () function, use the method of temporary file, decrypt and change the file pointer. This method has been tested and proved to be feasible. However, it is necessary to apply two file controls, which is inefficient and not applicable to the DSO method.
Thus, re-consider the process of intercepting PHP to read files and load them to the cache, after laborious searching, the zend-scanner.c in the Zend engine was invented to do this. Start to modify this file.

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: 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;

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.