简体   繁体   English

如何修改 scapy 数据包有效载荷

[英]How to modify scapy packet payload

I have a python file that declares sets of packets to be sent through a system that modifies the payload and sends them back.我有一个 python 文件,它声明要通过修改有效负载并将它们发回的系统发送的数据包集。 A script imports the packets from the python file, sends and receives them and needs to be able to predict what the modified packets will look like when they come back.脚本从 python 文件导入数据包,发送和接收它们,并且需要能够预测修改后的数据包返回时的样子。

My question is, how can I produce packets with modified payload from the list of packets read from the file?我的问题是,如何从从文件读取的数据包列表中生成具有修改负载的数据包?

The input file defines packets with variable length headers, something like:输入文件定义了具有可变长度标头的数据包,例如:

payload_len = 50
pkts = (Ether()/IP()/Raw(payload_len*b'\x00'), \
        Ether()/IP()/TCP()/Raw(payload_len*b'\x00'), \
        Ether()/IP()/UDP()/Raw(payload_len*b'\x00')

The system that modifies the packets puts a four byte known tag (eg 0xdeadbeef ) in the payload.修改数据包的系统将一个四字节的已知标签(例如0xdeadbeef )放入有效载荷中。 It can put that tag either at the start or the end of the payload.它可以将该标签放在有效负载的开头或结尾。

So the script needs to do something like the following for every packet in the list:因此脚本需要对列表中的每个数据包执行如下操作:

from packet_list import *
predict = pkts
predict[0].payload[0] = b'\xde'
predict[0].payload[1] = b'\xad'
predict[0].payload[2] = b'\xbe'
predict[0].payload[3] = b'\xef'

or或者

predict[2].payload[payload_len-4] = b'\xde'
predict[2].payload[payload_len-3] = b'\xad'
predict[2].payload[payload_len-2] = b'\xbe'
predict[2].payload[payload_len-1] = b'\xef'

You can use load in order to access Raw bytes:您可以使用load来访问Raw字节:

for pkt in pkts:
    payload = pkt.lastlayer()
    payload.load = b"\xde\xad\xbe\xef" + payload.load[4:] # or payload.load[:-4] + b"\xde\xad\xbe\xef"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM