Fix asmsearch in Python 3
This commit is contained in:
parent
c94ba0d5d7
commit
f186b2127e
|
@ -37,7 +37,7 @@ class Nasm(object):
|
|||
asmcode = decode_string_escape(asmcode)
|
||||
asmcode = re.sub("PTR|ptr|ds:|DS:", "", asmcode)
|
||||
infd = tmpfile()
|
||||
outfd = tmpfile()
|
||||
outfd = tmpfile(binary_file=True)
|
||||
infd.write(asmcode)
|
||||
infd.flush()
|
||||
execute_external_command("%s -f bin -o %s %s" % (config.NASM, outfd.name, infd.name))
|
||||
|
|
39
lib/utils.py
39
lib/utils.py
|
@ -726,7 +726,7 @@ def _decode_string_escape_py3(str_):
|
|||
"""
|
||||
|
||||
# Based on: http://stackoverflow.com/a/4020824
|
||||
return str_.decode("unicode_escape")
|
||||
return codecs.decode(str_, "unicode_escape")
|
||||
|
||||
|
||||
def decode_string_escape(str_):
|
||||
|
@ -793,15 +793,52 @@ def _bytes_chr_py3(i):
|
|||
return bytes([i])
|
||||
|
||||
|
||||
def to_binary_string(text):
|
||||
"""
|
||||
Converts a string to a binary string if it is not already one. Returns a str
|
||||
in Python 2 and a bytes in Python3.
|
||||
|
||||
Use this instead of six.b when the text may already be a binary type
|
||||
"""
|
||||
raise Exception('Should be overriden')
|
||||
|
||||
|
||||
def _to_binary_string_py2(text):
|
||||
"""
|
||||
Converts a string to a binary string if it is not already one. Returns a str
|
||||
in Python 2 and a bytes in Python3.
|
||||
|
||||
Do not use directly, use to_binary_string instead.
|
||||
"""
|
||||
return str(text)
|
||||
|
||||
|
||||
def _to_binary_string_py3(text):
|
||||
"""
|
||||
Converts a string to a binary string if it is not already one. Returns a str
|
||||
in Python 2 and a bytes in Python3.
|
||||
|
||||
Do not use directly, use to_binary_string instead.
|
||||
"""
|
||||
if isinstance(text, six.binary_type):
|
||||
return text
|
||||
elif isinstance(text, six.string_types):
|
||||
return six.b(text)
|
||||
else:
|
||||
raise Exception('only takes string types')
|
||||
|
||||
|
||||
# Select functions based on Python version
|
||||
if six.PY2:
|
||||
decode_string_escape = _decode_string_escape_py2
|
||||
bytes_iterator = _bytes_iterator_py2
|
||||
bytes_chr = _bytes_chr_py2
|
||||
to_binary_string = _to_binary_string_py2
|
||||
elif six.PY3:
|
||||
decode_string_escape = _decode_string_escape_py3
|
||||
bytes_iterator = _bytes_iterator_py3
|
||||
bytes_chr = _bytes_chr_py3
|
||||
to_binary_string = _to_binary_string_py3
|
||||
else:
|
||||
raise Exception("Could not identify Python major version")
|
||||
|
||||
|
|
19
peda.py
19
peda.py
|
@ -2593,7 +2593,7 @@ class PEDA(object):
|
|||
magic_bytes = ["0x00", "0xff", "0xdead", "0xdeadbeef", "0xdeadbeefdeadbeef"]
|
||||
|
||||
ops = [x for x in asmcode.split(';') if x]
|
||||
def buildcode(code="", pos=0, depth=0):
|
||||
def buildcode(code=b"", pos=0, depth=0):
|
||||
if depth == wildcard and pos == len(ops):
|
||||
yield code
|
||||
return
|
||||
|
@ -2603,6 +2603,7 @@ class PEDA(object):
|
|||
elif c == 0:
|
||||
asm = self.assemble(ops[pos])
|
||||
if asm:
|
||||
print(repr(code) + '; ' + repr(asm))
|
||||
for code in buildcode(code + asm, pos+1, depth):
|
||||
yield code
|
||||
else:
|
||||
|
@ -2626,16 +2627,16 @@ class PEDA(object):
|
|||
|
||||
for machine_code in buildcode():
|
||||
search = re.escape(machine_code)
|
||||
search = search.replace(decode_hex_escape(b"dead"),"..")\
|
||||
.replace(decode_hex_escape(b"beef"),"..")\
|
||||
.replace(decode_hex_escape(b"00"),".")\
|
||||
.replace(decode_hex_escape(b"ff"),".")
|
||||
search = search.replace(decode_hex_escape(b"dead"), b"..")\
|
||||
.replace(decode_hex_escape(b"beef"), b"..")\
|
||||
.replace(decode_hex_escape(b"00"), b".")\
|
||||
.replace(decode_hex_escape(b"ff"), b".")
|
||||
|
||||
if rop and 'ret' not in asmcode:
|
||||
search = search + ".{0,24}\\xc3"
|
||||
searches.append("%s" % (search))
|
||||
search += b".{0,24}\\xc3"
|
||||
searches.append(search)
|
||||
|
||||
search = "(?=(%s))" % ("|".join(searches))
|
||||
search = b"(?=(" + b"|".join(searches) + b"))"
|
||||
candidates = self.searchmem(start, end, search)
|
||||
|
||||
if rop:
|
||||
|
@ -5056,7 +5057,7 @@ class PEDACmd(object):
|
|||
if result:
|
||||
text = ""
|
||||
for (addr, (byte, code)) in result:
|
||||
text += "%s : (%s)\t%s\n" % (to_address(addr), byte, code)
|
||||
text += "%s : (%s)\t%s\n" % (to_address(addr), byte.decode('utf-8'), code)
|
||||
pager(text)
|
||||
|
||||
return
|
||||
|
|
Loading…
Reference in New Issue