Jump to content

Need help at rewriting a C++ Function.


Busti
 Share

Recommended Posts

Hi all, i need some help at rewriting a C++ function, i hope you can help me this time again.

VOID AxlDetourFunction(
 LPVOID  F,  /// function to detour
 LPVOID  H,  /// my new function
 LPVOID* pG) /// old function
{
 LPVOID pv;
 DWORD  dw, ul;
 MEMORY_BASIC_INFORMATION mbi;

 /// find out about F's segment
 VirtualQuery(F,&mbi,sizeof(mbi));

 /// allocate a new writable segment
 pv = VirtualAlloc(NULL,mbi.RegionSize,
    MEM_COMMIT,PAGE_READWRITE);

 /// copy F's segment to the new one
 MoveMemory(pv,mbi.BaseAddress,mbi.RegionSize);

 /// make the new segment read/exec-only
 VirtualProtect(pv,mbi.RegionSize,
    PAGE_EXECUTE_READ,&dw);

 /// the old function starts at the same offset
 /// in the new segment where it started in the old one
 *pG = (LPBYTE)pv + ((LPBYTE)F - (LPBYTE)mbi.BaseAddress);

 /// make F's segment writable
 VirtualProtect(mbi.BaseAddress,mbi.RegionSize,
    PAGE_EXECUTE_READWRITE,&dw);

 /// write the jump opcode
 ((LPBYTE)F)[0] = (BYTE)0xE9;
 /// calculate the jump offset
 ul = (LPBYTE)H - &((LPBYTE)F)[6];
 /// write the jump offset (since there is no guarantee
 /// that the offset's DWORD is DWORD aligned, this is
 /// safer even if not really needed on modern CPUs)
 MoveMemory(((LPBYTE)F)[1],&ul,sizeof(ul));

 /// protected F's segment as it was before
 VirtualProtect(mbi.BaseAddress,
    mbi.RegionSize,dw,&dw);

}
Edited by Busti
My UDF's : Startet on : 06.06.2006_CaseSearchOrReplaceStr();~> Searches OR Replaces a String,;~> With or Without Casesensivity
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...