前言:
本文针对 PHP 加密实验样本来自如下网站:
http://www.phpjm.net/encode.html
加密思路
1、将原始文件 gzcompress、base64_encode 处理得到长度为 X 的加密串
2、将加密串按照 M+4+1+N 的长度进行分割,X=M+4+1+N,M 和 N 有随机性,每次加密出来结果不一样
3、将长度为 4 的串加密处理,按照 gzcompress、base64_encode、base64_encode、strtr(包含一个替换字典 rpd)的步骤,得到加密后的串 str4 和替换字典 rpd
4、将 str4、rpd、strM、str1、strN 明文显示在加密后的文件中,但自解析方法通过层层混淆加密隐藏起来。
思路来源于:https://www.52pojie.cn/thread-794057-1-1.html
解密方法
<?php
// php phpjm.php encode.php
function find_data($code)
{
$code = explode('return', $code);
$code = $code[3];
preg_match_all('/\\"(.*?)\\"/', $code, $result);
$result = $result[0];
return $result;
}
function find_key($code)
{
$str4 = str_replace('"', '', $code[13]);
$rpd = str_replace('"', '', $code[14]);
$str1 = str_replace('"', '', $code[0]);
$strM = substr($code[15], strpos($code[15], '\''));
$strN = substr($code[16], 0, strpos($code[16], '\''));
return array($str4, $rpd, $str1, $strM, $strN);
}
function decrypt($data, $key)
{
$str4 = gzuncompress(base64_decode(base64_decode(strtr($key[0], $key[1], strrev($key[1])))));
$str = $key[3] . $str4 . $key[2] . $key[4];
$output = gzuncompress(base64_decode($str));
$output = preg_replace('/^;\?>/', '', $output);
$output = preg_replace('/<\?php unset\((.*?)\?>$/', '', $output);
return $output;
}
$input_file = $argv[1];
$output_file = $argv[1] . '.de.php';
$code = file_get_contents($input_file);
$data = find_data($code);
$key = find_key($data);
$decrypted = decrypt($data, $key);
file_put_contents($output_file, $decrypted);
echo '解密后文件已写入到 ', $output_file, PHP_EOL;
使用方法:
php phpjm.php encode.php