mirror of
https://github.com/hakril/PythonForWindows
synced 2026-06-08 14:31:45 +00:00
Better handling of read_string for string at the end of readable memory
This commit is contained in:
+22
-3
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user