python-3.xparsingnetwork-programmingnetwork-protocolskaitai-struct

"installing" Kaitai Struct Python


I need help with installing Kaitai Struct on my Laptop.

I installed python-kaitaistruct and compiled the network files.

But i get an import error:

Traceback (most recent call last):
  File "test2.py", line 1, in <module>
    from ethernet_frame import *
  File "/home/bene/python/ethernet_frame.py", line 15, in <module>
    from ipv6_packet import Ipv6Packet
  File "/home/bene/python/ipv6_packet.py", line 17, in <module>
    from ipv4_packet import Ipv4Packet
  File "/home/bene/python/ipv4_packet.py", line 17, in <module>
    from ipv6_packet import Ipv6Packet
ImportError: cannot import name 'Ipv6Packet

My folder looks like this:

insgesamt 76K
drwxr-xr-x  3 bene bene  330 19. Jan 16:03 .
drwx------ 24 bene bene 4,0K 19. Jan 16:06 ..
-rw-r--r--  1 bene bene   42  5. Jan 12:38 country.py
-rw-r--r--  1 bene bene 8,0K  5. Jan 12:09 dns_packet.py
-rw-r--r--  1 bene bene 1,6K  5. Jan 12:09 ethernet_frame.py
-rw-r--r--  1 bene bene 3,0K  5. Jan 12:09 icmp_packet.py
-rw-r--r--  1 bene bene 7,7K  5. Jan 12:09 ipv4_packet.py
-rw-r--r--  1 bene bene 2,7K  5. Jan 12:09 ipv6_packet.py
-rw-r--r--  1 bene bene 6,4K  5. Jan 12:09 microsoft_network_monitor_v2.py
-rw-r--r--  1 bene bene 7,0K  5. Jan 12:09 pcap.py
drwxr-xr-x  2 bene bene  180  5. Jan 12:12 __pycache__
-rw-r--r--  1 bene bene 1,1K  5. Jan 12:09 tcp_segment.py
-rw-r--r--  1 bene bene  518  5. Jan 12:32 test1.py
-rw-r--r--  1 bene bene  596 19. Jan 15:56 test2.py
-rw-r--r--  1 bene bene  667  5. Jan 12:38 test.py
-rw-r--r--  1 bene bene  880  5. Jan 12:09 udp_datagram.py
-rw-r--r--  1 bene bene  986  5. Jan 12:09 windows_systemtime.py

and the file i executed:

from ethernet_frame import *
import socket

s = socket.socket(socket.AF_PACKET,socket.SOCK_RAW,socket.ntohs(3))

def network(buf):
    io = BytesIO(buf)
    ksio = KaitaiStream(io)
    pkt = EthernetFrame(ksio)
    dummy = pkt.ipv4_body.src_ip_addr
    print(dummy)

while True:
    p = s.recvfrom(65565)
    network(p) 

Can someone help me maybe i installed it wrong? Or a Full Guide how to install and use it would be cool :DD

Thank you <3


Solution

  • I had a similar issue and after checking the .py file created by the compiler I found editing the import sequence solved the issue. Example:

    import ipv4_packet was causing the same error as you have.

    I checked ipv4_packet.py and it had an import statement from ipv6_packet import Ipv6Packet.

            # This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild
    
    from pkg_resources import parse_version
    from kaitaistruct import __version__ as ks_version, KaitaiStruct, KaitaiStream, BytesIO
    from enum import Enum
    
    
    if parse_version(ks_version) < parse_version('0.7'):
        raise Exception("Incompatible Kaitai Struct Python API: 0.7 or later is required, but you have %s" % (ks_version))
    
    from udp_datagram import UdpDatagram
    from tcp_segment import TcpSegment
    from ipv6_packet import Ipv6Packet
    from icmp_packet import IcmpPacket
    
    class Ipv4Packet(KaitaiStruct):
    
        class ProtocolEnum(Enum):
    

    ipv6_packet inturn tries to import the class from ipv4_packet: from ipv4_packet import Ipv4Packet, and causes the import error. If the lines (ipv4_packet .py):

    from udp_datagram import UdpDatagram
    from tcp_segment import TcpSegment
    from ipv6_packet import Ipv6Packet
    from icmp_packet import IcmpPacket
    

    are moved to after the class definition there is no error.

    Just playing around with Kaitai Struct for the first time this morning, I am sure this does not need to be done manually and there is an issue in the compilation I/we are doing, but this works if you just want to have a quick play.