Better handling of read_string for string at the end of readable memory

This commit is contained in:
hakril
2016-09-15 22:16:36 +02:00
committed by Clement Rouault
parent 63cdb3bbeb
commit 861884099f
2 changed files with 35 additions and 4 deletions
+22 -3
View File
@@ -112,20 +112,39 @@ class WindowsTestCase(unittest.TestCase):
calc.write_memory(k32.baseaddr, "XD")
self.assertEqual(calc.read_memory(k32.baseaddr, 2), "XD")
def test_read_wstring(self):
def test_read_string(self):
test_string = "TEST_STRING"
string_to_write = test_string + "\x00"
with Calc32() as calc:
addr = calc.virtual_alloc(0x1000)
calc.write_memory(addr, "\x00".join(test_string + "\x00"))
calc.write_memory(addr, string_to_write)
self.assertEqual(calc.read_string(addr), test_string)
def test_read_string_end_page(self):
test_string = "TEST_STRING"
string_to_write = test_string + "\x00"
with Calc32() as calc:
addr = calc.virtual_alloc(0x1000) + 0x1000 - len(string_to_write)
calc.write_memory(addr, string_to_write)
self.assertEqual(calc.read_string(addr), test_string)
def test_read_wstring(self):
test_string = "TEST_STRING"
string_to_write = test_string + "\x00"
with Calc32() as calc:
addr = calc.virtual_alloc(0x1000)
calc.write_memory(addr, "\x00".join(string_to_write))
self.assertEqual(calc.read_wstring(addr), test_string)
def test_read_wstring_end_page(self):
test_string = "TEST_STRING"
string_to_write = test_string + "\x00"
with Calc32() as calc:
# Setup string addr at end of page
addr = calc.virtual_alloc(0x1000) + 0x1000 - 26
calc.write_memory(addr, "\x00".join(test_string + "\x00"))
calc.write_memory(addr, "\x00".join(string_to_write))
self.assertEqual(calc.read_wstring(addr), test_string)
# Native execution
+13 -1
View File
@@ -538,8 +538,20 @@ class Process(AutoHandle):
def read_string(self, addr):
"""Read an ascii string at ``addr``"""
res = []
read_size = 0x100
readden = 0
for i in itertools.count():
x = self.read_memory(addr + (i * 0x100), 0x100)
try:
x = self.read_memory(addr + readden, read_size)
except winproxy.Kernel32Error as e:
if read_size == 2:
raise
# handle read_wstring at end of page
# Of read failed: read only the half of size
# read_size must remain a multiple of 2
read_size = read_size / 2
continue
readden += read_size
if "\x00" in x:
res.append(x.split("\x00", 1)[0])
break