Nmap 進階掃描技術與腳本引擎

深入探討 Nmap 進階掃描技術,包含 NSE 腳本引擎應用、自訂腳本開發、防火牆規避技術,以及滲透測試實戰範例

前言

在前一篇文章中,我們介紹了 Nmap 的基礎使用方法。本文將深入探討 Nmap 的進階掃描技術,包含 NSE(Nmap Scripting Engine)腳本引擎的深度應用、自訂腳本開發、防火牆規避技術,以及實際滲透測試場景中的應用範例。

進階掃描選項

進階主機發現技術

除了基本的 ping 掃描外,Nmap 提供多種主機發現方法:

TCP SYN 發現(-PS)

發送 TCP SYN 封包到指定連接埠,若收到 SYN/ACK 或 RST 回應則表示主機存活。

1
sudo nmap -PS22,80,443 192.168.1.0/24

TCP ACK 發現(-PA)

發送 TCP ACK 封包,通常用於繞過無狀態防火牆。

1
sudo nmap -PA80,443 192.168.1.0/24

UDP 發現(-PU)

發送 UDP 封包到指定連接埠。

1
sudo nmap -PU53,161 192.168.1.0/24

ICMP 發現選項

1
2
3
4
5
6
7
8
# ICMP Echo Request
sudo nmap -PE 192.168.1.0/24

# ICMP Timestamp Request
sudo nmap -PP 192.168.1.0/24

# ICMP Address Mask Request
sudo nmap -PM 192.168.1.0/24

組合多種發現方法

1
sudo nmap -PS22,80,443 -PA80,443 -PU53 -PE 192.168.1.0/24

進階連接埠掃描技術

NULL 掃描(-sN)

不設定任何 TCP 標誌位元,用於規避某些防火牆。

1
sudo nmap -sN 192.168.1.1

FIN 掃描(-sF)

只設定 FIN 標誌位元。

1
sudo nmap -sF 192.168.1.1

Xmas 掃描(-sX)

設定 FIN、PSH、URG 標誌位元(像聖誕樹燈一樣閃亮)。

1
sudo nmap -sX 192.168.1.1

Window 掃描(-sW)

類似 ACK 掃描,但會檢查 RST 封包的視窗欄位。

1
sudo nmap -sW 192.168.1.1

Maimon 掃描(-sM)

使用 FIN/ACK 組合,某些 BSD 系統會對此有不同回應。

1
sudo nmap -sM 192.168.1.1

自訂 TCP 標誌掃描

1
2
3
4
5
# 設定 SYN 和 FIN 標誌
sudo nmap --scanflags SYNFIN 192.168.1.1

# 設定 URG、PSH、SYN 標誌
sudo nmap --scanflags URGPSHSYN 192.168.1.1

Idle/Zombie 掃描(-sI)

這是一種極為隱蔽的掃描技術,利用「殭屍」主機來進行掃描,您的 IP 位址完全不會出現在目標日誌中。

1
2
3
4
5
# 首先找到合適的殭屍主機(IP ID 序列必須是可預測的)
nmap --script ipidseq 192.168.1.0/24

# 使用殭屍主機進行掃描
sudo nmap -sI zombie_host:port target_host

原理說明

  1. 探測殭屍主機的 IP ID
  2. 偽造來源 IP 向目標發送 SYN
  3. 再次探測殭屍主機的 IP ID
  4. 根據 IP ID 變化判斷連接埠狀態

進階版本偵測

1
2
3
4
5
6
7
8
# 提高版本偵測強度(0-9,預設 7)
nmap -sV --version-intensity 9 192.168.1.1

# 輕量版本偵測(快速但較不精確)
nmap -sV --version-light 192.168.1.1

# 嘗試所有探測(最慢但最精確)
nmap -sV --version-all 192.168.1.1

NSE 腳本引擎深入解析

Nmap Scripting Engine(NSE)是 Nmap 最強大的功能之一,允許使用者撰寫腳本來擴展 Nmap 的功能。

腳本類別詳解

類別說明
auth認證相關,嘗試繞過或取得認證資訊
broadcast透過廣播發現區域網路中的主機
brute暴力破解認證
default預設執行的腳本(使用 -sC 時)
discovery取得更多目標資訊
dos阻斷服務攻擊測試
exploit嘗試利用已知弱點
external使用外部資源取得資訊
fuzzer模糊測試
intrusive可能對目標造成影響的腳本
malware偵測惡意軟體
safe安全腳本,不會對目標造成影響
version版本偵測相關
vuln弱點檢測

腳本選擇進階用法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 使用萬用字元
nmap --script "http-*" 192.168.1.1

# 排除特定腳本
nmap --script "http-* and not http-brute" 192.168.1.1

# 組合多個類別
nmap --script "vuln and safe" 192.168.1.1

# 使用布林運算
nmap --script "(http-* or ssl-*) and not intrusive" 192.168.1.1

常用 NSE 腳本範例

HTTP 相關腳本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# 取得網頁標題
nmap --script http-title -p 80,443,8080 192.168.1.1

# 列舉網站目錄
nmap --script http-enum -p 80 192.168.1.1

# 偵測 HTTP 方法
nmap --script http-methods -p 80 192.168.1.1

# 取得 robots.txt
nmap --script http-robots.txt -p 80 192.168.1.1

# SQL 注入測試
nmap --script http-sql-injection -p 80 192.168.1.1

# 偵測 WAF
nmap --script http-waf-detect -p 80 192.168.1.1

SMB 相關腳本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 列舉 SMB 共用
nmap --script smb-enum-shares -p 445 192.168.1.1

# 列舉 SMB 使用者
nmap --script smb-enum-users -p 445 192.168.1.1

# 檢測 SMB 弱點(如 EternalBlue)
nmap --script smb-vuln-* -p 445 192.168.1.1

# 取得 SMB 系統資訊
nmap --script smb-os-discovery -p 445 192.168.1.1

SSL/TLS 相關腳本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 檢測 SSL 弱點
nmap --script ssl-* -p 443 192.168.1.1

# 列舉 SSL 憑證資訊
nmap --script ssl-cert -p 443 192.168.1.1

# 列舉支援的加密套件
nmap --script ssl-enum-ciphers -p 443 192.168.1.1

# 檢測 Heartbleed 弱點
nmap --script ssl-heartbleed -p 443 192.168.1.1

暴力破解腳本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# SSH 暴力破解
nmap --script ssh-brute -p 22 192.168.1.1

# FTP 暴力破解
nmap --script ftp-brute -p 21 192.168.1.1

# MySQL 暴力破解
nmap --script mysql-brute -p 3306 192.168.1.1

# 使用自訂字典
nmap --script ssh-brute --script-args userdb=users.txt,passdb=passwords.txt -p 22 192.168.1.1

腳本參數傳遞

1
2
3
4
5
6
7
8
# 設定腳本參數
nmap --script http-enum --script-args http-enum.basepath="/admin/" -p 80 192.168.1.1

# 多個參數
nmap --script http-brute --script-args http-brute.path="/login",http-brute.method="POST" -p 80 192.168.1.1

# 從檔案讀取參數
nmap --script-args-file script-args.txt 192.168.1.1

自訂 NSE 腳本開發

NSE 腳本使用 Lua 語言撰寫。以下是建立自訂腳本的基礎。

腳本結構

 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
-- 腳本描述
description = [[
這是一個範例腳本,用於示範 NSE 腳本的基本結構。
]]

-- 腳本類別
categories = {"discovery", "safe"}

-- 授權資訊
author = "Your Name"
license = "Same as Nmap--See https://nmap.org/book/man-legal.html"

-- 引入必要的函式庫
local shortport = require "shortport"
local http = require "http"
local stdnse = require "stdnse"

-- 定義連接埠規則(決定腳本何時執行)
portrule = shortport.http

-- 主要執行邏輯
action = function(host, port)
    local response = http.get(host, port, "/")

    if response.status == 200 then
        return "Web server is responding"
    else
        return "Web server returned status: " .. response.status
    end
end

自訂 HTTP 標頭檢查腳本

建立檔案 http-security-headers.nse

 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
37
38
39
40
description = [[
檢查網站的安全標頭設定。
]]

categories = {"discovery", "safe"}
author = "Security Researcher"

local shortport = require "shortport"
local http = require "http"

portrule = shortport.http

action = function(host, port)
    local response = http.get(host, port, "/")

    if not response or not response.header then
        return nil
    end

    local security_headers = {
        "x-frame-options",
        "x-content-type-options",
        "x-xss-protection",
        "strict-transport-security",
        "content-security-policy"
    }

    local results = {}

    for _, header in ipairs(security_headers) do
        local value = response.header[header]
        if value then
            table.insert(results, header .. ": " .. value)
        else
            table.insert(results, header .. ": MISSING")
        end
    end

    return stdnse.format_output(true, results)
end

使用自訂腳本

1
2
3
4
5
6
7
8
9
# 指定腳本路徑
nmap --script /path/to/http-security-headers.nse -p 80,443 192.168.1.1

# 將腳本放入 Nmap 腳本目錄
# Linux: /usr/share/nmap/scripts/
# macOS: /usr/local/share/nmap/scripts/

# 更新腳本資料庫
nmap --script-updatedb

防火牆規避技術

封包分割(-f)

將 TCP 標頭分割成多個較小的 IP 片段,可規避某些封包過濾系統。

1
2
3
4
5
6
7
8
# 基本分割(8 bytes)
sudo nmap -f 192.168.1.1

# 更小的分割
sudo nmap -f -f 192.168.1.1

# 指定 MTU
sudo nmap --mtu 16 192.168.1.1

誘餌掃描(-D)

使用誘餌 IP 位址混淆目標的日誌。

1
2
3
4
5
# 使用指定的誘餌
sudo nmap -D decoy1,decoy2,decoy3,ME 192.168.1.1

# 使用隨機誘餌
sudo nmap -D RND:10 192.168.1.1

注意:誘餌 IP 必須是存活的主機,否則可能導致目標被 SYN 洪水攻擊。

偽造來源 IP(-S)

1
sudo nmap -S spoofed_ip -e interface -Pn 192.168.1.1

警告:您將無法接收回應,僅適用於某些特殊情況。

偽造來源連接埠(–source-port)

某些防火牆允許來自特定連接埠(如 53、80)的流量。

1
2
sudo nmap --source-port 53 192.168.1.1
sudo nmap -g 80 192.168.1.1  # -g 是 --source-port 的簡寫

附加隨機資料(–data-length)

正常的 Nmap 掃描封包大小是固定的,附加隨機資料可規避基於封包大小的偵測。

1
sudo nmap --data-length 25 192.168.1.1

MAC 位址偽造(–spoof-mac)

1
2
3
4
5
6
7
8
# 指定 MAC 位址
sudo nmap --spoof-mac 00:11:22:33:44:55 192.168.1.1

# 使用特定廠商的 MAC
sudo nmap --spoof-mac Apple 192.168.1.1

# 使用隨機 MAC
sudo nmap --spoof-mac 0 192.168.1.1

時間控制規避

1
2
3
4
5
6
7
8
# 極慢掃描,規避 IDS 時間窗口
sudo nmap -T0 --max-rate 1 192.168.1.1

# 設定掃描延遲
sudo nmap --scan-delay 10s 192.168.1.1

# 隨機化目標順序
sudo nmap --randomize-hosts 192.168.1.0/24

組合規避技術

1
2
sudo nmap -sS -T2 -f --data-length 24 --source-port 53 \
    -D RND:5 --randomize-hosts 192.168.1.0/24

實戰範例

範例 1:完整的滲透測試初始掃描

 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
#!/bin/bash
TARGET=$1
OUTPUT_DIR="./nmap_results"

mkdir -p $OUTPUT_DIR

# 快速掃描常用連接埠
echo "[*] Quick scan of top ports..."
nmap -T4 --top-ports 1000 -oA $OUTPUT_DIR/quick_scan $TARGET

# 完整 TCP 掃描
echo "[*] Full TCP port scan..."
sudo nmap -sS -T4 -p- -oA $OUTPUT_DIR/full_tcp $TARGET

# UDP 掃描(常用連接埠)
echo "[*] UDP scan of top ports..."
sudo nmap -sU --top-ports 100 -oA $OUTPUT_DIR/udp_scan $TARGET

# 服務版本偵測
echo "[*] Service version detection..."
nmap -sV --version-intensity 9 -p $(grep -oP '\d+/open' $OUTPUT_DIR/full_tcp.gnmap |
    cut -d'/' -f1 | tr '\n' ',') -oA $OUTPUT_DIR/version_scan $TARGET

# 弱點掃描
echo "[*] Vulnerability scan..."
nmap --script vuln -p $(grep -oP '\d+/open' $OUTPUT_DIR/full_tcp.gnmap |
    cut -d'/' -f1 | tr '\n' ',') -oA $OUTPUT_DIR/vuln_scan $TARGET

範例 2:Web 應用程式偵察

1
2
3
4
5
6
7
8
# 綜合 Web 掃描
nmap -sV --script "http-* and not brute and not dos" \
    -p 80,443,8080,8443 192.168.1.1 -oA web_recon

# 特定腳本組合
nmap --script "http-title,http-headers,http-methods,http-enum,http-robots.txt,\
http-sitemap-generator,http-waf-detect,http-waf-fingerprint" \
    -p 80,443 192.168.1.1

範例 3:內部網路探索

1
2
3
4
5
6
# 主機發現
sudo nmap -sn -PS22,80,443,3389 -PA80,443 -PU53,161 \
    192.168.1.0/24 -oA internal_hosts

# 針對發現的主機進行服務掃描
sudo nmap -sS -sV -sC -O -T4 -iL live_hosts.txt -oA internal_services

範例 4:Active Directory 環境掃描

1
2
3
4
5
6
7
8
# 發現網域控制站
nmap -p 389,636,3268,3269,88,53 --script "ldap-* and not brute" \
    192.168.1.0/24 -oA dc_discovery

# SMB 掃描
nmap -p 139,445 --script smb-enum-domains,smb-enum-groups,\
smb-enum-processes,smb-enum-sessions,smb-enum-shares,\
smb-enum-users,smb-os-discovery 192.168.1.1 -oA smb_enum

範例 5:隱蔽掃描(規避 IDS/IPS)

1
2
3
4
5
6
7
8
sudo nmap -sS -T2 \
    -f --data-length 24 \
    --source-port 53 \
    --scan-delay 5s \
    -D RND:5,ME \
    --randomize-hosts \
    -p 21,22,23,25,53,80,110,139,143,443,445,993,995,3306,3389 \
    192.168.1.0/24 -oA stealth_scan

範例 6:IPv6 掃描

1
2
3
4
5
# IPv6 目標掃描
nmap -6 -sS -sV fe80::1 -oA ipv6_scan

# 發現本地 IPv6 主機
nmap -6 --script targets-ipv6-multicast-* --script-args newtargets

效能優化技巧

並行處理

1
2
3
4
5
# 設定最小並行主機數
nmap --min-hostgroup 64 192.168.1.0/24

# 設定最小並行探測數
nmap --min-parallelism 100 192.168.1.0/24

超時設定

1
2
3
4
5
# 設定主機超時
nmap --host-timeout 30m 192.168.1.0/24

# 設定探測超時
nmap --max-rtt-timeout 500ms 192.168.1.0/24

重試設定

1
2
# 減少重試次數以加快速度
nmap --max-retries 2 192.168.1.0/24

輸出處理與分析

使用 grep 處理輸出

1
2
3
4
5
# 找出所有開放的連接埠
grep -oP '\d+/open' scan.gnmap | sort -u

# 找出所有存活主機
grep "Status: Up" scan.gnmap | cut -d' ' -f2

使用 xmlstarlet 處理 XML 輸出

1
2
3
4
5
# 列出所有開放的連接埠
xmlstarlet sel -t -v "//port[@protocol='tcp']/@portid" scan.xml

# 列出所有服務
xmlstarlet sel -t -m "//port/service" -v "@name" -n scan.xml

轉換為其他格式

1
2
3
4
5
# XML 轉 HTML
xsltproc scan.xml -o scan.html

# 使用 nmap 內建樣式
xsltproc /usr/share/nmap/nmap.xsl scan.xml -o scan.html

安全與法律注意事項

  1. 取得書面授權:永遠在獲得明確授權後才進行掃描。未經授權的掃描可能違反電腦犯罪法律。

  2. 了解範圍:清楚知道您被授權掃描的範圍,不要超出授權範圍。

  3. 避免阻斷服務:某些掃描技術可能對目標造成負載,請謹慎使用 -T5 或積極的腳本。

  4. 保護掃描結果:掃描結果可能包含敏感資訊,請妥善保管。

  5. 記錄所有活動:保留掃描日誌,以便在需要時證明您的活動是授權的。

參考資源

總結

Nmap 的進階功能提供了強大的網路探索和安全評估能力。透過 NSE 腳本引擎,您可以自動化複雜的偵測任務;透過防火牆規避技術,您可以在更嚴格的網路環境中進行評估。

掌握這些進階技術需要時間和練習。建議在實驗環境中多加練習,並持續關注 Nmap 的更新和新腳本的發布。記住,這些技術應該只用於合法的滲透測試和安全評估工作。

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