Skip to content

Instantly share code, notes, and snippets.

@zsxsoft
Created December 16, 2019 12:28
Show Gist options
  • Save zsxsoft/41356c8d5c2aaca1087e4afb085f575b to your computer and use it in GitHub Desktop.
Save zsxsoft/41356c8d5c2aaca1087e4afb085f575b to your computer and use it in GitHub Desktop.
<?php
require 'vendor/autoload.php';
use PhpParser\Error;
use PhpParser\NodeDumper;
use PhpParser\ParserFactory;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Scalar\String_;
$path = 'xxxxx/static/pie/.pie.tif';
$file = file_get_contents($path);
$variable = 'aaaa';
$map = [];
$tokens = token_get_all($file);
$kstr2 = false;
foreach ($tokens as $token) {
if ($token[0] === T_VARIABLE) {
if (!isset($map[$token[1]])) {
if (!preg_match('/^\$[a-zA-Z0-9_]+$/', $token[1])) {
$file = str_replace($token[1], '$' . $variable++, $file);
$map[$token[1]] = $variable;
}
}
} else if ($token[0] === T_CONSTANT_ENCAPSED_STRING) {
$text = var_export(eval('return ' . $token[1] . ';'), true);
$file = str_replace($token[1], $text, $file);
if ($kstr2) {
$file = str_replace($token[1], var_export(_kstr2(eval('return ' . $token[1] . ';')), true), $file);
$kstr2 = false;
}
} else if ($token[0] == T_STRING) {
if ($token[1] == '_kstr2') {
$kstr2 = true;
}
}
}
$file = preg_replace_callback("/base64_decode\('(.*?)'\)/", function ($matches) {
return '"' . base64_decode($matches[1]) . '"';
}, $file);
$file = preg_replace_callback("/_kstr2\('(.*?)'\)/", function ($matches) {
return '"' . $matches[1] . '"';
}, $file);
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$stmts = $parser->parse($file);
$traverser = new NodeTraverser();
$traverser->addVisitor(new class extends NodeVisitorAbstract {
public function leaveNode(Node $node) {
if ($node instanceof Concat) {
if ($node->left instanceof String_ && $node->right instanceof String_) {
$left = $node->left->value;
$right = $node->right->value;
return new String_($left . $right);
}
}
}
});
$stmts = $traverser->traverse($stmts);
$prettyPrinter = new PhpParser\PrettyPrinter\Standard;
$file = $prettyPrinter->prettyPrintFile($stmts);
file_put_contents('1.php', $file);
function _kstr2($aaaa)
{
$aaab = strlen($aaaa); $aaac = ''; $aaad = ord($aaaa[0]) - 30; for ($aaae = 1; $aaae < $aaab; $aaae += 2) { if ($aaae + 1 < $aaab) { $aaac .= chr(ord($aaaa[$aaae + 1]) + $aaad); $aaac .= chr(ord($aaaa[$aaae]) + $aaad);
} else { $aaac .= chr(ord($aaaa[$aaae]) + $aaad);
}
} return $aaac;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment