Search code examples
phpcencryptionblenc

Error when i load a page encrypted by blenc (C and PHP code)


When i load in web server a page previously encrypted with BLENC this shows:

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.

Using:

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

this is the code where I encrypt:

$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>");
}

how can I solve this?

EDIT

checking The repository for blenc i found this

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;
}

anyone can explain whats happening here ?


Solution

  • The problem is you’re not putting the proper contents into your blenc.key_file.

    blenc_encrypt() explains that it returns a redistributable key that must be saved into the key file. This is not the $encryption_key that you pass to blenc_encrypt() to encrypt the code. It is the key that is used to allow blenc to decrypt the code so that it can be run.

    In your code, you are calling blenc_encrypt(), and not saving the redistributable key it returns. You are then appending the encryption key to the key file, which is incorrect.

    What you need to do instead is this:

    $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>");
    }
    

    You'll then need to ensure that the contents of the blenc.key_file that's generated are included in the file specified by blenc.key_file ini directive.

    Once you've done that, your encrypted files should load correctly in your web server. You can then distribute your encrypted files and the blenc.key_file to your customers.


    The C code you included in your edit is part of blenc's decryption engine. The first part is the main loop for `blenc_compile()`, which takes a file, decodes it, and (if successful) passes it on to the Zend engine for compilation. The latter two functions are just helpers for generating MD5 digests and driving the actual Blowfish decryption. A full understanding of those is quite complicated, and not necessary to understand and fix the problem.