Python Forum
getting wrong source IP address using socket and struct
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getting wrong source IP address using socket and struct
#2
Here is a printout after some updated code:

Protocol: 1 192.168.56.1 -> 192.168.56.1
Version: 4
Header Length: 5 TTL: 128
ICMP -> Type: 0 Code: 0


It seems my headers are fine, except maybe the Type 0 code meaning unreachable.
class ICMP:
    def __init__(self, buff):
        header = struct.unpack('<BBHHH', buff)
        self.type = header[0]
        self.code = header[1]
        self.sum = header[2]
        self.id = header[3]
        self.seq = header[4]
            try:
            while True:
                raw_buffer = sniffer.recvfrom(65535)[0]

                ip_header = IP(raw_buffer[0:24])
                print(f'Version: {ip_header.ver}')
                print(f'Header Length: {ip_header.ihl} TTL: {ip_header.ttl}')
                offset = ip_header.ihl * 4
                buf = raw_buffer[offset:offset + 8]
                icmp_header = ICMP(buf)
                print('ICMP -> Type: %s Code: %s\n' %
                      (icmp_header.type, icmp_header.code))
                print('Protocol: %s %s -> %s' % (ip_header.protocol,
                                         ip_header.src_address,
                                                 
                                         ip_header.dst_address))
any suggestions?
Reply


Messages In This Thread
RE: getting wrong source IP address using socket and struct - by billykid999 - May-25-2023, 08:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  struct.error: unpack requires a buffer of 14 bytes upasana 0 8,700 Apr-18-2018, 11:06 AM
Last Post: upasana

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020