Automating Payloads & Delivery with Metasploit
Last updated
Last updated
is an automated attack framework developed by Rapid7
that streamlines the process of exploiting vulnerabilities through the use of pre-built modules that contain easy-to-use options to exploit vulnerabilities and deliver payloads to gain a shell on a vulnerable system. It can make exploiting a vulnerable system so easy that some Cybersecurity training vendors limit how many times it can be used on lab exams. Here at Hack The Box, we encourage experimenting with tools in our lab environments until you have a solid foundational understanding. Most organizations will not limit us on which tools we can or cannot use on an engagement. However, they will expect us to know what we are doing. Therefore, it is our responsibility to seek an understanding as we learn. Not understanding the effects of the tools we use can be destructive in a live penetration test or audit. This is one primary reason we should consistently seek a deeper understanding of the tools, techniques, methodologies, and practices we learn.
In this section, we will interact with the community edition
of Metasploit on Pwnbox. We will use pre-built modules
and craft payloads with MSFVenom
. It is important to note that many established cybersecurity firms utilize the paid edition of Metasploit called Metasploit Pro
to conduct penetration tests, security audits, and even social engineering campaigns. If you want to explore the differences between the community edition and Metasploit Pro, you can check out this .
We could spend the rest of this module covering everything about Metasploit, but we are only going to go so far as to work with the very basics within the context of shells & payloads.
Let's start working hands-on with Metasploit by launching the Metasploit framework console as root (sudo msfconsole
)
We can see there is creative ASCII art presented as the banner at launch and some numbers of particular interest.
2131
exploits
592
payloads
These numbers can change as the maintainers add and remove code or if you import a module for use into Metasploit. Let's get familiar with Metasploit payloads by using a classic exploit module
that can be used to compromise a Windows system. Remember that Metasploit can be used for more than just exploitation. We can also use different modules to scan & enumerate targets.
In this case, we will be using enumeration results from a nmap
scan to pick a Metasploit module to use.
In the output, we see several standard ports that are typically open on a Windows system by default. Remember that scanning and enumeration is an excellent way to know what OS (Windows or Linux) our target is running to find an appropriate module to run with Metasploit. Let's go with SMB
(listening on 445
) as the potential attack vector.
Once we have this information, we can use Metasploit's search functionality to discover modules that are associated with SMB. In the msfconsole
, we can issue the command search smb
to get a list of modules associated with SMB vulnerabilities:
We will see a long list of Matching Modules
associated with our search. Notice the format each module is in. Each module has a number listed on the far left of the table to make selecting the module easier, a Name
, Disclosure Date
, Rank
, Check
and Description
.
Let's look at one module, in particular, to understand it within the context of payloads.
56 exploit/windows/smb/psexec
56
The number assigned to the module in the table within the context of the search. This number makes it easier to select. We can use the command use 56
to select the module.
exploit/
This defines the type of module. In this case, this is an exploit module. Many exploit modules in MSF include the payload that attempts to establish a shell session.
windows/
This defines the platform we are targeting. In this case, we know the target is Windows, so the exploit and payload will be for Windows.
smb/
This defines the service for which the payload in the module is written.
psexec
This defines the tool that will get uploaded to the target system if it is vulnerable.
Once we select the module, we will notice a change in the prompt that gives us the ability to configure the module based on parameters specific to our environment.
This is one area where Metasploit shines in terms of ease of use. In the output of the module options, we see various options and settings with a description of what each setting means. We will not be using SERVICE_DESCRIPTION
, SERVICE_DISPLAY_NAME
and SERVICE_NAME
in this section. Notice how this particular exploit will use a reverse TCP shell connection utilizing Meterpreter
. A Meterpreter shell gives us far more functionality than a raw TCP reverse shell, as we established in this module's earlier sections. It is the default payload that is used in Metasploit.
We will want to use the set
command to configure the following settings as such:
These settings will ensure that our payload is delivered to the proper target (RHOSTS
), uploaded to the default administrative share (ADMIN$
) utilizing credentials (SMBPass
& SMBUser
), then initiate a reverse shell connection with our local host machine (LHOST
).
After we issue the exploit
command, the exploit is run, and there is an attempt to deliver the payload onto the target utilizing the Meterpreter payload. Metasploit reports back each step of this process, as seen in the output. We know this was successful because a stage
was sent successfully, which established a Meterpreter shell session (meterpreter >
) and a system-level shell session. Keep in mind that Meterpreter is a payload that uses in-memory DLL injection to stealthfully establish a communication channel between an attack box and a target. The proper credentials and attack vector can give us the ability to upload & download files, execute system commands, run a keylogger, create/start/stop services, manage processes, and more.
Like other command language interpreters (Bash, PowerShell, ksh, etc...), Meterpreter shell sessions allow us to issue a set of commands we can use to interact with the target system. We can use the ?
to see a list of commands we can use. We will notice limitations with the Meterpreter shell, so it is good to attempt to use the shell
command to drop into a system-level shell if we need to work with the complete set of system commands native to our target.
To make sure you have all the exploits loaded run
Notice how exploit
is outside of the parentheses. This can be interpreted as the MSF module type being an exploit, and the specific exploit & payload is written for Windows. The attack vector is SMB
, and the Meterpreter payload will be delivered using . Let's learn more about using this exploit and delivering the payload by using the options
command.
In this case, as detailed in the : "This module uses a valid administrator username and password (or password hash) to execute an arbitrary payload. This module is similar to the "psexec" utility provided by SysInternals. This module is now able to clean up after itself. The service created by this tool uses a randomly chosen name and description. "