Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中清理PDF用户输入_Python_Pdf_Sanitization - Fatal编程技术网

在Python中清理PDF用户输入

在Python中清理PDF用户输入,python,pdf,sanitization,Python,Pdf,Sanitization,我的应用程序允许用户上传PDF文件。这些文件看起来应该相对相似,并且是相同格式的变体。我知道PDF有时可能包含恶意内容——例如,在Adobe Reader或类似软件中打开时将执行的Javascript 我在网上看到了一些软件包,例如PDFiD,它可以帮助您查看可能有问题的PDF。这似乎允许您查看所有底层内容类型。我目前的想法是了解我的文档应该包含哪些内容类型,然后如果文件有不寻常的内容,就阻止它们被上传 有没有一种简单的方法可以使用Python自动清除PDF中的恶意内容,删除其中可能包含的所有可

我的应用程序允许用户上传PDF文件。这些文件看起来应该相对相似,并且是相同格式的变体。我知道PDF有时可能包含恶意内容——例如,在Adobe Reader或类似软件中打开时将执行的Javascript

我在网上看到了一些软件包,例如PDFiD,它可以帮助您查看可能有问题的PDF。这似乎允许您查看所有底层内容类型。我目前的想法是了解我的文档应该包含哪些内容类型,然后如果文件有不寻常的内容,就阻止它们被上传


有没有一种简单的方法可以使用Python自动清除PDF中的恶意内容,删除其中可能包含的所有可执行代码?我知道有一种PDF/a格式可以实现类似的功能,但是有没有像PyPDF2这样的软件包具有消毒功能?

我相信这就是答案:

from pdfid import PDFiD
new_file = PDFiD('path/to/file', disarm=True)
将获取PDF的元素

<Keywords>
    <Keyword Count="56" HexcodeCount="0" Name="obj"/>
    <Keyword Count="56" HexcodeCount="0" Name="endobj"/>
    <Keyword Count="32" HexcodeCount="0" Name="stream"/>
    <Keyword Count="32" HexcodeCount="0" Name="endstream"/>
    <Keyword Count="1" HexcodeCount="0" Name="xref"/>
    <Keyword Count="1" HexcodeCount="0" Name="trailer"/>
    <Keyword Count="1" HexcodeCount="0" Name="startxref"/>
    <Keyword Count="8" HexcodeCount="0" Name="/Page"/>
    <Keyword Count="0" HexcodeCount="0" Name="/Encrypt"/>
    <Keyword Count="0" HexcodeCount="0" Name="/ObjStm"/>
    <Keyword Count="0" HexcodeCount="0" Name="/JS"/>
    <Keyword Count="0" HexcodeCount="0" Name="/JavaScript"/>
    <Keyword Count="0" HexcodeCount="0" Name="/AA"/>
    <Keyword Count="0" HexcodeCount="0" Name="/OpenAction"/>
    <Keyword Count="0" HexcodeCount="0" Name="/AcroForm"/>
    <Keyword Count="0" HexcodeCount="0" Name="/JBIG2Decode"/>
    <Keyword Count="0" HexcodeCount="0" Name="/RichMedia"/>
    <Keyword Count="0" HexcodeCount="0" Name="/Launch"/>
    <Keyword Count="0" HexcodeCount="0" Name="/EmbeddedFile"/>
    <Keyword Count="0" HexcodeCount="0" Name="/XFA"/>
    <Keyword Count="0" HexcodeCount="0" Name="/Colors &gt; 2^24"/>
</Keywords>


如果是可疑的,就把一切都计算为0我相信这就是答案:

from pdfid import PDFiD
new_file = PDFiD('path/to/file', disarm=True)
将获取PDF的元素

<Keywords>
    <Keyword Count="56" HexcodeCount="0" Name="obj"/>
    <Keyword Count="56" HexcodeCount="0" Name="endobj"/>
    <Keyword Count="32" HexcodeCount="0" Name="stream"/>
    <Keyword Count="32" HexcodeCount="0" Name="endstream"/>
    <Keyword Count="1" HexcodeCount="0" Name="xref"/>
    <Keyword Count="1" HexcodeCount="0" Name="trailer"/>
    <Keyword Count="1" HexcodeCount="0" Name="startxref"/>
    <Keyword Count="8" HexcodeCount="0" Name="/Page"/>
    <Keyword Count="0" HexcodeCount="0" Name="/Encrypt"/>
    <Keyword Count="0" HexcodeCount="0" Name="/ObjStm"/>
    <Keyword Count="0" HexcodeCount="0" Name="/JS"/>
    <Keyword Count="0" HexcodeCount="0" Name="/JavaScript"/>
    <Keyword Count="0" HexcodeCount="0" Name="/AA"/>
    <Keyword Count="0" HexcodeCount="0" Name="/OpenAction"/>
    <Keyword Count="0" HexcodeCount="0" Name="/AcroForm"/>
    <Keyword Count="0" HexcodeCount="0" Name="/JBIG2Decode"/>
    <Keyword Count="0" HexcodeCount="0" Name="/RichMedia"/>
    <Keyword Count="0" HexcodeCount="0" Name="/Launch"/>
    <Keyword Count="0" HexcodeCount="0" Name="/EmbeddedFile"/>
    <Keyword Count="0" HexcodeCount="0" Name="/XFA"/>
    <Keyword Count="0" HexcodeCount="0" Name="/Colors &gt; 2^24"/>
</Keywords>


如果可疑,则将一切都计算为0

Didier Stevens在python中有一些很棒的PDF取证工具。您可以编写一些内容来删除最常见的恶意内容。我之所以说“大多数”,是因为PDF中除了嵌入JavaScript或ActionScript之外,可能还有很多恶意的东西。然而,它是基于ruby libs的,而且自2010年以来(从网站上看)似乎没有积极开发……Didier Stevens在python中有一些很棒的PDF取证工具。您可以编写一些内容来删除最常见的恶意内容。我之所以说“大多数”,是因为PDF中除了嵌入JavaScript或ActionScript之外,可能还有很多恶意的东西。然而,它是基于ruby libs的,而且自2010年以来(从网站上看)似乎没有进行过积极的开发。。。