修改Zend引擎实现PHP源码加密的原理及实践

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

 

PHP文件的源码都是明文,这对于某些商业用途来说,并不适合。
  因此考虑使用加密的手段保护源码。
  
  实在不耐烦等待Zend出编译器,而且编译和加密本质上不是一回
  事儿。自己动手、开始修改
  
  一、基本原理
  考虑截获PHP读取源文件的接口。一开始,我考虑从ApachePHP
  之间的接口处 处理,参见apachesrc/modules/PHP4/mod_PHP4.c
  (这个是PHPstatic方式编译进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: 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]),
  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值