2

This is my code to calculate the checksum manually using RFC 1071:

from scapy.all import *

data = IP(src='192.168.1.34', dst='192.168.1.1')

convert_packet_bytes = bytes(data)

mylst = []

for start,end in zip(convert_packet_bytes[0::1], convert_packet_bytes[2::1]):
    multi = start*256+end    
    mylst.append(multi)

print(sum(mylst) % 65535)

my_code_result = 64954

And this calculates the checksum using Scapy's show2:

from scapy.all import *

ip_packet = IP(src='192.168.1.34', dst='192.168.1.1')

ip_packet .show2()

scapy_result : 63349

In the first code i am trying to implement a checksum calcolator using rfc 1071 in python to calculate the checksum for the ip protocol in scapy and in the second code i am using the scapy's show2 function to calculate the ip checksum and compare it with my own implementation of checksum in the first code using rfc 1071

but the problem is the first checksum code result which is my code is 64954 and the second checksum code which is from the scapy's show2 function is 63349 they are not the same i am trying to get the same result by implementing the rfc 1071 in python

Bishop
  • 55
  • 6
  • 1
    I don't think your slices are doing what you intend. You're not getting pairs of adjacent bytes without overlaps. You're getting pairs offset by two (skipping a byte in between them) and using every byte (other than a few at the ends) twice. Try `zip(convert_packet_bytes[0::2], convert_packet_bytes[1::2])` (or, if you don't mind some iterator shenanigans, `zip(*[iter(convert_packets_bytes)]*2)`). I also don't think you're doing the ones-complement addition correctly, you need to add in the overflow that you're discarding with the mod operator. – Blckknght Feb 12 '23 at 02:23

1 Answers1

0

this problem occurs by |\xe7 when i convert the IP() packet to bytes :

b'E\x00\x00\x14\x00\x01\x00\x00@\x00|\xe7\x7f\x00\x00\x01\x7f\x00\x00\x01'

so we have to remove |\xe7 :

b'E\x00\x00\x14\x00\x01\x00\x00@\x00\x7f\x00\x00\x01\x7f\x00\x00\x01'

and then calculate the checksum :

from scapy.all import *

data = IP()

packet_bytes = bytes(data)
convert_packet_bytes = packet_bytes[0:10] + packet_bytes[12:]

mylst = []

for start,end in zip(convert_packet_bytes[0::2], convert_packet_bytes[1::2]):
    #print(start, end)
    multi = start*256+end    
    mylst.append(multi)

print(-sum(mylst) % 65535)

result is : 31975

if we go to scapy and use show2() to calculate the checksum of IP() we will find it the same :

from scapy.all import *

ip_packet = IP(src='192.168.1.34', dst='192.168.1.1')

ip_packet .show2()

result is : 0x7ce7 == 31975

and the problem is solved perfectly

Bishop
  • 55
  • 6