php - 加载由 blenc 加密的页面时出错(C 和 PHP 代码)

标签 php c encryption blenc

当我在网络服务器中加载一个之前用 BLENC 加密的页面时,这会显示:

Severity: Warning
Message: blenc_compile: Validation of script 'path\to\file\R2k_Controller.php' failed. MD5_FILE: 3f6958c4bee8ba0d4cb3a0e67e0e2bde MD5_CALC: 02998505e69466a2f7f3af5d4555a352

Severity: Error
Message: blenc_compile: Validation of script 'path\to\file\R2k_Controller.php' failed, cannot execute.

使用:

  • PHP 5.6.14 x64 NTS
  • Blenc 1.1.4b
  • IIS 7.5

这是我加密的代码:

$oDir = NULL;
if(PHP_SAPI === 'cli'){
    $path   = $argv[1];
   $path2  = '';
    if(count($argv)>1){
        $oDir = new RecDir($path,false);
        while (false !== ($archivo = $oDir->read())) {
         if(strstr($archivo,".php") !== FALSE){
            $path2=substr_replace($archivo,$path,strpos($archivo,$path),strlen($path));
            $source_code = file_get_contents($path2);
            blenc_encrypt($source_code, $argv[2] . $path2,$FKEY);
            echo($archivo . " >>> " . $argv[2] . $path2 . PHP_EOL);
         }
        }
        $oDir->close();
      file_put_contents( $argv[2] ."blenc.key_file", $FKEY."\n"); //, FILE_APPEND
    }
   else{
      echo("Error: parametos incorrectos" . PHP_EOL);
   }
}
else{
    echo("<html><head>Error</head><body>Acceso denegado!</body></html>");
}

我该如何解决这个问题?

编辑

检查 blenc 的存储库我发现了这个

for (zend_hash_internal_pointer_reset(php_bl_keys);
         zend_hash_get_current_data(php_bl_keys, (void **)&key) == SUCCESS;
         zend_hash_move_forward(php_bl_keys)) {

        decoded = php_blenc_decode(encoded, *key, script_len - sizeof(blenc_header), &decoded_len TSRMLS_CC);

        md5 = emalloc(33);
        php_blenc_make_md5(md5, decoded, decoded_len TSRMLS_CC);

        if(!strncmp(md5, header->md5, 32)) {

            validated = TRUE;
            efree(md5);
            break;

        }

        zend_error(E_WARNING, "blenc_compile: Validation of script '%s' failed. MD5_FILE: %s MD5_CALC: %s\n",
                                file_handle->filename, header->md5, md5);

        efree(md5);
        md5 = NULL;

        efree(decoded);
        decoded_len = 0;

    }

static void php_blenc_make_md5(char *result, void *data, unsigned int data_len TSRMLS_DC)
{
    PHP_MD5_CTX   context;
    unsigned char digest[16];

    PHP_MD5Init(&context);
    PHP_MD5Update(&context, data, data_len);
    PHP_MD5Final(digest, &context);

    make_digest(result, digest);

}

b_byte *php_blenc_decode(void *input, unsigned char *key, int in_len, int *out_len TSRMLS_DC)
{   
    BLOWFISH_CTX ctx;
    unsigned long hi, low;
    int i;
    b_byte *retval;

    Blowfish_Init (&ctx, (unsigned char*)key, strlen(key));

    if(in_len % 8) {

        zend_error(E_WARNING, "Attempted to decode non-blenc encrytped file.");
        return estrdup("");

    } else {

        retval = emalloc(in_len + 1);

    }

    memset(retval, '\0', sizeof(retval));

    hi = 0x0L;
    low = 0x0L;

    for(i = 0; i < in_len; i+=8) {

        hi |= (unsigned int)((char *)input)[i] & 0xFF;
        hi = hi << 8;
        hi |= (unsigned int)((char *)input)[i+1] & 0xFF;
        hi = hi << 8;
        hi |= (unsigned int)((char *)input)[i+2] & 0xFF;
        hi = hi << 8;
        hi |= (unsigned int)((char *)input)[i+3] & 0xFF;

        low |= (unsigned int)((char *)input)[i+4] & 0xFF;
        low = low << 8;
        low |= (unsigned int)((char *)input)[i+5] & 0xFF;
        low = low << 8;
        low |= (unsigned int)((char *)input)[i+6] & 0xFF;
        low = low << 8;
        low |= (unsigned int)((char *)input)[i+7] & 0xFF;

        Blowfish_Decrypt(&ctx, &hi, &low);

        retval[i] = hi >> 24;
        retval[i+1] = hi >> 16;
        retval[i+2] = hi >> 8;
        retval[i+3] = hi;
        retval[i+4] = low >> 24;
        retval[i+5] = low >> 16;
        retval[i+6] = low >> 8;
        retval[i+7] = low;

        hi = 0x0L;
        low = 0x0L;

    }

    retval[in_len] = '\0';
    *out_len = strlen(retval);

    return retval;
}

谁能解释这里发生了什么?

最佳答案

问题是您没有将正确的内容放入 blenc.key_file。

blenc_encrypt()解释说它返回一个必须保存到 key 文件中的可再分发 key 。这不是您传递给blenc_encrypt() 以加密代码的$encryption_key。它是用于允许 blenc 解密代码以便它可以运行的 key 。

在您的代码中,您正在调用 blenc_encrypt(),并且不保存它返回的可再分发 key 。然后您将加密 key 附加到 key 文件,这是不正确的。

你需要做的是:

$oDir = NULL;
if(PHP_SAPI === 'cli'){
    $path   = $argv[1];
    $path2  = '';
    if(count($argv)>1){
        $oDir = new RecDir($path,false);
        while (false !== ($archivo = $oDir->read())) {
         if(strstr($archivo,".php") !== FALSE){
            $path2=substr_replace($archivo,$path,strpos($archivo,$path),strlen($path));
            $source_code = file_get_contents($path2);

            // Save $redistributable_key and save it to the key file
            $redistributable_key =  blenc_encrypt($source_code, $argv[2] . $path2,$FKEY);
            file_put_contents($argv[2] . "blenc.key_file", $redistributable_key . "\n", FILE_APPEND);

            echo($archivo . " >>> " . $argv[2] . $path2 . PHP_EOL);
         }
        }
        $oDir->close();
    }
   else{
      echo("Error: parametos incorrectos" . PHP_EOL);
   }
}
else{
    echo("<html><head>Error</head><body>Acceso denegado!</body></html>");
}

然后您需要确保生成的 blenc.key_file 的内容包含在由 blenc.key_file 指定的文件中ini 指令。

完成后,您的加密文件应该会正确加载到您的网络服务器中。然后,您可以将加密文件和 blenc.key_file 分发给您的客户。


您在编辑中包含的 C 代码是 blenc 解密引擎的一部分。第一部分是 blenc_compile() 的主循环,它获取一个文件,对其进行解码,然后(如果成功)将其传递给 Zend 引擎进行编译。后两个函数只是生成 MD5 摘要和驱动实际 Blowfish 解密的助手。完全理解这些是相当复杂的,并不是理解和解决问题所必需的。

关于php - 加载由 blenc 加密的页面时出错(C 和 PHP 代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34598949/

相关文章:

编译器优化,划分宏扩展

c - 为什么输出看起来像这样?

ssl - 一个域上有多个 SSL 证书

.net - 用于存储密码并随后检索的字符串加密

PHP - 定时 MySQLi 行更新?

php - get_results ("select * from table_name where id IN($array)")但它假设 $array 为 "Array"

c - 使用 malloc 函数创建的数组超出了所需的大小

php - 引用:什么是变量范围,哪些变量可从何处访问,什么是“ undefined variable ”错误?

php - Codeigniter Select_Sum 返回 "array"不是数值?

android - 从 url 下载视频文件并限制从应用程序外部下载的文件