将十六进制字符串转换为Scapy中的数据包 - python

我的目的是从pcap文件中嗅探一个数据包,修改该数据包的最后4个字节并发送它。现在,我正在这样做:

from scapy.all import *
from struct import pack
from struct import unpack

pkt = sniff(offline="my.pcap", count=1)

pkt_hex = str(pkt)
# getting output like '\x00\x04\x00 ... ... \x06j]'

last_4 = unpack('!I',pkt_hex[-4:])[0]
# getting last 4 bytes and converting it to integer

rest = pkt_hex[:-4]
# getting whole packet in string except last 4 bytes

new_pkt = rest + pack('>I',(last_4+1))
# created the whole packet again with last 4 bytes incremented by 1
# the new string is like '\x00\x04\x00 ... ... \x06j^'

现在我的问题是我无法将其转换回Scapy的分层数据包对象,因此无法使用sendp发送。

PS:我必须重新计算校验和。但是一旦将其转换为数据包对象,就可以按照this重新计算校验和。

参考方案

您可以使用原始数据包的类来重建数据包,但是程序中还有其他错误。

sniff函数上的official API documentation指出它返回一个列表:

sniff(prn=None, lfilter=None, count=0, store=1, offline=None, L2socket=None, timeout=None)

嗅探来自网络的数据包,并将其返回到数据包列表中。

因此,不是使用pkt_hex = str(pkt)提取数据包,而是提取正确的格式为pkt_hex = str(pkt[0])

完成后,您可以随意更改数据包,更新其校验和(如建议的here)并使用原始数据包的类来重建它,如下所示(请注意我的评论):

from scapy.all import *
from struct import pack
from struct import unpack

pkts = sniff(offline="my.pcap", count=1)

pkt = pkts[0] # <--- NOTE: correctly extract the packet

del pkt.chksum # <--- NOTE: prepare for checksum recalculation
del pkt[TCP].chksum # <--- NOTE: prepare for TCP checksum recalculation (depends on the transport layer protocol in use)

pkt_hex = str(pkt)
# getting output like '\x00\x04\x00 ... ... \x06j' <--- NOTE: there is no trailing ']'

last_4 = unpack('!I',pkt_hex[-4:])[0]
# getting last 4 bytes and converting it to integer

rest = pkt_hex[:-4]
# getting whole packet in string except last 4 bytes

new_hex_pkt = rest + pack('>I',(last_4+1))
# created the whole packet again with last 4 bytes incremented by 1
# the new string is like '\x00\x04\x00 ... ... \x06k' <--- NOTE: 'j' was incremented to 'k' (rather than ']' to '^')

new_pkt = pkt.__class__(new_hex_pkt) # <--- NOTE: rebuild the packet and recalculate its checksum

sendp(new_pkt) # <--- NOTE: send the new packet

编辑:

请注意,这不会保留数据包的时间戳,时间戳会更改为当前时间。为了保留原始时间戳,请将其分配给new_pkt.time

new_pkt.time = pkt.time

但是,如here所述,即使更改了数据包的时间戳并发送后,由于the timestamp is set in the receiving machine as the packet is received,更新的时间戳也不会反映在另一端的接收数据包中。

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

如何用'-'解析字符串到节点js本地脚本? - python

我正在使用本地节点js脚本来处理字符串。我陷入了将'-'字符串解析为本地节点js脚本的问题。render.js:#! /usr/bin/env -S node -r esm let argv = require('yargs') .usage('$0 [string]') .argv; console.log(argv…

Python:传递记录器是个好主意吗? - python

我的Web服务器的API日志如下:started started succeeded failed 那是同时收到的两个请求。很难说哪一个成功或失败。为了彼此分离请求,我为每个请求创建了一个随机数,并将其用作记录器的名称logger = logging.getLogger(random_number) 日志变成[111] started [222] start…

Python-Excel导出 - python

我有以下代码:import pandas as pd import requests from bs4 import BeautifulSoup res = requests.get("https://www.bankier.pl/gielda/notowania/akcje") soup = BeautifulSoup(res.cont…