首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP base64_decode + gzinflate 加密解密:原理分析與雙向實作

     編輯:PHP 相關知識

常見加密模式

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]);
}
?>

安全性分析與誤區

現代替代方案

  • OPcache:PHP 內建字節碼快取,部署時預編譯,原始碼可不部署
  • 加密擴充:ionCube Loader、SourceGuardian、Swoole Compiler
  • 原始碼混淆:PHP-Obfuscator 等工具(變數重命名、控制流扁平化)

檢測與清理被植入的 eval 後門

function scanEvalBackdoor($dir) {
    $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
    foreach ($files as $file) {
        if ($file->isFile() && $file->getExtension() === 'php') {
            $content = file_get_contents($file->getPathname());
            if (preg_match('/eval\s*\(.*base64_decode|gzinflate|str_rot13/', $content)) {
                echo "可疑檔案: {$file->getPathname()}\n";
            }
        }
    }
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
觀點說明
這是加密嗎?不是。編碼≠加密,無金鑰、可逆、安全性極低
能防反編譯嗎?不能,僅增加閱讀難度,工具自動解碼
eval() 風險極高,任意代碼執行,若解碼字串被竄改將導致 RCE
生產環境建議使用 OPcache、ionCube、Zend Guard 等真正的字節碼加密