【发布时间】:2020-09-18 04:46:18
【问题描述】:
我想得到一串原始字节(汇编代码)而不编码为另一种编码。由于字节的内容是shellcode,我不需要对其进行编码,而是想直接将其写为原始字节。 通过简化,我想将 "b'\xb7\x00\x00\x00'" 转换为 "\xb7\x00\x00\x00" 并获取原始字节的字符串表示形式。 例如:
>> byte_code = b'\xb7\x00\x00\x00\x05\x00\x00\x00\x95\x00\x00\x00\x00\x00\x00\x00'
>> uc_str = str(byte_code)[2:-1]
>> print(byte_code, uc_str)
b'\xb7\x00\x00\x00\x05\x00\x00\x00\x95\x00\x00\x00\x00\x00\x00\x00' \xb7\x00\x00\x00\x05\x00\x00\x00\x95\x00\x00\x00\x00\x00\x00\x00
目前我只有两个丑陋的方法,
>> uc_str = str(byte_code)[2:-1]
>> uc_str = "".join('\\x{:02x}'.format(c) for c in byte_code)
原始字节使用情况:
>> my_template = "const char byte_code[] = 'TPL'"
>> uc_str = str(byte_code)[2:-1]
>> my_code = my_template.replace("TPL", uc_str)
# then write my_code to xx.h
有没有pythonic方法可以做到这一点?
【问题讨论】:
标签: python-3.x