使用lua给wireshark编写uTP的Dissector
- xx_protocol.dissector = function(buffer,pinfo,tree)
- --定义这个协议的解析函数,最后会将这个函数注册到wireshark用来解析数据的表中。这个函数的三个参数很重要,是wireshark引擎在调用该函数是会传入,
- --buffer就是我们要分析的数据,
- --pinfo记录了前面分析过协议留下的信息,
- --tree是用来在详细框中添加我们信息的结构。
- do
- -- Desc: uTP Protocol lua version
- -- Author: WangYao, ipconfigme@gmail.com
- -- Date: 2011/10/19
- -- protol name
- local p_utp = Proto("utp", "Micro Transport Protocol");
- -- protocol fields
- local f_version = ProtoField.uint8("utp.version", "Version", base.DEC,
- {[1]="V1"}, 0x0F)
- local f_type = ProtoField.uint8("utp.type", "Type", base.DEC,
- {[0]="ST_DATA", [1]="ST_FIN", [2]="ST_STATE", [3]="ST_RESET", [4]="ST_SYN"}, 0xF0)
- local f_next_extension_type = ProtoField.uint8("utp.next_extension_type", "Next Extension Type", base.DEC,
- {[0]="No Extension", [1]="Selective acks", [2]="Extension bits"})
- local f_extension_len = ProtoField.uint8("utp.extension_len", "Extension Length", base.DEC)
- local f_extension_bitmask = ProtoField.bytes("utp.extension_bitmask", "Extension Bitmask", base.NONE)
- local f_connection_id = ProtoField.uint16("utp.connection_id", "Connection_ID", base.DEC)
- local f_timestamp_microseconds = ProtoField.uint32("utp.timestamp_microseconds", "timestamp_microseconds", base.DEC)
- local f_timestamp_difference_microseconds = ProtoField.uint32("utp.timestamp_difference_microseconds", "timestamp_difference_microseconds", base.DEC)
- local f_wnd_size = ProtoField.uint32("utp.wnd_size", "wnd_size", base.DEC)
- local f_seq_nr = ProtoField.uint16("utp.seq_nr", "seq_nr", base.DEC)
- local f_ack_nr = ProtoField.uint16("utp.ack_nr", "ack_nr", base.DEC)
- p_utp.fields = {f_version, f_type, f_next_extension_type, f_extension_len, f_extension_bitmask, f_connection_id, f_timestamp_microseconds, f_timestamp_difference_microseconds, f_wnd_size, f_seq_nr, f_ack_nr}
- -- other dissector
- local data_dis = Dissector.get("data")
- local bittorrent_dissector = Dissector.get("bittorrent.tcp")
- -- utp dissector, return OFFSET
- local function utp_dissector(buf,pkt,root)
- local buf_len = buf:len()
- local offset = 0
- -- check pack len
- if buf_len < 20 then return 0 end
- -- get fields
- local v_version = buf(offset, 1)
- local v_type = buf(offset, 1)
- offset = offset + 1
- local v_next_extension_type = buf(offset, 1)
- offset = offset + 1
- local v_connection_id = buf(offset, 2)
- offset = offset + 2
- local v_timestamp_microseconds = buf(offset, 4)
- offset = offset + 4
- local v_timestamp_difference_microseconds = buf(offset, 4)
- offset = offset + 4
- local v_wnd_size = buf(offset, 4)
- offset = offset + 4
- local v_seq_nr = buf(offset, 2)
- offset = offset + 2
- local v_ack_nr = buf(offset, 2)
- offset = offset + 2
- -- check uTP
- local i_version = bit.band(v_version:uint(), 0x0F)
- -- local i_type = bit.band(bit.rshift(v_type:uint(), 4), 0x0F)
- local i_type = bit.rshift(bit.band(v_type:uint(), 0xF0), 4)
- if( (i_version~=1) or (i_type~=0 and i_type~=1 and i_type~=2 and i_type~=3 and i_type~=4))
- then return 0 end
- local subtree = root:add(p_utp, buf(),"Micro Transport Protocol")
- -- just add header
- subtree:add(buf(0,0),"uTP Header: ")
- subtree:add(f_version, v_version)
- subtree:add(f_type, v_type)
- subtree:add(f_next_extension_type, v_next_extension_type)
- subtree:add(f_connection_id, v_connection_id)
- subtree:add(f_timestamp_microseconds, v_timestamp_microseconds)
- subtree:add(f_timestamp_difference_microseconds, v_timestamp_difference_microseconds)
- subtree:add(f_wnd_size, v_wnd_size)
- subtree:add(f_seq_nr, v_seq_nr)
- subtree:add(f_ack_nr, v_ack_nr)
- -- add pkt info
- pkt.cols.protocol = "uTP"
- if(i_type==0) then
- pkt.cols.info = "uTP ST_DATA"
- elseif(i_type==1) then
- pkt.cols.info = "uTP ST_FIN"
- elseif(i_type==2) then
- pkt.cols.info = "uTP ST_STATE"
- elseif(i_type==3) then
- pkt.cols.info = "uTP ST_RESET"
- elseif(i_type==4) then
- pkt.cols.info = "uTP ST_SYN"
- else
- pkt.cols.info = "uTP UNKNOW"
- end
- while(v_next_extension_type:uint()~=0) do
- -- add extension tree
- local extendtree = subtree:add(p_utp, buf(offset, buf_len-offset):tvb(),"Extension")
- if(v_next_extension_type:uint()==0) then
- extendtree:append_text(": NO Extension")
- elseif(v_next_extension_type:uint()==1) then
- extendtree:append_text(": Selective acks")
- elseif(v_next_extension_type:uint()==2) then
- extendtree:append_text(": Extension bits")
- end
- v_next_extension_type = buf(offset, 1)
- offset = offset + 1
- extendtree:add(f_next_extension_type, v_next_extension_type)
- local v_extension_len = buf(offset, 1)
- offset = offset + 1
- extendtree:add(f_extension_len, v_extension_len)
- local i_extension_len = v_extension_len:int()
- local v_extension_bitmask = buf(offset, i_extension_len)
- offset = offset + i_extension_len
- extendtree:add(f_extension_bitmask, v_extension_bitmask)
- end
- return offset
- end
- -- check packet is bittorrent? header legal
- local function check_bittorrent(buf)
- local len = buf:len()
- local pack_len = buf(0,4)
- local pack_type
- if(len<4) then
- return false
- elseif(buf(0,1):uint()==19 and len==68) then --handshake
- return true
- elseif(len==4) then --keepalive
- if(pack_len:uint()==0) then return true
- else return false
- end
- else
- pack_type = buf(4,1)
- --choke, unchoke, interested, not interested, have all, have none
- if(pack_type:uint()==0 or pack_type:uint()==1 or pack_type:uint()==2 or pack_type:uint()==3 or pack_type:uint()==0x0E or pack_type:uint()==0x0F) then
- if(pack_len:uint()==1) then return true
- else return false
- end
- --request, cancel, reject
- elseif(pack_type:uint()==6 or pack_type:uint()==8 or pack_type:uint()==0x10) then
- if(pack_len:uint()==13) then return true
- else return false
- end
- --port
- elseif(pack_type:uint()==9) then
- if(pack_len:uint()==3) then return true
- else return false
- end
- --have, suggest, allowed fast
- elseif(pack_type:uint()==4 or pack_type:uint()==0x0D or pack_type:uint()==0x11) then
- if(pack_len:uint()==5) then return true
- else return false
- end
- --bitfield, extend
- elseif(pack_type:uint()==5 or pack_type:uint()==0x14) then
- if(pack_len:uint()<1024) then return true
- else return false
- end
- --piece, max than 16K
- elseif(pack_type:uint()==7) then
- if(pack_len:uint()>=16384) then return true
- else return false
- end
- else
- return false
- end
- end
- return false
- end
- -- protocol dissector, include bittorrent
- function p_utp.dissector(buf,pkt,root)
- local len = buf:len()
- local offset = utp_dissector(buf, pkt, root)
- if len>offset and offset>0 then
- -- call bittorrent dissector
- -- pass split PIECE pack
- if check_bittorrent(buf(offset, len-offset)) then
- bittorrent_dissector:call(buf(offset, len-offset):tvb(), pkt, root)
- else
- data_dis:call(buf(offset,len-offset):tvb(), pkt, root)
- end
- elseif offset==0 then
- -- call data dissector
- data_dis:call(buf,pkt,root)
- end
- end
- -- add to DissectorTable
- local udp_table = DissectorTable.get("udp.port")
- -- udp_table:add(4135, p_utp)
- udp_table:add(10000, p_utp)
- end
使用lua给wireshark编写uTP的Dissector的更多相关文章
- Lua 学习 chapter30 编写c函数的技巧 - Jow的博客
目录 数组操作 字符串操作 在c函数中保存状态 生活总需要一点仪式感,然后慢慢的像那个趋向完美的自己靠近. 数组操作 Lua中的数组就是以特殊的方式使用边.像lua_setttable and lua ...
- FCEUX金手指加强版 - 使用Lua脚本语言编写FC/NES金手指脚本
一直觉得大部分的FC/NES模拟器的作弊码金手指不是那么方便使用, 比如魂斗罗1代, 玩家的武器可以通过修改0xAA的值来改变: 0x11为M弹(重机枪),0x12为F弹(圈圈),0x13为S弹(散弹 ...
- Lua编写wireshark插件初探——解析Websocket上的MQTT协议
一.背景 最近在做物联网流量分析时发现, App在使用MQTT协议时往往通过SSL+WebSocket+MQTT这种方式与服务器通信,在使用SSL中间人截获数据后,Wireshark不能自动解析出MQ ...
- Wireshark lua dissector 对TCP消息包合并分析
应用程序发送的数据报都是流式的,IP不保证同一个一个应用数据包会被抓包后在同一个IP数据包中,因此对于使用自制dissector的时候需要考虑这种情况. Lua Dissector相关资料可以见:ht ...
- 【wireshark】插件开发(二):Lua插件开发介绍
1. Wireshark对Lua的支持 本节相关内容可参考Wireshark开发指南第10章”Lua Support in Wireshark”. Wireshark集成了Lua解释器,以支持Lua脚 ...
- Lua语言在Wireshark中使用(转)
1. 检查Wireshark的版本是否支持Lua 打开Wireshark,点击“HelpàAbout Wireshark”菜单,查看弹出的对话框,如果有“with Lua 5.1”表示支持 ...
- Wireshark使用drcom_2011.lua插件协助分析drcom协议
drcom_2011.lua是来源于Google code上的一个开源项目中的一个插件,感谢网络大神的分享 需要使用drcom_2011.lua分析drcom协议的话,需要把drcom_2011.lu ...
- 【wireshark】Wireshark原理分析与二次开发系列
1.版权声明 本系列文章是本人花了很多心血写成,wireshark本是开源软件,本人也乐于技术知识和经验的分享,更是欣赏和推崇开源精神,因此任何看到本文的人都可以随意转载,但只有一个要求: 在大段甚至 ...
- 采访 Lua 发明人的一篇文章
采访 Lua 发明人的一篇文章 来源 https://blog.codingnow.com/2010/06/masterminds_of_programming_7_lua.html <Mast ...
随机推荐
- OpenTSDB案例总结
加宽行可增加扫描速度 采用组合rowkey,利用数据本地性加快扫描 少数宽行,并不比多数窄行节省空间 缩短Column family 和 column的名字 合并若干列.
- IE8下String的Trim()方法失效的解决方法
String的Trim()方法失效,在ie8下是有这样的情况的,解决方法也很简单使用$.trim(str)即可,需要的朋友可以了解下 用jquery的trim()方法,$.trim(str)就可以了.
- 形形色色Node工程Angular2
最近项目要用的 一些无关紧要的文件夹, demo是一些示例, dist是webpack打包后发布的代码,server是用node启动服务,typings和tsconfig是一些ts配置. npm in ...
- win7 任务计划 任务映像已损坏或篡改(异常来自HRESULT:0x80041321)
转自win7 任务计划 任务映像已损坏或篡改(异常来自HRESULT:0x80041321) 请这样操作: 1. 以管理员身份运行命令提示符并执行命令 chcp 437 schtasks /query ...
- Swift初体验之图案锁
这篇在应用上貌似没有价值,貌似我写了好多实际上都没有价值,这里贴出来就是分享下. 自己写swift好多天了,感觉好多东西还是不太懂,边学边做,互勉! 先上图: 代码:下载
- 测来测去,感觉REQUESTS最实在
URLLIB,URLLIB2,PYCURL,HTTPIE,,,在测试PUT及认证时,还是REQUESTS胜出.. 测试过程及样例代码如下: import urllib import urllib2 i ...
- ANDROID_MARS学习笔记_S04_008_用Listview、自定义adapter显示返回的微博数据
一.简介 运行结果 二.代码1.xml(1)activity_main.xml <?xml version="1.0" encoding="utf-8"? ...
- 【HDOJ】3560 Graph’s Cycle Component
并查集的路径压缩. #include <stdio.h> #include <string.h> #define MAXNUM 100005 int deg[MAXNUM], ...
- bzoj1799
这是一道比较难的数位dp 因为逐位统计好像无法处理数位和整除原数的 但是有了刚才的bzoj1072的经验,我们能做的是逐位处理被一个数d整除的方案 不难想到先穷举数位和now,now最大也就162,可 ...
- bzoj3238
都LCP了很显然是要用到后缀数组的 显然前面的那个东西是可以直接算出来的 关键在于LCP的和怎么快速的计算 不难想到穷举height[i],然后判断这个height[i]可能成为多少对后缀的LCP 考 ...