大多数的php代码加密(不需要额外扩展就能运行的)原理上都是使用eval进行代码的执行,理论上,只要我们在php内核执行eval函数的时候,将其dump出来,就可以得到源代码。需要注意的是:

  1. 用户上传的代码是不可信的,因此需要一个沙盒

  2. 此法虽然方便,看似是一个万能解密的办法,但是 dump 数据的时候会有很多中间值,还是需要人工的做一个特征库,去识别过滤出需要的代码段

实现

在 php 扩展中, module init 的时候替换掉 zend_compile_string,主要代码如下

static zend_op_array *edump_compile_string(zval *source_string, char *filename TSRMLS_DC){    int c, len;    char *copy;     if (Z_TYPE_P(source_string) != IS_STRING) {        return orig_compile_string(source_string, filename TSRMLS_CC);    }     len  = Z_STRLEN_P(source_string);    copy = estrndup(Z_STRVAL_P(source_string), len);    if (len > strlen(copy)) {        for (c=0; c<len; c++) if (copy[c] == 0) copy[c] == '?';    }     php_printf("----- [tool.lu start] -----\n");    php_printf("%s\n", copy);    php_printf("----- [tool.lu end] -----\n");     yes = 1;    return orig_compile_string(source_string, filename TSRMLS_CC);}PHP_MINIT_FUNCTION(edump){    if (edump_hooked == 0) {        edump_hooked = 1;        orig_compile_string = zend_compile_string;        zend_compile_string = edump_compile_string;    }    return SUCCESS;}

使用docker作为沙盒