手把手教你編譯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非法獲取用戶(hù)隱私的過(guò)程。在wireshark中用戶(hù)可以自行編寫(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阵翎,這樣就算編譯成功了逢并。
步驟五之剧、啟動(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)獲得類(lèi)似如下嗅探結(jié)果(為了測(cè)試,我沒(méi)有采用本機(jī)回環(huán)地址许饿,而是將server在另一臺(tái)機(jī)子上執(zhí)行):
這樣阳欲,一個(gè)wireshark的c語(yǔ)言插件就初步編好了,需要更加詳細(xì)的協(xié)議解析器(dissector)編程方法陋率,可以去看官方的技術(shù)文檔:https://www.wireshark.org/docs/wsdg_html_chunked/ChDissectAdd.html