0

i have a piece of obfuscated code that i want to turn back to normal.

I came up with an idea of simple preg_replace_callback() and a preg_match() nested within it for the replacement.

For some reason though my code does not want to match, specially the preg_match in the callback

Here is a snippet of my code.

In my debbuging the arrays of preg_match_all() (I've tried regular preg_match() as well) return empty although it works outside of the callback.

<?php
$code = '
public function encrypt($UDw2y33pQ_22) { 
        goto eKGyEfM327pq; 
    YlzBCxTNCJzo: 
        $p7nCDJiybOtp = mcrypt_create_iv($I9ryzFUJtil4, 
    MCRYPT_RAND); 
        goto zqGNmmifhK7G; 
    eKGyEfM327pq: 
        $IE19PrOpdkbJ = "*97@$S8&8&*as&*SA7sa*SA7alnIU&7#$%gf^IsPj8"; 
        goto uR63Cxb5ZtRs; 
    uR63Cxb5ZtRs: 
        $I9ryzFUJtil4 = mcrypt_get_iv_size(MCRYPT_BLOWFISH, 
    MCRYPT_MODE_ECB); 
        goto YlzBCxTNCJzo; 
    TdHsUlVktK5b: 
        return base64_encode($pcLABic8Iiv1); 
        goto XJlxnac4pgAr; 
    zqGNmmifhK7G: 
        $pcLABic8Iiv1 = mcrypt_encrypt(MCRYPT_BLOWFISH, $IE19PrOpdkbJ, utf8_encode($UDw2y33pQ_22), MCRYPT_MODE_ECB, $p7nCDJiybOtp); 
        goto TdHsUlVktK5b; 
    XJlxnac4pgAr: 
}';
$deobfuscated = preg_replace_callback("/goto ([a-zA-Z0-9_]+);/", function($match){
    preg_match_all("/({$match[1]}:)((\s|\S)*?)([a-zA-Z0-9]{12}:)/ms", $code, $a);
    return $a[0][2];
},$code);

echo $deobfuscated;

Actual result:

public function encrypt($UDw2y33pQ_22) { 

YlzBCxTNCJzo: 
    $p7nCDJiybOtp = mcrypt_create_iv($I9ryzFUJtil4, MCRYPT_RAND); 

eKGyEfM327pq: 
    $IE19PrOpdkbJ = "*97@$S8&8&*as&*SA7sa*SA7alnIU&7#$%gf^IsPj8"; 

uR63Cxb5ZtRs: 
    $I9ryzFUJtil4 = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB); 

TdHsUlVktK5b: 
    return base64_encode($pcLABic8Iiv1); 

zqGNmmifhK7G: 
    $pcLABic8Iiv1 = mcrypt_encrypt(MCRYPT_BLOWFISH, $IE19PrOpdkbJ, utf8_encode($UDw2y33pQ_22), MCRYPT_MODE_ECB, $p7nCDJiybOtp); 

XJlxnac4pgAr: 
}

Expected result:

public function encrypt($UDw2y33pQ_22) { 
    $IE19PrOpdkbJ = "*97@$S8&8&*as&*SA7sa*SA7alnIU&7#$%gf^IsPj8"; 
    $I9ryzFUJtil4 = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB); 
    $p7nCDJiybOtp = mcrypt_create_iv($I9ryzFUJtil4, MCRYPT_RAND); 
    $pcLABic8Iiv1 = mcrypt_encrypt(MCRYPT_BLOWFISH, $IE19PrOpdkbJ, utf8_encode($UDw2y33pQ_22), MCRYPT_MODE_ECB, $p7nCDJiybOtp); 
    return base64_encode($pcLABic8Iiv1);
}
3
  • What is the actual and expected response? Commented Aug 14, 2017 at 13:07
  • Sorry, thank you for pointint this out. I edied the post to reflect those Commented Aug 14, 2017 at 13:15
  • You're trying to deobfuscate code? Is this your code? Seems weird that you're trying to deobfuscate an encrypt function. Commented Aug 14, 2017 at 13:20

1 Answer 1

2

$code is undefined within the closure. You need to pass it via use statement. Example:

$deobfuscated = preg_replace_callback("/goto ([a-zA-Z0-9_]+);/", function($match) use ($code) {
    preg_match_all("/({$match[1]}:)((\s|\S)*?)([a-zA-Z0-9]{12}:)/ms", $code, $a);
    return $a[0][2];
},$code);

You should also check for existing keys before trying to return $a[0][2].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.