了解Scapy数据结构

2024-03-29 10:04:02 发布

您现在位置:Python中文网/ 问答频道 /正文

你好,我一直在努力解决Scapy数据结构,我的意思是,数据包的存储方式和访问方式。在

所以我想做一点研究,而不是仅仅用辛塔克斯来做研究,以便更好地了解和熟悉它背后的含义。在

我见过这是一本词典,但不是这本词典是由什么组成的。在

我遇到了一个结构,我认为它是落后的,我希望你纠正我,如果我错了,但我认为这是有意义的:一个对象字典,其中每个对象是一个TCP/IP层。在

这样一切都有意义(除了我没有以太中的有效载荷,它将全部在IP之后,而有效载荷在IP中,这将是TCP之后的所有内容)

不管怎样,我认为这会有助于我们了解斯帕伊的结构, 虽然我知道它不是100%准确:

#Scapy

class Ether:

    def __init__(self,dst='ff:ff:ff:ff:ff:ff',src='00:00:00:00:00:00',type=0):

        self.dst=dst
        self.src=src
        self.type=type



class IP:

    def __init__(self,version=4,ihl=None,tos=0,leng=None,idd=1
                 ,flags=None,frag=0,ttl=64,proto=06,chksum=None,src='127.0.0.1',dst='127.0.0.1'):


        self.version = version
        self.ihl = ihl
        self.tos = tos
        self.leng = leng
        self.idd = idd
        self.flags = flags
        self.frag = frag
        self.ttl = ttl
        self.proto = proto
        self.chksum = chksum
        self.src = src
        self.dst = dst



class TCP:

    def __init__(self,sport=21,dport=80,seq=0,ack=0,dataofs=None,reserved=0
                 ,flags=0,window=8192,chksum=None,urgptr=0,options=0,payload=''):

        self.sport=sport;
        self.dport=dport;
        self.seq=seq
        self.ack=ack
        self.dataofs=dataofs
        self.reserved=reserved
        self.flags=flags
        self.window=window
        self.chksum=chksum
        self.urgptr=urgptr
        self.options=options
        self.payload=payload



pkt1 = {'Ether':Ether(src='ff:aa:bb:aa:dd:aa'),'IP':IP(src='192.168.1.10',dst='192.168.1.1')}

pkt2 = {'IP':IP(dst='8.8.8.8'),'TCP':TCP(dport=80)}

print pkt1['IP'].src

pkts = []

pkts.append(pkt1)

pkts.append(pkt2)

for pkt in pkts:

    print pkt['IP'].dst

print pkts[0]['Ether'].src

有这样的输出:

^{pr2}$

希望这是有益的,你能纠正我的错误。在


Tags: selfipsrcnoneinitdefclasstcp
1条回答
网友
1楼 · 发布于 2024-03-29 10:04:02

this article读取:

Scapy uses Python dictionaries as the data structure for packets. Each packet is a collection of nested dictionaries with each layer being a child dictionary of the previous layer, built from the lowest layer up. Each field (such as the Ethernet dst value or ICMP type value) is a key:value pair in the appropriate layer. These fields (and nested layers) are all mutable so we can reassign them in place using the assignment operator.

相关问题 更多 >