Mac OS X上使用Wireshark抓包


原文地址:http://blog.csdn.net/phunxm/article/details/38590561

 

0.關於 Wireshark

(1)about

Wireshark is the world’s foremost and widely-used network protocol analyzer.
It lets you see what’s happening on your network at a microscopic level.


Wireshark(前稱Ethereal)是當今世界最流行的網絡協議嗅探、抓包和分析工具,它使我們得以窺探網絡上流動的數據及其協議細節。
網絡管理員使用 Wireshark 來檢測網絡問題;網絡安全工程師使用 Wireshark 來檢查網絡安全相關問題;開發者可以使用 Wireshark 來開發調試新的通信協議;普通使用者可以使用 Wireshark 來學習網絡協議棧相關的知識。


(2)tcpdump
在一些 UN*X 發行版中,tcpdump 通常作為標准的軟件包被默認安裝,可以執行 “tcpdump” 命令來確定是否已安裝了 tcpdump。

 

[plain]  view plain  copy
 
 
 
  1. faner@THOMASFAN-MB0:~|⇒  tcpdump --help  
  2. tcpdump version tcpdump version 4.9.0 -- Apple version 79.60.1  
  3. libpcap version 1.8.1 -- Apple version 67.60.1  
  4. LibreSSL 2.2.7  
  5. Usage: tcpdump [-aAbdDefhHIJKlLnNOpqStuUvxX#] [ -B size ] [ -c count ]  
  6.         [ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]  
  7.         [ -i interface ] [ -j tstamptype ] [ -M secret ] [ --number ]  
  8.         [ -Q in|out|inout ]  
  9.         [ -r file ] [ -s snaplen ] [ --time-stamp-precision precision ]  
  10.         [ --immediate-mode ] [ -T type ] [ --version ] [ -V file ]  
  11.         [ -w file ] [ -W filecount ] [ -y datalinktype ] [ -z postrotate-command ]  
  12.         [ -Z user ] [ expression ]  
tcpdump 是命令行界面的(CUI), wireshark 是圖形用戶界面的(GUI), Wireshark 相對 tcpdump 而言,界面更友好、功能更強大。

 

(3)lipcap
Wireshark/TShark uses libpcap to capture live network data.
WinPcap is the Windows version of the libpcap library; it includes a driver to support capturing packets. Wireshark uses this library to capture live network data on Windows.

 

UN*X 平台下的 Wireshark 是基於libpcap 實現的,Windows 平台下的 Wireshark 則是基於 winpcap 實現的。

libpcap(Packet Capture Libray)即數據包捕獲函數庫,源於 tcpdump 項目。最開始是由 tcpdump 中剝離出的抓包、過濾、capture file 的讀寫代碼提煉而成,現在也是由 tcpdump 項目的開發組織(tcpdump.org)維護。

捕獲抓包機制是在數據鏈路層增加一個旁路處理(並不干擾系統自身的網絡協議棧的處理),對發送和接收的數據包通過 UN*X 內核做過濾和緩沖處理,最后直接傳遞給上層應用程序。一個包的捕捉分為三個主要部分,包括面向底層包捕獲、面向中間層的數據包過濾和面向應用層的用戶接口。

libpcap 作為捕捉網絡數據包的庫,它是一個獨立於系統的用戶級 API 接口,為底層網絡檢測提供了一個可移植的框架。

鑒於 wireshark 和 tcpdump 都共同使用 libpcap 作為其底層抓包的庫,可以用 Wireshark 打開 tcpdump 抓包保存的 pcap 文件進行分析。

 

(4)wireshark for macOS

Wireshark 1.x 針對 UNIX Like 系統的 GUI 發行版界面采用的是 X Window(1987年更改X版本到X11)。Mac OS X 在 Mountain Lion 之后放棄 X11,取而代之的是開源的 XQuartz(X11.app)。因此,在 Mac OS X 上安裝 Wireshark 1.x 之前,需要先下載安裝 Quartz。

最新版的Wireshark 2.x摒棄了陳舊的 GTK+X11 界面,無需先下載安裝 Quartz,取而代之的是與 Windows 發行版保持一致的 Qt 界面,簡潔清爽。

因此,若安裝最新 2.x 版本,可略過 XQuartz 安裝和 BPF 權限問題等相關章節,直接跳至文末閱讀最后一章,相關章節可按需選(通)讀。

 

1.安裝 XQuartz

XQuartz(XQuartz-2.7.6.dmg)安裝完成后,按照提示需要 注銷 重新 登錄,以使 XQuartz 作為默認的 X11 Server。

安裝成功后,在終端輸入“xterm --help”可查看命令行幫助,輸入“xterm -version”可查看 xterm 版本信息。在終端輸入“xterm”或通過菜單“Applications->Terminal”可啟動X Window(XQuartz)的終端(xterm)。

 

2.安裝 Wireshark

安裝 OS X 10.6 and later Intel 64-bit 版本(Wireshark 1.12.0 Intel 64.dmg)。

安裝成功后,在終端輸入“wireshark --help”可查看命令行幫助,輸入“wireshark --v”可查看 wireshark 版本信息。

 

3.啟動 Wireshark

首次啟動 Wireshark,提示通過前端 X11 窗口可以操控 Wireshark;由於需要編譯字體包,可能需要耗費幾分鍾。

如果事先沒有安裝 X11(XQuartz),則會提示引導安裝 X11。

如果事先已經安裝好 XQuartz(X11),則會引導選擇 X11.app 所在路徑。——最新版本的 Wireshark 1.12.4 Intel 64.dmg 貌似會自動識別已安裝的 X11 路徑而無需手動指定。

點擊 Browse 到“/Applications/Utilities/XQuartz.app”,將 XQuartz 作為 Wireshark 的 X11 前端。——最新版本的 Wireshark 1.12.4 Intel 64.dmg 貌似會自動識別已安裝的 X11 路徑而無需手動指定。

如果不出意外的話,Wireshark 將成功啟動。

 

關於 Mac OS X 升級 Yosemite 后無法啟動 Wireshark 的問題

Mac OS X 升級 Yosemite 后,無法啟動 Wireshark,是因為 Yosemite 將 X11 外鏈文件夾從原來的 /usr 移動到了 /opt 下。可通過以下三種方式解決:

-解決方法1:重裝或升級 XQuartz

-解決方法2:將 /opt/X11 拷貝一份到 /usr/X11:sudo mv /opt/X11 /usr/X11

-解決方法3:將 /opt/X11 軟鏈一份到 /usr/X11:sudo ln -s /opt/X11 /usr/X11

 

4.選擇網卡,開始抓包

點擊啟動首頁中的“Interface List”(相當於菜單“Capture->Interfaces”),可以查看本機活躍網卡(通過系統菜單Apple->About This Mac->More Info->Overview->System Report或終端命令 ifconfig 也可查看)。

由於本人 iMac 使用無線接入,故 en1(Wireless NIC)和 lo0(本機環路)活躍(active),有線網卡 en0(Wired NIC)未使用(inactive)。注意:MBP 上沒有有線網卡,en0 即為無線網卡 ,可通過 ifconfig 命令自行識別。

這里可以直接勾選 en1,點擊 Start 按鈕使用默認 Capture Options(默認勾選了混雜模式[promiscuous mode])進行抓包;可以在啟動首頁點選“Wi-Fi:en1”打開“Edit Interface Settings”,設置(例如可配置 Capture Filters 只捕捉指定類型或條件的數據包)或使用默認 Capture Options 開始抓包;也可以從菜單“Capture->Options”着手,選擇網卡設置選項開始抓包。

Ascapture filter strings are directly passed from Wireshark/TShark tolibpcap, the available capture filter syntax depends on the libpcap version installed.

這里選擇了無線網卡(Wi-Fi: en1),怎么抓到的是以太網包(Ethernet Packet)呢?實際上它是某些 BSD 提供的偽以太網頭(fake Ethernet headers)。如果要抓取 IEEE802.11 無線包(Beacon Frame 等),則需要開啟監聽模式[monitor mode],並且將 Link-layer header type 選為 802.11 系列選項。

另外,在 Display Filters 中填寫了“http”,將從捕捉到的數據包中過濾出並只顯示 HTTP 協議包。

 

5.監聽抓取802.11無線幀

在“Edit Interface Settings”或“Capture Options”中勾選“Capture packets in monitor mode”開啟監聽模式,然后勾選 Link-layer header type 為“802.11”開始抓取無線包。

 

說明:

(1)iMac 開啟 Monitor Mode 時,可能會阻塞網卡導致上不了網,參見下文相關說明。

(2)Windows 下抓取 802.11 無線幀Management/Control/DataFrame),需要 Wireshark 配合 AirPcap 或使用 OmniPeek

(3)常見 802.11 無線幀( 802.11 frames)類型:

 

(1)Management Frame

 

Type/Subtype: Beacon frame (0x0008,Bit[5:0]=00,1000B)

 

Type/Subtype: Probe Response (0x0005,Bit[5:0]=00,0101B)

 

(2)Control Frame

 

Type/Subtype: 802.11 Block Ack (0x0019,Bit[5:0]=01,1001B)

 

Type/Subtype: Request-to-send (0x001b,Bit[5:0]=01,1011B)

 

Type/Subtype: Clear-to-send (0x001c,Bit[5:0]=01,1100B)

 

Type/Subtype: Acknowledgement (0x001d,Bit[5:0]=01,1101B)

 

(3)Data Frame

 

Type/Subtype: Data (0x0020,Bit[5:0]=10,0000B)

 

Type/Subtype: Null function (No data) (0x0024,Bit[5:0]=10,0100B)

 

Type/Subtype: QoS Data (0x0028,Bit[5:0]=10,1000B)

 

Type/Subtype: QoS Data + CF-Ack + CF-Poll (0x002b,Bit[5:0]=10,1011B)

 

6.通過 RVI 抓取 iPhone 數據包

(1)RVI 簡介

使用 Mac 抓取 iPhone 數據包可通過共享和代理兩種方式:

 

  • 使用 Mac 的網絡共享功能將 Mac 的網絡通過 WiFi 共享給 iPhone 連接;
  • 使用代理軟件(例如 CharlesFlidder for MonoAndiparos)在Mac上建立HTTP代理服務器。

 

這兩種方式都是將 iPhone 的網絡流量導入到 Mac 電腦中,通過 Mac 連接互聯網。這就要求 Mac 本身是聯網的,對於網絡共享的方式還要求 Mac 本身的網絡不能使用 WiFi,而且在 iPhone 上只能使用 WiFi 連接,無法抓取到 xG(2G/3G/4G) 網絡包。

蘋果在 iOS 5 中新引入了“遠程虛擬接口(Remote Virtual Interface,RVI)”的特性,可以在 Mac 中建立一個虛擬網絡接口來作為 iOS 設備的網絡棧,這樣所有經過 iOS 設備的流量都會經過此虛擬接口。此虛擬接口只是監聽 iOS 設備本身的協議棧(但並沒有將網絡流量中轉到 Mac 本身的網絡連接上),所有網絡連接都是 iOS 設備本身的,與 Mac 電腦本身聯不聯網或者聯網類型無關。iOS設備本身可以為任意網絡類型(WiFi/xG),這樣在 Mac 電腦上使用任意抓包工具(tcpdump、Wireshark、CPA)抓取 RVI 接口上的數據包就實現了對 iPhone 的抓包。

Mac OS X 對 RVI 的支持是通過終端命令 rvictl 提供的,在終端(Terminal)中輸入“ rvictl  ? ”命令可查看幫助:

==================================================

rvictl Options:

           -l, -L                     List currently active devices

           -s, -S                     Start a device or set of devices

           -x, -X                    Stop a device or set of devices

==================================================

(2)使用 “ rvictl  -s ”命令創建虛擬接口

首先,通過 MFI USB 數據線將 iPhone 連接到安裝了 Mac OS+Xcode 4.2(or later) 的 Mac 機上。iOS 7 以上需要搭配 Xcode 5.0(or later),抓包過程中必須保持連接。

然后,通過 iTunes->Summary 或者 Xcode->Organizer->Devices 獲取 iPhone 的 UDID(identifier)。

接着,使用“rvictl -s”命令創建 RVI 接口,使用 iPhone 的 UDID 作為參數。

==================================================

$rvictl -s <UDID>

==================================================

創建成功后,在終端通過 ifconfig 命令可以看到多了一個 rvi0 接口。當有多個 iOS 設備連接 iMac 時,依次是 rvi1,rvi2…,使用“ rvictl  -l ”命令可以列出所有掛接的虛擬接口。

在 Wireshark 首頁選擇 rvi0,使用默認的 Capture Options 即可開始對 iPhone 進行抓包。

下圖為抓取的 WiFi 數據包:

其中 iPhone 手機連接 WiFi 動態獲取的 IP 地址為 192.168.199.230。

下圖為抓取的 3G 數據包:

說明:聯通 3G 上網動態分配的都是 172.20 網段(例如172.20.124.251)的這種B類私網地址(172.16.0.0~172.32.255.255)。手機百度中輸入 IP,ip138 中顯示 112.97.24.178 是深圳聯通附近基站對外的公網網關IP地址。

(3)使用“ rvictl  -x ”命令刪除虛擬接口

 

使用 rvictl -x 命令刪除 RVI 接口,使用 iPhone 的 UDID 作為參數。

 

==================================================

 

$rvictl -x <UDID>

 

==================================================

7.關於監控模式(Monitor Mode)

 

(1)《Wireshark FAQ》:Q 10.1: How can I capture raw 802.11 frames, including non-data (management, beacon) frames?

NOTE: an interface running in monitor mode will, on most if not all platforms, not be able to act as a regular network interface; putting it into monitor mode will, in effect, take your machine off of whatever network it's on as long as the interface is in monitor mode, allowing it only to passively capture packets.

This means that you should disable name resolution when capturing in monitor mode; otherwise, when Wireshark (or TShark, or tcpdump) tries to display IP addresses as host names, it will probably block for a long time trying to resolve the name because it will not be able to communicate with any DNS or NIS servers.

 (2)《AirSnort FAQ》:Q 3: What is the difference betwen monitor and promiscuous mode?

Monitor mode enables a wireless NIC to capture packets without associating with an access point or ad-hoc network. This is desirable in that you can choose to "monitor" a specific channel, and you need never transmit any packets. In fact transmiting is sometimes not possible while in monitor mode (driver dependent). Another aspect of monitor mode is that the NIC does not care whether the CRC values are correct for packets captured in monitor mode, so some packets that you see may in fact be corrupted.

 

8.關於 Wireshark 的 BPF 權限問題

實際上,我在 OS X 10.9.4 上安裝 XQuartz+Wireshark 非常順利,沒有出現什么諸如無法發現網卡(no interface available)的權限問題。

安裝前執行“ ls -l /dev/bpf* ”,bpf0/bpf1/bpf2/bpf3 只有“rw-------”權限;安裝完成時,再次執行“ ls -l /dev/bpf* ”,發現權限已經升級為了 “rw-rw----”。

即 Wireshark 已經給當前安裝使用 Wireshark 的 user(administrator)所在的 group 配置了 rw 權限。相當於執行過“sudo chmod g+rw /dev/bpf* ”,並且已經被配置到隨機啟動,下次開機不用重新執行該命令。

--------------------------------------------------------------------------------

/CapturePrivileges- you must have sufficient privileges to capture packets, e.g. special privileges allowing capturing as a normal user (preferred) or root / Administrator privileges.

In order to capture packets under BSD (including Mac OS X), you must have read access  to the BPF devices in /dev/bpf*.

Enabling and using the "root" user in Mac OS X

Platform-Specific information about capture privileges

Howto securely configure Mac OS X for network packet sniffing with Wireshark

--------------------------------------------------------------------------------

 

9.在越獄 iPhone 上指定網卡(WiFi/xG)抓包

上文主要講解的是在不越獄正常權限情況下,通過 RVI 虛擬接口掛鈎抓取 iPhone 數據包,一台 iPhone 通常只對應一個 RVI 虛擬接口。但是 iPhone 本身支持兩種上網模式——WiFi/xG(類似PC工作站多網卡情形),當 xG 和 WiFi 同時活躍時(en0 為無線網卡,pdp_ip0 為 3G 網卡),iOS 將 WiFi 作為優先默認路由通道。

那么,當需要調試分析 xG/WiFi 切換或並存時的一些路由策略或臨界流量問題時,如何抓取指定通道(WiFi/xG)的數據包呢?使用 RVI 通過包源 IP 可能可以區分,但是由於權限等問題,總是不那么方便。此時,通過越獄,在 iPhone 上安裝遠程登錄工具包(OpenSSH)、網絡診斷分析命令行工具 network-cmds (arp,ifconfig,ping,traceroute,netstat),然后在 WinPC 或 iMac 上通過SSH隧道發送tcpdump命令指定WiFi/xG網卡進行抓包(SSH 遠程控制 iPhone 運行 tcpdump 抓包),最后通過 Wireshark 對 pcap 文件進行分析或統計。

 

10.關於數據包的源/目的地址

以第4節中 GET http://blog.csdn.net/phunxm 為例,僅做簡單的分析。

 

###############################################################

No 148:發包 的源MAC/IP地址為本機 MAC/IP 地址。
(1)Destination Mac 為 00:00:0c:07:ac:24(CISCO All-HSRP-routers_24)
socket 編程通常在 IP 層以上進行,一般不關心 MAC 地址。我們的電腦也不可能與 CSDN 博客服務器直接相連,一般要經過很多中間節點(hops)。根據鄰居協議,目的 MAC 地址為下一跳(next hop,[default] gateway/Router)的 MAC 地址。
通過 Network Utility 或“route -n get default”或“netstat -rn”命令可獲得默認網關為 10.64.66.1。通過“arp -a”顯示“? (10.64.66.1) at0:0:c:7:ac:24 on en1 ifscope [ethernet]”。

(2)Destination IP 為 10.14.36.100
按道理,目的 IP 地址應該為 CSDN 博客服務器(blog.csdn.net)主機的 IP117.79.157.201),為什么這里的卻為 A 類私網地址(10.14.36.100)呢?

這是因為內網使用了 HTTP Proxy,10.14.36.100 是 proxy.pac(參考:原理 、 腳本 、設置 、自動化)基於域名判斷出口返回的 Proxy Server 的IP地址。

 

###############################################################

No 251: 收包(HTTP/1.1 200 OK)的目的 MAC/IP 地址為接收方(本機)的 MAC/IP 地址。

(1)源 IP 地址為 Proxy Server 的 IP 地址,同上。

(2)源 MAC 地址一般應為【默認】網關的 MAC 地址(進出都要過關),實際抓包卻發現 MAC 地址並非【默認】網關的MAC地址,這是什么情況呢?

通過 Windows 的“route print”或 Mac OS X/UNIX 下的 “netstat -r”命令可查看路由表(Routing Tables)信息。通過 Windows 下的“tracert  10.14.36.100”或 Mac OS X/UNIX 下的“traceroute 10.14.36.100”命令可查看本機到代理 10.14.36.100 之間的路由信息,第一跳並非默認網關(10.64.66.1),而是10.64.66.2!

這是因為采用了熱備路由(HSRP),HSRP 協議利用優先級決定哪個路由器成為活動路由器,收包的那個陌生源 MAC 地址即是熱備路由(10.64.66.2)的 MAC 地址!

 

11.在 Mac OS X 上安裝體驗最新 Wireshark 2.0.0

升級到 XQuartz-2.7.8 后,貌似 Wireshark 出現了無法發現網卡的問題,索性卸載了之前通過 dmg with pkg 安裝的 Wireshark 1.12.0,完整的卸載指令可參考 complete uninstall  instructions

Wireshark 官網 於 18 November,2015 發布了 Stable Release (2.0.0),可前往下載 OS X 10.6 and later Intel 64-bit .dmg  安裝包進行安裝體驗。這次,我使用 brew-cask 來安裝 wireshark 軟件,完整的安裝日志可參考《brew-cask install wireshark.log》。

鑒於 Mac OS X 在 Mountain Lion 之后已不再自帶 X11,新版的 Wireshark 2.0.0 也摒棄了陳舊的 GTK+X11 界面,取而代之的是與 Windows 版保持一致的 Qt 界面,簡潔清爽。

 

“Wireshark’s Debian- and RPM-based package definitions provide the new interface in the “wireshark-qt” package and the old interface in the “wireshark-gtk” package. 

The OS X installer only provides the new interface.”

—— Wireshark 2.0.0 Release Notes - What’s New

 

以下選取了啟動界面和針對本文(Capture Filter 為 host blog.csdn.net,Display Filter 可選 http.host == blog.csdn.net)的通信抓包截圖,以饗讀者一睹為快。

 

 

 

 

若運行 wireshark.app 彈窗提示“You don't have permission to capture on that device”:

則可考慮使用 root 賬戶啟動 wireshark:

 

[plain]  view plain  copy
 
 
 
  1. faner@MBP-FAN:~|⇒  cd Applications   
  2. faner@MBP-FAN:~/Applications|⇒  sudo wireshark   

 

參考:

Getting a Packet Trace with Sniffing Tools under OS X

 

Wireshark 數據包分析實戰

Wireshark capture under Mac OS X

Launching Wireshark 1.10.0 on Mac OS X Mountain Lion

Mac OS X Lion使用Wireshark遠程抓包

 

在MAC電腦上抓取iPhone數據包的方法

iOS APP網絡分析之RVI》《在iOS設備上使用RVI進行抓包

iOS聯網在MAC機器上進行抓包》《使用數據線+RVI調試iPhone網絡流量

升級iOS7后利用RVI和Wireshark抓包失效?》《Mavericks- cannot capture from iPhone using RVI

 

Wireshark抓包針對無線網卡的設置》《使用Wireshark抓取無線網卡的數據包

About Wireless Diagnostics》 《OSX Lion Wi-Fi Diagnostics

騰訊 GT:APP 性能隨身調測工具》《Android 實時抓包分析:善用 adb 調試橋進行 tcpdump 抓包

 

蹭WiFi那點破事》《淺析無線網絡數據窺探技術

Mac OS安裝Aircrack》《Aircrack 無線破解詳細解說》《Aircrack-ng破解WEP、WPA-PSK加密利器

BT5 + Wireshark玩wifi捕獲和中間人攻擊》《BT5 aircrack-ng破解無線密碼(wpa/wep)

 

安裝參考了這篇文章 但實際上按照本文操作即可
https://theagileadmin.com/2017/05/26/tcpdump-and-wireshark-on-osx/

步驟:

brew cask install wireshark-chmodbpf

下載並安裝(需要權限,輸入密碼即可)

實際上並沒有圖標,只需要進入該文件夾
/Users/用戶名/Library/Caches/Homebrew/Cask/
找到
wireshark-chmodbpf--2.4.1.dmg

打開再安裝一次即可安裝成功,且有應用圖標。

卸載辦法1:重新打開wireshark-chmodbpf--2.4.1.dmg參考


辦法2

sudo chown {your name} /dev/bpf*
sudo open  /Applications/Wireshark.app


 
鏈接:https://www.jianshu.com/p/89b2ccd772b9
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM