php+zend引擎加密原理,修改Zend引擎实现PHP源码加密的原理及实践-PHP教程,PHP应用...

php文件的源码都是明文,这对于某些商业用途来说,并不适合。

因此考虑使用加密的手段保护源码。

实在不耐烦等待zend出编译器,而且编译和加密本质上不是一回事儿。自己动手、开始修改。

一、基本原理

考虑截获php读取源文件的接口。一开始,我考虑从apache和php之间的接口处 处理,参见apache的src/modules/php4/mod_php4.c (这个是php用static方式编译进apache,make install 后的文件),在send_php()函数中截获文件指针,采用临时文件的方式,解密后替换文件指针。这种方 法经过测试实践,证明是可行的。但是,必须使用两次文件操作,效率低下,而且对于dso方式不可采用。

由此,重新考虑截获php读取文件并装载至缓存的过程,经过费力的寻找,发现在zend引擎中zend-scanner.c是做此处理的。开始对此文件修改。

二、实现方法示意

采用libmcrypt作为加 密模块,现在采用的是des方法ecb模式加密,

下面是文件加密的源代码:

/* 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: cant 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: cant malloc file buffer.n");

exit(2);

}

if((block_buffer=malloc(php_cachesize))==null){

printf("fatal: cant 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每次读取的块长度可能有所不同)

四、说明

我的机器是sun ultra1, solaris 2.7, gcc 2.95 , apache 1.3.12,

php 4.01pl2, libmcrypt 2.2.4

我的c水平很差,请大家见谅。这里只是原理说明。

感谢老米飞刀等提供的协助。

源码遵从gnu,需要注意,libmcrypt提供的某些加密方式不是free的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值