题 制作libmagic /文件检测.docx文件


如其他地方所见,docx,xl​​sx和pttx是ZIP。将它们上传到我的Web应用程序时, file (通过 libmagic 和python-magic)将它们检测为ZIP。

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

我知道可以修改 /etc/magic 但格式(magic(5))对我来说太复杂了。我发现 关于Debian错误问题的错误报告 但是从2008年开始它似乎不会很快修复。

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

有任何想法吗?

最后,我找到了 docx等魔术数字列表,但我无法将这些转换为 magic(5) 格式。


17
2017-12-06 11:11






答案:


您可以使用

0       string  PK\x03\x04\x14\x00\x06\x00      Microsoft Office Open XML Format

在/ 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
---

但是将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

17
2018-04-09 10:12



我将该文件(msooxml)的内容添加到/ etc / magic(在debian上)并且它有效。 - Jay K
这对我也有用 - 虽然我犯了使用它的错误 ~/file-5.11/magic/Magdir/msooxml source,对我正在使用的某些powerpoint示例文件不起作用。中的版本 file-5.17 虽然工作得很好(也许与标签或... dunno有关)。 - dsummersl
FWIW,我在Scientific Linux 6上尝试了这个,但显然仍在使用 file 5.04,它将MIME类型标记截断为64个字符(但警告你),如@ stanley-c所述。我也试过Mac OS X Mavericks,但无法让它应用规则(虽然它警告我不需要逃避[和第二条规则中的。)。 - jwadsack


文件,5.13之前的版本,将MIME类型截断为64个字符。因此,使用msooxml的内容,来自file -bi命令的MIME类型变为“mime application / vnd.openxmlformats-officedocument.wordprocessingml.d; charset = binary”


4
2018-05-16 21:33





如果使用libreoffice的docx,你可以在/ etc / magic中添加内容(如下):

# start by checking for ZIP local file header signature
0               string          PK\003\004
!:strength +10
>1104           search/300      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

0
2018-02-24 06:41



试过这个,但它导致一些以前错误检测到的xlsx文件被正确检测到,但也导致一些以前正确检测到的xlsx文件不再被检测到 - Motin