Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is the hard way of doing it: if your app is supposed to be running as Administrator on a pre-Vista Windows, you could get the address of the API via <code>::GetProcAddress()</code>, give yourself privileges to write to its memory page, and overwrite the beginning of the API's code with a "<code>jmp</code>" assembly instruction jumping into the address of your override function. Make sure your overwrite function takes the same arguments and is declared as <code>__cdecl</code>.</p> <p>Expanded answer follows.</p> <p>The "standard" technique for API hooking involves the following steps:</p> <h2>1: Inject your DLL into the target process</h2> <p>This is usually accomplished by first allocating memory in the target process for a string containing the name/path of your DLL (e.g. "MyHook.dll"), and then creating a remote thread in the target process whose entry point is <code>kernel32::LoadLibraryA()</code> passing the name of your DLL as argument. <a href="http://www.autoitscript.com/forum/topic/87240-windows-api-hooking-injecting-a-dll/" rel="nofollow">This page</a> has an implementation of this technique. You'll have to wrestle a bit with privileges, but it's guaranteed to work 100% on Windows XP and earlier OSes. I'm not sure about Vista and post-Vista, <em>Address Space Layout Randomization</em> might make this tricky.</p> <h2>2. Hook the API</h2> <p>Once your DLL is loaded into the target process, its <code>DllMain()</code> will be executed automatically, giving you a chance to run anything you want in the target process. From within your <code>DllMain</code>, use <code>::LoadLibraryA()</code> to get the <code>HMODULE</code> of the library containing the API you want to hook (e.g. "user32.dll") and pass it to <code>::GetProcAddress()</code> together with the name of the API you want to hook (e.g. "MessageBeep") to get the address of the API itself. Eventaully give yourself privileges to write to that address' page, and overwrite the beginning of the API with a <code>jmp</code> instruction jumping into your detour (i.e. into your "version" of the API to hook). Note that your detour needs to have the same signature and calling convention (usually <code>_cdecl</code>) as the API you want to hook, or else monsters will be awakened.</p> <p>As described here, this technique is somewhat destructive: you can't call back into the original API from the detour, as the original API has been modified to jump into yours and you'll end up with a very tight and nice infinite loop. There are many different techniques that would allow you to preserve and/or call back into the original API, one of which is hooking the <code>...A()</code> versions of the API and then calling into the <code>...W()</code> versions (most if not all of the <code>...A()</code> Windows API's convert ASCII strings into UNICODE strings and end up calling into their <code>...W()</code> counterparts).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload