Detours 2.1 Simplification

Technical Discussions not Related Directly to MHS. For Example, Coding, Hex Editing, General Hacking, Etc.

Moderators: g3nuin3, SpeedWing, WhiteHat

Detours 2.1 Simplification

Postby PsychoTron » Sat Sep 04, 2010 9:38 pm

I've been trying to simplify the library to avoid so much redundant code.

So far I have this much, it appears to be working, and saves me a bit of typing. One problem is of scope, I cannot access the DetourFunc::Hook(), Unhook calls from "main.cpp" for instance..

I have it declared in "MoneyHook.h", defined in "MoneyHook.cpp", it is simply used like..

DetourFunc MoneyHookFunc("AddMoney", Real_Function, Hook_Function);

But from main, I want to be able to go..

MoneyHookFunc.Hook();

But I can't.. ??

DetourFunc.h
Code: Select all
#pragma once
#include "stdafx.h"
#include "Logger.h"

class DetourFunc
{
private:
   string ID;
   PVOID* RealFunction;
   PVOID HookFunction;

public:
   DetourFunc(string, PVOID*, PVOID);
   ~DetourFunc();

   void Hook();
   void Unhook();
};


DetourFunc.cpp
Code: Select all
#include "DetourFunc.h"

// Constructor
DetourFunc::DetourFunc(string id, PVOID* real, PVOID hook)
{
   ID = id;
   RealFunction = real;
   HookFunction = hook;
}

// Destructor
DetourFunc::~DetourFunc(void)
{
     //   DetourFunc::Unhook(); // Probably needs some checks.... To avoid unnecessarily calling Uhook..
}

// Hook
void DetourFunc::Hook()
{
   DetourTransactionBegin();
   DetourUpdateThread( GetCurrentThread() );
   DetourAttach(RealFunction, HookFunction);
   DetourTransactionCommit();
   Log("Hooked: " + ID);
}

// Unhook
void DetourFunc::Unhook()
{
   DetourTransactionBegin();
   DetourUpdateThread( GetCurrentThread() );
   DetourDetach(RealFunction, HookFunction);
   DetourTransactionCommit();
   Log("Unhooked: " + ID);
}
PsychoTron
Hackleberry Fin
 
Posts: 21
Joined: Sun Aug 29, 2010 7:02 am

Re: Detours 2.1 Simplification

Postby L. Spiro » Sun Sep 05, 2010 7:36 am

You may be better off creating a DetourFunc object and invoking Hook() on that.

Code: Select all
DetourFunc dfObj;
dfObj.Hook();



L. Spiro
Our songs remind you of songs you’ve never heard.
User avatar
L. Spiro
L. Spiro
 
Posts: 3129
Joined: Mon Jul 17, 2006 10:14 pm
Location: Tokyo, Japan

Re: Detours 2.1 Simplification

Postby PsychoTron » Sun Sep 05, 2010 11:35 pm

L. Spiro wrote:You may be better off creating a DetourFunc object and invoking Hook() on that.

Code: Select all
DetourFunc dfObj;
dfObj.Hook();


L. Spiro


I'm not sure I understand.

That's basically what MoneyHookFunc is, isn't it? (It's an object based on the class DetourFunc.)

Here are the other files.. See if you can't show me what you're talking about. (Any suggestions to make this better are welcome, I'm a fan of one liners, it's the C# coder in me.)

MoneyHook.h
Code: Select all
#pragma once
#include "Tools.h"

typedef ULONG (__stdcall * AddMoney)(ULONG);

extern "C"
{
   static ULONG __stdcall Hook_AddMoney(ULONG);
}

void MoneyHook_Hook();
void MoneyHook_Unhook();


MoneyHook.cpp
Code: Select all
#include "MoneyHook.h"

// Pointer: AddMoney Function (.exe)
AddMoney Real_AddMoney = (AddMoney)(0x0044B2C0);

// Hooked AddMoney Function
ULONG __stdcall Hook_AddMoney(ULONG amount)
{
   return Real_AddMoney(amount * 10);
}

DetourFunc MoneyHookFunc("AddMoney",&(PVOID&)Real_AddMoney,(PVOID)Hook_AddMoney);

void MoneyHook_Hook()
{
   MoneyHookFunc.Hook();
}

void MoneyHook_Unhook()
{
   MoneyHookFunc.Unhook();
}


main.cpp
Code: Select all
#include "main.h"
#include "MoneyHook.h"

bool APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
   switch (ul_reason_for_call)
   {
      case DLL_PROCESS_ATTACH:
      {
         DisableThreadLibraryCalls(hModule);
         LogT();
         MoneyHook_Hook();
         return true;         
      }break;
      case DLL_THREAD_ATTACH: break;
      case DLL_THREAD_DETACH: break;
      case DLL_PROCESS_DETACH:
      {   
         MoneyHook_Unhook();
         Log("\n--GEX Detached--");
      }break;
   }
    return TRUE;
}
PsychoTron
Hackleberry Fin
 
Posts: 21
Joined: Sun Aug 29, 2010 7:02 am

Re: Detours 2.1 Simplification

Postby L. Spiro » Tue Sep 07, 2010 5:11 am

I was referring to your first post in which you gave the example:
I cannot access the DetourFunc::Hook(),

But perhaps you meant this only to show the fully scope of the function you want to call.


I still don’t see the problem. You want to call a function but can’t.
Well why not? Post an error.


L. Spiro
Our songs remind you of songs you’ve never heard.
User avatar
L. Spiro
L. Spiro
 
Posts: 3129
Joined: Mon Jul 17, 2006 10:14 pm
Location: Tokyo, Japan

Re: Detours 2.1 Simplification

Postby PsychoTron » Tue Sep 07, 2010 6:49 pm

L. Spiro wrote:I was referring to your first post in which you gave the example:
I cannot access the DetourFunc::Hook(),

But perhaps you meant this only to show the fully scope of the function you want to call.

I still don’t see the problem. You want to call a function but can’t.
Well why not? Post an error.

L. Spiro


I already explained it. ?

Look at my usage of MoneyHook_Hook() in main.cpp..

I want to call MoneyHookFunc.Hook() instead of MoneyHook_Hook().

In C#, I would need to do something like this..

// MoneyHook.h
public DetourFunc MoneyHookFunc;

Then I could access that directly from main.cpp.

// Main.cpp
#include "MoneyHook.h"

MoneyHookFunc.Hook();

----

Edit: Okay, I found a solution..

extern DetourFunc MoneyHookFunc;

I had hoped to do it without making it global, but I can't see any other way to increase the scope so, this will work for now.
PsychoTron
Hackleberry Fin
 
Posts: 21
Joined: Sun Aug 29, 2010 7:02 am


Return to Technical Unrelated

Who is online

Users browsing this forum: No registered users and 0 guests

cron