python scapy 抓包_python 抓包保存为pcap文件并解析的实例

本文介绍了如何使用 Python 的 Scapy 模块进行网络抓包,并将捕获的数据包保存为 pcap 文件。通过 `sniff()` 函数进行抓包,结合 BPF 过滤规则和回调函数 `write_cap` 处理数据包,每三个 TCP 操作保存为一个 pcap 文件。同时,文章还展示了如何解析 pcap 文件,利用 `rdpcap()` 和 `sessions()` 函数提取会话数据。
摘要由CSDN通过智能技术生成

首先是抓包,使用scapy模块,

sniff()函数 在其中参数为本地文件路径时,操作为打开本地文件

若参数为BPF过滤规则和回调函数,则进行Sniff,回调函数用于对Sniff到的数据包进行处理

import os

from scapy.all import *

pkts=[]

count=0

pcapnum=0

filename=''

def test_dump_file(dump_file):

print "Testing the dump file..."

if os.path.exists(dump_file):

print "dump fie %s found." %dump_file

pkts=sniff(offline=dump_file)

count = 0

while (count<=2):

print "----Dumping pkt:%s----" %dump_file

print hexdump(pkts[count])

count +=1

else:

print "dump fie %s not found." %dump_file

def write_cap(x):

global pkts

global count

global pcapnum

global filename

pkts.append(x)

count +=1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值