在scapy中读取数据包时将十六进制转换为ascii——Python



我正试图将十六进制转换为ascii,但在这方面遇到了极大的困难--

>>> pkt[9][3].sprintf("%Raw.load%")
 "'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x1d\x01\x04\xfe\t\x00\xb4\xc0\xa8\x00!\x00'"
>>> pkt[9][3].sprintf("%Raw.load%").decode("hex")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/encodings/hex_codec.py", line 42, in hex_decode
output = binascii.a2b_hex(input)
TypeError: Odd-length string

在Scapy中,str()函数将在数据包上为您返回一个十六进制字符串。

来自Scapy十六进制字符串-

>>> pkt_str = str(pkt)
>>> pkt_str
'x00PVxfcxcePx00x0c)+Sx19x08x00Ex00x00Tx00x00@x00@x01Z|xc0xa8
x19x82x04x02x02x01x08x00x9cx90Zax00x01xe6xdapIxb6xe5x08x00
x08tnx0bx0crx0ex0fx10x11x12x13x14x15x16x17x18x19x1ax1b
x1cx1dx1ex1f !"#$%&'()*+,-./01234567'

一旦你转换了它,你就可以把它重新移植回Scapy。我指出的文件中都提到了这一点。

最新更新