// CHook - by Darawk
//
// CHook is a function hooking class, that provides you with 4 different
// types of function hooking. Each type has it's own unique advantages
// and disadvantages. Under different conditions, different hooking
// methods are appropriate. So, i've provided you here with the three
// major one's, along with one new one that AFAIK has never been done
// (or at least, never published).
//
// 1. Jmp Patch - This method is the most common. It patches the first
// five bytes of the function to be hooked with a relative jump.
// However, instead of requiring you to unhook each time you want to
// call the original function, CHook uses an LDE(Length Disassembler
// Engine) to determine the length of the first few instructions in the
// function. It then copies these instructions out to a buffer, and
// appends another relative jump to them. That relative jump points
// just past the relative jump written in by the hook in the hooked
// function. So, when you want to call the original function, you
// just call the OriginalFunc() method and it executes this code stub
// in order to call the original function without unhooking.
//
// 2. IAT Redirection - The IAT, or Import Address Table when loaded into
// memory contains the addresses of each function imported by a given
// module(EXE's are included in "modules"). IAT redirection just over-
// writes the address of the imported function in the IAT, to point to
// the hook. The downsides of this are that it only works on imported
// functions(i.e. not functions that are internal to a module), and it
// is module-specific(this could actually be an advantage if you want
// your hook to effect only one module).
//
// 3. EAT Redirection - The EAT, or Export Address Table is similar to the
// IAT. Except in the opposite direction. When a module exports a function
// so that it can be used by other modules, it stores the address of
// that function in it's EAT. EAT redirection overwrites that address
// with the offset of your hook. EAT redirection will not effect any
// currently loaded modules. It will only effect modules loaded after the
// redirection has been made. It will also effect subsequent calls to
// GetProcAddress(), as they will return the address of your hook instead of
// the real function.
//
// 4. Debug registers - This is the new method that I mentioned above. All
// x86 CPU's have things called debug registers. These registers are only
// directly accessible from privelege level 0(kernel mode). However, they
// can be manipulated using the GetThreadContext/SetThreadContext API's
// from privelege level 3(application level). The intended purpose of the
// debug registers is for use by debuggers, in setting breakpoints on memory
// access or execution. Breakpoints generated by the debug registers are
// generally called "hardware breakpoints". Whenever a hardware breakpoint
// gets tripped the CPU generates an int-1(single step, same thing as the trap
// flag) exception. So, to hook functions with them, all you need to do is
// set the hardware breakpoint on the first byte of the function you want to
// hook, and set up an exception handler to catch these exceptions. The major
// advantage of this method, is that these breakpoints will not modify the
// process image in any way. So, a complete checksum of the hooked modules
// image will come out perfectly clean. However, they aren't quite the holy
// grail of game hacking. They do have one major downside. Which is that
// there are only four of them. Though there are eight actual debug registers,
// two of them are unused and two others are used to describe the status of the
// other four. So, it's only possible to simultaneously have four of these
// types of hooks set.
//
// This code depends on the mlde32 library by Under-X, available
// here: http://vx.netlux.org/vx.php?id=em24
// You need to add the mlde32.obj file to your MSVC++ project. If you use a different compiler
// then i'm afraid your on your own as to figuring out how to add the .obj file. But it shouldn't
// be too difficult.
#pragma once
// Gotta do this for the vectored exception handling
#define _WIN32_WINNT 0x0600
#include
#include
#include
0 comentarios:
Publicar un comentario