如何使用Python将十六进制字符串转换为pcap文件?

时间:2015-09-26 09:46:13

标签: python string hex converter pcap

我有一个Python脚本,它将文件转换为十六进制字符串。现在我需要将这些字符串转换为pcap文件。从png文件转换后的十六进制字符串是大约174页的Word文件。代码,我用它来制作一个字符串:

 filename = 'test.png'
 with open(filename, 'rb') as f:
 img = f.read()
 print(binascii.hexlify(img))

那么,甚至可以做这样的转换器吗?

1 个答案:

答案 0 :(得分:1)

您可以使用工具text2pcap工具(wireshark tool

这是使用此工具的函数的示例:

def string_to_pcap_file(packet_string, output_pcap_file):
    cmd = "echo 0000    " + packet_string + " >> tmp.txt"
    os.system(cmd)
    cmd = "text2pcap " + "tmp.txt " + output_pcap_file 
    os.system(cmd)
    os.system("rm tmp.txt")

mac_da = '00 11 22 33 44 55 '
mac_sa = '55 55 55 55 55 55 '
vlan = '81 00 11 22 '
ether_type = '08 00 '
data = '45 00 00 2a 04 d2 00 00 7f 06 36 fd 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 '
temp_str = mac_da + mac_sa + ether_type + data

string_to_pcap_file(temp_str, 'new_pcap_file.pcap')