python修改pcap包指定字段的值

您所在的位置:网站首页 wireshark字段值 python修改pcap包指定字段的值

python修改pcap包指定字段的值

2024-07-03 02:18:17| 来源: 网络整理| 查看: 265

用wireshark抓包后要修改固定字段的值,代码如下:

实现思路:

1. 用wireshark 打开pcap包,找到要修改的字段位置

2.用scapy读取pcap包,取出指定位置的值,判断是否为要替换的值,如果是替换新值后,把数据流拼接成新的一条完成数据流,放入新的数据包中,如果不符合的不修改直接放回,具体代码如下:

 一次修改单一IP的值

from tqdm import tqdm from scapy.utils import PcapReader, PcapWriter import socket OLD_SOURCE_IP = '192.168.1.100' OLD_DEST_IP = '192.168.2.100' OLD_TEID = '0x0e60b0a4' NEW_TEID = '0xd970575e' NEW_SOURCE_IP = '172.16.1.100' NEW_DEST_IP = '172.16.1.200' i_filepath = "tpcu_200000.pcap" # pcap to read o_filepath = "out_{0}".format(i_filepath) # pcap to write i_open_file = PcapReader(i_filepath) o_open_file = PcapWriter(o_filepath) # opened file to write new_pkt = '' for packet in tqdm(i_open_file): byteArray = packet['Raw'].load # format ip source_ip = socket.inet_ntoa(byteArray[26:30]) des_ip = socket.inet_ntoa(byteArray[30:34]) if source_ip == OLD_SOURCE_IP and des_ip == OLD_DEST_IP: # ip to bytes new_source_ip = socket.inet_aton(NEW_SOURCE_IP) new_dest_ip = socket.inet_aton(NEW_DEST_IP) # hex to bytes new_teid = bytes.fromhex(NEW_TEID.replace('0x','')) new_pkt = byteArray[0:26] + new_source_ip + new_dest_ip + byteArray[34:46] + new_teid+byteArray[50:] # write new pacp package o_open_file.write(new_pkt) elif source_ip == OLD_DEST_IP and des_ip == OLD_SOURCE_IP: new_source_ip = socket.inet_aton(NEW_DEST_IP) new_dest_ip = socket.inet_aton(NEW_SOURCE_IP) new_teid = bytes.fromhex(NEW_TEID.replace('0x', '')) new_pkt = byteArray[0:26] + new_source_ip + new_dest_ip + byteArray[34:46] + new_teid + byteArray[50:] o_open_file.write(new_pkt) else: new_pkt = byteArray o_open_file.write(new_pkt)

  批量修改IP的值,dpkt 读取pcap包的性能是scapy的10倍以上,批量操作时读取用dpkt进行读取,替换时间从12s优化到3s左右。

from tqdm import tqdm from scapy.utils import PcapReader, PcapWriter import socket from config import * from dpkt.pcap import Reader ''' data = [{"source_ip":[old_source_ip,new_source_ip], "dest_ip":[old_dest_ip,new_dest_ip],"teid":[old_teid,new_teid]}, {"source_ip":[old_source_ip,new_source_ip], "dest_ip":[old_dest_ip,new_dest_ip],"teid":[old_teid,new_teid]} ] ''' # pcap to write o_filepath = "out_dpkt_{0}".format(i_filepath) i_open_file = PcapReader(i_filepath) # opened file to write o_open_file = PcapWriter(o_filepath) new_pkt = '' flags = 0 def bytes_format_ip(byte): return socket.inet_ntoa(byte) def ip_format_bytes(ip): return socket.inet_aton(ip) def hex_format_bytes(hex_value): return bytes.fromhex(hex_value.replace('0x','')) with open(i_filepath,'rb') as f: for _, byteArray in tqdm(Reader(f)): flags = 0 source_ip = bytes_format_ip(byteArray[26:30]) des_ip = bytes_format_ip(byteArray[30:34]) teid = byteArray[46:50] for i in data: if teid == hex_format_bytes(i["teid"][0]) and source_ip == i["source_ip"][0] and des_ip == i["dest_ip"][0]: new_source_ip = ip_format_bytes(i["source_ip"][-1]) new_dest_ip = ip_format_bytes(i["dest_ip"][-1]) new_teid = hex_format_bytes(i["teid"][-1]) new_pkt = byteArray[0:26] + new_source_ip + new_dest_ip + byteArray[34:46] + new_teid + byteArray[50:] o_open_file.write(new_pkt) flags = 1 break # Unqualified direct return if flags == 0: o_open_file.write(byteArray) # if __name__ == '__main__': # update_ip_teid()

  



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭