安全策略 • 安全策略範例 • 像素快取同步策略 • 零配置安全策略 • 其他安全注意事項
強烈建議您在使用 ImageMagick 之前,先建立適合您當地環境的安全策略。
預設策略為開放,這適用於在安全環境中執行的 ImageMagick 安裝,例如在 Docker 容器中或防火牆後方。
ImageMagick 是一個允許您操作影像的工具。 雖然它提供了廣泛的功能,但在安全性和便利性之間通常需要權衡。 為了確保最佳安全性,您可以限制 ImageMagick 僅讀取或寫入網路安全的影像格式,例如 GIF、JPEG 和 PNG。 或者,您可以自訂安全策略以滿足您當地環境或組織策略的需求。 此策略可以包含諸如記憶體使用限制、允許的讀寫路徑、序列中影像數量的限制、最大工作流程執行時間、影像像素允許的磁碟空間、遠端連線的密碼,以及允許或拒絕哪些編碼器等細節。 通過自訂安全策略,您可以幫助保護您的環境,並確保 ImageMagick 是您本地系統的負責任成員,例如防止大型影像過載。
設定 ImageMagick 資源使用限制以防止潛在的危險情況非常重要。 例如,如果您不小心從網際網路上下載了製作成產生非常大的影像(例如 20000 x 20000 像素)的影像,ImageMagick可能會嘗試分配必要的資源(例如記憶體和磁碟空間),並且您的系統可能會拒絕請求或導致程式退出。 或者,您的電腦可能會暫時變慢或沒有回應,或者 ImageMagick 可能會被迫中止。 為避免此類情況,您可以在 policy.xml 設定檔中設定限制。
請記住,一個環境中認為合理的東西在另一個環境中可能不適合。 例如,您可能在安全環境中對 ImageMagick 進行了沙箱處理,而其他人可能將其用於在公開可訪問的網站上處理影像。 或者,ImageMagick 可能在具有大量記憶體的主機上執行,而另一個執行個體在資源有限的裝置上執行。 對於具有大量記憶體的主機,允許大型影像處理可能是有意義的,但在資源有限的裝置上則不然。 如果您在公共網站上使用 ImageMagick,您可能希望通過停用某些編碼器(例如 MVG 或 HTTPS)來提高安全性。
為了幫助您入門,從 7.1.1-16 版本開始,ImageMagick 提供了您在安裝 ImageMagick 時可以選擇的安全策略。 選擇範圍
- 開放
- ImageMagick 安裝的預設策略是開放安全策略。 此策略專為在防火牆保護或 Docker 容器內等安全設定中使用而設計。 在此框架內,ImageMagick 可以廣泛訪問資源和功能。 此策略為影像處理提供了方便且可調整的選項。 但是,請務必注意,在監管較少的條件下,它可能會出現安全漏洞。 因此,組織應根據其特定用例和安全先決條件徹底評估開放策略的適當性。
- 受限
- 有限安全策略的主要目標是在便利性和安全性之間找到平衡點。此策略涉及停用潛在危險的功能,例如特定的編碼器,如 SVG 或 HTTP。此外,它還對記憶體、儲存空間和處理時間等資源的使用設定了一些限制,所有這些限制都是可調整的。在需要降低處理潛在惡意或高要求圖像的潛在威脅,同時保留對流行圖像格式的基本功能的情況下,此策略非常有利。
- 安全
- 此嚴格的安全策略優先考慮實施嚴格的控制和受限的資源使用,以便在使用 ImageMagick 時建立一個極其安全的環境。它停用了可能具有危險性的功能,包括特定的編碼器,例如 SVG 或 HTTP。該策略促進了安全措施的定制化,以符合當地環境的需求和組織的準則。此協議包含明確的細節,例如記憶體消耗限制、讀寫的允許途徑、圖像序列的限制、工作流程的最大允許持續時間、用於圖像數據的磁碟空間分配,甚至還包括遠端連線的未公開密碼。通過採用此強大的策略,實體可以提升其整體安全狀況並減輕潛在的漏洞。
- 網路安全
- 此專為網路安全使用而設計的安全協議著重於在公開可訪問的環境(例如網站)中應用 ImageMagick 的情況。它停用了讀取或寫入網路安全格式(例如 GIF、JPEG 和 PNG)以外的任何圖像格式的功能。此外,此策略還禁止執行圖像過濾器和間接讀取,從而防止潛在的安全漏洞。通過實施這些限制,網路安全策略增強了對公眾可訪問系統的保護,降低了利用 ImageMagick 功能進行潛在攻擊的風險。
在 Linux 下,請使用 --with-security-policy={open, limited, secure, websafe}
組態腳本選項來選擇策略。在 Windows 下,當您執行組態應用程式時,會顯示此選項。
我們建議您檢查 policy.xml
組態檔中的每個規則。根據您組織的需求調整參數。您可以根據您的安全需求修改允許的圖像格式、設定特定的路徑和限制某些操作。請記住,自訂安全策略是在功能性和安全性之間取得微妙的平衡。過於嚴格的策略可能會阻礙合法的圖像處理任務,而過於寬鬆的策略則可能會引入漏洞。
範例安全策略
以下是一個範例安全策略
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policymap [
<!ELEMENT policymap (policy)*>
<!ATTLIST policymap xmlns CDATA #FIXED "">
<!ELEMENT policy EMPTY>
<!ATTLIST policy xmlns CDATA #FIXED "">
<!ATTLIST policy domain NMTOKEN #REQUIRED>
<!ATTLIST policy name NMTOKEN #IMPLIED>
<!ATTLIST policy pattern CDATA #IMPLIED>
<!ATTLIST policy rights NMTOKEN #IMPLIED>
<!ATTLIST policy stealth NMTOKEN #IMPLIED>
<!ATTLIST policy value CDATA #IMPLIED>
]>
<!--
Creating a security policy that fits your specific local environment
before making use of ImageMagick is highly advised. You can find guidance on
setting up this policy at https://imagemagick.dev.org.tw/script/security-policy.php,
and it's important to verify your policy using the validation tool located
at https://imagemagick-secevaluator.doyensec.com/.
Web-safe ImageMagick security policy:
This security protocol designed for web-safe usage focuses on situations
where ImageMagick is applied in publicly accessible contexts, like websites.
It deactivates the capability to read from or write to any image formats
other than web-safe formats like GIF, JPEG, and PNG. Additionally, this
policy prohibits the execution of image filters and indirect reads, thereby
thwarting potential security breaches. By implementing these limitations,
the web-safe policy fortifies the safeguarding of systems accessible to
the public, reducing the risk of exploiting ImageMagick's capabilities
for potential attacks.
-->
<policymap>
<!-- Set maximum parallel threads. -->
<policy domain="resource" name="thread" value="2"/>
<!-- Set maximum time in seconds or neumonics, e.g. "2 minutes". When this
limit is exceeded, an exception is thrown and processing stops. -->
<policy domain="resource" name="time" value="60"/>
<!-- Set maximum number of open pixel cache files. When this limit is
exceeded, any subsequent pixels cached to disk are closed and reopened
on demand. -->
<policy domain="resource" name="file" value="768"/>
<!-- Set maximum amount of memory in bytes to allocate for the pixel cache
from the heap. When this limit is exceeded, the image pixels are cached
to memory-mapped disk. -->
<policy domain="resource" name="memory" value="256MiB"/>
<!-- Set maximum amount of memory map in bytes to allocate for the pixel
cache. When this limit is exceeded, the image pixels are cached to
disk. -->
<policy domain="resource" name="map" value="512MiB"/>
<!-- Set the maximum width * height of an image that can reside in the pixel
cache memory. Images that exceed the area limit are cached to disk. -->
<policy domain="resource" name="area" value="16KP"/>
<!-- Set maximum amount of disk space in bytes permitted for use by the pixel
cache. When this limit is exceeded, the pixel cache is not be created
and an exception is thrown. -->
<policy domain="resource" name="disk" value="1GiB"/>
<!-- Set the maximum length of an image sequence. When this limit is
exceeded, an exception is thrown. -->
<policy domain="resource" name="list-length" value="16"/>
<!-- Set the maximum width of an image. When this limit is exceeded, an
exception is thrown. -->
<policy domain="resource" name="width" value="4KP"/>
<!-- Set the maximum height of an image. When this limit is exceeded, an
exception is thrown. -->
<policy domain="resource" name="height" value="4KP"/>
<!-- Periodically yield the CPU for at least the time specified in
milliseconds. -->
<policy domain="resource" name="throttle" value="2"/>
<!-- Do not create temporary files in the default shared directories, instead
specify a private area to store only ImageMagick temporary files. -->
<!-- -->
<!-- Force memory initialization by memory mapping select memory
allocations. -->
<policy domain="cache" name="memory-map" value="anonymous"/>
<!-- Ensure all image data is fully flushed and synchronized to disk. -->
<policy domain="cache" name="synchronize" value="true"/>
<!-- Replace passphrase for secure distributed processing -->
<!-- -->
<!-- Do not permit any delegates to execute. -->
<policy domain="delegate" rights="none" pattern="*"/>
<!-- Do not permit any image filters to load. -->
<policy domain="filter" rights="none" pattern="*"/>
<!-- Don't read/write from/to stdin/stdout. -->
<policy domain="path" rights="none" pattern="-"/>
<!-- don't read sensitive paths. -->
<policy domain="path" rights="none" pattern="/etc/*"/>
<!-- Indirect reads are not permitted. -->
<policy domain="path" rights="none" pattern="@*"/>
<!-- Deny all image modules and specifically exempt reading or writing
web-safe image formats. -->
<policy domain="module" rights="none" pattern="*" />
<policy domain="module" rights="read | write" pattern="{GIF,JPEG,PNG,WEBP}" />
<!-- This policy sets the number of times to replace content of certain
memory buffers and temporary files before they are freed or deleted. -->
<policy domain="system" name="shred" value="1"/>
<!-- Enable the initialization of buffers with zeros, resulting in a minor
performance penalty but with improved security. -->
<policy domain="system" name="memory-map" value="anonymous"/>
<!-- Set the maximum amount of memory in bytes that is permitted for
allocation requests. -->
<policy domain="system" name="max-memory-request" value="256MiB"/>
</policymap>
為了防止一個會話在同時處理多個會話時消耗所有可用的記憶體,此策略會將大型圖像快取到磁碟。如果圖像超過像素快取磁碟限制,程式將會退出。此外,還設定了一個時間限制,以防止任何處理任務執行太長時間。如果圖像的寬度或高度大於 8192 像素,或者如果圖像序列超過 32 幀,則處理將停止並拋出異常。
從 ImageMagick 7.0.1-8 開始,您可以防止使用任何委託或所有委託(通過將模式設定為「*」)。在這些版本之前,您可以使用 coder 的網域並將權限設定為無,並將 glob 模式設定為 HTTPS 以防止使用委託。此外,還會阻止使用者執行任何圖像過濾器和執行間接讀取。例如,如果您想從檔案(例如 caption:@myCaption.txt)讀取文字,則需要停用此路徑策略。
在 ImageMagick 7.1.1-16 之前的策略 glob 模式 是*區分大小寫*的。為了獲得預期的行為,編碼器和模組必須為大寫(例如「EPS」而不是「eps」)或使用不區分大小寫的模式,例如 [Pp][Nn][Gg]。
例如,當您限制 HTTPS 編碼器時,您可以預期以下結果
$ magick https://imagemagick.dev.org.tw/image/wizard.png wizard.jpg convert: attempt to perform an operation not allowed by the security policy `HTTPS' convert: no images defined `wizard.jpg'
從 ImageMagick 7.0.4-7 版本開始,您可以方便地拒絕存取所有代理和編解碼器,只允許一小部分經過驗證的網路安全影像類型。例如:
<policy domain="delegate" rights="none" pattern="*" /> <policy domain="module" rights="none" pattern="*" /> <policy domain="module" rights="read | write" pattern="{GIF,JPEG,PNG,WEBP}" />
模組策略可以啟用或停用整個模組的讀取或寫入功能。若要僅讀取或寫入影像格式,請改用編解碼器策略。例如,我們停用僅讀取一些與 Postscript 相關的格式,但您仍然可以寫入這些格式
<policy domain="coder" rights="write" pattern="{PDF,PS,PS2,PS3,XPS}" />
從 ImageMagick 7.0.7-0 版本開始,您可以使用匿名記憶體映射而不是從堆積配置像素快取和一些內部緩衝區。因此,像素會初始化為零,導致效能略有下降。您也可以在釋放或刪除某些記憶體緩衝區(需要 7.1.0-38 版本)和暫存檔案之前,將其內容打亂。 shred 值是指使用隨機資料替換內容的次數。例如:
<policy domain="system" name="memory-map" value="anonymous"/> <policy domain="cache" name="memory-map" value="anonymous"/> <policy domain="system" name="shred" value="1"/>
出於效能考量,第一次傳遞會透過重複隨機序列來快速覆蓋緩衝區或檔案的內容,速度很快。後續的傳遞速度會慢一個數量級,但會為緩衝區或檔案的長度產生加密強度高的隨機位元組。
某些影像處理演算法(例如小波轉換)可能需要消耗大量記憶體才能完成。ImageMagick 為這些大型資源請求維護一個獨立的記憶體池,並且從 7.0.6-1 版本開始,允許您設定最大請求限制。如果超過限制,則配置會改為在磁碟上映射記憶體。在此,我們透過策略限制最大記憶體請求
<policy domain="system" name="max-memory-request" value="256MiB"/>
從 ImageMagick 7.0.4-23 版本開始,您可以限制序列中最大影像數量。例如,若要將影像序列限制為最多 64 幀,請使用
<policy domain="resource" name="list-length" value="64"/>
請注意,策略中的數值為浮點數,並帶有可選的 SI 前綴(例如 10MiB)。
從 ImageMagick 7.0.6-0 版本開始,您可以使用 SetMagickSecurityPolicy() (MagickCore) 或 MagickSetSecurityPolicy() (MagickWand) 以程式設計方式設定 ImageMagick 安全性策略。
從 ImageMagick 7.0.8-11 版本開始,您可以設定模組安全性策略。例如,若要防止解譯 Postscript 或 PDF,請使用
<policy domain="module" rights="none" pattern="{ps,pdf,xps}/>
從 ImageMagick 7.0-10-52 版本開始,您可以設定字型策略。指定 ImageMagick 在使用者未指定字型偏好設定時預設使用的 Unicode 字型路徑
<policy domain="system" name="font" value="/usr/share/fonts/arial-unicode.ttf"/>
請注意,在檔案路徑萬用字元模式中,請使用反斜線字元 (\) 來跳脫否則會被解譯為特殊字元的字元。例如
<policy domain="path" rights="none" pattern="c:\\\\*"/>
您可以使用以下命令驗證您的策略變更是否生效
$ magick identify -list policy Path: ImageMagick-7/policy.xml Policy: Cache name: memory-map value: anonymous Policy: Cache name: synchronize value: true Policy: Resource name: list-length value: 32 Policy: Resource name: time value: 120 Policy: Resource name: thread value: 2 Policy: Resource name: file value: 768 Policy: Resource name: disk value: 1GiB Policy: Resource name: map value: 512MiB Policy: Resource name: memory value: 256MiB Policy: Resource name: area value: 16KP Policy: Resource name: height value: 8KP Policy: Resource name: width value: 8KP Policy: Resource name: temporary-path value: /opt/tmp Policy: Coder rights: Write pattern: {HTTP,HTTPS,MVG,PS,PDF} Policy: Filter rights: None pattern: * Policy: Path rights: None pattern: @* Policy: System name: font value: ImageMagick-7/arial-unicode.ttf Path: [built-in] Policy: Undefined rights: None
請注意,由於隱藏屬性,共用密鑰策略未列出。
Doyensec 提供了一個策略評估工具,可以協助您設計和稽核您的安全性策略。該工具位於imagemagick-secevaluator.doyensec.com。
像素快取同步策略
將影像像素寫入磁碟時,ImageMagick 會先預先配置磁碟檔案,這比完全用零填滿檔案更快。為了進一步提升效能,檔案會在磁碟上映射記憶體。這可以將效能提升多達 5 倍,但有可能在填入資料時,磁碟檔案的可用空間不足,導致作業系統 (OS) 發出 SIGBUS 訊號,阻止 ImageMagick 繼續執行。若要防止發生 SIGBUS 訊號,請使用以下安全性策略
<policy domain="cache" name="synchronize" value="True"/>
設定為 True 可確保所有影像資料都已完全清除並同步到磁碟。不過,這會影響效能,但優點包括確保在系統當機時仍有有效的影像檔案,以及在磁碟空間不足以容納影像像素快取時及早回報。
零組態安全性策略
ImageMagick 的零配置構建不允許外部配置文件。要定義您的安全策略,您必須改為編輯 MagickCore/policy-private.h 源碼模組,添加您的策略語句,然後構建 ImageMagick 發行版。以下是一個零配置安全策略的示例
static const char *ZeroConfigurationPolicy = \ "<policymap> \ <policy domain=\"coder\" rights=\"none\" pattern=\"MVG\"/> \ </policymap>";
其他安全注意事項
如果您在 ImageMagick 中發現漏洞,請先確定該漏洞是否可以通過安全策略來減輕。默認情況下,ImageMagick 是開放的。使用安全策略添加約束以滿足您本地安全治理的要求。如果您確信安全策略無法解決該漏洞,請將該漏洞發布為 安全公告。大多數漏洞會在 48 小時內得到審查和解決。
有幾種方法可以讓 ImageMagick 更安全
- 使用網絡安全圖像格式:限制 ImageMagick 僅讀取或寫入網絡安全圖像格式(如 GIF、JPEG 和 PNG)有助於提高安全性。
- 自訂安全策略:您可以自訂安全策略以滿足您本地環境或組織策略的需求。此策略可以涵蓋以下方面:內存使用量、允許的讀寫路徑、序列中允許的圖像數量、工作流程可以運行的最長時間、圖像像素允許的磁碟空間量、遠程連接的秘密密碼以及允許或拒絕哪些編碼器。
- 設置資源使用限制:您可以設置資源限制,例如內存使用量、磁碟空間和工作流程運行時間,以防止潛在的有害情況。
- 使用沙盒:沙盒是一種安全技術,允許您在受限環境中運行程序,以防止其訪問敏感信息或更改系統。
- 禁用潛在危險的編碼器:如果您在公共網站上使用 ImageMagick,您可能希望通過禁用某些編碼器(例如 MVG 或 HTTPS)來提高安全性。
- 防止執行圖像濾鏡和間接讀取:您可以防止用戶執行圖像濾鏡和執行間接讀取以提高安全性。
- 使用最新版本的 ImageMagick:使用最新版本的 ImageMagick 非常重要,這樣才能利用最新的安全修復程序和更新。