【问题标题】:Check if Zip file is encrypted or password protected using PHP检查 Zip 文件是否使用 PHP 加密或密码保护
【发布时间】:2019-10-22 23:23:23
【问题描述】:

我正在编写一个扫描程序,它将查找可能被黑客入侵/恶意软件文件。一项要求是使用某些 PHP 函数检查 zip(或任何压缩)文件是否受密码保护。

我不想添加任何额外的软件要求,所以应该使用 PHP 5.3+ 在多台服务器上工作。 (是的,我知道 5.3 很旧,但该过程可能需要在较旧的 PHP 安装上运行。)如果此检测在较新的 PHP 版本中可用,那么我可以拥有只能在较新的 PHP 版本上运行的代码。

我可以使用file_get_contents() 函数将文件内容读入字符串。如何检查该字符串是否表明 zip 文件受密码保护?请注意,我不想解压缩文件,只需检查它是否有密码保护。

谢谢。

【问题讨论】:

    标签: php passwords zip


    【解决方案1】:

    此代码似乎可以工作,但可能会有所改进。

    这个过程似乎涉及两个步骤:

    • 使用 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 中的文件进行任何处理。

    【讨论】:

    • 谢谢瑞克·赫勒威尔!我注意到在使用加密的 zip 文件时需要注意一些细微的差异。当使用zipcloak(加密以前压缩的zip文件)时,zip文件中的所有项目(文件和目录)都带有加密标志。当使用zip -ezip -P 时,只有文件(而不是目录)带有这个标志。不幸的是,您的代码在第二种情况下失败(iff 目录存在于存档中)。可以使用例如检查加密标志。 zipinfo(查找带有大写字母 BT 的行)或 zipdetails(通用标志 -> 位 #0 = 1)
    猜你喜欢
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    • 2017-11-01
    • 2014-12-30
    • 1970-01-01
    相关资源
    最近更新 更多