C语言将IP保存为pcap程序,使用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脚本:

local getTcpStream =Field.new("tcp.stream")

local getSrcIp =Field.new("ip.src")

local getDstIp =Field.new("ip.dst")

local getSrcPort =Field.new("tcp.srcport")

local getDstPort =Field.new("tcp.dstport")

local getIpVersion =Field.new("ip.version")

local tcpStreamTable = {}--每一条流的索引哈希表

local dataWriterTable = {}--每一条流的dumper

do

local function packet_listener()

local tap =Listener.new("frame", "tcp.port == 443")

--frame是监听器的名称,tcp是wireshark过滤器规则

function tap.reset()

print("tap reset")

end

function tap.packet(pinfo,tvb)

--回调函数,每收到一个包执行一次。

local tcpStream = getTcpStream()

local srcIp = getSrcIp()

local dstIp = getDstIp()

local srcPort = getSrcPort()

local dstPort = getDstPort()

local ipVersion = getIpVersion()

local tcpStreamNumber =tonumber(tostring(tcpStream))

if(tcpStreamTable[tcpStreamNumber])

then

dataWriterTable[tcpStreamNumber]:dump_current()

else

local packetTuple4 =tostring(tcpStream).."_"..tostring(srcIp).."_"..tostring(srcPort).."_"..tostring(dstIp).."_"..tostring(dstPort).."_"..tostring(ipVersion)..".pcap"

tcpStreamTable[tcpStreamNumber] =tcpStream

dataWriterTable[tcpStreamNumber] =Dumper.new(packetTuple4)

dataWriterTable[tcpStreamNumber]:dump_current()

--print(type(tcpStreamTable[tcpStreamNumber]),type(tcpStreamNumber),type(dataWriterTable[tcpStreamNumber]),type(tcpStreamTable))

end

end

function tap.draw()

--结束执行

print("tap.draw")

end

end

--监听报文

packet_listener()

--tcpStreamTable =nil

end

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个修改后的示例代码: ``` #include <stdio.h> #include <stdlib.h> #include <pcap.h> void packet_handler(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes); int main(int argc, char *argv[]) { char *dev = argv[1]; // 指定网卡 char errbuf[PCAP_ERRBUF_SIZE]; pcap_t *handle; struct bpf_program fp; bpf_u_int32 mask; bpf_u_int32 net; if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) { fprintf(stderr, "Can't get netmask for device %s\n", dev); net = 0; mask = 0; } handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf); // 打开网卡 if (handle == NULL) { fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf); exit(1); } if (pcap_compile(handle, &fp, "port 80", 0, net) == -1) { // 指定端口 fprintf(stderr, "Couldn't parse filter %s: %s\n", "port 80", pcap_geterr(handle)); exit(1); } if (pcap_setfilter(handle, &fp) == -1) { // 应用过滤器 fprintf(stderr, "Couldn't install filter %s: %s\n", "port 80", pcap_geterr(handle)); exit(1); } pcap_dumper_t *dumper; char filename[] = "capture.pcap"; dumper = pcap_dump_open(handle, filename); // 打开pcap文件 if (dumper == NULL) { fprintf(stderr, "Couldn't open file %s: %s\n", filename, pcap_geterr(handle)); exit(1); } pcap_loop(handle, 10, packet_handler, (u_char *)dumper); // 抓10个包 pcap_dump_close(dumper); // 关闭pcap文件 pcap_close(handle); return 0; } void packet_handler(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes) { pcap_dump((u_char *)user, h, bytes); // 将捕获的数据包写入pcap文件 } ``` 这个程序可以捕获指定网卡上的端口为80的数据包,并且限制捕获大小为BUFSIZ。同时,它还会将捕获到的数据包写入名为"capture.pcap"的pcap文件中。你可以根据自己的需求修改端口和捕获大小的设置,以及修改保存文件名。同时,你需要定义一个 packet_handler 函数来将捕获的数据包写入pcap文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值