简体   繁体   中英

Converting a scapy packet to string produces an E?

I'm currently working on using scapy for sending data packets, and I've run into a weird issue. When I create a packet as such:

pack = IP(dst="127.0.0.1", id=local_ID)/UDP()/chunk

and then convert that packet to a string (so I can send it via a socket)

sendPack = str(pack)

the result of sendPack is wrong.

For instance, in my test file, I have the numbers 1-8000 ordered as such

1
2
3
...

then, when I print("SEND_PACK: "+sendPack) it produces the following:

E
2
3
...

Everything else is perfect except for the E

I can't understand where that E is coming from, or what it means.

It's also worth noting that I have verified that pack contains the correct data, and that regardless of what the first line of the test file is, the first line of the output is always an E

Thanks!

To those interested, I fixed the issue by doing the following:

As pointed out above, the E was a result of me printing the packet, not it's contents. In order to access the contents I wanted, I had to do the following:

sendPack = pack[UDP].load       #Get the packet's load at the UDP layer
id = pack[IP].ID                #Get the ID at the IP layer

The documentation for Scapy is sparse, so I didn't realize that I could access the individual fields of each packet this way.

Here's where I found this fix

First, you are printing a packet, not the content of your UDP datagram. The first two fields of an IPv4 packet are version and IHL which values are respectively 4 (for IP version 4) and 5 (5 words of 32 bits) by default in Scapy. This gives 45 if you put it on one byte in hexadecimal which is letter "E" in ascii. This is why you are always seeing an E as the first byte of your string.

If you want to check what is in your packet, you should use Scapy's show() function: sendPack.show() .

Second, you could use Scapy's send() , function to send your packet without bothering about sockets yourself: send(sendPack)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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