python 抓包保存为pcap文件并解析

本文介绍了如何使用Python的scapy模块进行网络抓包,并将捕获的数据保存为pcap文件。sniff()函数在不同参数配置下分别用于读取本地pcap文件和实时抓包。在解析pcap文件时,着重说明了如何自动遍历多个文件,并依据源IP和目标IP对数据包进行分类。
摘要由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
    if count ==3:                         <sp
  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值