Flag: Tornado! Hurricane!

 Forums >>  Debuggers  >>  # PAGE_GUARD/ReadProcessMemory bug as anti-debug trick

Topic created on: July 22, 2009 22:37 CDT by nezumi .

working on a project for a spectrography cherry group I found a bug in Windows (W2K SP4, XP SP3, Vista 32/64 SP1), well actually two bugs: one in Windows, another in OllyDbg.

if PAGE_GUARD page is created by VirtualAlloc() call, ReadProcessMemory() turns off the guard page status without any exception and returns a failure status indicator. however, the second call of ReadProcessMemory()returns a positive status (because PAGE_GUARD was turned off), so when the application will try to access to that page - there will be no exception (as it's supposed to be), because there is no guard anymore.

so, it's easy to create a sensor to detect debuggers/dumpers. allocate a page with PAGE_GUARD attribute and check it from time to time: just install SEH handler and read the content. no exception means we're fucked, oh, sorry, dumped. I tested PE-TOOLS and other popular dumpers and they all were detected.

OllyDbg stops when the application accesses PAGE_GUARD, giving the message "Break-on-access when reading" in the status bar, and does not allow us to pass the exception to the application. even we go to options->exceptions and add 0x80000001 (STATUS_GUARD_PAGE) exception to the list, Olly will ignore it! guess, PAGE_GUARD is just a part of "memory-breakpoint" engine, so no way to pass PAGE_GUARD exception to the application, so it's easy to detect the debugger (I tested OllyDbg 1.10).

to demonstrate these bugs I wrote a simple POC, please download it and tell me that do you think. thanks.

  EliCZ     July 23, 2009 01:51.24 CDT
The "bug" is documented here: http://msdn.microsoft.com/en-us/library/aa366549(VS.85).aspx

"If a guard page exception occurs during a system service, the service fails and typically returns some failure status indicator. Since the system also removes the relevant memory page's guard page status, the next invocation of the same system service won't fail due to a STATUS_GUARD_PAGE_VIOLATION exception (unless, of course, someone reestablishes the guard page)."

  nezumi     July 23, 2009 03:33.05 CDT
EliCZ
if so, why ReadProcessMemory() turns PAGE_GUARD off for pages allocated by VirtualAlloc(), but does not turn it off for VirtualProtect() as well as for PE image?

"typically returns" is not good for the official documentation and I quoted that part of the documentation on my blog.

PAGE_GUARD should work as it's expected to. if sometimes it works, sometimes does not - this means: it does not work at all, so this is a bug :)

btw, it's _not_ documented that OllyDbg does not pass the exception to applications :)

anyway, thanks for feedback! you helped me to realize that my post it's not clean enough...

  EliCZ     July 24, 2009 02:26.37 CDT
> if so, why ReadProcessMemory() turns PAGE_GUARD off for pages allocated by VirtualAlloc(), but [b]does not turn it off for VirtualProtect() as well as for PE image?

Can you write an example demonstrating this?


> PAGE_GUARD should work as it\'s expected to. if sometimes it works, sometimes does not - this means: it does not work at all, so this is a bug :)

I think it works as described in MSDN.
How _exactly_ should it work for you?


As I understand Windows it works like this:

* When an exception occurs in user-mode, a user-mode exception is raised (except stack growing).

* When an exception occurs in kernel-mode, a kernel-mode exception is raised.

* If kernel-mode exception happens during system service (typically when probing user-mode buffers) that has __try/__except, service returns an error status.

* Any touching of guarded page turns off PAGE_GUARD; when touched in user-mode an exception is dispatched to user-mode handler, when touched by system service a status is returned.

Am I wrong somewhere?

Note: Registration is required to post to the forums.

There are 31,323 total registered users.


Recently Created Topics
[help] Unpacking VMP...
Mar/12
Reverse Engineering ...
Jul/06
hi!
Jul/01
let 'IDAPython' impo...
Sep/24
set 'IDAPython' as t...
Sep/24
GuessType return une...
Sep/20
About retrieving the...
Sep/07
How to find specific...
Aug/15
How to get data depe...
Jul/07
Identify RVA data in...
May/06


Recent Forum Posts
Finding the procedur...
rolEYder
Question about debbu...
rolEYder
Identify RVA data in...
sohlow
let 'IDAPython' impo...
sohlow
How to find specific...
hackgreti
Problem with ollydbg
sh3dow
How can I write olly...
sh3dow
New LoadMAP plugin v...
mefisto...
Intel pin in loaded ...
djnemo
OOP_RE tool available?
Bl4ckm4n


Recent Blog Entries
halsten
Mar/14
Breaking IonCUBE VM

oleavr
Oct/24
Anatomy of a code tracer

hasherezade
Sep/24
IAT Patcher - new tool for ...

oleavr
Aug/27
CryptoShark: code tracer ba...

oleavr
Jun/25
Build a debugger in 5 minutes

More ...


Recent Blog Comments
nieo on:
Mar/22
IAT Patcher - new tool for ...

djnemo on:
Nov/17
Kernel debugger vs user mod...

acel on:
Nov/14
Kernel debugger vs user mod...

pedram on:
Dec/21
frida.github.io: scriptable...

capadleman on:
Jun/19
Using NtCreateThreadEx for ...

More ...


Imagery
SoySauce Blueprint
Jun 6, 2008

[+] expand

View Gallery (11) / Submit