提问人:B3st0fth3w0rst 提问时间:11/8/2023 更新时间:11/8/2023 访问量:37
转换包含 ASCII 字符的十六进制字符串
Convert HEX string that contains ASCII characters
问:
我在从 scapy 解码数据(或发送时编码)时遇到问题。
我正在尝试使用 scapy 发送一些 UDP HEX 数据,然后将其解码回原始形式,但收到的 HEX 字符串中包含 ASCII 字符。
from scapy.all import *
from scapy.layers.l2 import Ether
from scapy.layers.inet import IP, UDP
test_data = ["000ad74340b81e4941002900020202000200"] # This is actually read from a file.
source_ip = "192.168.1.12"
destination_ip = "192.168.1.4"
source_mac = "70:85:c2:f3:18:8e"
destination_mac = "80:ce:62:f2:94:22"
source_port = 5005
destination_port = 5005
tmp = []
byte_str = [(test_data[0][i:i + 2]) for i in range(0, len(test_data[0]), 2)]
for i in range(len(byte_str)):
tmp.append(int(f"0x{byte_str[i]}", 16))
data_to_send = bytearray(tmp)
print("b'" + ''.join(f'\\x{c:02x}' for c in data_to_send) + "'")
pkt = Ether(dst=destination_mac, src=source_mac)/\
IP(dst=destination_ip, src=source_ip, flags=2)/\
UDP(dport=destination_port, sport=source_port)/data_to_send
del pkt[IP].chksum
del pkt[UDP].chksum
pkt.show2()
sendp(pkt)
发送的内容
###[ Ethernet ]###
dst = 80:ce:62:f2:94:22
src = 70:85:c2:f3:18:8e
type = IPv4
###[ IP ]###
version = 4
ihl = 5
tos = 0x0
len = 46
id = 1
flags = DF
frag = 0
ttl = 64
proto = udp
chksum = 0xb75d
src = 192.168.1.12
dst = 192.168.1.4
\options \
###[ UDP ]###
sport = 5005
dport = 5005
len = 26
chksum = 0xaeed
###[ Raw ]###
load = '\x00\n\\xd7C@\\xb8\x1eIA\x00)\x00\x02\x02\x02\x00\x02\x00'
.
Sent 1 packets.
收到的是,我正试图弄清楚我是如何回到或类似的东西的。\x00\n\\xd7C@\\xb8\x1eIA\x00)\x00\x02\x02\x02\x00\x02\x00
000ad74340b81e4941002900020202000200
\x00\x0a\xd7\x43\x40\xb8\x1e\x49\x41\x00\x29\x00\x02\x02\x02\x00\x02\x00
当我转换为十六进制值列表时,然后将它们转换为列表整数以创建字节数组,然后我可以将其转换回。000ad74340b81e4941002900020202000200
['00', '0a', 'd7', '43', '40', 'b8', '1e', '49', '41', '00', '29', '00', '02', '02', '02', '00', '02', '00']
[0, 10, 215, 67, 64, 184, 30, 73, 65, 0, 41, 0, 2, 2, 2, 0, 2, 0]
bytearray(b'\x00\n\xd7C@\xb8\x1eIA\x00)\x00\x02\x02\x02\x00\x02\x00')
b'\x00\x0a\xd7\x43\x40\xb8\x1e\x49\x41\x00\x29\x00\x02\x02\x02\x00\x02\x00'
但是当我从 scapy UDP 数据包中读取有效负载信息时,我得到以下字符串,当我将其转换为字节串时,它仍然具有,我无法将 ot 转换回上面的原始十六进制数据。"b'\\x00\\n\\xd7C@\\xb8\\x1eIA\\x00)\\x00\\x02\\x02\\x02\\x00\\x02\\x00'"
\\
我怎样才能将str转换为single,这样我就可以用它来取回原始的十六进制数据?"\\x00\\n\\xd7C@\\xb8\\x1eIA\\x00)\\x00\\x02\\x02\\x02\\x00\\x02\\x00"
b'\x00\n\xd7C@\xb8\x1eIA\x00)\x00\x02\x02\x02\x00\x02\x00'
\
print("b'" + ''.join(f'\\x{c:02x}' for c in bytearray(RECEIVED_DATA) + "'")
答:
使用 和 在表示之间转换:bytes.hex()
str.fromhex()
>>> s = "000ad74340b81e4941002900020202000200" # ASCII string of hex digits
>>> s
'000ad74340b81e4941002900020202000200'
>>> b = bytes.fromhex(s)
>>> b # Default *display* representation of bytes.
b'\x00\n\xd7C@\xb8\x1eIA\x00)\x00\x02\x02\x02\x00\x02\x00'
>>> list(b) # decimal values of the bytes are correct
[0, 10, 215, 67, 64, 184, 30, 73, 65, 0, 41, 0, 2, 2, 2, 0, 2, 0]
>>> # Display a custom way (no ASCII). This is *just* for display.
>>> print("b'" + ''.join(f'\\x{n:02x}' for n in b) + "'")
b'\x00\x0a\xd7\x43\x40\xb8\x1e\x49\x41\x00\x29\x00\x02\x02\x02\x00\x02\x00'
>>> b.hex() # display bytes as hexadecimal string again
'000ad74340b81e4941002900020202000200'
>>> b.hex(sep=' ') # with a separator if desired.
'00 0a d7 43 40 b8 1e 49 41 00 29 00 02 02 02 00 02 00'
请注意,对象元素的类型是,因此您可以遍历它们并在需要时执行计算:bytes
int
>>> type(b[0])
<class 'int'>
>>> for n in b:
... print(f'{n:3d} {n:02X}')
...
0 00
10 0A
215 D7
67 43
64 40
184 B8
30 1E
73 49
65 41
0 00
41 29
0 00
2 02
2 02
2 02
0 00
2 02
0 00
评论
payload = str(packet[UDP].payload.load)
\
\\
payload = packet[UDP].payload.load
str
评论
\n
0x0a