checksumming part finish
This commit is contained in:
parent
b757a3c2b0
commit
92e1407729
2 changed files with 112 additions and 11 deletions
|
|
@ -1,7 +1,77 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
#from __future__ import unicode_literals
|
||||
import ipaddress
|
||||
from scapy.all import *
|
||||
import array
|
||||
import struct
|
||||
|
||||
# stolen from scapy (little endian system)
|
||||
def checksum_scapy(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>>8)&0xff)|s<<8) & 0xffff
|
||||
|
||||
|
||||
# 1. convert to array of "bytes"
|
||||
# 2. import into an array
|
||||
# 3. sum everything up
|
||||
|
||||
|
||||
def sum_for_udp(packet):
|
||||
sums = ""
|
||||
sums += struct.pack("H", packet[UDP].sport)
|
||||
sums += struct.pack("H", packet[UDP].dport)
|
||||
sums += struct.pack("H", packet[UDP].len)
|
||||
|
||||
return sums
|
||||
|
||||
def sum_for_v6(packet):
|
||||
# hdr.ipv6.src_addr, /* 128 */
|
||||
# hdr.ipv6.dst_addr, /* 128 */
|
||||
# meta.length_without_ip_header, /* 32 */
|
||||
# 24w0, /* 24 */
|
||||
# hdr.ipv6.next_header, /* 8 */
|
||||
# /* total: 324 */
|
||||
|
||||
# // UDP header
|
||||
# hdr.udp.src_port, /* 16 */
|
||||
# hdr.udp.dst_port, /* 16 */
|
||||
# hdr.udp.payload_length /* 16 */
|
||||
# /* all: 372 */
|
||||
|
||||
# order does not matter!
|
||||
sums = ""
|
||||
|
||||
sums += ipaddress.IPv6Address(packet[IPv6].src.decode("utf-8")).packed
|
||||
sums += ipaddress.IPv6Address(packet[IPv6].dst.decode("utf-8")).packed
|
||||
sums += struct.pack("H", packet[IPv6].plen)
|
||||
sums += struct.pack("B", packet[IPv6].nh)
|
||||
|
||||
print("{} - {} - {}".format(len(sums), sums, checksum_scapy(sums)))
|
||||
|
||||
return sums
|
||||
|
||||
def sum_for_v4(packet):
|
||||
# Get diffs -- for UDP
|
||||
# udp_v4 =
|
||||
# hdr.ipv4.src_addr,
|
||||
# hdr.ipv4.dst_addr,
|
||||
# 8w0,
|
||||
# hdr.ipv4.protocol,
|
||||
# meta.length_without_ip_header,
|
||||
sums = ""
|
||||
|
||||
sums += ipaddress.IPv4Address(packet[IP].src.decode("utf-8")).packed
|
||||
sums += ipaddress.IPv4Address(packet[IP].dst.decode("utf-8")).packed
|
||||
sums += struct.pack("H", packet[IP].len - 20) # -20 for ip header
|
||||
sums += struct.pack("H", packet[IP].proto) # udp / tcp
|
||||
|
||||
return sums
|
||||
|
||||
if __name__ == '__main__':
|
||||
p = []
|
||||
|
|
@ -41,18 +111,16 @@ if __name__ == '__main__':
|
|||
chk_new = packet_rebuild[UDP].chksum
|
||||
print("chk1 = {} chk2={}".format(chk_old, chk_new))
|
||||
|
||||
# Get diffs -- for UDP
|
||||
# udp_v4 =
|
||||
# hdr.ipv4.src_addr,
|
||||
# hdr.ipv4.dst_addr,
|
||||
# 8w0,
|
||||
# hdr.ipv4.protocol,
|
||||
# meta.length_without_ip_header,
|
||||
sums = ""
|
||||
|
||||
# // UDP header
|
||||
# hdr.udp.src_port,
|
||||
# hdr.udp.dst_port,
|
||||
# hdr.udp.payload_length
|
||||
if IP in packet:
|
||||
sums += sum_for_v4(packet_rebuild)
|
||||
if IPv6 in packet:
|
||||
sums += sum_for_v6(packet_rebuild)
|
||||
# if UDP in packet:
|
||||
# sums += sum_for_udp(packet_rebuild)
|
||||
|
||||
print("Checksum-parts {} for {}".format(checksum_scapy(sums), packet_rebuild.__repr__()))
|
||||
|
||||
|
||||
# Checksums:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue