转换包含 ASCII 字符的 HEX 字符串

问题描述 投票:0回答:1

我在从 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 数据包中读取有效负载信息时,我得到以下字符串

"b'\\x00\\n\\xd7C@\\xb8\\x1eIA\\x00)\\x00\\x02\\x02\\x02\\x00\\x02\\x00'"
,当我将其转换为字节串时,它仍然具有
\\
并且我无法将 ot 转换回原始十六进制以上数据。

如何使用单个

"\\x00\\n\\xd7C@\\xb8\\x1eIA\\x00)\\x00\\x02\\x02\\x02\\x00\\x02\\x00"
将 str
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) + "'")
取回原始十六进制数据?

python udp hex ascii scapy
1个回答
0
投票

使用

bytes.hex()
str.fromhex()
在表示形式之间进行转换:

>>> s = "000ad74340b81e4941002900020202000200"  # ASCII string of hex digits
>>> s
'000ad74340b81e4941002900020202000200'
>>> b = bytes.fromhex(s)
>>> b  # *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
[0, 10, 215, 67, 64, 184, 30, 73, 65, 0, 41, 0, 2, 2, 2, 0, 2, 0]
>>> 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'
© www.soinside.com 2019 - 2024. All rights reserved.