Magika 是一款基于 AI 的新型文件类型检测工具,依靠深度学习的最新进展提供精确的检测。在幕后,Magika 使用了一个定制的高度优化模型,该模型仅重约几 MB,即使在单个 CPU 上运行,也能在毫秒内实现精准的文件识别。Magika 在包含约 1 亿个样本、覆盖 200 多种内容类型(包括二进制和文本文件格式)的数据集上进行了训练和评估,在我们的测试集上达到了平均约 99% 的准确率。
以下是 Magika 命令行输出的示例:
Magika 被大规模用于提升 Google 用户的安全性,通过将 Gmail、Drive 和 Safe Browsing 的文件路由到适当的安全和内容策略扫描器,每周处理数千亿样本。Magika 也已集成到 VirusTotal(示例)和 abuse.ch(示例)中。
更多背景信息可阅读我们在 Google 的 OSS 博客上发布的首篇公告,查阅 Magika 网站,以及阅读我们的 研究论文,该论文已被 IEEE/ACM 国际软件工程大会 (ICSE) 2025 收录。
你也可以无需安装,直接使用我们的 网页演示 来体验 Magika,该演示在浏览器中本地运行!
-r 递归扫描目录。high-confidence、medium-confidence 和 best-guess,可以控制对错误的容忍度。Magika 提供了用 Rust 编写的 CLI,可通过多种方式安装。
通过 magika Python 包:
pipx install magika
通过 brew(macOS / Linux):
brew install magika
通过安装脚本:
curl -LsSf https://securityresearch.google/magika/install.sh | sh
或:
powershell -ExecutionPolicy Bypass -c "irm https://securityresearch.google/magika/install.ps1 | iex"
通过 magika-cli Rust 包:
cargo install --locked magika-cli
pip install magika
npm install magika
以下是一些快速示例,帮助你快速上手。
要了解 Magika 的内部工作机制,请参阅 Magika 网站上的 核心概念 部分。
% cd tests_data/basic && magika -r * | head
asm/code.asm: Assembly (code)
batch/simple.bat: DOS batch file (code)
c/code.c: C source (code)
css/code.css: CSS source (code)
csv/magika_test.csv: CSV document (code)
dockerfile/Dockerfile: Dockerfile (code)
docx/doc.docx: Microsoft Word 2007+ document (document)
docx/magika_test.docx: Microsoft Word 2007+ document (document)
eml/sample.eml: RFC 822 mail (text)
empty/empty_file: Empty file (inode)
% magika ./tests_data/basic/python/code.py --json
[
{
"path": "./tests_data/basic/python/code.py",
"result": {
"status": "ok",
"value": {
"dl": {
"description": "Python source",
"extensions": [
"py",
"pyi"
],
"group": "code",
"is_text": true,
"label": "python",
"mime_type": "text/x-python"
},
"output": {
"description": "Python source",
"extensions": [
"py",
"pyi"
],
"group": "code",
"is_text": true,
"label": "python",
"mime_type": "text/x-python"
},
"score": 0.996999979019165
}
}
}
]
% cat tests_data/basic/ini/doc.ini | magika -
-: INI configuration file (text)
% magika --help
Determines file content types using AI
Usage: magika [OPTIONS] [PATH]...
Arguments:
[PATH]...
List of paths to the files to analyze.
Use a dash (-) to read from standard input (can only be used once).
Options:
-r, --recursive
Identifies files within directories instead of identifying the directory itself
--no-dereference
Identifies symbolic links as is instead of identifying their content by following them
--colors
Prints with colors regardless of terminal support
--no-colors
Prints without colors regardless of terminal support
-s, --output-score
Prints the prediction score in addition to the content type
-i, --mime-type
Prints the MIME type instead of the content type description
-l, --label
Prints a simple label instead of the content type description
--json
Prints in JSON format
--jsonl
Prints in JSONL format
--format <CUSTOM>
Prints using a custom format (use --help for details).
The following placeholders are supported:
%p The file path
%l The unique label identifying the content type
%d The description of the content type
%g The group of the content type
%m The MIME type of the content type
%e Possible file extensions for the content type
%s The score of the content type for the file
%S The score of the content type for the file in percent
%b The model output if overruled (empty otherwise)
%% A literal %
-h, --help
Print help (see a summary with '-h')
-V, --version
Print version
更多 CLI 示例和文档,请访问 https://crates.io/crates/magika-cli。
>>> from magika import Magika
>>> m = Magika()
>>> res = m.identify_bytes(b'function log(msg) {console.log(msg);}')
>>> print(res.output.label)
javascript
>>> from magika import Magika
>>> m = Magika()
>>> res = m.identify_path('./tests_data/basic/ini/doc.ini')
>>> print(res.output.label)
ini
>>> from magika import Magika
>>> m = Magika()
>>> with open('./tests_data/basic/ini/doc.ini', 'rb') as f:
>>> res = m.identify_stream(f)
>>> print(res.output.label)
ini
更多 Python 模块示例和文档,请参阅 Python Magika 模块 部分。
请查阅 Magika 网站 获取关于以下内容的详细文档:
请直接通过 [email protected] 联系我们。
Apache 2.0;详细信息请参见 LICENSE。
本项目并非 Google 的官方项目。Google 不提供任何支持,并且 Google 特此声明不对其质量、适销性或特定用途的适用性承担任何保证。