2023-08-03 19:12:22 +00:00
# ImageMagick安全性
2023-01-16 14:53:23 +00:00
< details >
2023-08-03 19:12:22 +00:00
< summary > < a href = "https://cloud.hacktricks.xyz/pentesting-cloud/pentesting-cloud-methodology" > < strong > ☁️ HackTricks云 ☁️< / strong > < / a > -< a href = "https://twitter.com/hacktricks_live" > < strong > 🐦 推特 🐦< / strong > < / a > - < a href = "https://www.twitch.tv/hacktricks_live/schedule" > < strong > 🎙️ Twitch 🎙️< / strong > < / a > - < a href = "https://www.youtube.com/@hacktricks_LIVE" > < strong > 🎥 YouTube 🎥< / strong > < / a > < / summary >
2023-01-16 14:53:23 +00:00
2023-08-03 19:12:22 +00:00
* 你在一家**网络安全公司**工作吗? 你想在HackTricks中看到你的**公司广告**吗?或者你想获得**PEASS的最新版本或下载PDF格式的HackTricks**吗?请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 发现我们的独家[NFT收藏品**The PEASS Family**](https://opensea.io/collection/the-peass-family)
* 获取[**官方PEASS和HackTricks周边产品**](https://peass.creator-spring.com)
* **加入**[**💬**](https://emojipedia.org/speech-balloon/) [**Discord群组** ](https://discord.gg/hRep4RUj7f ) 或 [**Telegram群组** ](https://t.me/peass ) 或 **关注**我在**Twitter**上的[** 🐦**](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md)[**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* **通过向**[**hacktricks repo**](https://github.com/carlospolop/hacktricks) **和** [**hacktricks-cloud repo** ](https://github.com/carlospolop/hacktricks-cloud ) **提交PR来分享你的黑客技巧。**
2023-01-16 14:53:23 +00:00
< / details >
2023-08-03 19:12:22 +00:00
**本文摘自** [**https://blog.doyensec.com/2023/01/10/imagemagick-security-policy-evaluator.html** ](https://blog.doyensec.com/2023/01/10/imagemagick-security-policy-evaluator.html )****
2023-01-16 14:53:23 +00:00
2023-08-03 19:12:22 +00:00
在我们的审计中, 我们偶尔会遇到ImageMagick安全策略配置文件( `policy.xml`),用于限制库的默认行为和资源消耗。在实际应用中,这些文件通常包含了从互联网上收集的大量建议。这通常有两个原因:
2023-01-16 14:53:23 +00:00
2023-08-03 19:12:22 +00:00
* 在库的在线文档页面上, 只是对其选项进行了一般描述, 没有清晰地解释策略允许的每个安全指令的具体作用。尽管策略可定义的选项的架构复杂性和粒度是新手的主要障碍, 但相应的知识库可以更加友好。默认情况下, ImageMagick带有一个无限制的策略, 开发人员必须根据自己的使用情况进行调整。根据文档的说法, _"这为在沙盒环境中运行的ImageMagick安装提供了最大的效用, 例如在Docker实例中或在防火墙后面, 其中安全风险大大降低, 与公共网站相比。"_ 也提供了一个安全严格的策略,然而,正如[过去所指出的](https://www.synacktiv.com/en/publications/playing-with-imagetragick-like-its-2016.html),并不总是配置良好。
* ImageMagick支持100多种主要文件格式( 不包括子格式) 的图像格式。多年来影响该库的臭名昭著的漏洞导致了许多紧急的安全修复和解决方法, 其中包括添加策略项来排除受影响的格式和功能( 2016年的ImageTragick, @taviso的通过GhostScript的RCE, 2018年的@insertScript通过PDF密码的shell注入, 2020年的@alexisdanizan, 2021年的@alexisdanizan) 。
2023-01-16 14:53:23 +00:00
2023-08-03 19:12:22 +00:00
### 朝着更安全的策略 <a href="#towards-safer-policies" id="towards-safer-policies"></a>
2023-01-16 14:53:23 +00:00
2023-08-03 19:12:22 +00:00
基于此, 我们决定研究ImageMagick安全策略解析器接受的所有选项的影响, 并编写了一个[工具,以帮助开发人员和安全团队设计和审计这些文件](https://imagemagick-secevaluator.doyensec.com/)。由于可用选项的数量以及需要明确拒绝所有不安全的设置,这通常是一项手动任务,可能无法识别破坏策略强度的微妙绕过方式。设置看似有效但实际上没有提供任何真正安全性益处的策略也很容易。该工具的检查基于我们的研究,旨在帮助开发人员加固策略并改进应用程序的安全性,以确保策略提供有意义的安全性益处,并且不能被攻击者破坏。
2023-01-16 14:53:23 +00:00
2023-08-03 19:12:22 +00:00
该工具可以在[imagemagick-secevaluator.doyensec.com/](https://imagemagick-secevaluator.doyensec.com/)找到。
2023-01-16 14:53:23 +00:00
2023-08-03 19:12:22 +00:00
### 允许列表与拒绝列表方法 <a href="#allowlist-vs-denylist-approach" id="allowlist-vs-denylist-approach"></a>
2023-01-16 14:53:23 +00:00
2023-08-03 19:12:22 +00:00
在线上可以找到一些看似安全的策略,其中指定了类似以下的不安全编码器列表:
2023-01-16 14:53:23 +00:00
```xml
2023-08-03 19:12:22 +00:00
...
< policy domain = "coder" rights = "none" pattern = "EPHEMERAL" / >
< policy domain = "coder" rights = "none" pattern = "EPI" / >
< policy domain = "coder" rights = "none" pattern = "EPS" / >
< policy domain = "coder" rights = "none" pattern = "MSL" / >
< policy domain = "coder" rights = "none" pattern = "MVG" / >
< policy domain = "coder" rights = "none" pattern = "PDF" / >
< policy domain = "coder" rights = "none" pattern = "PLT" / >
< policy domain = "coder" rights = "none" pattern = "PS" / >
< policy domain = "coder" rights = "none" pattern = "PS2" / >
< policy domain = "coder" rights = "none" pattern = "PS3" / >
< policy domain = "coder" rights = "none" pattern = "SHOW" / >
< policy domain = "coder" rights = "none" pattern = "TEXT" / >
< policy domain = "coder" rights = "none" pattern = "WIN" / >
< policy domain = "coder" rights = "none" pattern = "XPS" / >
...
2023-01-16 14:53:23 +00:00
```
2023-08-03 19:12:22 +00:00
在ImageMagick 6.9.7-7中, 进行了一个未列出的更改。策略解析器的行为从在策略中至少有一个`none`权限规则时禁止使用编码器,改为在策略中对编码器的最后匹配规则进行尊重。这意味着现代策略可以采用允许列表的方法,首先拒绝所有编码器的权限,然后启用经过审查的编码器。一个更安全的策略应该指定:
2023-01-16 14:53:23 +00:00
```xml
2023-08-03 19:12:22 +00:00
...
< policy domain = "delegate" rights = "none" pattern = "*" / >
< policy domain = "coder" rights = "none" pattern = "*" / >
< policy domain = "coder" rights = "read | write" pattern = "{GIF,JPEG,PNG,WEBP}" / >
...
2023-01-16 14:53:23 +00:00
```
2023-08-03 19:12:22 +00:00
### 大小写敏感性 <a href="#case-sensitivity" id="case-sensitivity"></a>
2023-01-16 14:53:23 +00:00
2023-08-03 19:12:22 +00:00
考虑以下指令:
2023-01-16 14:53:23 +00:00
```
2023-08-03 19:12:22 +00:00
...
< policy domain = "coder" rights = "none" pattern = "ephemeral,epi,eps,msl,mvg,pdf,plt,ps,ps2,ps3,show,text,win,xps" / >
...
2023-01-16 14:53:23 +00:00
```
2023-08-03 19:12:22 +00:00
使用这个方法, 转换仍然是允许的, 因为策略模式是区分大小写的。编码器和模块在策略中必须始终是大写的( 例如, “EPS”而不是“eps”) 。
2023-01-16 14:53:23 +00:00
2023-08-03 19:12:22 +00:00
### 资源限制 <a href="#resource-limits" id="resource-limits"></a>
2023-01-16 14:53:23 +00:00
2023-08-03 19:12:22 +00:00
在ImageMagick中实现拒绝服务是相当容易的。为了获得一组新的有效载荷, 方便的方法是在Github库的最近打开的问题中搜索“oom”或类似的关键词。这是一个问题, 因为接受潜在恶意输入的ImageMagick实例( 通常是这种情况) 总是容易受到攻击。因此, 该工具还会报告如果策略没有明确设置合理的限制。
2023-01-16 14:53:23 +00:00
2023-08-03 19:12:22 +00:00
### 策略分割 <a href="#policy-fragmentation" id="policy-fragmentation"></a>
2023-01-16 14:53:23 +00:00
2023-08-03 19:12:22 +00:00
一旦定义了策略, 就很重要确保策略文件生效。通过分发捆绑的ImageMagick软件包或通过多个软件包管理器安装的依赖项可能会指定相互干扰的不同策略。在本地机器上快速使用`find`命令可以识别多个`policy.xml`文件的出现:
2023-01-16 14:53:23 +00:00
```shell-session
$ find / -iname policy.xml
# Example output on macOS
/usr/local/etc/ImageMagick-7/policy.xml
/usr/local/Cellar/imagemagick@6/6.9.12-60/etc/ImageMagick-6/policy.xml
/usr/local/Cellar/imagemagick@6/6.9.12-60/share/doc/ImageMagick-6/www/source/policy.xml
/usr/local/Cellar/imagemagick/7.1.0-45/etc/ImageMagick-7/policy.xml
/usr/local/Cellar/imagemagick/7.1.0-45/share/doc/ImageMagick-7/www/source/policy.xml
# Example output on Ubuntu
/usr/local/etc/ImageMagick-7/policy.xml
/usr/local/share/doc/ImageMagick-7/www/source/policy.xml
/opt/ImageMagick-7.0.11-5/config/policy.xml
/opt/ImageMagick-7.0.11-5/www/source/policy.xml
```
2023-08-03 19:12:22 +00:00
策略也可以使用[-limit](https://imagemagick.org/script/command-line-options.php#limit)命令行参数、[MagickCore API](https://imagemagick.org/api/resource.php#SetMagickResourceLimit)方法或环境变量进行配置。
2023-01-16 14:53:23 +00:00
2023-08-03 19:12:22 +00:00
### 一个初始的限制策略 <a href="#a-starter-restrictive-policy" id="a-starter-restrictive-policy"></a>
2023-01-16 14:53:23 +00:00
2023-08-03 19:12:22 +00:00
从官方文档中描述的最严格的策略开始,我们设计了一个收集了我们所有观察结果的限制策略:
2023-01-16 14:53:23 +00:00
```xml
< policymap xmlns = "" >
2023-08-03 19:12:22 +00:00
< policy domain = "resource" name = "temporary-path" value = "/mnt/magick-conversions-with-restrictive-permissions" / > <!-- the location should only be accessible to the low - privileged user running ImageMagick -->
< policy domain = "resource" name = "memory" value = "256MiB" / >
< policy domain = "resource" name = "list-length" value = "32" / >
< policy domain = "resource" name = "width" value = "8KP" / >
< policy domain = "resource" name = "height" value = "8KP" / >
< policy domain = "resource" name = "map" value = "512MiB" / >
< policy domain = "resource" name = "area" value = "16KP" / >
< policy domain = "resource" name = "disk" value = "1GiB" / >
< policy domain = "resource" name = "file" value = "768" / >
< policy domain = "resource" name = "thread" value = "2" / >
< policy domain = "resource" name = "time" value = "10" / >
< policy domain = "module" rights = "none" pattern = "*" / >
< policy domain = "delegate" rights = "none" pattern = "*" / >
< policy domain = "coder" rights = "none" pattern = "*" / >
< policy domain = "coder" rights = "write" pattern = "{PNG,JPG,JPEG}" / > <!-- your restricted set of acceptable formats, set your rights needs -->
< policy domain = "filter" rights = "none" pattern = "*" / >
< policy domain = "path" rights = "none" pattern = "@*" / >
< policy domain = "cache" name = "memory-map" value = "anonymous" / >
< policy domain = "cache" name = "synchronize" value = "true" / >
<!-- <policy domain="cache" name="shared - secret" value="my - secret - passphrase" stealth="True"/> Only needed for distributed pixel cache spanning multiple servers -->
< policy domain = "system" name = "shred" value = "2" / >
< policy domain = "system" name = "max-memory-request" value = "256MiB" / >
< policy domain = "resource" name = "throttle" value = "1" / > <!-- Periodically yield the CPU for at least the time specified in ms -->
< policy xmlns = "" domain = "system" name = "precision" value = "6" / >
2023-01-16 14:53:23 +00:00
< / policymap >
```
2023-08-03 19:12:22 +00:00
您可以使用`identify`命令验证安全策略是否生效:
2023-01-16 14:53:23 +00:00
```
identify -list policy
Path: ImageMagick/policy.xml
...
```
2023-08-03 19:12:22 +00:00
您还可以在开发定制策略时使用我们的评估工具进行测试。
2023-01-16 14:53:23 +00:00
< details >
2023-08-03 19:12:22 +00:00
< summary > < a href = "https://cloud.hacktricks.xyz/pentesting-cloud/pentesting-cloud-methodology" > < strong > ☁️ HackTricks 云 ☁️< / strong > < / a > -< a href = "https://twitter.com/hacktricks_live" > < strong > 🐦 推特 🐦< / strong > < / a > - < a href = "https://www.twitch.tv/hacktricks_live/schedule" > < strong > 🎙️ Twitch 🎙️< / strong > < / a > - < a href = "https://www.youtube.com/@hacktricks_LIVE" > < strong > 🎥 YouTube 🎥< / strong > < / a > < / summary >
2023-01-16 14:53:23 +00:00
2023-08-03 19:12:22 +00:00
* 您在一家**网络安全公司**工作吗?您想在 HackTricks 中**宣传您的公司**吗?或者您想获得**PEASS 的最新版本或下载 HackTricks 的 PDF 版本**吗?请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 发现我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)收藏品——[**The PEASS Family**](https://opensea.io/collection/the-peass-family)
* 获取[**官方 PEASS & HackTricks 商品**](https://peass.creator-spring.com)
* **加入**[**💬**](https://emojipedia.org/speech-balloon/) [**Discord 群组** ](https://discord.gg/hRep4RUj7f ) 或 [**Telegram 群组** ](https://t.me/peass ),或者**关注**我在**Twitter**上的[**🐦**](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md)[**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* **通过向**[**hacktricks 仓库**](https://github.com/carlospolop/hacktricks) **和** [**hacktricks-cloud 仓库** ](https://github.com/carlospolop/hacktricks-cloud ) **提交 PR 来分享您的黑客技巧。**
2023-01-16 14:53:23 +00:00
< / details >