大多數的php代碼加密(不需要額外擴展就能運行的)原理上都是使用eval進行代碼的執行,理論上,只要我們在php內核執行eval函數的時候,將其dump出來,就可以得到源代碼。需要注意的是:
-
用戶上傳的代碼是不可信的,因此需要一個沙盒
-
此法雖然方便,看似是一個萬能解密的辦法,但是 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;
}