Advertisement
waliedassar

Detect Wow64 User-Mode Hooks (Method 2)

Sep 9th, 2012
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.59 KB | None | 0 0
  1. //http://waleedassar.blogspot.com - (@waleedassar)
  2. //Code to detect Wow64 user-mode hooks
  3. #include "stdafx.h"
  4. #include "windows.h"
  5.  
  6. struct UNICODE_STRING
  7. {
  8.     unsigned short len;
  9.     unsigned short max_len;
  10.     wchar_t* pStr;
  11. };
  12.  
  13. extern "C"
  14. {
  15.     int __stdcall ZwQueryVirtualMemory(HANDLE,void*,int,void*,int,unsigned long*);
  16. }
  17.  
  18. wchar_t* GetBaseName(wchar_t* string)
  19. {
  20.      unsigned long i=lstrlenW(string);
  21.      while(string[i-1]!='\\') i--;
  22.      return &string[i];
  23. }
  24.  
  25. int main(int argc, char* argv[])
  26. {
  27.     unsigned short cs_=0;
  28.     unsigned char* WOW32Reserved=0;
  29.     unsigned long PEB64=0;
  30.     __asm
  31.     {
  32.         pushad
  33.         mov eax, dword ptr fs:[0xC0]
  34.         mov WOW32Reserved,eax
  35.         mov eax,dword ptr fs:[0x30]
  36.         add eax,0x1000
  37.         mov PEB64,eax
  38.         mov cs_,cs
  39.         popad
  40.     }
  41.     if(!WOW32Reserved) return 1;  //not 64-bit system
  42.     if(  (*WOW32Reserved==0xEA)&&( *(unsigned short*)(WOW32Reserved+5)!=cs_ )  )
  43.     {
  44.         unsigned long CpupReturnFromSimulatedCode=*(unsigned long*)(WOW32Reserved+1);
  45.         MEMORY_BASIC_INFORMATION MBI={0};
  46.         VirtualQuery((void*)CpupReturnFromSimulatedCode,&MBI,sizeof(MBI));
  47.         if(MBI.Type==MEM_IMAGE)
  48.         {
  49.              char* p=(char*)LocalAlloc(LMEM_ZEROINIT,0x1000);
  50.              if(ZwQueryVirtualMemory(GetCurrentProcess(),(void*)CpupReturnFromSimulatedCode,0x2 /*filename*/,p,0x1000,0) >= 0)
  51.              {
  52.                   if( ((UNICODE_STRING*)p)->len)
  53.                   {
  54.                          if(lstrcmpiW(L"wow64cpu.dll",GetBaseName(((UNICODE_STRING*)p)->pStr))==0)
  55.                          {
  56.                              LocalFree(p);
  57.                              return 1;
  58.                          }
  59.                   }
  60.              }
  61.              LocalFree(p);
  62.         }
  63.     }
  64.     MessageBox(0,"Hooked Wow64 process","waliedassar",0);
  65.     return 0;  //hooked
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement