1. 程式人生 > 其它 >WireShark編譯C語言.dll外掛全程(詳細)

WireShark編譯C語言.dll外掛全程(詳細)

一.編譯一遍WireShark原始碼

參考:https://www.wireshark.org/docs/wsdg_html_chunked/ChSetupWin32.html

1.1安裝預備環境

首先安裝Chocolately,類似ubuntu的apt-get,用這個省力很多

使用管理員許可權開啟windows powershell,執行下面指令

PS$>Set-ExecutionPolicy AllSigned
PS$>Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

安裝完成後,繼續使用powershell視窗進行下面操作。

1.1.1安裝VS2019 Community(已安裝的請跳過,或者也可以直接去微軟官方下載)

PS$> choco install -y visualstudio2019community visualstudio2019-workload-nativedesktop

在Visual Studio Installer勾選如下元件:

(1)選中C++桌面開發

(2)

1.1.2 安裝QT

推薦安裝QT5.15.2(官方教程裡使用的版本,現在QT出到6了不知道能不能使)

https://download.qt.io/official_releases/online_installers/

下載.exe,選中Qt 5.15.2,必須勾選msvc2019_64,Qt Debug Information Files,其餘隨意

推薦預設目錄,空間不足也可安裝至其他碟符,最好在根目錄下

1.1.3安裝Python3

PS$> choco install -y python3

執行即可,也可官網安裝,儘量安裝在預設目錄

Py官方安裝的預設目錄是在你的使用者資料夾下,而WireShark希望你安裝在C盤根目錄下,所以推薦choco安裝

1.1.4安裝Perl

Strawberry和Active二選一即可,如果後面編譯報錯了提示perl奔潰,解除安裝然後回來重灌

PS$> choco install -y strawberryperl
# ...or...
PS$> choco install -y activeperl

1.1.5 安裝Git,用來獲取WireShark原始碼

PS$> choco install -y git

1.1.6安裝CMake

PS$> choco install -y cmake

1.1.7 安裝Asciidoctor, Xsltproc, DocBook

安裝Asciidoctor需要一個java runtime,可以自選一種,或者直接按照下面的來就行

PS$>choco install -y zulu
PS$> choco install -y asciidoctorj xsltproc docbook-bundle

1.1.8 安裝winflexbison

PS$> choco install -y winflexbison3

1.2 開始編譯

在c盤根目錄下建立資料夾"Development"

啟動一個CMD命令列

>cd C:\Development
>git clone https://gitlab.com/wireshark/wireshark.git

克隆完成後,點選開始選單,找到Visual Studio 2019->x64 Native Tools Command Prompt for VS 2019

使用Vs2019的命令列視窗,全程不要關閉,執行以下操作:

> set WIRESHARK_BASE_DIR=C:\Development\wireshark
> rem set WIRESHARK_LIB_DIR=c:\wireshark-win64-libs
> set QT5_BASE_DIR=C:\Qt\5.15.2\msvc2019_64
WIRESHARK_BASE_DIR指的是wireshark原目錄,clone到哪填哪
QT5_BASE_DIR是你的QT5編譯器目錄,若不是預設安裝,填入相應的目錄即可

接著建立資料夾wsbuild64,
> mkdir C:\Development\wsbuild64
> cd C:\Development\wsbuild64

執行

> cmake -G "Visual Studio 16 2019" -A x64 ..\wireshark

提示以下資訊:

-- Configuring done
-- Generating done
-- Build files have been written to: C:/Development/wsbuild64

即構建成功

開始編譯

> msbuild /m /p:Configuration=RelWithDebInfo Wireshark.sln

若C:\Development\wsbuild64\run\RelWithDebInfo\下有Wireshark.exe,可執行,第一階段結束

2.編譯外掛

建立wireshark\plugins\epan\foo目錄,並在該目錄建立packet-foo.c檔案(這個foo是你的外掛名)

//這段程式碼僅供參考與演示,只能顯示個協議名。TCP協議埠號43300

編寫外掛的教程勞煩各位百度,之後我可能也寫一篇關於編寫C外掛的隨筆

#include "config.h"
#include <epan/packet.h>

#define FOO_PORT 43300

static int proto_foo = -1;

static int
dissect_foo(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree _U_, void *data _U_)
{
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "FOO");
    /* Clear the info column */
    col_clear(pinfo->cinfo,COL_INFO);

    return tvb_captured_length(tvb);
}

void
proto_register_foo(void)
{
    proto_foo = proto_register_protocol (
        "FOO Protocol", /* name        */
        "FOO",          /* short_name  */
        "foo"           /* filter_name */
        );
}

void
proto_reg_handoff_foo(void)
{
    static dissector_handle_t foo_handle;

    foo_handle = create_dissector_handle(dissect_foo, proto_foo);
    dissector_add_uint("tcp.port", FOO_PORT, foo_handle);
}

找到wireshark原始碼目錄下的CMakeListsCustom.txt.example重新命名為CMakeListsCustom.txt

找到這樣一段:

1 # Fail CMake stage if any of these plugins are missing from source tree
2 set(CUSTOM_PLUGIN_SRC_DIR
3 #    private_plugins/foo
4 # or
5 #    plugins/epan/foo
6 )

去掉這裡第5行的井號,如下

1 # Fail CMake stage if any of these plugins are missing from source tree
2 set(CUSTOM_PLUGIN_SRC_DIR
3 #    private_plugins/foo
4 # or
5     plugins/epan/foo
6 )

儲存即可,這裡的foo就是你的協議名稱

將wireshark\plugins下的plugin.rc.in檔案複製進foo目錄,同時wireshark\plugins\epan\gryphon目錄下的CMakeLists.txt複製到foo目錄

將複製來的CMakeLists.txt開啟,將文字中的“gryphon”都替換為"foo"

開啟你沒關閉的x64 Native Tools Command Prompt for VS 2019視窗(若關了再開啟就行cd到wsbuild64)

再次執行

> msbuild /m /p:Configuration=RelWithDebInfo Wireshark.sln

若C:\Development\wsbuild64\run\RelWithDebInfo\plugins\3.5\epan下有一個foo.dll就說明成功了!

若失敗了,執行

>msbuild /m /p:Configuration=RelWithDebInfo Wireshark.sln /t:Clean

再執行上面那條指令,若還是報錯,請順著這篇文章找錯

3.測試

開啟C:\Development\wsbuild64\run\下的Wireshark.exe

進行抓包,這裡我使用了兩個虛擬機器,一個烏班圖一個kali,nat連線,主機當閘道器,抓取對應的虛擬網絡卡

kali當伺服器,ubuntu當客戶端,使用python3網路程式設計,從菜鳥教程抄一段程式碼

ip都是服務端的ip,測試前請確保你的網路拓撲正確,都能ping通

kali下建立指令碼

 1 #coding=utf-8
 2 #!/usr/bin/python3
 3 # 檔名:server.py
 4 
 5 # 匯入 socket、sys 模組
 6 import socket
 7 import sys
 8 
 9 # 建立 socket 物件
10 serversocket = socket.socket(
11             socket.AF_INET, socket.SOCK_STREAM) 
12 
13 # 獲取本地主機名
14 host = '192.168.226.140'
15 
16 port = 43300
17 
18 # 繫結埠號
19 serversocket.bind((host, port))
20 
21 # 設定最大連線數,超過後排隊
22 serversocket.listen(5)
23 
24 while True:
25     # 建立客戶端連線
26     clientsocket,addr = serversocket.accept()      
27 
28     print("連線地址: %s" % str(addr))
29     
30     msg='歡迎訪問菜鳥教程!'+ "\r\n"
31     clientsocket.send(msg.encode('utf-8'))
32     clientsocket.close()

ubuntu下建立指令碼

 1 #!/usr/bin/python3
 2 # 檔名:client.py
 3 
 4 # 匯入 socket、sys 模組
 5 import socket
 6 import sys
 7 
 8 # 建立 socket 物件
 9 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
10 
11 
12 host = '192.168.226.140' 
13 
14 # 設定埠號
15 port = 43300
16 
17 # 連線服務,指定主機和埠
18 s.connect((host, port))
19 
20 # 接收小於 1024 位元組的資料
21 msg = s.recv(1024)
22 
23 s.close()
24 
25 print (msg.decode('utf-8'))

先執行服務端指令碼,然後開啟主機wireshark開始抓包,再執行客戶端指令碼

如下即抓包成功: