当前位置: 首页>>代码示例>>Python>>正文


Python Packet.add_layer方法代码示例

本文整理汇总了Python中packet.Packet.add_layer方法的典型用法代码示例。如果您正苦于以下问题:Python Packet.add_layer方法的具体用法?Python Packet.add_layer怎么用?Python Packet.add_layer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在packet.Packet的用法示例。


在下文中一共展示了Packet.add_layer方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: get_packet

# 需要导入模块: from packet import Packet [as 别名]
# 或者: from packet.Packet import add_layer [as 别名]
 def get_packet(self):
     selected_transport_layer = self.combo_box_transport.currentIndex()
     packet = Packet()
     eth_layer = {}
     eth_layer["LayerType"] = "Ethernet"
     eth_layer["Source MAC"] = str(self.line_edit_source_mac.text())
     eth_layer["Destination MAC"] = str(self.line_edit_destination_mac.text())
     eth_layer["Type"] = 8
     ip_layer = {}
     ip_layer["LayerType"] = "IP"
     ip_layer["Source Address"] = str(self.line_edit_source_ip.text())
     ip_layer["Destination Address"] = str(self.line_edit_destination_ip.text())
     ip_layer["TTL"] = self.spin_box_ttl.value()
     ip_layer["Version"] = 4
     transport_layer = {}
     if selected_transport_layer == 0:
         ip_layer["Protocol"] = 6
         transport_layer["LayerType"] = "TCP"
         transport_layer["Source port"] = self.spin_box_tcp_source_port.value()
         transport_layer["Destination port"] = self.spin_box_tcp_destination_port.value()
         transport_layer["Sequence"] = self.spin_box_tcp_sequence.value()
         transport_layer["Acknowledgement"] = self.spin_box_tcp_acknowledgement.value()
         transport_layer["Data"] = str(self.line_edit_tcp_data.text())
     elif selected_transport_layer == 1:
         ip_layer["Protocol"] = 1
         transport_layer["LayerType"] = "ICMP"
         transport_layer["Type"] = self.spin_box_icmp_type.value()
         transport_layer["Code"] = self.spin_box_icmp_code.value()
         transport_layer["Data"] = str(self.line_edit_icmp_data.text())
     else:
         ip_layer["Protocol"] = 17
         transport_layer["LayerType"] = "UDP"
         transport_layer["Source port"] = self.spin_box_udp_source_port.value()
         transport_layer["Destination port"] = self.spin_box_udp_destination_port.value()
         transport_layer["Data"] = str(self.line_edit_udp_data.text())
     packet.add_layer(eth_layer)
     packet.add_layer(ip_layer)
     packet.add_layer(transport_layer)
     return packet
开发者ID:alexis-ld,项目名称:pycket,代码行数:41,代码来源:forgingGUI.py

示例2: parse

# 需要导入模块: from packet import Packet [as 别名]
# 或者: from packet.Packet import add_layer [as 别名]
    def parse(self):
        if self._file is None:
            print "Error : PcapReader : Open a file before using parse"
            return
        if self._packets:
            self._packets[:] = []

        # Parsing du fichier via dpkt
        pcap = dpkt.pcap.Reader(self._file)
        for ts, buf in pcap:
            # Parsing des donnees du paquet
            eth = dpkt.ethernet.Ethernet(buf)

            # On ne traite que les paquets IP
            if eth.type != dpkt.ethernet.ETH_TYPE_IP:
                continue

            packet = Packet(buf)
            # Recuperation des donnees de la couche ethernet
            eth_layer = {}
            eth_layer['LayerType'] = "Ethernet"
            # Un header ethernet fait 14 bytes (2 * 6 bytes d'addresses MAC  + l'ethertype ou la taille du payload (2 bytes))
            eth_layer['Header length'] = 14
            eth_layer['Type'] = socket.ntohs(eth.type)
            eth_layer['Destination MAC'] = network_utils.eth_addr(eth.dst)
            eth_layer['Source MAC'] = network_utils.eth_addr(eth.src)
            packet.add_layer(eth_layer)

            ip = eth.data

            ip_layer = {}
            # Recuperation des donnees de la couche ip
            ip_layer['LayerType'] = 'IP'
            ip_layer['Version'] = ip.v
            ip_layer['Header length'] = ip.hl
            ip_layer['TTL'] = ip.ttl
            ip_layer['Protocol'] = ip.p
            # on converti les adresses IP binaire en leur notation string
            ip_layer['Source Address'] = socket.inet_ntop(socket.AF_INET, ip.src)
            ip_layer['Destination Address'] = socket.inet_ntop(socket.AF_INET, ip.dst)
            # On rajoute la couche IP recuperee au paquet
            packet.add_layer(ip_layer)

            # TCP protocol
            if ip.p == dpkt.ip.IP_PROTO_TCP:
                tcp = ip.data
                # Recuperation des donnees de la couche TCP
                tcp_layer = {}
                tcp_layer['LayerType'] = 'TCP'
                tcp_layer['Source port'] = tcp.sport
                tcp_layer['Destination port'] = tcp.dport
                tcp_layer['Sequence'] = tcp.seq
                tcp_layer['Acknowledgement'] = tcp.ack
                tcp_layer['Header length'] = tcp.off
                tcp_layer['Data'] = tcp.data
                # On rajoute la couche TCP recuperee au paquet
                packet.add_layer(tcp_layer)
            # ICMP protocol
            elif ip.p == dpkt.ip.IP_PROTO_ICMP:
                icmp = ip.data
                # Recuperation des donnees de la couche ICMP
                icmp_layer = {}
                icmp_layer['LayerType'] = 'ICMP'
                icmp_layer['Type'] = icmp.type
                icmp_layer['Code'] = icmp.code
                icmp_layer['Checksum'] = icmp.sum
                icmp_layer['Data'] = icmp.data
                # On rajoute la couche ICMP recuperee au paquet
                packet.add_layer(icmp_layer)
            # UDP protocol
            elif ip.p == dpkt.ip.IP_PROTO_UDP:
                udp = ip.data
                # Recuperation des donnees de la couche UDP
                udp_layer = {}
                udp_layer['LayerType'] = 'UDP'
                udp_layer['Source port'] = udp.sport
                udp_layer['Destination port'] = udp.dport
                udp_layer['Length'] = udp.ulen
                udp_layer['Checksum'] = udp.sum
                udp_layer['Data'] = udp.data
                # On rajoute la couche UDP recuperee au paquet
                packet.add_layer(udp_layer)
            self._packets.append(packet)
        self.close_file()
开发者ID:gh0std4ncer,项目名称:pycket,代码行数:86,代码来源:PcapReader.py


注:本文中的packet.Packet.add_layer方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。