简体   繁体   English

如何检查 PHP 中是否加密了 Zip 存档?

[英]How to check if encrypted Zip archive in PHP?

As I understand it, the status is archive encrypted contains in general purpose bit flag.据我了解,状态是存档加密包含在通用位标志中。 I try to check this with ZipArchive::statname(), but it seems that can not get the information by this method.我尝试使用 ZipArchive::statname() 进行检查,但似乎无法通过此方法获取信息。

What else I can do?我还能做什么? Read the archive and parse the headers?阅读档案并解析标题? I know that I can call system(), but I do not want to use this method because of its specificity (some hosting this function is disabled).我知道我可以调用 system(),但我不想使用这种方法,因为它的特殊性(某些托管此功能已禁用)。

ZIP file header: (encrypted file vs normal file) ZIP 文件头:(加密文件与普通文件)

在此处输入图片说明

09 seemts to be the encryption flag. 09好像是加密标志。

Check 7th byte is 0x09检查第 7 个字节是0x09

function zip_is_encrypted($filename) {
  $handle = fopen($filename, "rb");
  $contents = fread($handle, 7);
  fclose($handle);
  return $contents[6] == 0x09;
}

Here is the ZIP standard: http://www.pkware.com/documents/casestudies/APPNOTE.TXT .这是 ZIP 标准: http : //www.pkware.com/documents/casestudies/APPNOTE.TXT

From section 4.3.7:来自第 4.3.7 节:

4.3.7  Local file header:

  local file header signature     4 bytes  (0x04034b50)
  version needed to extract       2 bytes
  general purpose bit flag        2 bytes
  compression method              2 bytes
  ...

From section 4.4.4:从第 4.4.4 节:

4.4.4 general purpose bit flag: (2 bytes)

    Bit 0: If set, indicates that the file is encrypted.
    ...

So you need to check FIRST BIT of the seventh byte and not the whole byte.所以你需要检查第七个字节的FIRST BIT而不是整个字节。 You must do the checking for each file, as each file can be encrypted individually (section 4.3.6).您必须对每个文件进行检查,因为每个文件都可以单独加密(第 4.3.6 节)。

That's not working, because the Header can repeat n-times.这是行不通的,因为 Header 可以重复 n 次。

To workaround this, open the zip zip_open() and try to read every entry zip_entry_open()要解决此问题,请打开 zip zip_open() 并尝试读取每个条目 zip_entry_open()

If there is an unreadable entry, the zip could be encrypted!如果存在不可读的条目,则 zip 可能被加密!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM