使用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 ...
随机推荐
- 网页增重不可控?试试 OneAPM Cloud Test
再次发生了!HTTP Archive 报告在收集了 50 万个最受欢迎的网站的技术信息,经过整理分析后指出:2015 年,网页的平均「体重」增加了 16%,达到了 2,262 KB,近似于 2014 ...
- Django自定义上传目录
由于数据库的upload_to功能,有时不能满足每次上传灵活自定义的需求, 基于DEF的上传,有时不能满足基于CLASS的视图要求, 于是,只好慢慢用土法实现. 当然,首先,要使用上传功能时,form ...
- go与json
Go语言对JSON进行编码和解码 http://outofmemory.cn/code-snippet/3766/Go-language-to-JSON-to-coding-jiema package ...
- Android UI:机智的远程动态更新策略
问题描述 做过Android开发的人都遇到过这样的问题:随着需求的变化,某些入口界面通常会出现 UI的增加.减少.内容变化.以及跳转界面发生变化等问题.每次发生变化都要手动修改代码,而入口界面通常具有 ...
- reviewboard搭建
reviewboard的搭建 系统:fedora 19 内核版本:3.9.5-301.fc19.x86_64 步骤 命令 备注 安装mysql # yum -y install mysql mysql ...
- Android textAppearance的属性设置及TextView属性详解
textAppearance的属性设置 android:textAppearance="?android:attr/textAppearanceSmall" android:tex ...
- 利用if else 判断方程有几个根
static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Green; ...
- POJ_3579_Median_(二分,查找第k大的值)
描述 http://poj.org/problem?id=3579 给你一串数,共C(n,2)个差值(绝对值),求差值从大到小排序的中值,偶数向下取. Median Time Limit: 1000M ...
- Linux停SVN提交时强制写日志
Linux下SVN提交时强制写日志 SVN默认可以不写注释提交,有时候可能忘记写注释,有的人也没有写注释的习惯,导致翻看history的时候都不知道做了哪些更改,可以依照以下步骤修改SVN配置,强制提 ...
- Java中Map遍历的四种方案
在Java中如何遍历Map对象 方式一 这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用. Map<Integer, Integer> map = new HashM ...