在网络安全中,我们通常使用strings
二进制文件来提取内存转储中的任何明文字符串数据。我试着用Python来做同样的事情。
from struct import unpack
find_str = "King"
strings = []
for stream in data.streams:
if type(stream.data) == bytes:
# Not a particular readable solution, but orders of magnitude faster than
# alternatives: https://stackoverflow.com/a/57543519/9400421
unpacked = list(unpack(f'{len(stream.data)}c', stream.data))
string = ''
null = b'\x00'
for byte in unpacked:
try:
# ultimately need to track multiple strings arrays for each
# popular encoding scheme to catch diverse string encodings.
decoded = byte.decode('ASCII')
print(byte, '=>', decoded)
if byte == null:
print(byte, '=>', 'null')
if string != '':
strings.append(string)
string = ''
else:
string += decoded
except:
print("couldn't decode:", byte)
if string != '':
strings.append(string)
string = ''
print(strings)
输出:. , '*', '\x7f', '\x10', '\x10', '\x04', '\x01', '\x12+', '\x7f', '*', '\x7f', '@', '\x10', '\x02', '\x01', '\x10\x13+', '\x7f', '\x0c', '\x01',
我的问题是,这输出了大量的解码值,这些值显然不是普通字符--它们被解码为十六进制字符串。
我的第一个问题是:为什么这些十六进制字符串不解码为普通字符,但不触发我的catch
语句?我认为任何通过解码方法“干净”地解码为字符的东西都会被我的代码过滤掉。
我的第二个问题是:如何丢弃“垃圾”字符/从“干净”解码字符中过滤它们?
发布于 2022-09-13 06:15:32
解决方案归结为这一点,即将字节解码为字符串,并且只保留可打印的字符。
>>> data = b"A \x04 test \x12 string\x00\x00\x00."
>>> ''.join([x for x in data.decode('ascii') if x.isprintable()])
'A test string.'
看起来您的代码可以简化为:
stream_strings = []
for stream in data.streams:
if type(stream.data) == bytes:
result = ''.join([x for x in stream.data.decode('ascii') if x.isprintable()])
stream_strings.append(result)
print(stream_strings)
https://stackoverflow.com/questions/73696814
复制相似问题