繁体   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