问题标签 [libmagic]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
0 回答
195 浏览

linux - 安全解析文件(或 libmagic)的输出

我正在编写一些代码,我依靠文件实用程序来确定任意文件的文件类型,通常是音频文件。在大多数情况下,它工作得很好,例如一个 ogg 文件可能会给出以下输出:

Ogg 数据,Vorbis 音频,单声道,44100 Hz,~80000 bps,创建者:Xiph.Org libVorbis I (1.0.1)

一个简单的正则表达式可以将其归类为 ogg vorbis。但是对于其他一些文件类型,file 会变得更聪明,例如 nsf(NES 声音格式)文件可以产生以下输出:

NES 声音文件(Konchano 的“塞尔达传说”,版权所有 1987 Nintendo),版本 1,8 首曲目,NTSC

“NES Sound File”已经够清晰了,但后面跟着一串非结构化数据,显然是从文件本身复制过来的。恶意用户可以创建一个 nsf 文件,其中该字符串被替换为“Ogg 数据,Vorbis 音频”之类的内容,从而使分类变得更加困难。

现在假设我通过丢弃括号内的任何内容来解决这个问题(忽略曲目标题本身可能包含括号的事实),随之而来的是一个 Protracker 模块:

4 通道 Protracker 模块声音数据标题:“space_debris”

同样,直接来自文件的不受信任的数据位于不同的位置,现在带有前缀“标题:”。我可以尝试将其过滤掉,但实际上这变得很麻烦。

我在手册页中找不到任何帮助。真的没有办法告诉文件不要将这些不安全的字符串混合到它的输出中吗?或者文件根本不是这项工作的正确工具?

0 投票
0 回答
211 浏览

python - 调用 Libmagic dll 函数返回 126 错误

我正在开发一个需要使用 lib 魔术库来检测文件 mime 类型的项目,我正在使用 64 位版本的 windows(参见:https ://github.com/nscaife/file-windows )我的项目本身是我将从 python 调用它的 C dll。加载库工作正常,但是当我使用 GetProcAddress() 访问某些函数时,它返回 NULL 并且 GetLastError() 函数返回 126。请参阅我的代码:

这里有什么问题?

0 投票
0 回答
585 浏览

nginx - 如何通过 Nginx 上的文件头检测文件 mime?

我想返回一个没有扩展名的文件。
但是 nginx 用application/octet-stream.

我想根据文件头而不是扩展名返回一个具有正确 mime的文件,例如使用file --mime-type -b <filename>

我应该怎么做?

0 投票
2 回答
1533 浏览

python - Pyinstaller - libmagic not found after compiling app

Problem

I'm writing an application in Python. It works just fine in my python environment, but when I compile it with PyInstaller and try to run the resulting executable, I get the following error: ImportError: failed to find libmagic. Check your installation

I have a feeling that this has something to do with "python-magic-bin", an install containing the binaries that were required for a module named "magic", because if I uninstall "python-magic-bin" from my Python environment and try to run the app, I get the same exact error. I think that for some reason these binaries aren't being carried to the compiled exe.

If it makes a difference, I installed the "python-magic-bin" from a .whl file. This install added a folder called "libmagic" files libmagic.dll and magic.mgc to the directory that "magic" was installed to.

Question

If I'm right about the problem, how do I get PyInstaller to carry over the binaries that "magic" needs?

Recreate the Problem

The following can be done to recreate the problem:

  1. Copy and paste this code into your editor. Save it as a file called "test.py"

    /li>
  2. Download "python_magic_bin-0.4.14-py2.py3-none-win32.whl" from this link and use the following commands in the interpreter to install the required libraries to Python 3.6

    /li>
  3. Open a command prompt in the same directory as that "test.py" file and use the following command to compile the program using pyinstaller:

    /li>
  4. After it's done, move to the newly created /dist/test directory (cd ./dist/test) and run the .exe using:

    /li>

After running it, you should see an error reading: ImportError: failed to find libmagic. Check your installation and Failed to execute script test

Spec File

This is the spec file I'm using to compile my project.

Versions

For this project, I'm using:

  • Python 3.6.3
  • PyInstaller 3.3.1
  • Libmagic 1.0
  • Python-Magic 0.4.15
  • Python-Magic-Bin 0.4.14
0 投票
2 回答
67 浏览

python - 我如何让 Mac 10.13 将模块安装到 3.x 安装而不是 2.7

我正在尝试实际学习python。

我通过 easy_install 安装了 PIP,然后我想玩一些 mp3 文件,所以我在项目目录中通过 pip 安装了 eyed3。问题是它将模块安装到了 mac 标准的 python 2.7 中。我发现了这一点,因为它一直告诉我,当脚本由于缺少 libmagic 等库而无法运行时,无论我做什么,它都会将我安装的任何库放入 2.7 中,因此在运行 python3 时找不到。我的问题是如何让我的系统几乎忽略 2.7 安装并使用我拥有的 3.7 安装。

我一直在想我做错了什么,因为大量的教程轻而易举地通过它,到目前为止只有一个提到你会在版本之间发生冲突。我真的很想学习 python,并希望能得到一些帮助来克服这个障碍。

0 投票
1 回答
138 浏览

ruby-on-rails - 如何将rails的模板渲染为PDF并确保它是带有FileMagic的PDF?

问题:应用程序有一个由各种打印机组成的网络。其中一些通过 PrintNode 工作。他们应该接收编码为 base64 的字符串。接收我的文档的代码看起来像

method应该包含方法——在我的例子中是 raw_base64 或 pdf_base64。所以我有下一个代码

Controller.render我用方法渲染文档

正如我所见,文档是一个字符串(包括 html),因此 FileMagic.mime 返回 text/html(我认为是这样)。这是个问题。我不在打印机附近,不能只用真正的打印机进行测试。此外,我们有 5 种打印机类型(条形码、a4 等)。所以我不确定我是否可以使用带注释的方法(text/html)。

所以主要问题Controller.render- 我可以用方法渲染真正的 PDF吗?(如果是,那么如何?)

注意:我们使用 PrinceXML 和 gem 王子。因此,当同一个控制器用于通过浏览器返回 PDF 时,我会得到有效的好 PDF。

0 投票
2 回答
358 浏览

ruby - 即使已安装 libmagic,也无法在 Mac 上安装 ruby​​-filemagic 0.7.1

即使已安装 libmagic,也无法在 Mac 上安装 ruby​​-filemagic 0.7.1。

错误是这样的:

检查 -lgurx 中的 main()... 否

检查 -lmagic 中的 magic_open()... 否

*** 错误:缺少编译此模块所需的库

* extconf.rb 失败 *

几乎所有 ruby​​-filemagic 安装的答案都是brew install libmagic. 但是,它不起作用。

0 投票
1 回答
520 浏览

regex - 如何为libmagic编写一个魔术文件来检测srt(字幕)文件

hhvm 使用 libmagic 来检测文件的 mime 类型。当我上传 srt 格式的字幕文件时,将关键字“extern”作为一行的第一个单词,hhvm (libmagic) 将其检测为 text/xc,并且 symfony 中的验证不包括该文件。

这就是为什么我需要一个魔法文件让 libmagic 正确识别 srt 文件。魔术文件的语法对我来说很复杂。有人可以帮助我或提供如何操作的提示吗?我已经阅读了手册页文件(1)和魔法(5)

srt 文件格式很简单:

linux file 命令将此文本检测为 text/xc。

0 投票
1 回答
2322 浏览

php - php 7.2 finfo 魔术文件

我有一个 Laravel 5 项目,让用户下载 .ai(插图文件)。问题是 Laravel 将 .ai 文件检测为 application/pdf。

我正在使用此功能检测 mime 类型

我也尝试过使用这种方法,但得到了相同的结果

我想,这一定是 PHP 不知道 .ai 文件是什么的问题。我深入研究了 finfo,我知道默认的 mime 定义被编译到 PHP 中,但我看到 finfo_open 有第二个参数“magic_file”,我认为这是一个可以插入不同 mime 定义文件的路径的地方.

我尝试使用 Ubuntu 的 /etc/magic.mime 文件,但 finfo 给了我

错误。我认为这是因为 magic.mime 文件格式不正确。

大多数在线主题都创建了自定义 PHP 函数或其他一些 hack 来检测 mime 类型,但我觉得这不是正确的解决方案。

在哪里可以找到最新的 mime 定义文件以及如何将它们加载到 PHP 或 finfo 中?

我的环境:

0 投票
2 回答
3505 浏览

django - 为什么 .WMV 文件的 mime 类型为“video/x-ms-asf”而不是“video/x-ms-wmv”?

我需要在我正在构建的 Django Web 应用程序中只接受 MP4 视频和 WMV 视频。为此,我会在文件到达服务器后检查文件的 mime 类型。

据我了解,MP4 文件具有“video/mp4”mime 类型,这正是我在我的网络应用程序上收到的。问题出在 WMV 文件上,根据我发现的每个站点(例如:thisthat),该文件的 mime 类型应为“video/x-ms-wmv”。当我在服务器上获取这些文件并使用python-magic“video/x-ms-asf”作为其 mime 类型检查它们时。

我使用不同的在线转换器将一些 youtube 视频转换为 WMV 视频,但结果始终相同。

所以实际上,我不知道我在这里做错了什么。

  1. 也许我有一个问题,WMV 视频也可以有“video/x-ms-asf”mime 类型,而不仅仅是“video/x-ms-wmv”
  2. 可能python-magic没有正确读取 mime 类型,我认为情况并非如此。

任何帮助都深表感谢。

作为旁注,我使用python-magic而不是 django 的file.content_type,因为第二个不可靠。只需将文件的扩展名更改为错误的扩展名,file.content_type就会给出扩展名 mime 类型而不是真实的扩展名。