Intro to PyShark for Programmatic Packet Analysis

来源:互联网 发布:linux批量创建文件 编辑:程序博客网 时间:2024/06/10 08:58

https://thepacketgeek.com/intro-to-pyshark-for-programmatic-packet-analysis/

I can hardly believe it took me this long to find PyShark, but I am very glad I did! PyShark is a wrapper for the Wireshark CLI interface, tshark, so all of the Wireshark decoders are available to PyShark! It’s so amazing that I started a new project just so I could use this amazing new tool: Cloud-Pcap.

You can use PyShark to sniff from a interface or open a saved capture file, as the docs show on the overview page here:

Once a capture object is created, either from a LiveCapture or FileCapture method, several methods and attributes are available at both the capture and packet level.  The power of PyShark is the access to all of the packet decoders built into tshark.  I’m going to just give a sneak peek of some of the things you can do in this post and there will be a few accompanying posts that follow to go more in depth.

1. Getting packet summaries (similar to tshark capture output):

This will give access to attributes like packet number, relative and delta times, IP addresses, protocol, and a brief info line.

2. Drilling down into packet attributes by layer:

3. Iterating through the packets and applying a function to each:

…and this is just the sneak peak!!  Who knew that the getting the power of tshark & Wireshark in your python scripts and applications would be this easy!  The only caveat that I’ve found so far is the performance. I’ve thrown a lot of packets at PyShark and it can really slow down once you start running through captures of a couple thousand packets. Some things have been done to preserve memory that will be covered in the following posts.

I certainly hope you’re as excited as I am at this point. There’s plenty more to come, so check back soon!

Series Navigation
0 0