手把手教你編譯wireshark3.1的c語(yǔ)言插件(windows平臺(tái)2019-3-20)

手把手教你編譯wireshark3.1的c語(yǔ)言插件(windows平臺(tái)2019-3-20)

資料來(lái)源:
https://www.wireshark.org/docs/wsdg_html_chunked/ChSetupWin32.html (官方教程)
http://eucifyy.com/wireshark-dissector-mwe.html (python代碼)

簡(jiǎn)介

wireshark是一個(gè)著名的網(wǎng)絡(luò)嗅探軟件,前陣子央視著名的315晚會(huì)也有一個(gè)教授使用wireshark向我們展示某些不法app非法獲取用戶隱私的過(guò)程。在wireshark中用戶可以自行編寫(xiě)插件來(lái)做自定義的協(xié)議的解析器。有兩種途徑,一種是c插件的方式,在windows平臺(tái)下,體現(xiàn)為一個(gè)動(dòng)態(tài)鏈接庫(kù).dll文件,放在wireshark插件目錄下,這個(gè)c插件的優(yōu)點(diǎn)是速度快,但缺點(diǎn)也很明顯,編譯工程量大,兼容性差,你這個(gè)版本下編譯的插件,到另一個(gè)版本的wireshark下就完全不能用了。還有一個(gè)是lua腳本的方式,這個(gè)十分方便,兼容性也好,制作也簡(jiǎn)單,網(wǎng)上教程也一大堆,但不知道為什么,wireshark官方并不推薦這個(gè)??傊覀冞€是按照wireshark官方的教程來(lái)編譯c插件吧。

步驟一、編譯一次wireshark源碼

這個(gè)步驟十分繁瑣,但好在官方寫(xiě)的十分詳細(xì):https://www.wireshark.org/docs/wsdg_html_chunked/ChSetupWin32.html。按照官方說(shuō)好的編譯完一遍。這樣你就安裝好了環(huán)境,同時(shí)編譯出了不少目標(biāo)文件,這些目標(biāo)文件待會(huì)編譯插件的時(shí)候要用。

步驟二、編譯c插件

1.創(chuàng)建源文件

按照這個(gè)https://www.wireshark.org/docs/wsdg_html_chunked/ChDissectAdd.html
創(chuàng)建wireshark\plugins\epan\foo目錄,并在里面寫(xiě)出packet-foo.c文件(這個(gè)foo是你的插件名)

#include "config.h"

#include <epan/packet.h>

#define FOO_PORT 1234

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 out stuff in 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"       /* abbrev     */
        );
}
void proto_reg_handoff_foo(void)
{
    static dissector_handle_t foo_handle;

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


2.復(fù)制其他插件文件作為模板

之后將wireshark\plugins目錄下的plugin.rc.in文件復(fù)制進(jìn)foo目錄,以及wireshark\plugins\epan\gryphon目錄下的CMakeLists.txt也復(fù)制進(jìn)foo目錄。

3.修改插件信息

使用記事本打開(kāi)CMakeLists.txt,將里面的“gryphon”文本都替換為foo。

4.修改自制插件配置

進(jìn)入wireshark目錄,把里面的“CMakeListsCustom.txt.example”重命名為“CMakeListsCustom.txt”,打開(kāi)CMakeListsCustom.txt,去掉里面的某個(gè)"#",使得"set(CUSTOM_PLUGIN_SRC_DIR plugins/epan/foo)"這樣一個(gè)語(yǔ)句完整,這里的plugins/epan/foo就是你的插件的目錄。

set(CUSTOM_PLUGIN_SRC_DIR
    plugins/epan/foo
)

# Do not fail CMake stage if any of the optional plugins are missing from source tree
set(_OPTIONAL_CUSTOM_PLUGIN_SRC_DIR
    plugins/epan/bar
)

foreach(  _plugin_dir ${_OPTIONAL_CUSTOM_PLUGIN_SRC_DIR} )
    if( EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${_plugin_dir}/CMakeLists.txt )
        list( APPEND CUSTOM_PLUGIN_SRC_DIR ${_plugin_dir} )
    else()
        message( WARNING "Custom plugins: No ${_plugin_dir}/CMakeLists.txt file found - ignoring" )
    endif()
endforeach()

步驟四、重新編譯wireshark源碼

這次的重新編譯,比初次編譯要快不少,原因就是初次編譯以及生成了足夠了目標(biāo)文件,這次編譯只要編譯你這個(gè)插件就行了。
編譯完后你會(huì)在你的build目錄下run\RelWithDebInfo\plugins\3.1\epan里看到foo.dll,這樣就算編譯成功了。


捕獲2.JPG

步驟五、啟動(dòng)wireshark并測(cè)試

啟動(dòng)run\RelWithDebInfo下的wireshark.exe,使用python寫(xiě)一個(gè)基于foo協(xié)議的程序并測(cè)試,腳本如下:

# server.py
from socket import *

serverSocket = socket(AF_INET, SOCK_DGRAM) # UDP
serverSocket.bind(('', 1234))

while True:
    message, address = serverSocket.recvfrom(1024) # buffer size
    serverSocket.sendto('thanks', address)
# client.py
import time
from socket import *

clientSocket = socket(AF_INET, SOCK_DGRAM)
clientSocket.settimeout(1)
message = 'test'
addr = ('127.0.0.1', 1234)

start = time.time()
clientSocket.sendto(message, addr)
try:
    data, server = clientSocket.recvfrom(1234)
    end = time.time()
    elapsed = end - start
    print '%s %d' % (data, elapsed)
except timeout:
    print 'REQUEST TIMED OUT'

注意以上腳本必須用python2來(lái)運(yùn)行,如果你已經(jīng)安裝了python3,你可以再安裝一個(gè)python2并在bin目錄里面添加如下內(nèi)容的python2.bat:

@echo off
%~dp0/python.exe %*

將bin目錄添加到環(huán)境變量path即可通過(guò)python2命令執(zhí)行python2。
還需要注意的是,按照默認(rèn)配置編譯的wireshark并不支持監(jiān)聽(tīng)127.0.0.1這種本機(jī)回環(huán)地址。你需要安裝一個(gè)Npcap loopback adapter(https://nmap.org/download.html)
來(lái)使得wireshark能夠監(jiān)聽(tīng)該地址。
分別打開(kāi)兩個(gè)終端執(zhí)行如下命令

>python2 server.py
>python2 client.py

如果以上命令成功,你的client窗口會(huì)收到一個(gè)thanks的返回結(jié)果,你可以通過(guò)過(guò)濾器設(shè)置udp.port==1234來(lái)獲得類似如下嗅探結(jié)果(為了測(cè)試,我沒(méi)有采用本機(jī)回環(huán)地址,而是將server在另一臺(tái)機(jī)子上執(zhí)行):


捕獲.JPG

這樣,一個(gè)wireshark的c語(yǔ)言插件就初步編好了,需要更加詳細(xì)的協(xié)議解析器(dissector)編程方法,可以去看官方的技術(shù)文檔:https://www.wireshark.org/docs/wsdg_html_chunked/ChDissectAdd.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容