eval(gzinflate(base64_decode('加密字串')));
流程:原始碼 → gzdeflate() 壓縮 → base64_encode() 編碼 → 埋入 eval。
<?php
function encodeFile($filename) {
if (!is_file($filename) || !is_writable($filename)) return false;
$content = file_get_contents($filename);
// 避免重複加密
if (strpos($content, 'gzinflate(base64_decode') !== false) return false;
// 移除註解與空白(可選)
$content = php_strip_whitespace($filename);
// 去除 PHP 標籤
$content = trim($content);
if (strpos($content, '') $content = substr($content, 0, -2);
// 壓縮編碼
$encoded = base64_encode(gzdeflate($content, 9));
$wrapped = "";
return file_put_contents($filename, $wrapped);
}
// 使用
encodeFile('target.php');
echo "加密完成";
?>
<?php
function decodeString($encoded, $outputFile = 'decoded.php') {
$decoded = gzinflate(base64_decode($encoded));
if ($decoded === false) {
die('解壓失敗:資料損壞或非 gzdeflate 格式');
}
file_put_contents($outputFile, $decoded);
echo "解密成功,已儲存至 $outputFile";
}
// 使用:從加密檔提取 base64 字串
$encrypted = file_get_contents('target.php');
preg_match("/base64_decode\('([^']+)'\)/", $encrypted, $matches);
if ($matches) {
decodeString($matches[1]);
}
?>
| 觀點 | 說明 |
|---|---|
| 這是加密嗎? | 不是。編碼≠加密,無金鑰、可逆、安全性極低 |
| 能防反編譯嗎? | 不能,僅增加閱讀難度,工具自動解碼 |
| eval() 風險 | 極高,任意代碼執行,若解碼字串被竄改將導致 RCE |
| 生產環境建議 | 使用 OPcache、ionCube、Zend Guard 等真正的字節碼加密 |