python 抓包保存为pcap文件并解析的实例
作者:intfre 时间:2023-04-03 03:52:04
首先是抓包,使用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: <span style="font-family: Arial, Helvetica, sans-serif;">#每3个TCP操作封为一个包(为了检测正确性,使用时尽量增多)</span>
pcapnum +=1
pname="pcap%d.pcap"%pcapnum
wrpcap(pname,pkts)
filename ="./pcap%d.pcap"%pcapnum
test_dump_file(filename)
pkts=[]
count=0
if __name__=='__main__':
print "Start packet capturing and dumping ..."
sniff(filter="dst net 127.0.0.1 and tcp",prn=write_cap) #BPF过滤规则
下面是对pcap文件的解析,会自动查找下一个pcap文件,按照src.ip和dst.ip进行划分
# -*- coding: cp936 -*-
import re
import zlib
import os
from scapy.all import *
num=1
a=rdpcap("pcap1.pcap") #循环打开文件
while True:
try:
num+=1
file_name="pcap%d.pcap" % num
b=rdpcap(file_name)
a=a+b
except:
break
print "[*] Read pcap file ok"
print "[*] Begin to parse pcapfile..."
print a
try:
#print "[*] OPen new pcap_file %s" % pcap_file
sessions=a.sessions()
for session in sessions:
print "[*]New session %s" % session
data_payload=""
for packet in sessions[session]:
try:
data_payload +=str(packet[TCP].payload)
print "[**] Data:%s" % data_payload
except:
pass
except:
print "[*]no pcapfile..."
来源:https://blog.csdn.net/nibiru_holmes/article/details/50364939
标签:python,抓包,pcap
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python面经之16个高频面试问题总结
2022-09-10 18:36:33
讲解MySQL数据库的数据类型和建库策略
2008-12-17 14:39:00
python入门之井字棋小游戏
2021-12-11 11:35:36
![](https://img.aspxhome.com/file/2023/2/123632_0s.jpg)
Flask实现图片的上传、下载及展示示例代码
2023-07-14 20:46:17
![](https://img.aspxhome.com/file/2023/2/63962_0s.png)
Python的函数的一些高阶特性
2022-06-04 15:06:28
Puppeteer环境搭建的详细步骤
2024-05-13 09:58:56
Mysql全局ID生成方法
2023-07-02 13:59:53
详解Laravel模型事件和模型事件在Trait中的使用
2023-06-17 17:19:28
js贪吃蛇游戏实现思路和源码
2024-04-10 11:03:57
![](https://img.aspxhome.com/file/2023/8/136798_0s.jpg)
详解MySQL 8.0 之不可见索引
2024-01-22 17:41:46
python循环输出三角形图案的例子
2022-05-16 08:38:37
![](https://img.aspxhome.com/file/2023/5/103265_0s.jpg)
双屏显示提升前端开发10%工作效率
2009-03-16 18:22:00
![](https://img.aspxhome.com/file/UploadPic/20093/16/screen-34s.jpg)
SQL Server的触发器你了解多少
2024-01-14 21:15:57
Python实现无损放大图片的示例代码
2022-12-15 12:50:12
![](https://img.aspxhome.com/file/2023/7/114677_0s.jpg)
对python csv模块配置分隔符和引用符详解
2023-04-14 13:52:20
perl命令行参数内建数组@ARGV浅析
2022-11-04 06:58:47
不得不看的JS基础知识(事件触发篇)
2008-12-04 16:38:00
利用python爬取软考试题之ip自动代理
2023-01-30 01:17:28
![](https://img.aspxhome.com/file/2023/4/127024_0s.png)
springBoot下实现java自动创建数据库表
2024-01-24 12:26:31
![](https://img.aspxhome.com/file/2023/5/70325_0s.png)
MySQL中Distinct和Group By语句的基本使用教程
2024-01-26 09:51:53