def __calc_udp_csum(saddr, daddr, udp_data, is_ipv6=False): size = len(udp_data) seq = [ saddr, daddr, b'\x00\x11', utils.number2bytes(size, 2), udp_data, ] if is_ipv6: size += 24 else: size += 12 if 0 != size % 2: seq.append(b"\0") size += 1 data = b"".join(seq) csum = socket.htons(fn_utils.calc_csum(data, size)) # csum = __calc_checksum(data, size) # import socket # print(csum,socket.htons(csum_t)) if csum == 0: return 0xffff return csum
def fill_ip_hdr_checksum(pkt_list): """填充IP包头校检和 :param pkt: 列表形式的IP数据包,比如[11,23,...] :return: """ hdr_len = (pkt_list[0] & 0x0f) * 4 checksum = fn_utils.calc_csum(bytes(pkt_list), hdr_len) pkt_list[10:12] = ( (checksum & 0xff00) >> 8, checksum & 0x00ff, ) return
def build_udp_packets(saddr, daddr, sport, dport, message, mtu=1500, is_udplite=False, is_ipv6=False): """构建UDP数据包""" if mtu > 1500 or mtu < 576: raise ValueError("the value of mtu is wrong!") msg_len = 8 + len(message) # 构建UDP数据头 udp_hdr = [ (sport & 0xff00) >> 8, sport & 0x00ff, (dport & 0xff00) >> 8, dport & 0x00ff, ] if is_udplite: udp_hdr += [ 0, 8, 0, 0, ] csum = fn_utils.calc_csum(bytes(udp_hdr)) udp_hdr[6] = (csum & 0xff00) >> 8 udp_hdr[7] = csum & 0xff else: udp_hdr += [ (msg_len & 0xff00) >> 8, msg_len & 0x00ff, 0, 0, ] if not is_udplite: csum = __calc_udp_csum(saddr, daddr, b"".join([bytes(udp_hdr), message]), is_ipv6=is_ipv6) udp_hdr[6] = (csum & 0xff00) >> 8 udp_hdr[7] = csum & 0xff pkt_data = b"".join(( bytes(udp_hdr), message, )) if not is_ipv6: pkt_id = random.randint(1, 65535) else: pkt_id = random.randint(1, 0xffffffff) flow_label = random.randint(1, 0x0fffff) pkts = [] flags_df = 1 if is_udplite: p = 136 else: p = 17 if is_ipv6 and mtu - 40 <= msg_len: ipv6hdr = __build_ipv6_hdr(flow_label, msg_len, p, 128, saddr, daddr) ip6data = b"".join([ ipv6hdr, message, ]) return [ ip6data, ] if is_ipv6: # IPV6分包在扩展头提供,因此要算成48 ip_hdr_size = 48 else: ip_hdr_size = 20 step = mtu - ip_hdr_size - (mtu - ip_hdr_size) % 8 b = 0 e = step # pkt_id = 1 n = 0 every_offset = int(step / 8) while 1: bdata = pkt_data[b:e] slice_size = len(bdata) if e >= msg_len: finish = True else: finish = False if finish: flags_mf = 0 else: flags_mf = 1 offset = n * every_offset if not is_ipv6: ippkt = build_ip_packet(slice_size + 20, p, saddr, daddr, bdata, pkt_id=pkt_id, flags_df=flags_df, flags_mf=flags_mf, offset=offset) else: ipv6hdr = __build_ipv6_hdr(flow_label, slice_size, 44, 128, saddr, daddr) frag_hdr = __build_ipv6_fragment_hdr(p, offset, flags_mf, pkt_id) ippkt = b"".join([ ipv6hdr, frag_hdr, bdata, ]) pkts.append(ippkt) if finish: break n += 1 b = e e = b + step return pkts
def build_udp_packets(saddr, daddr, sport, dport, message, mtu=1500, is_udplite=False, is_ipv6=False): """构建UDP数据包""" if mtu > 1500 or mtu < 576: raise ValueError("the value of mtu is wrong!") msg_len = 8 + len(message) # 构建UDP数据头 udp_hdr = [ (sport & 0xff00) >> 8, sport & 0x00ff, (dport & 0xff00) >> 8, dport & 0x00ff, ] if is_udplite: udp_hdr += [ 0, 8, 0, 0, ] csum = fn_utils.calc_csum(bytes(udp_hdr)) udp_hdr[6] = (csum & 0xff00) >> 8 udp_hdr[7] = csum & 0xff else: udp_hdr += [ (msg_len & 0xff00) >> 8, msg_len & 0x00ff, 0, 0, ] if not is_udplite: csum = __calc_udp_csum(saddr, daddr, b"".join([bytes(udp_hdr), message]), is_ipv6=is_ipv6) udp_hdr[6] = (csum & 0xff00) >> 8 udp_hdr[7] = csum & 0xff pkt_data = b"".join( (bytes(udp_hdr), message,) ) if not is_ipv6: pkt_id = random.randint(1, 65535) else: pkt_id = random.randint(1, 0xffffffff) flow_label = random.randint(1, 0x0fffff) pkts = [] flags_df = 1 if is_udplite: p = 136 else: p = 17 if is_ipv6 and mtu - 40 <= msg_len: ipv6hdr = __build_ipv6_hdr(flow_label, msg_len, p, 128, saddr, daddr) ip6data = b"".join([ipv6hdr, message, ]) return [ip6data, ] if is_ipv6: # IPV6分包在扩展头提供,因此要算成48 ip_hdr_size = 48 else: ip_hdr_size = 20 step = mtu - ip_hdr_size - (mtu - ip_hdr_size) % 8 b = 0 e = step # pkt_id = 1 n = 0 every_offset = int(step / 8) while 1: bdata = pkt_data[b:e] slice_size = len(bdata) if e >= msg_len: finish = True else: finish = False if finish: flags_mf = 0 else: flags_mf = 1 offset = n * every_offset if not is_ipv6: ippkt = build_ip_packet(slice_size + 20, p, saddr, daddr, bdata, pkt_id=pkt_id, flags_df=flags_df, flags_mf=flags_mf, offset=offset ) else: ipv6hdr = __build_ipv6_hdr(flow_label, slice_size, 44, 128, saddr, daddr) frag_hdr = __build_ipv6_fragment_hdr(p, offset, flags_mf, pkt_id) ippkt = b"".join([ipv6hdr, frag_hdr, bdata, ]) pkts.append(ippkt) if finish: break n += 1 b = e e = b + step return pkts