从字节转换为字符串不起作用 // Python/Scapy

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

我在从数据类型字节转换为字符串时遇到了一些麻烦。 我正在使用 scapy 生成 dns 查询,捕获响应,现在我正在尝试使用此响应。

这里是生成和捕获的代码以及结果:

from scapy.all import *

dns = IP()/UDP()/DNS()
dns[IP].dst = "194.25.0.70"
dns["DNS Question Record"].qname = "12354987.xxxx.xxxx.de"
dns["DNS Question Record"].qtype = 35

ans, unans = sr(dns) 
ans.summary()

输出:

Begin emission:
Finished sending 1 packets.


Received 4 packets, got 1 answers, remaining 0 packets
IP / UDP / DNS Qry "b'12354987.xxxx.xxxx.de.'"  ==> IP / UDP / DNS Ans "b'\x00\n\x00\x00\x01s\x08SIPS+D2T\x00\x05_sips\x04_tcp\x0c12354987\x07xxxx\x0bxxxx\x02de\x00'" 

这个效果很好。下一步是提取响应的相关部分:

naptr = ans[0][1][UDP][DNS][DNSRR].rdata

输出:

b'\x00\n\x00\x00\x01s\x08SIPS+D2T\x00\x05_sips\x04_tcp\x0c12354987\x07xxxxy\x0bxxxx\x02de\x00'

我尝试使用 .decode()、.decode('utf-8')、.decode('ascii')、str(naptr) 和 scapy 文档中找到的一些函数来转换这种类型的字节。但注意确实有效。 每次我得到相同的结果:

'\x00\n\x00\x00\x01s\x08SIPS+D2T\x00\x05_sips\x04_tcp\x0c12354987\x07xxxx\x0bxxxx\x02de\x00'

您有什么想法吗?或者这里有人已经使用过 scapy 吗? 非常感谢。

BR 丹尼斯

python byte scapy decoding
1个回答
0
投票

您提供的二进制数据似乎代表 DNS NAPTR 记录。在这种情况下,您可能需要解释 NAPTR 记录的结构并提取相关信息。 NAPTR 记录格式包括各种字段,如顺序、首选项、标志、服务、正则表达式和替换。

from scapy.all import *

# Create DNS query
dns = IP()/UDP()/DNS()
dns[IP].dst = "194.25.0.70"
dns["DNS Question Record"].qname = "12354987.xxxx.xxxx.de"
dns["DNS Question Record"].qtype = 35

# Send DNS query and receive response
ans, unans = sr(dns) 

# Print summary of the response
ans.summary()

# Extract the NAPTR record
naptr = ans[0][1][UDP][DNS][DNSRR].rdata

# Assuming the format of the NAPTR record is something like:
# HHHH16sHHHHHHHHHs

# Unpack the binary data
decoded_data = struct.unpack("!HHHH16sHHHHHHHHHs", naptr)

# Extract individual fields
order, preference, flags, service, regex, replacement = decoded_data

# Convert bytes to string for certain fields
service_str = service.decode('utf-8')
regex_str = regex.decode('utf-8')
replacement_str = replacement.decode('utf-8')

# Print the extracted information
print(f"Order: {order}")
print(f"Preference: {preference}")
print(f"Flags: {flags}")
print(f"Service: {service_str}")
print(f"Regex: {regex_str}")
print(f"Replacement: {replacement_str}")
© www.soinside.com 2019 - 2024. All rights reserved.