def build_ip_packet(pkt_len, protocol, saddr, daddr, message, pkt_id=1, flags_df=0, flags_mf=0, offset=0): """创建IP数据包 :param pkt_len:包长度 :param saddr: bytes类型的源地址 :param daddr: bytes类型的目的地址 :param message:消息内容 :param pkt_id: 包ID :param flags_df: 分段df位 :param flags_mf:分段mf位 :param offset:包偏移 :return ip_pkt: """ if pkt_len < __IP_HDR_SIZE: raise ValueError("the value of pkt_len must be less than 20") if protocol < 0 or protocol > 255: raise ValueError("the value of protocol is wrong") tpl = b'E\x00\x00\x14\x00\x01\x00\x00@\x00z\xea\x00\x00\x00\x00\x00\x00\x00\x00' L = list(tpl) # 修改地址 csum = (L[10] << 8) | L[11] csum = checksum.calc_checksum_for_ip_change(tpl[12:16], saddr, csum) csum = checksum.calc_checksum_for_ip_change(tpl[16:20], daddr, csum) L[12:16] = saddr L[16:20] = daddr # 修改包长度 old_v = (L[2] << 8) | L[3] new_v = pkt_len csum = fn_utils.calc_incre_csum(csum, old_v, new_v) L[2:4] = ((pkt_len & 0xff00) >> 8, pkt_len & 0x00ff,) # 修改包ID old_v = (L[4] << 8) | L[5] new_v = pkt_id csum = fn_utils.calc_incre_csum(csum, old_v, new_v) L[4:6] = ((pkt_id & 0xff00) >> 8, pkt_id & 0x00ff,) # 修改flags以及offset old_v = (L[6] << 8) | L[7] new_v = (flags_df << 14) | (flags_mf << 13) | offset csum = fn_utils.calc_incre_csum(csum, old_v, new_v) L[6:8] = ((new_v & 0xff00) >> 8, new_v & 0x00ff,) # 修改协议 old_v = L[9] new_v = protocol csum = fn_utils.calc_incre_csum(csum, old_v, new_v) L[9] = protocol # 修改校检和 # L[10:12] = (0, 0,) # csum = fn_utils.calc_csum(bytes(L), 20) L[10:12] = ((csum & 0xff00) >> 8, csum & 0x00ff,) return b"".join((bytes(L), message,))
def __modify_src_address(self, new_ip_pkt, pkt_list): """修改源地址""" old_checsum = (pkt_list[10] << 8) | pkt_list[11] new_csum = checksum.calc_checksum_for_ip_change( bytes(pkt_list[12:16]), new_ip_pkt, old_checsum ) pkt_list[10:12] = ((new_csum & 0xff00) >> 8, new_csum & 0x00ff,) pkt_list[12:16] = new_ip_pkt
def build_ip_packet(pkt_len, protocol, saddr, daddr, message, pkt_id=1, flags_df=0, flags_mf=0, offset=0): """创建IP数据包 :param pkt_len:包长度 :param saddr: bytes类型的源地址 :param daddr: bytes类型的目的地址 :param message:消息内容 :param pkt_id: 包ID :param flags_df: 分段df位 :param flags_mf:分段mf位 :param offset:包偏移 :return ip_pkt: """ if pkt_len < __IP_HDR_SIZE: raise ValueError("the value of pkt_len must be less than 20") if protocol < 0 or protocol > 255: raise ValueError("the value of protocol is wrong") tpl = b'E\x00\x00\x14\x00\x01\x00\x00@\x00z\xea\x00\x00\x00\x00\x00\x00\x00\x00' L = list(tpl) # 修改地址 csum = (L[10] << 8) | L[11] csum = checksum.calc_checksum_for_ip_change(tpl[12:16], saddr, csum) csum = checksum.calc_checksum_for_ip_change(tpl[16:20], daddr, csum) L[12:16] = saddr L[16:20] = daddr # 修改包长度 old_v = (L[2] << 8) | L[3] new_v = pkt_len csum = fn_utils.calc_incre_csum(csum, old_v, new_v) L[2:4] = ( (pkt_len & 0xff00) >> 8, pkt_len & 0x00ff, ) # 修改包ID old_v = (L[4] << 8) | L[5] new_v = pkt_id csum = fn_utils.calc_incre_csum(csum, old_v, new_v) L[4:6] = ( (pkt_id & 0xff00) >> 8, pkt_id & 0x00ff, ) # 修改flags以及offset old_v = (L[6] << 8) | L[7] new_v = (flags_df << 14) | (flags_mf << 13) | offset csum = fn_utils.calc_incre_csum(csum, old_v, new_v) L[6:8] = ( (new_v & 0xff00) >> 8, new_v & 0x00ff, ) # 修改协议 old_v = L[9] new_v = protocol csum = fn_utils.calc_incre_csum(csum, old_v, new_v) L[9] = protocol # 修改校检和 # L[10:12] = (0, 0,) # csum = fn_utils.calc_csum(bytes(L), 20) L[10:12] = ( (csum & 0xff00) >> 8, csum & 0x00ff, ) return b"".join(( bytes(L), message, ))