the_open_door
coffee
ESEN
← Back to index

ImageMagick Profile File Read (CVE-2022-44268)

ATT&CKT1190
Full command arsenal in the cheatsheet

CVE-2022-44268 is an arbitrary file read that comes through no parameter at all: it comes inside a PNG. It affects ImageMagick 7.1.0-49 and earlier, and the vector is any application that accepts a user image and processes it, which is nearly every one that resizes, crops or builds thumbnails.

A PNG can carry text chunks with metadata. One of them declares a profile, and on affected versions ImageMagick treated its value as a local file path: processing the image opened that file and placed its contents in the output image's metadata. So the file is not read at upload time, it is read when the server does its job, and it comes back hidden in the image handed to us.

Generating the poisoned PNG:


git clone https://github.com/Sybil-Scan/imagemagick-lfi-poc

python3 generate.py -f /etc/passwd -o image.png

Upload it, let the application process it, download the result. What has to be read is not the image but its metadata:


exiftool <output.png>

The content shows up under Raw Profile Type, in hexadecimal, with its length in front. Undoing that is one more step:


cat <hex.txt> | tr -d '\n' | xxd -r -p

The neat part of this vulnerability is that it chains with itself: the first read pulls /etc/passwd to learn which users exist, and the second pulls whatever the application's source code already told us the path of. If the site keeps its database in a file (a SQLite, a config holding the connection string), that is what to ask for.

Requirements. ImageMagick 7.1.0-49 or earlier processing an image we control, and being able to retrieve the output image: if the application processes and returns nothing, there is nowhere to read the result. The file is read with the privileges of whatever runs ImageMagick, which on a web app is the server user.

How to detect it. The convert, magick or mogrify process opening files that are not images (/etc/passwd, .db, .conf, .env) is the signal, visible with auditd on openat filtered by executable. In the content, an input PNG whose tEXt chunk carries a profile key with an absolute path inside is anomalous: no camera and no editor writes that. And in the output, an image whose Raw Profile Type metadata contains plain system text.

Defence. Update ImageMagick. Beyond that, ImageMagick's policy.xml can forbid delegates and whole paths, which is a mitigation worth having in place regardless; and the sensible design is to process uploaded images in an isolated process with no access to the application's filesystem.

It is a special case of arbitrary file read, and in practice it chains into credentials written into the source, which is what ends up being read.

Machines where it's used