欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Scapy模拟网络包和解析pcap包数据

程序员文章站 2022-03-18 23:31:59
...
from scapy.all import *

#模拟udp协议,发送5个udp包,data部分为11,02,33
data = struct.pack('=BHI', 11,02,33)
pkt = IP(src='192.168.1.81', dst='192.168.1.10')/UDP(sport=12345,dport=5555)/data
send(pkt, inter=1, count=5)


#解析pcap包
pcapfile = r"C:\Users\xxx\Desktop\udp.pcap"
pcaps = rdpcap(pcapfile)
for data in pcaps:
    if 'UDP' in data:
        s = repr(data)
        if data['UDP'].sport == 12345:
            print s
            print "UDP协议>>>>>源IP地址:{},源端口:{},目的IP地址:{},目的端口:{}".format(data['IP'].src,data['UDP'].sport,data['IP'].dst,data['UDP'].dport)
            print data['Ether'].dst
            print data['UDP'].len
            print repr_hex(data['Raw'].load)
            break
    # if 'TCP' in data:
    #     s = repr(data)
    #     print s