Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate a packet checksum without sending it?

Tags:

I'm using scapy, and I want to create a packet and calculate its' checksum without sending it. Is there a way to do it?

Thanks.

like image 231
Dima.Go Avatar asked May 10 '11 16:05

Dima.Go


People also ask

How do you manually calculate checksum?

To calculate the checksum of an API frame: Add all bytes of the packet, except the start delimiter 0x7E and the length (the second and third bytes). Keep only the lowest 8 bits from the result. Subtract this quantity from 0xFF.

How is checksum of an IP packet calculated?

To calculate the checksum, we can first calculate the sum of each 16 bit value within the header, skipping only the checksum field itself. Note that these values are in hexadecimal notation. To obtain the checksum we take the ones' complement of this result: b861 (as shown underlined in the original IP packet header).

How do you calculate checksum online?

Enter Hexadecimal value separated by space e.g. if you have "0xaa 0xaa 0xaa" then enter "aa aa aa" and press the "calculate" button to calculate the 8-bit checksum. the checksum of string of all entered bytes and the checksum will be equal to zero.


2 Answers

I've also tried to avoid show2() because it print the packet. I've found in the source a better solution:

del packet.chksum packet = packet.__class__(bytes(packet)) 

This code regenerate the packet with the correct checksum without any print and actually is what show2() run in the background before printing.

like image 124
Almog Cohen Avatar answered Oct 18 '22 19:10

Almog Cohen


You need to delete the .chksum value from the packet after you create it; then call .show2()

>>> from scapy.layers.inet import IP >>> from scapy.layers.inet import ICMP >>> from scapy.layers.inet import TCP >>> target = "10.9.8.7" >>> ttl = 64 >>> id = 32711 >>> sport = 2927 >>> dport = 80 >>> pak = IP(dst=target, src = "100.99.98.97", ttl=ttl, flags="DF", id=id, len=1200, chksum = 0)/TCP(flags="S", sport=sport, dport=int(dport), options=[('Timestamp',(0,0))], chksum = 0) >>> del pak[IP].chksum >>> del pak[TCP].chksum >>> pak.show2() ###[ IP ]###   version   = 4L   ihl       = 5L   tos       = 0x0   len       = 1200   id        = 32711   flags     = DF   frag      = 0L   ttl       = 64   proto     = tcp   chksum    = 0x9afd   src       = 100.99.98.97   dst       = 10.9.8.7   \options   \ ###[ TCP ]###      sport     = 2927      dport     = www      seq       = 0      ack       = 0      dataofs   = 8L      reserved  = 0L      flags     = S      window    = 8192      chksum    = 0x2c0e      urgptr    = 0      options   = [('Timestamp', (0, 0)), ('EOL', None)] >>> 
like image 33
Mike Pennington Avatar answered Oct 18 '22 19:10

Mike Pennington