Monday, December 3, 2012

PHP Code To Binary And Back, Eval'd

Have no clue why the fuck you'd want to do this except for some rudimentary obfuscation, but I sure as hell had fun playing around with it.
[sourcecode language="php"]
<?php
function binary_chunk($str)
{
$func = str_split($str);
$binary_chunk = '';
$l = count($func);
$x = 0;
foreach ($func as $k => $chr) {
$binary_chunk .= decbin(ord($chr));
($x == $l - 1) ?: $binary_chunk .= ' ';
$x++;
}

return $binary_chunk;
}

function decode_binary_chunk($bin)
{
$decoded_binary = '';
$binary_chunk = explode(' ',$bin);
foreach ($binary_chunk as $chunk) {
$decoded_binary .= chr(bindec($chunk));
}
return $decoded_binary;
}

$binary_chunk = binary_chunk('function add($a,$b){return $a+$b;}');
echo "\nFunction chunked to binary:\n";
echo $binary_chunk;
echo "\n\n";

$decoded_binary = decode_binary_chunk($binary_chunk);
echo "Binary chunks glued together:\n";
echo $decoded_binary;
echo "\n\n";

eval($decoded_binary);
echo "Result of decoded function add(1,1):\n";
$x = add(1,1);
echo "$x\n\n";
[/sourcecode]


Sample Output


binary-obfuscation

No comments:

Post a Comment