基本原理
一、基本原理
首先考虑截获PHP读取源文件的接口。起初尝试从Apache与PHP之间的接口处入手,参考Apache源码中的src/modules/php4/mod_php4.c(这是将PHP以静态方式编译进Apache后,执行make install生成的文件)。在send_php()函数中截获文件指针,采用临时文件的方式,解密后替换文件指针。经过测试实践,这种方法虽然可行,但必须进行两次文件操作,导致效率低下,而且不适用于DSO(动态共享对象)方式加载PHP的情况。
因此,重新考虑截获PHP读取文件并将其装载至缓存的过程。经过仔细查找,发现Zend引擎中的zend-scanner.c负责处理这一环节。开始对该文件进行修改。
实现方法示意
二、实现方法示意
采用libmcrypt作为加密模块,目前使用的是DES方法的ECB模式加密。
以下是文件加密的源代码:
C++代码
/* 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 ------------------------------------*/由于ECB模式是块长度确定的块加密算法,此处填充了一些空字符以满足块大小要求。
接下来,修改php代码中的 Zend/zend-scanner.c 文件如下:
(我的php版本是4.01pl2, SUNsparc/solaris 2.7, gcc 2.95;)
在文件前加入以下定义:
#define MCRYPT_BACKWARDS_COMPATIBLE 1
#include < mcrypt.h >
然后,注释掉大约3510行前后的YY_INPUT的定义。
接着,修改大约5150行前后的yy_get_next_buffer()函数:
在函数头加上以下定义:
void *tempbuf;
char *key;
char debugstr[255];
int td,keysize;
int x,y;
FILE *fp;
然后,注释掉以下代码:
YY_INPUT( (&yy_current_buffer- >yy_ch_buf[number_to_move]),
yy_n_chars, num_to_read );
这一句。
改为:
tempbuf=malloc(num_to_read);
if((yy_n_chars=fread(tempbuf,1,num_to_read,yyin))!=0){
/*decode*/#define password "PHPphp111222"
#define 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, 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);最后,编译php,按正常方法安装即可。由于对libtool不太熟悉,因此选择static方式,并在configure时加入了--with-mcrypt参数,这样就不需要自己手工修改Makefile。
测试及结果
三、测试及结果
编译php和apache后,使用ecb.c编译出来的encryptphp工具加密了几个文件,大小分别为小于1K、10K+和40K+。在处理40K大小的文件时出现错误,其他文件均正常运行。
这是因为块的ECB加密方式决定了必须使用定长块,所以,请诸位同好指点采用何种流加密方式可以兼顾到zend每次读取8192字节的缓存处理方式。(其他平台上 zend每次读取的块长度可能有所不同)