waliedassar

The PAGE_GUARD Anti-Dumping Trick (Method 2)

Sep 7th, 2012
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.42 KB | None | 0 0
  1. //http://waleedassar.blogspot.com - (@waleedassar)
  2. //Code to show how to use the "PAGE_GUARD" anti-dumping trick.
  3. #include "stdafx.h"
  4. #include "windows.h"
  5.  
  6.  
  7. #pragma data_seg("walied4")
  8. int x=1;
  9. #pragma data_seg()
  10.  
  11. unsigned long touch;
  12.  
  13.  
  14.  
  15. int __cdecl Handler(EXCEPTION_RECORD* pRec,void* est,CONTEXT* pContext,void* disp)
  16. {
  17.     unsigned long old;
  18.     if(pRec->ExceptionCode==0x80000001)
  19.     {
  20.         touch=0;
  21.         VirtualProtect(&x,0x1000,PAGE_READWRITE|PAGE_GUARD,&old);  //restore PAGE_GUARD
  22.         *(unsigned long*)((unsigned char*)pContext+0xB8)=((unsigned long)(pRec->ExceptionAddress))+10;
  23.         return ExceptionContinueExecution;
  24.     }
  25.     return ExceptionContinueSearch;
  26. }
  27.  
  28. void __stdcall watch()
  29. {
  30.     __asm
  31.     {
  32.         push offset Handler
  33.         push dword ptr fs:[0]
  34.         mov dword ptr fs:[0],esp
  35.     }
  36.     while(1)
  37.     {
  38.         touch=1;
  39.         __asm
  40.         {
  41.               mov dword ptr[x],1  ; //Should trigger 0x80000001
  42.         }
  43.         if(touch) break;
  44.     }
  45.     ExitProcess(0);
  46. }
  47.  
  48. int main(int argc, char* argv[])
  49. {
  50.     unsigned long fake_base=(unsigned long)(&x);  //the page is initially PAGE_COPYWRITE
  51.     *(unsigned long*)fake_base=0xCECECECE;        //Now it is PAGE_READWRITE
  52.     unsigned long old;
  53.     VirtualProtect((void*)fake_base,0x1000,PAGE_READWRITE|PAGE_GUARD,&old);
  54.     unsigned long tid;
  55.     CreateThread(0,0x1000,(LPTHREAD_START_ROUTINE)&watch,0,0,&tid);
  56.     //---------------Useless stuff--------------
  57.     MessageBox(0,"Try to dump me","waliedassar",0);
  58.     ExitProcess(0);
  59.     return 0;
  60. }
Add Comment
Please, Sign In to add comment