master-thesis/bin/checksum_from_scapy.py

72 lines
2 KiB
Python
Raw Normal View History

2019-06-23 11:56:49 +00:00
#!/usr/bin/env python3
# Code copied from /usr/lib/python3/dist-packages/scapy/utils.py
2019-06-23 12:19:06 +00:00
import struct
import array
2019-06-23 11:56:49 +00:00
if struct.pack("H",1) == b"\x00\x01": # big endian
def checksum(pkt):
if len(pkt) % 2 == 1:
pkt += b"\0"
s = sum(array.array("H", pkt))
s = (s >> 16) + (s & 0xffff)
s += s >> 16
s = ~s
return s & 0xffff
else:
def checksum(pkt):
2019-06-23 12:19:06 +00:00
# padding to even length
2019-06-23 11:56:49 +00:00
if len(pkt) % 2 == 1:
pkt += b"\0"
2019-06-23 12:19:06 +00:00
# create array with unsigned shorts,
# sum it up -> results in 16 bit uint (?)
2019-06-23 11:56:49 +00:00
s = sum(array.array("H", pkt))
print("sum={}".format(s))
# This step is understood
2019-06-23 12:19:06 +00:00
# (s & 0xffff) -> mask for 16 bit, truncate rest
# (s >> 16) right shift by 16 bits
# >>> 2**17 >> 16
# 2
# Basically:
# add higher 16 bits to lower 16 bits
2019-06-23 11:56:49 +00:00
s = (s >> 16) + (s & 0xffff)
2019-06-23 12:19:06 +00:00
# Now we add the remaining possible 16 bits
# i.e. above we take bits 0..15 + 16..31
# now we add [0..15 + 16..31] + "32..47"
# I assume this is for catching the carry
# over bits from the previous calculation
2019-06-23 11:56:49 +00:00
s += s >> 16
2019-06-23 12:19:06 +00:00
# according to https://stackoverflow.com/questions/8305199/the-tilde-operator-in-python
# ~ is the bitwise complement operator in python
# which essentially calculates -x - 1
# 2 complement
2019-06-23 11:56:49 +00:00
s = ~s
2019-06-23 12:19:06 +00:00
# (s>>8)&0xff) -> filter bits 8..15
# | = bitwise OR
# s<<8 : left shift everything in original s
# | s<<8: ?
# & 0xffff: filter / allow only 16 bits
# Seems like the idea is to filter correctly for
# 16 bit - unclear why "s & 0xffff" does not do the job
2019-06-23 11:56:49 +00:00
return (((s>>8)&0xff)|s<<8) & 0xffff
2019-06-23 12:19:06 +00:00
if __name__ == '__main__':
import sys
pkt = sys.argv[1]
pkt_bytes = pkt.encode('utf-8')
print(pkt_bytes)
print(checksum(pkt_bytes))
ipv4 = "AAAA"
ipv6 = "BBAA"