使 libmagic/file 检测 .docx 文件

Jon*_*tke 18 linux unix debian mime

正如在别处看到的,docx、xlsx 和 pttx 是 ZIP。将它们上传到我的 Web 应用程序时,file(通过libmagicpython-magic)将它们检测为 ZIP。

我将文件的内容作为 blob 存储在数据库中,但自然我不想让用户相信这是什么类型的文件。所以我想file在下载过程中信任并自动生成一个文件名。

我知道可以修改,/etc/magic但格式 ( magic(5)) 对我来说太复杂了。我在 Debian bugs 上找到了一个关于这个问题的错误报告,但由于它是 2008 年的,它似乎不会很快得到修复。

我想我唯一的其他选择是确实信任用户(但仍将内容存储为 blob)并仅根据文件名检查文件扩展名。这样我就可以禁止某些扩展并允许其他扩展。当用户重新下载他的文件时,他可以以任何方式上传它。但是,如果文件与他人共享,则此解决方案是不安全的,因为您可以简单地重命名文件以允许上传。

有任何想法吗?

最后,我找到了 docx 等的幻数列表,但我无法将它们转换为magic(5)格式。

use*_*517 18

您可以使用

0       string  PK\x03\x04\x14\x00\x06\x00      Microsoft Office Open XML Format
Run Code Online (Sandbox Code Playgroud)

在 /etc/magic 中根据您提供的信息识别一般文件类型。

(但是,这可能并不普遍:PK\x03\x04\x00\x14\x08\x08在 LibreOffice 生成的 XLSX 文件的开头已经观察到。)

更高版本的 Ubuntu 可以正确识别 .docx、.pptx 和 .xlsx 文件。在文件实用程序的源代码中挖掘,我找到了~/file-5.09/magic/Magdir/msooxml进行识别的文件。您可以获得该文件的副本并将其添加到您的/etc/magic文件中。


包括已更新到 v 1.5 的文件副本


# $File: msooxml,v 1.5 2014/08/05 07:38:45 christos Exp $
# msooxml:  file(1) magic for Microsoft Office XML
# From: Ralf Brown <ralf.brown@gmail.com>

# .docx, .pptx, and .xlsx are XML plus other files inside a ZIP
#   archive.  The first member file is normally "[Content_Types].xml".
#   but some libreoffice generated files put this later. Perhaps skip
#   the "[Content_Types].xml" test?
# Since MSOOXML doesn't have anything like the uncompressed "mimetype"
#   file of ePub or OpenDocument, we'll have to scan for a filename
#   which can distinguish between the three types

# start by checking for ZIP local file header signature
0       string      PK\003\004
!:strength +10
# make sure the first file is correct
>0x1E       regex       \\[Content_Types\\]\\.xml|_rels/\\.rels
# skip to the second local file header
# since some documents include a 520-byte extra field following the file
# header, we need to scan for the next header
>>(18.l+49) search/2000 PK\003\004
# now skip to the *third* local file header; again, we need to scan due to a
# 520-byte extra field following the file header
>>>&26      search/1000 PK\003\004
# and check the subdirectory name to determine which type of OOXML
# file we have.  Correct the mimetype with the registered ones:
# http://technet.microsoft.com/en-us/library/cc179224.aspx
>>>>&26     string      word/       Microsoft Word 2007+
!:mime application/vnd.openxmlformats-officedocument.wordprocessingml.document
>>>>&26     string      ppt/        Microsoft PowerPoint 2007+
!:mime application/vnd.openxmlformats-officedocument.presentationml.presentation
>>>>&26     string      xl/     Microsoft Excel 2007+
!:mime application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
>>>>&26     default     x       Microsoft OOXML
---
Run Code Online (Sandbox Code Playgroud)

但是将 V1.2 留在这里供后代使用。

在此处包含副本,因为上面的链接可能会随着文件包的更新而过时。

#------------------------------------------------------------------------------
# $File: msooxml,v 1.2 2013/01/25 23:04:37 christos Exp $
# msooxml:  file(1) magic for Microsoft Office XML
# From: Ralf Brown <ralf.brown@gmail.com>

# .docx, .pptx, and .xlsx are XML plus other files inside a ZIP
#   archive.  The first member file is normally "[Content_Types].xml".
# Since MSOOXML doesn't have anything like the uncompressed "mimetype"
#   file of ePub or OpenDocument, we'll have to scan for a filename
#   which can distinguish between the three types

# start by checking for ZIP local file header signature
0               string          PK\003\004
# make sure the first file is correct
>0x1E           string          [Content_Types].xml
# skip to the second local file header
#   since some documents include a 520-byte extra field following the file
#   header,  we need to scan for the next header
>>(18.l+49)     search/2000     PK\003\004
# now skip to the *third* local file header; again, we need to scan due to a
#   520-byte extra field following the file header
>>>&26          search/1000     PK\003\004
# and check the subdirectory name to determine which type of OOXML
#   file we have
#   Correct the mimetype with the registered ones:
#     http://technet.microsoft.com/en-us/library/cc179224.aspx
>>>>&26         string          word/           Microsoft Word 2007+
!:mime application/vnd.openxmlformats-officedocument.wordprocessingml.document
>>>>&26         string          ppt/            Microsoft PowerPoint 2007+
!:mime application/vnd.openxmlformats-officedocument.presentationml.presentation
>>>>&26         string          xl/             Microsoft Excel 2007+
!:mime application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
>>>>&26         default         x               Microsoft OOXML
!:strength +10
Run Code Online (Sandbox Code Playgroud)