使用Lua脚本对pcap文件进行切流操作

  本文主要讲述如何使用Lua以及wireshark提供的API接口,来实现切流的操作。本质是利用wireshark的强大功能快速的实现报文级操作。

  在这篇文章中说了如何使用tshark写切流的脚本。总的来说效率低下,主要原因是使用tshark我无法做到报文级操作(如果你知道,请告诉我),只能整个整个数据包的读取,然后根据过滤条件整个报文输出。对于一个pcap文件需要多次遍历,pcap报文中有几条流,就要读取遍历报文几次,时间复杂度高。如遇p2p报文,耗时太长。

  使用C语言libpcap解析pcap报文,加上glib实现流管理,也是可以做到只历一遍报文。这里面使用Lua,其一是因为wireshark提供了强大的报文、流管理以及过滤器的功能,可以加以利用;其二是因为使用Lua脚本能够快速的构建和实现特定的小功能。

  在网站https://www.wireshark.org/docs/wsdg_html_chunked/的第11章中说明了wireshark给Lua提供的API接口。本次主要应用到里面的Dumper,Field,Listener三个类。Dumper主要作用是写文件,Field主要作用是获取wireshark提供的各种报文属性,Listenser是一个监听器,监听符合过滤规则的报文。具体的用法参考网站的说明。以下便是完成的Lua脚本:

[python]  view plain  copy
  1. local getTcpStream =Field.new("tcp.stream")  
  2. local getSrcIp =Field.new("ip.src")  
  3. local getDstIp =Field.new("ip.dst")  
  4. local getSrcPort =Field.new("tcp.srcport")  
  5. local getDstPort =Field.new("tcp.dstport")  
  6. local getIpVersion =Field.new("ip.version")  
  7.    
  8. local tcpStreamTable = {}--每一条流的索引哈希表  
  9. local dataWriterTable = {}--每一条流的dumper  
  10.    
  11. do  
  12.     local function packet_listener()  
  13.         local tap =Listener.new("frame""tcp.port == 443")  
  14.         --frame是监听器的名称,tcp是wireshark过滤器规则  
  15.                
  16.         function tap.reset()  
  17.             print("tap reset")  
  18.         end  
  19.                
  20.         function tap.packet(pinfo,tvb)  
  21.             --回调函数,每收到一个包执行一次。  
  22.                      local tcpStream = getTcpStream()  
  23.                      local srcIp = getSrcIp()  
  24.                      local dstIp = getDstIp()  
  25.                      local srcPort = getSrcPort()  
  26.                      local dstPort = getDstPort()  
  27.                      local ipVersion = getIpVersion()  
  28.                      local tcpStreamNumber =tonumber(tostring(tcpStream))  
  29.                       
  30.                      if(tcpStreamTable[tcpStreamNumber])  
  31.                      then  
  32.                             dataWriterTable[tcpStreamNumber]:dump_current()  
  33.                      else  
  34.                             local packetTuple4 =tostring(tcpStream).."_"..tostring(srcIp).."_"..tostring(srcPort).."_"..tostring(dstIp).."_"..tostring(dstPort).."_"..tostring(ipVersion)..".pcap"  
  35.                             tcpStreamTable[tcpStreamNumber] =tcpStream  
  36.                             dataWriterTable[tcpStreamNumber] =Dumper.new(packetTuple4)  
  37.                             dataWriterTable[tcpStreamNumber]:dump_current()  
  38.                      --print(type(tcpStreamTable[tcpStreamNumber]),type(tcpStreamNumber),type(dataWriterTable[tcpStreamNumber]),type(tcpStreamTable))  
  39.                      end  
  40.         end  
  41.                
  42.         function tap.draw()  
  43.             --结束执行  
  44.             print("tap.draw")  
  45.         end  
  46.     end  
  47.        --监听报文  
  48.     packet_listener()  
  49.        --tcpStreamTable =nil  
  50. end  

  我是在windows上面执行上述写好的lua脚本的,将lua脚本放到C:\ProgramFiles\Wireshark目录下面,然后cmd到该目录下面,执行命令tshark-X lua_script:cut_flow.lua -r file_name.pcap,Linux上面的话是需要安装lua,重新编译wireshark的。最终切出的pcap报文名称是以流号+四元组+IP版本号命名的。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值