利用Python库Scapy解析pcap文件的方法
作者:BabyKylin 时间:2021-07-26 12:59:01
每次写博客都是源于纳闷,python解析pcap这么常用的例子网上竟然没有,全是一堆命令行执行的python,能用吗?玩呢?
pip安装scapy,然后解析pcap:
import scapy
from scapy.all import *
from scapy.utils import PcapReader
packets=rdpcap("./test.pcap")
for data in packets:
if 'UDP' in data:
s = repr(data)
print(s)
print(data['UDP'].sport)
break
打印s,是为了看到所有的字段:
<Ether dst=ff:ff:ff:ff:ff:ff src=30:fc:68:33:7a:e2 type=0x800 |<IP version=4 ihl=5 tos=0x0 len=143 id=37312 flags= frag=0 ttl=64 proto=udp chksum=0x644d src=192.168.1.1 dst=192.168.1.255 options=[] |<UDP sport=1024 dport=5001 len=123 chksum=0x3051 |<Raw load='\x01\x01\x0e\x00\xe1+\x83\xc7\xb2\x9f\x00e\x00\x00\x00\x06\x00\tTL-WR886N\x00\x0b\x00\x035.0\x00\x07\x00\x01\x01\x00\x05\x00\x1130-fc-68-33-7a-e2\x00\x08\x00\x0b192.168.1.1\x00\t\x00\ntplogin.cn\x00\n\x00\rTL-WR886N 5.0\x00\x0c\x00\x051.7.3' |>>>>
然后就能用data['UDP'].sport打印udp的源端口了。
现在基本的字段都能看到了,如果还想看别的协议的字段,可以参考源码:
https://github.com/secdev/scapy/tree/master/scapy/layers
L2层在l2.python,ip,tcp等在inet.py中,例如tcp的一些字段名称在这:
来源:https://blog.csdn.net/u011412226/article/details/80656884
标签:Python,Scapy,pcap
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SQL Server双服务器架设并数据自动同步教程
2023-07-03 02:59:53
SQL 中主标识列IDENTITY使用技巧
2011-06-02 08:47:00
python 中Arduino串口传输数据到电脑并保存至excel表格
2022-05-17 05:41:11
![](https://img.aspxhome.com/file/2023/6/69056_0s.png)
scrapy头部修改的方法详解
2023-09-13 05:58:10
![](https://img.aspxhome.com/file/2023/0/82720_0s.png)
MYSQL创建触发程序的方法
2009-07-30 08:38:00
关于CSS中字号控制的兼容性研究[译]
2009-02-24 16:35:00
学习ASP.NET八天入门:第五天
2007-08-07 13:43:00
OpenCV实现人脸识别
2023-01-07 02:38:58
thinkPHP学习笔记之安装配置篇
2023-09-27 18:32:03
asp压缩access数据库方法代码
2008-08-08 12:22:00
FrontPage2002简明教程六:图片库
2008-09-17 11:30:00
python 中的pycrypto 算法加密
2022-01-07 11:23:33
[译]2009年海外Web设计风潮(下)
2009-01-23 09:34:00
![](https://img.aspxhome.com/file/UploadPic/20091/23/trend-38-39s.gif)
Python爬取网页信息的示例
2021-02-09 10:43:21
![](https://img.aspxhome.com/file/2023/9/71549_0s.png)
python爬虫竟然被小伙用来算命
2023-08-28 17:35:20
![](https://img.aspxhome.com/file/2023/3/86663_0s.png)
读取MySQL的log方法
2011-07-01 12:04:57
python如何通过psutil获取服务器cpu、内存、磁盘使用率
2022-11-07 01:01:29
Pytorch:dtype不一致问题(expected dtype Double but got dtype Float)
2023-07-05 21:57:33
![](https://img.aspxhome.com/file/2023/1/66281_0s.png)
深入认识javascript中的eval函数
2008-08-03 16:44:00
python实现网上购物系统
2021-06-12 16:32:26