简体   繁体   English

用scapy指定数据包长度

[英]Specifying packet length with scapy

I'm trying to send a specific packet size (100 bytes) with scapy but cant seem to get it. 我正在尝试用scapy发送一个特定的数据包大小(100字节),但似乎无法得到它。

I'm using this to start. 我正在用它开始。

sr(IP(dst="192.168.1.1")/TCP(dport=443))

Looking at the docs / help I cant tell if I can use PacketLenField to specify the length of the packet. 查看文档/帮助我无法判断是否可以使用PacketLenField指定数据包的长度。 I can do it with NMAP & NSE but would like to do it outside of NMAP. 我可以用NMAP和NSE来做,但是想在NMAP之外做。

Any ideas on this one? 关于这个的任何想法?

Thanks! 谢谢!

You can just add on the required number of bytes as a String when crafting the packet eg: 您可以在制作数据包时添加所需的字节数作为字符串,例如:

payload = 'ZZZZZZZZZZZZZZZZZZZZZ'
pkt = Ether() / IP() / TCP() / payload

will work. 将工作。 You just need to adjust the length of the payload as you require. 您只需根据需要调整有效负载的长度。

Scapy's Raw() function populates the payload of the packet. Scapy的Raw()函数填充数据包的有效负载。 If you know your header size, you only need to fill in the remaining bytes with random data. 如果您知道标题大小,则只需使用随机数据填写剩余字节。

You can use RandString() to generate random padding. 您可以使用RandString()生成随机填充。 The following command sends a packet of length 100 (and listens for a response): 以下命令发送长度为100的数据包(并侦听响应):

sr(IP(dst="192.168.1.1")/TCP(dport=443)/Raw(RandString(size=72))

You may use inet.Padding() from scapy library: 您可以使用scapy库中的inet.Padding()

packet = IP(dst="192.168.1.1")/TCP(dport=443)

if len(packet)<100"
    #"\x00" is a single zero byte
    myString = "\x00"*(100 - len(packet))
    packet = packet/inet.Padding(myString)

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

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