flat assembler
Message board for the users of flat assembler.
  
       
      Index
      > Windows > API hooking example question. | 
  
| Author | 
  | 
              
| 
                  
                   TheLord 19 May 2008, 13:52 
                  It does not answer to the image base address stuff, but :
 
                  
                If you just have to hook MessageBoxA in a specific process, why dont you rewrite the first bytes ? seeing the function, it start with 5 bytes prologues : 7E4507AA > 8BFF MOV EDI,EDI ; ntdll.7C910208 7E4507AC 55 PUSH EBP 7E4507AD 8BEC MOV EBP,ESP you could replace them by a relative jmp which would be 5 bytes too (counting the dest addr). You can save the old_bytes and "call" them within your hook function if you need to truely call the messagebox ? imo it's simpler than hooking via the IAT (in your case)  | 
              |||
                  
  | 
              
| 
                  
                   Pinecone_ 19 May 2008, 14:01 
                  of coarse lol i clearly was not thinking strait when i said hook GetProcAddress to return a different value. However my problem is still the same.
 
                  
                Where the MessageBoxA API resides in memory that does not allow write access, so i must use VirtualProtect to give me write access so i can apply the patch (jmp to my code). The problem is that VirtualProtect requires the base address of the memory page, i dont know how to find this base address without hard-coding it which is always a bad idea btw: Hooking via IAT i thought was the most simple form possible: change a pointer in the process' memory to point to your function instead of the API function - which is not possible in this case as the GetProcAddress is used in several locations to get the address of MessageBoxA API, i just thought WAY too much into it before and came up with my stupid idea of hooking GetProcAddress when that is unnecessary Edit: I've just found a function called VirtualQuery. from reading the msdn description (http://msdn.microsoft.com/en-us/library/aa366902.aspx), i think this returns the value i need in a structure, i'll post back with test results another time, for now im tired and going to sleep  | 
              |||
                  
  | 
              
| 
                  
                   TheLord 19 May 2008, 14:39 
                  I think you can get the process base address by using VirtualQueryEx() within MEMORY_BASIC_INFORMATION structure
 
                  
                EDIT : too late :p  | 
              |||
                  
  | 
              
| 
                  
                   Pinecone_ 19 May 2008, 14:57 
                  i couldnt sleep  
                  
                I found that VirtualQuery was returning 0 and my structure was not being filled. Passing a kernel-mode pointer to this function can result in no information being returned, due to security issues. In this case, the return value is zero.  | 
              |||
                  
  | 
              
| 
                  
                   revolution 19 May 2008, 15:06 
                  You have the answer to getting the base address in your first post     
                  
                Code: and reg, $FFFFF000  | 
              |||
                  
  | 
              
| 
                  
                   Pinecone_ 19 May 2008, 20:55 
                  I can't believe it's that simple  
                  
                Edit: Yay it works  | 
              |||
                  
  | 
              
| 
                  
                   asmrox 19 May 2008, 23:05 
                  5 bytes jump?
 
                  
                what if you alloc memory out of range of signed int? you cant address all 32 bits, just 31, its 2147483648b (2 gigabytes.) If VirtualAllocEx returns adres 2Gb+ program will crash. You can use 2 optionS: 7 bytes: mov edx,addr jmp edx or 6 bytes: push addr ret i prefer hooking at the end of function, usually its retn XX nop nop nop nop nop.  | 
              |||
                  
  | 
              
| 
                  
                   Pinecone_ 20 May 2008, 00:25 
                  im not using virtualAllocEx to put my code into the process, im using a dll injected into it, regular dll's should not be loaded in addresses over what can be referenced by an signed integer, those spaces are reserved for OS dll's aren't they? 
                  
                 | 
              |||
                  
  | 
              
| 
                  
                   LocoDelAssembly 20 May 2008, 01:36 
                  asmrox wrote: 5 bytes jump? Actually it shouldn't be possible for a user-mode executable to allocate 2GB+ memory address space but even if it could there is no problem at all, remember that arithmetic is modular here.  | 
              |||
                  
  | 
              
| 
                  
                   Pinecone_ 20 May 2008, 13:41 
                  what do you mean "that arithmetic is modular here"? 
                  
                 | 
              |||
                  
  | 
              
| 
                  
                   LocoDelAssembly 22 May 2008, 01:33 
                  http://en.wikipedia.org/wiki/Modular_arithmetic
 
                  
                So, provided that the page that holds the jump is writable the following code should work Code: ; EAX = VirtualAlloc* result lea eax, [eax-jmp_to_patch-5] mov [jmp_to_patch+1], eax . . . jmp_to_patch: jmp $DEADBEEF  | 
              |||
                  
  | 
              
| 
                  
                   rugxulo 24 May 2008, 21:14 
                  Pinecone_ wrote: 
  | 
              |||
                  
  | 
              
< Last Thread | Next Thread >  | 
    
Forum Rules: 
  | 
    
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.