簡體   English   中英

檢查 Zip 文件是否使用 PHP 加密或密碼保護

[英]Check if Zip file is encrypted or password protected using PHP

我正在編寫一個掃描儀,它將查找可能被黑客入侵/惡意軟件文件。 一項要求是使用 PHP function 檢查 zip(或任何壓縮)文件是否受密碼保護。

我不想添加任何額外的軟件要求,所以應該在多個服務器上工作,使用 PHP 5.3+。 (是的,我知道 5.3 是舊的,但該過程可能需要在較舊的 PHP 安裝上運行。)如果此檢測在較新的 PHP 版本中可用,那么我可以擁有只能在較新的 Z2FEC392304A5F9BACZD.2834 版本上運行的代碼。

我可以使用file_get_contents() function 將文件的內容讀入字符串。 如何檢查該字符串是否表明 zip 文件受密碼保護? 請注意,我不想解壓縮文件,只需檢查它是否有密碼保護。

謝謝。

此代碼似乎可以工作,但可能會得到改進。

該過程似乎涉及兩個步驟:

  • 使用 zip_open 打開文件,返回一個資源。 沒有資源,zip 打不開,可能是密碼了

  • 使用 zip_read 讀取 zip 中的文件。 如果失敗,那么可能會被密碼

在這兩種情況下,返回 true,表示 zip 文件上的可能密碼。

// try to open a zip file; if it fails, probably password-protected
function check_zip_password($zip_file = '') {
    /*
    open/read a zip file
    return true if passworded
     */
    if (!$zip_file) { // file not specified
        return false;
    }
    $zip = zip_open($zip_file);     // open the file
    if (is_resource($zip)) {        // file opened OK
        $zipfile = zip_read($zip);  // try read of zip file contents
        if (!$zipfile) { // couldn't read inside, so passworded
            return true;
            } 
            else 
            { // file opened and read, so not passworded
            return false;
        }
    } else { // couldn't open the file, might be passworded
        return true;
    }
    return false; // file exists, but not password protected
}

請注意,代碼僅確定 zip 內的文件無法訪問,因此它們可能受密碼保護。 該代碼不會嘗試對 zip 中的文件進行任何處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM