Zeggy
04-20-2008, 06:56 PM
I'm trying to make a simple PHP encoder.
Here's what I got so far:
EDIT: See below
It doesn't really do much, just uses base64 and gzdeflate to 'encode' chunks of code.
I'm wondering, what else could I do to improve the encoding process? I've seen some other encoders (http://www.rightscripts.com/phpencode/index.php), and they seem to generate some extra code to confuse you.
I'm also thinking of using tokens to possibly obfuscate some code, and optimize it at the same time. I don't have much experience using tokens, maybe somebody could give me some tips/warnings?
Updated code:
<?php
$compressed = stripslashes($_POST['code']);
$newline = array("\r\n", "\n", "\r");
$replace = ' ';
$compressed = str_replace($newline, $replace, $compressed);
$code = token_get_all($compressed);
$new_code = "";
foreach($code as $token)
{
if ($token == ";")
{
$new_code .= $token;
}
else
{
$name = token_name(intval($token[0]));
if ($name != "T_COMMENT" &&
$name != "T_ML_COMMENT" &&
$name != "T_OPEN_TAG" &&
$name != "T_CLOSE_TAG")
{
$new_code .= $token[1];
}
else
{
//echo $name . " thrown away.
";
}
}
}
for ($i = 1; $i <= 10; $i++) {
$compressed = gzdeflate($new_code, 9);
$compressed = chunk_split(base64_encode($compressed));
$compressed = "eval(gzinflate(base64_decode('" . $compressed . "')));";
}
?>
<form method="post" action="encode.php">
<textarea style="width: 900px; height: 600px;" name="code"><?=$compressed?></textarea>
<input type="submit" value="Encode!" />
</form>
I added some code to remove comments, php open/close tags (since they don't belong in an eval expression) and new lines.
Here's what I got so far:
EDIT: See below
It doesn't really do much, just uses base64 and gzdeflate to 'encode' chunks of code.
I'm wondering, what else could I do to improve the encoding process? I've seen some other encoders (http://www.rightscripts.com/phpencode/index.php), and they seem to generate some extra code to confuse you.
I'm also thinking of using tokens to possibly obfuscate some code, and optimize it at the same time. I don't have much experience using tokens, maybe somebody could give me some tips/warnings?
Updated code:
<?php
$compressed = stripslashes($_POST['code']);
$newline = array("\r\n", "\n", "\r");
$replace = ' ';
$compressed = str_replace($newline, $replace, $compressed);
$code = token_get_all($compressed);
$new_code = "";
foreach($code as $token)
{
if ($token == ";")
{
$new_code .= $token;
}
else
{
$name = token_name(intval($token[0]));
if ($name != "T_COMMENT" &&
$name != "T_ML_COMMENT" &&
$name != "T_OPEN_TAG" &&
$name != "T_CLOSE_TAG")
{
$new_code .= $token[1];
}
else
{
//echo $name . " thrown away.
";
}
}
}
for ($i = 1; $i <= 10; $i++) {
$compressed = gzdeflate($new_code, 9);
$compressed = chunk_split(base64_encode($compressed));
$compressed = "eval(gzinflate(base64_decode('" . $compressed . "')));";
}
?>
<form method="post" action="encode.php">
<textarea style="width: 900px; height: 600px;" name="code"><?=$compressed?></textarea>
<input type="submit" value="Encode!" />
</form>
I added some code to remove comments, php open/close tags (since they don't belong in an eval expression) and new lines.