资源预览内容
第1页 / 共8页
第2页 / 共8页
第3页 / 共8页
第4页 / 共8页
第5页 / 共8页
第6页 / 共8页
第7页 / 共8页
第8页 / 共8页
亲,该文档总共8页全部预览完了,如果喜欢就下载吧!
资源描述
实用文档用lua语言编写Wireshark插件解析自定义协议(y001115922012-01)Wireshark默认支持大量网络协议,我们可以在 Wireshark主菜单Internals Support Protocols (slow!)”查看当前支持的所有协议。 Wireshark 1.6.2版本已支持1170种协议,包括 我们熟悉的 Diameter、GTP、FTP、SCTP等协议。上述都是业界的通用协议,然而对我们产品的自定义协议,Wireshark是不得而知的,抓包后只能看到一串二进制码流,导致开发调试或者测试分析效率较低。有什么办法让 Wireshark也能解析自定义协议呢?本文介绍的用lua编写Wireshark解析器插件就是一种常用方法。 Wireshark软件内嵌一个lua语言执行引擎并提供一系列 lua函数 接口,从而满足用户各种各样的协议解析目的。下面以一个简单的自定义协议为例,演示如何编写Wireshark解析插件。不过本文只是业余学习的笔记和简单实践,不能覆盖“ lua编写Wireshark解析器插件”的所有知识点(更 全面的介绍请参考 Wireshark软件自带手册第11章“Lua Support in Wireshark,只是开源软件的手册似 乎总是不够详尽。)。1 .配置 Wireshark执行lua脚本通过 Wireshark主菜单Help About Wireshark ”可以查看当前安装版本已经内嵌Lua 5.1执行引擎。默认安装情况下Wireshark会在安装路径下生成一个init.lua ,它是 Wireshark启动过程执行的第一个lua脚本。一般来说,我们可以在此文件中添加dofile函数调用其他lua脚本,实现各种扩展目的。例如,默认安装后init.lua文件末尾有一句:dofile(DATA_DIR.console.lua) 此语句执行了安装目录下的console.lua脚本,该脚本也是 Wireshark自带的,用途是在主菜单Tool下创建一个子菜单Lua。我们甚至可以在上述语句之前添加注释-符来取消它。dofile是lua基础库提供的一个函数,用途如下: dofile (filename)Opens the named file and executes its contents as a Lua chunk. When called without arguments, dofile executes the contents of the standard input (stdin). Returns all values returned by the chunk. In case of errors, dofile propagates the error to its caller (that is, dofile does not run in protected mode).我们下来编写的解析器也是以.lua脚本形式保存,然后在 init.lua文件末尾中添加一个dofile调用即可。2 .需要解析的自定义协议假设我们需要基于 UDP协议实现一种根据员工ID查询员工姓名的信息服务,客户端向服务器端的12345端口发送QueryRequest,服务器返回应答 QueryResponse,数据结构如下:消息名称消息结构QueryRequestUint16 usMsgType;消息类型,查询请求=0Uint32 uiEmployeeID; 员工工号QueryResponseUint16 usMsgType;消息类型,查询应答 =1Uint32 uiEmployeeID;员工工号Uint16 usQueryResult;查询操作结果,成功 =0,失败=1Char32 szEmployeeName;/查询结果,如果成功,填写员工姓名;否则填写失败原因假设员工数据库信息有两条记录如下:员工ID员工姓名1Liu Dehua2Zhang Xueyou将上述协议实现之后,服务器端部署在192.168.56.1,客户端在192.168.56.22。以下就是 启动1次查询过程的Wireshark抓包结果:压 Frame 3: 48 byt片f OH 明什已 C3B44B bytes Captured (3B4 bits)H Ethernet IIB Src: Q8:00:27:fS:35 :22 (08:00:27:f6:35 :22). 口st: 08:00:27:00:14:3c (08:00:27:00:14 :3cE Internet Protocol Versiorn 4, rc: 12.168 56*22 (192A6B. 56. 22)# Dst: 192,168- 56-1 Cl52BL68a 56.1)tt User Datagram Protocol k src Porx; 234 5$ (2 3-3 56), Dst Port: 124 5 (1234 5)Data bytes)Data: 000001000000Length: 6上图是 QueryRequest 消息,uEmployeelD 指定为 1。王 Frame 4: 32 bytes on wire (656 bits), 82 bytes captured C65S tHts)4 ET her net II, Src: da:DO:27:QO:U :3C (DB:0O:27:D0:14:3C)p CST : D8:CO:27:fS:35:22 (00:00:27:133:35 :22) xi Internet Protocol Version 4, Src: 192.168, 56.L C192.168r 56,1, D5t: 192,168, 56. 22 Cl92.168.56.22)3 user oat agram Pratocclh srt port: 12345 (123d5). det port:&56 (23J 56)-Data 0。bytesjData: 01000100000000004c697520446560756100000000000000,LengtfiF 4 0上图是QueryResponse消息,返回uEmployeeID=1的查询结果。从上面2个图可见,Wireshark已经自动地把以太网帧、IP报文、UDP数据报都解析出 来,唯独我们自定义协议的消息无法解析,以Data来显示。把上述抓包结果保存到一个pcap文件中。下面我们逐步实现对该协议的解析。3. Wireshark 的 lua 函数接口3.1. Proto 协议在Wireshark中Proto主要用途是声明一个新的协议,进而可以给它编写解析器函数。3.1.1. Proto.new 声明一个新的协议例如:my_proto = Proto (myProto,My Protocol for Query Employee,My Self-defined Protocol)3.1.2. proto.dissector 解析器函数例如:function my_proto.dissector(buffer,pinfo,tree)pinfo.cols.protocol:set(myProto)pinfo.cols.info:set(This is a message of myProto)enddissector函数的第一个参数 buffer对应需要解析的二进制码流,第二个参数pinfo对应PacketList窗口的信息,tree是Packet Details窗口的树结构,如下图所示:pcapVice shark 1.6.2(SVH Rev 38931 fra /trunh-1.日Ie Edit iew $ aptue Analyze StatisticsTelephony 工ooh ntemals y,elpH B 0 JC 昌/电看量回亘1 0 d触已1例回烟施 Filltr.v Expressi on. . 门/研ApplyWqsTiSource。.与 ti 4.ti qnPratccilLm容th Xnfc1 1326041063-0158102 l192a 16SB 5 6a 22192.168. 56.1UDPW昌 Source port: 29&5 763T192.160.5S.22UDP827?0tlm port: i3 1326041105.780939 192.168,5 6, 22168. 56,111fdp48 source port: 24 1326CM11O5.802222 瓯 5立 1192168,82 Source porr; 15 1326D4LL08.50D716 192.169.56.22192.168. 56.1P48 Source port: 26 1326041108.901966 192.168.56.1192.168.56.22UDP82 Source port: i.- -1+. rramc 1 :上各 t/TES on _1 re f33i1 tlTSJ. 43 byr5 caprured -C3 bltr)- Ethernet II. 5rj CadmusC O_f S: 3 5 ; ? 2 (O8:O0:27:fS;35 :22n D5t : each us Co. .00 :14 : 3c S自:QQ:”;OQ:1 a internet Protocol Version 4, Src: 122.168.56.22 C192.168. 56.22, Dst: :.92.16S. 56.1 (1S2.168.56 +i user Datagram proroccl, sre Fort: 23456 Q345), D5t Fort: -italk (1234 0+i 口as ( byre53tree文案大全bufferWireshark已经为我们提供了处理这三个参数的接口了。有关 buffer的lua API ,可以参 考官方手册”11.12. Functions for handling packet data”,如下图所示:Functions for handling packet dat日+ 电 ByteArrap国 Irit田 Tvb+ TvbR5nge* 茴 Ulnt有关 pinfo 的 lua API ,可以参考官方手册”11.9. Obtaining packet information ”,如下图所Obtaining packet information+ Address+ Column +,Ldlumns + Pinfo有关tree的lua API ,可以参考官方手册“11.11. Adding information to the dissection tree如下图所示:-IjJ Adding information to the di$ection
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号