Skip to content

Instantly share code, notes, and snippets.

@alexander-hanel
Last active April 8, 2019 17:26
Show Gist options
  • Save alexander-hanel/4320d1974284673f79cd303ddea643f9 to your computer and use it in GitHub Desktop.
Save alexander-hanel/4320d1974284673f79cd303ddea643f9 to your computer and use it in GitHub Desktop.
def hexdump(src, length=16):
FILTER = ''.join([(len(repr(chr(x))) == 3) and chr(x) or '.' for x in range(256)])
lines = []
for c in xrange(0, len(src), length):
chars = src[c:c+length]
hex = ' '.join(["%02x" % ord(x) for x in chars])
printable = ''.join(["%s" % ((ord(x) <= 127 and FILTER[ord(x)]) or '.') for x in chars])
lines.append("%04x %-*s %s\n" % (c, length*3, hex, printable))
return ''.join(lines)
def decoder(data, key):
decoded = ""
for count, byte in enumerate(data):
k = key[count % len(key)]
temp = chr(ord(k) ^ ord(byte))
decoded += temp
return decoded
def extract(index):
key = idc.get_bytes(index-6, 4)
size = Word(index - 2)
return key, size
def get_string(k,s,i):
"""type can be encoded-ascii, encoded-wide or non-encoded ascii"""
temp_str = idc.get_bytes(i,s)
if k == "\x00\x00\x00\x00":
return temp_str.replace("\x00","")
temp_data = idc.get_bytes(i,s)
temp_str = decoder(temp_data, k)
if "\x00" in temp_str[:4]:
# decode wide char
temp_data = idc.get_bytes(i,s*2)
temp_str = decoder(temp_data, k).replace("\x00","")
if all(c in string.printable for c in temp_str):
return temp_str
else:
return None
def dec_here():
i = here()
k,s = extract(i)
print get_string(k,s,i)
idc.set_cmt(i, get_string(k,s,i),1)
def dec_ops():
i = idc.get_operand_value(here(),1)
k,s = extract(i)
print get_string(k,s,i)
idc.set_cmt(i, get_string(k,s,i), 1)
dec_here()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment