Fix 'MemoryReadSafe' not restoring original memory protection correctly #16
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Run this script:
The expected output should be something like this (because we didn't touch the page):
But it's actually this:
The reason for this behavior is that before single stepping, the debugger reads the next 16 bytes to check if the following instruction is
pushfd
orpush ss
. To do this, it calls theMemoryReadSafe
function, which initially tries to useReadProcessMemory
and fails because there is a guarded page. Then it sets a more permissible page protection (PAGE_EXECUTE_READ) on the whole range. After the job is done, it will try to restore the original protection from the initial call toVirtualProtectEx
. The problem is that if the original page contains several different types of protection (like one page isPAGE_READWRITE
and another isPAGE_READWRITE | PAGE_GUARD
), it'll simply overwrite the whole page range with the first page protection (e.g.PAGE_READWRITE
).The solution is to save and restore each page individually.
GleeBug doesn't have this problem because it just fails to read memory entirely, without changing memory protection